Skip to content

Commit

Permalink
Run bandit on components (#513)
Browse files Browse the repository at this point in the history
Let's run bandit on the components as well.
  • Loading branch information
RobbeSneyders authored Oct 12, 2023
1 parent 785940e commit e6501fb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ repos:
types: [python]
files: |
(?x)^(
components/.*|
src/.*|
examples/.*|
tests/.*|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def query(
if image.startswith("http"):
return self.__search_knn_api__(image_url=image)
else:
assert Path(image).exists(), f"{image} does not exist."
assert Path(image).exists(), f"{image} does not exist." # nosec B101
return self.__search_knn_api__(image=image)
elif embedding_input:
return self.__search_knn_api__(embedding_input=embedding_input)
Expand Down
13 changes: 8 additions & 5 deletions components/pii_redaction/src/pii_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ def random_replacements(n=10):
letters = string.ascii_lowercase
lettters_digits = string.ascii_lowercase + string.digits
emails = [
"".join(random.choice(letters) for i in range(5)) + "@example.com"
"".join(random.choice(letters) for i in range(5)) + "@example.com" # nosec B311
for i in range(n)
]
keys = [
"".join(random.choice(lettters_digits) for i in range(32)) for i in range(n)
"".join(random.choice(lettters_digits) for i in range(32)) # nosec B311
for i in range(n)
]
ip_addresses = REPLACEMENTS_IP
return {"EMAIL": emails, "KEY": keys, "IP_ADDRESS": ip_addresses}
Expand All @@ -72,11 +73,11 @@ def replace_ip(value, replacements_dict):
"""Replace an IP address with a synthetic IP address of the same format."""
try:
ipaddress.IPv4Address(value)
return random.choice(replacements_dict["IP_ADDRESS"]["IPv4"])
return random.choice(replacements_dict["IP_ADDRESS"]["IPv4"]) # nosec B311
except ValueError:
try:
ipaddress.IPv6Address(value)
return random.choice(replacements_dict["IP_ADDRESS"]["IPv6"])
return random.choice(replacements_dict["IP_ADDRESS"]["IPv6"]) # nosec B311
except ValueError:
# this doesn't happen if we already use ipaddress filter in the detection
print("Invalid IP address")
Expand Down Expand Up @@ -129,7 +130,9 @@ def redact_pii_text(text, secrets, replacements, add_references=False):
if secret["tag"] == "IP_ADDRESS":
replacement = replace_ip(secret["value"], replacements)
else:
replacement = random.choice(replacements[secret["tag"]])
replacement = random.choice( # nosec B311
replacements[secret["tag"]],
)
replaced_secrets[secret["value"]] = replacement
subparts.append(replacement)
replaced_secrets[secret["value"]] = replacement
Expand Down
2 changes: 1 addition & 1 deletion components/prompt_based_laion_retrieval/src/clip_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def query(
if image.startswith("http"):
return self.__search_knn_api__(image_url=image)
else:
assert Path(image).exists(), f"{image} does not exist."
assert Path(image).exists(), f"{image} does not exist." # nosec B101
return self.__search_knn_api__(image=image)
elif embedding_input:
return self.__search_knn_api__(embedding_input=embedding_input)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ convention = "google"
skips = ["B108", "B307", "B404", "B602"]

[tool.bandit.assert_used]
skips = ["tests/test_*.py"]
skips = ["**/test_*.py", "**/*_test.py"]


0 comments on commit e6501fb

Please sign in to comment.