-
Notifications
You must be signed in to change notification settings - Fork 194
refactor: enhance mask_sensitive_info in BaseAPI #3555
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
Merged
vorasmit
merged 2 commits into
resilient-tech:develop
from
karm1000:refactor-mask-sensitive-info
Aug 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
india_compliance/gst_india/api_classes/test_mask_sensitive_info.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import frappe | ||
from frappe.tests import IntegrationTestCase | ||
|
||
from india_compliance.gst_india.api_classes.base import BaseAPI | ||
|
||
|
||
class TestMaskSensitiveInfo(IntegrationTestCase): | ||
def test_base_api_mask_sensitive_info_mapping(self): | ||
"""Test that BaseAPI correctly maps sensitive info to appropriate locations""" | ||
api = BaseAPI() | ||
mapping = api._get_sensitive_info_mapping() | ||
|
||
self.assertIn("headers", mapping) | ||
self.assertIn("output", mapping) | ||
self.assertIn("data", mapping) | ||
self.assertIn("body", mapping) | ||
|
||
self.assertIn("x-api-key", mapping["headers"]) | ||
self.assertNotIn("x-api-key", mapping["output"]) | ||
self.assertNotIn("x-api-key", mapping["data"]) | ||
self.assertNotIn("x-api-key", mapping["body"]) | ||
|
||
def test_mask_sensitive_info_headers_only(self): | ||
"""Test that sensitive headers are masked only in headers""" | ||
api = BaseAPI() | ||
|
||
log = frappe._dict( | ||
{ | ||
"request_headers": { | ||
"x-api-key": "secret_key", | ||
"content-type": "application/json", | ||
}, | ||
"output": {"x-api-key": "should_not_be_masked", "result": "success"}, | ||
"data": {"x-api-key": "should_not_be_masked", "param": "value"}, | ||
"body": None, | ||
} | ||
) | ||
|
||
api.mask_sensitive_info(log) | ||
|
||
self.assertEqual(log.request_headers["x-api-key"], api.PLACEHOLDER) | ||
self.assertEqual(log.request_headers["content-type"], "application/json") | ||
|
||
self.assertEqual(log.output["x-api-key"], "should_not_be_masked") | ||
self.assertEqual(log.data["x-api-key"], "should_not_be_masked") | ||
|
||
def test_mask_sensitive_info_with_request_body(self): | ||
"""Test that sensitive info in request body is handled correctly""" | ||
api = BaseAPI() | ||
|
||
log = frappe._dict( | ||
{ | ||
"request_headers": {"content-type": "application/json"}, | ||
"output": {"result": "success"}, | ||
"data": { | ||
"params": {"test": "value"}, | ||
"body": {"password": "secret_password", "AppKey": "secret_app_key"}, | ||
}, | ||
} | ||
) | ||
|
||
api.mask_sensitive_info(log) | ||
|
||
self.assertEqual(log.data["body"]["password"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.data["body"]["AppKey"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.data["params"]["test"], "value") | ||
|
||
def test_mask_sensitive_info_comprehensive(self): | ||
"""Test complete masking with all data structures""" | ||
api = BaseAPI.__new__(BaseAPI) | ||
|
||
log = frappe._dict( | ||
{ | ||
"request_headers": { | ||
"x-api-key": "secret_api_key", | ||
"auth-token": "secret_auth_token", | ||
"content-type": "application/json", | ||
}, | ||
"output": { | ||
"auth_token": "response_auth_token", | ||
"sek": "session_encryption_key", | ||
"rek": "response_encryption_key", | ||
"result": "success", | ||
}, | ||
"data": { | ||
"app_key": "data_app_key", | ||
"body": {"app_key": "body_app_key", "username": "test_user"}, | ||
}, | ||
} | ||
) | ||
|
||
api.mask_sensitive_info(log) | ||
|
||
self.assertEqual(log.request_headers["x-api-key"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.request_headers["auth-token"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.request_headers["content-type"], "application/json") | ||
|
||
self.assertEqual(log.output["auth_token"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.output["sek"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.output["rek"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.output["result"], "success") | ||
|
||
self.assertEqual(log.data["app_key"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.data["body"]["app_key"], BaseAPI.PLACEHOLDER) | ||
self.assertEqual(log.data["body"]["username"], "test_user") | ||
|
||
def test_mask_sensitive_info_handles_missing_data(self): | ||
"""Test that masking works when some data structures are missing""" | ||
api = BaseAPI() | ||
|
||
log = frappe._dict( | ||
{ | ||
"request_headers": {"x-api-key": "secret_key"}, | ||
"output": None, | ||
"data": None, | ||
} | ||
) | ||
|
||
api.mask_sensitive_info(log) | ||
|
||
self.assertEqual(log.request_headers["x-api-key"], BaseAPI.PLACEHOLDER) | ||
|
||
def test_mask_sensitive_info_no_false_positives(self): | ||
"""Test that legitimate data with similar keys is not masked inappropriately""" | ||
api = BaseAPI.__new__(BaseAPI) | ||
|
||
log = frappe._dict( | ||
{ | ||
"request_headers": {"content-type": "application/json"}, | ||
"output": { | ||
"password_reset_link": "https://example.com/reset", | ||
"result": "success", | ||
}, | ||
"data": { | ||
"user_password_policy": "strong", | ||
"body": {"password": "actual_secret"}, | ||
}, | ||
} | ||
) | ||
|
||
api.mask_sensitive_info(log) | ||
|
||
self.assertEqual(log.output["password_reset_link"], "https://example.com/reset") | ||
self.assertEqual(log.data["user_password_policy"], "strong") | ||
self.assertEqual(log.data["body"]["password"], BaseAPI.PLACEHOLDER) | ||
|
||
def test_sensitive_info_overrides_functionality(self): | ||
"""Test that the override system works correctly - only specified locations are overridden""" | ||
|
||
class CustomAPI(BaseAPI): | ||
def _get_sensitive_info_overrides(self): | ||
return { | ||
"headers": ["custom-header", "x-api-key"], | ||
} | ||
|
||
api = CustomAPI.__new__(CustomAPI) | ||
mapping = api._get_sensitive_info_mapping() | ||
|
||
self.assertEqual(mapping["headers"], ["custom-header", "x-api-key"]) | ||
|
||
self.assertEqual(mapping["output"], BaseAPI.DEFAULT_MASK_MAP["output"]) | ||
self.assertEqual(mapping["data"], BaseAPI.DEFAULT_MASK_MAP["data"]) | ||
self.assertEqual(mapping["body"], BaseAPI.DEFAULT_MASK_MAP["body"]) | ||
|
||
def test_empty_overrides_uses_defaults(self): | ||
"""Test that empty overrides still use default mapping""" | ||
|
||
class NoOverrideAPI(BaseAPI): | ||
def _get_sensitive_info_overrides(self): | ||
return {} | ||
|
||
api = NoOverrideAPI.__new__(NoOverrideAPI) | ||
mapping = api._get_sensitive_info_mapping() | ||
|
||
self.assertEqual(mapping, BaseAPI.DEFAULT_MASK_MAP) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.