Fix `18+` false positives with NSFW check (#279)

Was noticing false positives with our test room names like: `planet-1688081266353-room-18`

Before:
```regex
/(\b|_)18+(\b|_)/i
```

After:
```regex
/(\b|_|-|\s|^)18\+(\b|_|-|\s|$)/i
```
This commit is contained in:
Eric Eastwood 2023-06-29 18:58:53 -05:00 committed by GitHub
parent 5de8cb4e35
commit 59c9d3180e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -1,7 +1,13 @@
'use strict';
const escapeStringRegexp = require('escape-string-regexp');
const NSFW_WORDS = ['nsfw', 'porn', 'nudes', 'sex', '18+'];
const NSFW_REGEXES = NSFW_WORDS.map((word) => new RegExp(`(\\b|_)${word}(\\b|_)`, 'i'));
const NSFW_REGEXES = NSFW_WORDS.map(
// We use `(\b|_|-|\s|^)` instead of just `(\b|_)` because the word boundary doesn't
// match next to the `+` sign in `18+`
(word) => new RegExp(`(\\b|_|-|\\s|^)${escapeStringRegexp(word)}(\\b|_|-|\\s|$)`, 'i')
);
// A very basic check for NSFW content that just looks for some keywords in the given
// text

View File

@ -13,6 +13,11 @@ describe('checkTextForNsfw', () => {
NSFW_foo: true,
'NSFW-foo': true,
'NSFW:foo': true,
'18+ only': true,
// Previous false positives that we ran into in the wild that should not be flagged
// as NSFW
'1888-great-blizzard': false,
'argon-18-element': false,
}).forEach(([inputText, expectedNsfw]) => {
it(`should return ${expectedNsfw} for '${inputText}'`, () => {
assert.strictEqual(