Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allowed keys for array-type keys #37

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "hatchling.build"
name = "supergood"
description = "The Python client for Supergood"
readme = "README.md"
version= "1.1.9"
version= "1.1.10"
requires-python = ">=3.7"
authors = [
{ name = "Alex Klarfeld" },
Expand Down
7 changes: 5 additions & 2 deletions src/supergood/helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gzip
import hashlib
import json
import re
import sys
from base64 import b64encode
from typing import Tuple
Expand Down Expand Up @@ -117,8 +118,10 @@ def redact_all_helper(elem, path=[], allowed=[]):
skeys += new_skeys
else:
key_path = ".".join(path)
if key_path in allowed:
# this is an allowed key. We do not want to redact it
# check to see if the generic keypath (no array indexes) is an allowed key
generic_keypath = re.sub(r"\[\d+\]", "[]", key_path)
if generic_keypath in allowed:
# do not mark allowed keys to be redacted
return []
(data_type, data_length) = describe_data(elem)
skeys.append(
Expand Down
40 changes: 40 additions & 0 deletions tests/redaction/test_redact_by_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
keys=[
("responseBody.string", "ALLOW"),
("responseBody.other_string", "REDACT"),
("responseBody[].data[].string", "ALLOW"),
]
),
"config": get_config(redact_by_default=True),
Expand Down Expand Up @@ -47,3 +48,42 @@ def test_redact_by_default(self, httpserver, supergood_client):
assert filtered[0]["keyPath"] == "responseBody.other_string"
assert filtered[0]["type"] == "string"
assert filtered[0]["length"] == 3

# Test that allowed keys with array indexers are not redacted
def test_allowed_keys_of_arrays(self, httpserver, supergood_client):
httpserver.expect_request("/200").respond_with_json(
[
{
"data": [
{"string": "abc", "other_string": "sensitive"},
{"string": "abc", "other_string": "sensitive"},
]
}
]
)
requests.get(httpserver.url_for("/200"))
supergood_client.flush_cache()
args = Api.post_events.call_args[0][0]
response_body = args[0]["response"]["body"]
metadata = args[0]["metadata"]
assert len(response_body) == 1
assert len(response_body[0]["data"]) == 2
assert response_body[0]["data"][0]["string"] == "abc"
assert response_body[0]["data"][1]["string"] == "abc"

assert response_body[0]["data"][0]["other_string"] == None
assert response_body[0]["data"][1]["other_string"] == None

assert len(metadata["sensitiveKeys"]) > 0
# # There are a bunch of request/response headers. Filter for just responseBody
filtered = list(
filter(
lambda x: x["keyPath"].startswith("responseBody"),
metadata["sensitiveKeys"],
)
)
assert len(filtered) == 2
assert filtered[0]["keyPath"] == "responseBody[0].data[0].other_string"
assert filtered[1]["keyPath"] == "responseBody[0].data[1].other_string"
assert filtered[0]["type"] == "string" and filtered[1]["type"] == "string"
assert filtered[0]["length"] == 9 and filtered[1]["length"] == 9
Loading