Skip to content

Commit

Permalink
Rename whitelist and blacklist to allowlist and blocklist
Browse files Browse the repository at this point in the history
  • Loading branch information
jobselko committed Dec 4, 2024
1 parent 8c14f75 commit 26bdff3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
48 changes: 24 additions & 24 deletions collectors/cveorg/keywords.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

WHITELIST = [
ALLOWLIST = [
"GIMP",
"Spring",
"dotnet",
Expand All @@ -9,9 +9,9 @@

# r'\b\.NET\b' does not match properly because word boundary \b does not cooperate well
# with dot.
WHITELIST_SPECIAL_CASES = [r"(?:\W|^)\.NET\b"]
ALLOWLIST_SPECIAL_CASES = [r"(?:\W|^)\.NET\b"]

BLACKLIST = [
BLOCKLIST = [
r"(HPE|Hewlett Packard Enterprise).*(IceWall|FlexNetwork|FlexFabric|OneView|Nimble)",
r"(Industrial Edge Management|Nucleus NET|SINEC).*[\n]*.*siemens",
r"(Jfinal|Final)[ _]CMS",
Expand Down Expand Up @@ -772,52 +772,52 @@
"zzcms",
]

BLACKLIST_CASE_SENSITIVE = ["iOS"]
BLOCKLIST_CASE_SENSITIVE = ["iOS"]

KEYWORD_WHITELIST = [
re.compile(rf"\b{keyword}\b", re.IGNORECASE) for keyword in WHITELIST
] + [re.compile(keyword) for keyword in WHITELIST_SPECIAL_CASES]
KEYWORD_ALLOWLIST = [
re.compile(rf"\b{keyword}\b", re.IGNORECASE) for keyword in ALLOWLIST
] + [re.compile(keyword) for keyword in ALLOWLIST_SPECIAL_CASES]

KEYWORD_BLACKLIST = [
re.compile(rf"\b{keyword}\b", re.IGNORECASE) for keyword in BLACKLIST
] + [re.compile(rf"\b{keyword}\b") for keyword in BLACKLIST_CASE_SENSITIVE]
KEYWORD_BLOCKLIST = [
re.compile(rf"\b{keyword}\b", re.IGNORECASE) for keyword in BLOCKLIST
] + [re.compile(rf"\b{keyword}\b") for keyword in BLOCKLIST_CASE_SENSITIVE]


def check_keywords(text):
"""
Checks if a specified text is relevant or not based on found keywords.
Returns tuple of matched blacklisted and whitelisted keywords.
Returns tuple of matched blocklisted and allowlisted keywords.
"""
whitelist = []
for word in (regex.search(text) for regex in KEYWORD_WHITELIST):
allowlist = []
for word in (regex.search(text) for regex in KEYWORD_ALLOWLIST):
if word is not None:
whitelist.append(word.group().strip())
allowlist.append(word.group().strip())

blacklist = []
for word in (regex.search(text) for regex in KEYWORD_BLACKLIST):
blocklist = []
for word in (regex.search(text) for regex in KEYWORD_BLOCKLIST):
if word is not None:
blacklist.append(word.group())
blocklist.append(word.group())

return sorted(blacklist), sorted(whitelist)
return sorted(blocklist), sorted(allowlist)


def should_create_snippet(text):
"""
Returns True if a snippet should be created, False otherwise.
Snippet should be created if:
words in `text` are in both whitelist and blacklist ([x], [x])
words in `text` are in whitelist only ([x], [])
words in `text` are not in whitelist or blacklist ([], [])
words in `text` are in both allowlist and blocklist ([x], [x])
words in `text` are in allowlist only ([x], [])
words in `text` are not in allowlist or blocklist ([], [])
Snippet should not be created if:
words in `text` are in blacklist only ([], [x])
words in `text` are in blocklist only ([], [x])
`text` is empty
"""
if not text:
return False

blacklist, whitelist = check_keywords(text)
blocklist, allowlist = check_keywords(text)

return False if (blacklist and not whitelist) else True
return False if (blocklist and not allowlist) else True
10 changes: 5 additions & 5 deletions collectors/cveorg/tests/test_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[
("Internet is a great thing!", ([], [])),
("IBM Tivoli is blue and red.", (["IBM Tivoli"], [])),
("we want to whitelist kernel", ([], ["kernel"])),
("we want to allowlist kernel", ([], ["kernel"])),
],
)
def test_check_keywords(text, expected_output):
Expand Down Expand Up @@ -85,13 +85,13 @@ def test_check_keywords_wordpress(text, expected_output):
@pytest.mark.parametrize(
"text, should_create",
[
# in both blacklist and whitelist
# in both blocklist and allowlist
("kernel and iOS in description", True),
# in whitelist only
# in allowlist only
("kernel and ios in description", True),
# not in whitelist or blacklist
# not in allowlist or blocklist
("something else in description", True),
# in blacklist only
# in blocklist only
("iOS in description", False),
# nothing to check
(None, False),
Expand Down

0 comments on commit 26bdff3

Please sign in to comment.