Skip to content

Commit

Permalink
Merge pull request #4345 from coralproject/fix/empty-wordlists
Browse files Browse the repository at this point in the history
handle empty wordlists in the wordlist worker
  • Loading branch information
tessalt authored Sep 26, 2023
2 parents f5c8d46 + 1a433e6 commit a513d0f
Showing 1 changed file with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface List {
category: WordListCategory;
locale: LanguageCode;
regex: RE2 | null;
regexIsEmpty: boolean;
}

const lists = new Map<string, List>();
Expand All @@ -42,7 +43,13 @@ const initialize = (
const regex =
phrases.length > 0 ? createServerWordListRegEx(locale, phrases) : null;

lists.set(key, { tenantID, category, locale, regex });
lists.set(key, {
tenantID,
category,
locale,
regex,
regexIsEmpty: phrases.length === 0,
});

logger.info(
{ tenantID, category, phrases: phrases.length },
Expand Down Expand Up @@ -73,7 +80,33 @@ const process = (
const listKey = computeWordListKey(tenantID, category);
const list = lists.get(listKey);

if (!list || list.regex === null) {
if (!list) {
return {
id,
tenantID,
ok: false,
err: new Error("word list for tenant not found"),
};
}

// Handle the case a phrase list is empty.
// If the regex is empty, we had no phrases to match against
// return that there are no matches as there can't be any matches.
if (list.regexIsEmpty) {
return {
id,
tenantID,
ok: true,
data: {
isMatched: false,
matches: [],
},
};
}

// If we made it here, the regex must be valid or something
// has gone very wrong!
if (!list.regex) {
return {
id,
tenantID,
Expand Down

0 comments on commit a513d0f

Please sign in to comment.