-
Notifications
You must be signed in to change notification settings - Fork 436
Jac/redact embedded creds #1035
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
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c1de34a
add redaction method
jacalata 7ea7f95
fix modules for installation
jacalata 5759969
update type annotations for string handlers
jacalata 59aa98a
skip new functionality in 3.6
jacalata bdab830
now redacts *all* instances in the string
jacalata 34908dd
skip tests in 3.6
jacalata 3295d79
format
jacalata 171b72a
remove support for python 3.6
jacalata df13b81
redact passwords more completely
jacalata 199e1ec
remove space from password end conditions
jacalata 99e304b
format
jacalata f261c5e
change to assume redacting xml
jacalata 1974fa0
Merge branch 'development' into jac/redact_embedded_creds
jacalata b53de16
delete output file
jacalata 4af13e9
format
jacalata 983f71f
add more tests
jacalata 3f60f6b
black
jacalata 2797db4
remove bad test line
jacalata 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
Binary file not shown.
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .strings import * |
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,65 @@ | ||
import requests | ||
|
||
from defusedxml.ElementTree import fromstring, tostring | ||
from functools import singledispatch | ||
from typing import TypeVar | ||
|
||
|
||
# the redact method can handle either strings or bytes, but it can't mix them. | ||
# Generic type so we can write the actual logic once, then use singledispatch to | ||
# create the replacement text with the correct type | ||
T = TypeVar("T", str, bytes) | ||
|
||
|
||
# TODO: ideally this would be in the logging config | ||
def safe_to_log(server_response: requests.Response) -> str: | ||
"""Checks if the server_response content is not xml (eg binary image or zip) | ||
and replaces it with a constant""" | ||
ALLOWED_CONTENT_TYPES = ("application/xml", "application/xml;charset=utf-8") | ||
if server_response.headers.get("Content-Type", None) not in ALLOWED_CONTENT_TYPES: | ||
return "[Truncated File Contents]" | ||
|
||
""" Check to determine if the response is a text response (xml or otherwise) | ||
so that we do not attempt to log bytes and other binary data. """ | ||
if not server_response.content or not server_response.encoding: | ||
return "" | ||
# max length 1000 | ||
loggable_response: str = server_response.content.decode(server_response.encoding)[:1000] | ||
redacted_response: str = redact_xml(loggable_response) | ||
return redacted_response | ||
|
||
|
||
# usage: _redact_any_type("<xml workbook password= cooliothesecond>") | ||
# -> b"<xml workbook password =***************"> | ||
def _redact_any_type(xml: T, sensitive_word: T, replacement: T, encoding=None) -> T: | ||
try: | ||
root = fromstring(xml) | ||
matches = root.findall(".//*[@password]") | ||
for item in matches: | ||
item.attrib["password"] = "********" | ||
matches = root.findall(".//password") | ||
for item in matches: | ||
item.text = "********" | ||
# tostring returns bytes unless an encoding value is passed | ||
return tostring(root, encoding=encoding) | ||
except Exception: | ||
# something about the xml handling failed. Just cut off the text at the first occurrence of "password" | ||
location = xml.find(sensitive_word) | ||
return xml[:location] + replacement | ||
|
||
|
||
@singledispatch | ||
def redact_xml(content): | ||
# this will only be called if it didn't get directed to the str or bytes overloads | ||
raise TypeError("Redaction only works on xml saved as str or bytes") | ||
|
||
|
||
@redact_xml.register | ||
def _(xml: str) -> str: | ||
out = _redact_any_type(xml, "password", "...[redacted]", encoding="unicode") | ||
return out | ||
|
||
|
||
@redact_xml.register # type: ignore[no-redef] | ||
def _(xml: bytes) -> bytes: | ||
return _redact_any_type(bytearray(xml), b"password", b"..[redacted]") |
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
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.