Repository navigation
Expand file tree
/
Copy pathredact.py
More file actions
81 lines (65 loc) · 3.03 KB
/
Copy pathredact.py
File metadata and controls
81 lines (65 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Redaction layer for file exports.
Strips or masks sensitive fields from report data before writing to a file.
Used only for file exports (not interactive terminal output or email body).
Invariants:
- ``redact_report_data`` returns a deep copy; the input is never mutated.
- Username redaction is by key (``data["username"]``); no generic regex is
used to avoid false positives on SKU names and model names.
- Repo redaction is by key (``{"repo": ...}`` entries anywhere in the dict,
recursively), so future sections are auto-covered.
- Text redaction matches email addresses and dollar amounts only; username
redaction is skipped for text to avoid over-redaction of ordinary words.
- All redaction functions are idempotent.
"""
from __future__ import annotations
import copy
import re
REDACT_USERNAME = "[redacted-user]"
REDACT_REPO = "[redacted-repo]"
REDACT_EMAIL = "[redacted-email]"
REDACT_AMOUNT = "[redacted-amount]"
_EMAIL_PATTERN = re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}")
_DOLLAR_PATTERN = re.compile(r"\$[\d,]+\.?\d*")
def redact_report_data(data: dict) -> dict:
"""Copy data, masking top-level username, repo values, and dollar strings.
Embedded names, email addresses, and numeric cost fields are not masked.
This partial field redaction does not anonymize the report.
"""
redacted = copy.deepcopy(data)
if "username" in redacted:
redacted["username"] = REDACT_USERNAME
_redact_repos_in_place(redacted)
_redact_strings_in_place(redacted)
return redacted
def redact_text(text: str) -> str:
"""Apply email and dollar-amount redaction to a plain-text string."""
text = _EMAIL_PATTERN.sub(REDACT_EMAIL, text)
text = _DOLLAR_PATTERN.sub(REDACT_AMOUNT, text)
return text
def _redact_strings_in_place(node) -> None:
"""Recursively replace string values containing dollar amounts.
Plain string items inside lists (e.g., warnings, insights) are also
scanned so they are redacted along with dict string values.
"""
if isinstance(node, dict):
for key, value in list(node.items()):
if isinstance(value, str) and _DOLLAR_PATTERN.search(value):
node[key] = _DOLLAR_PATTERN.sub(REDACT_AMOUNT, value)
elif isinstance(value, dict | list):
_redact_strings_in_place(value)
elif isinstance(node, list):
for index, item in enumerate(node):
if isinstance(item, str) and _DOLLAR_PATTERN.search(item):
node[index] = _DOLLAR_PATTERN.sub(REDACT_AMOUNT, item)
elif isinstance(item, dict | list):
_redact_strings_in_place(item)
def _redact_repos_in_place(node) -> None:
"""Recursively replace any ``"repo"`` string entry with the redaction marker."""
if isinstance(node, dict):
if "repo" in node and isinstance(node["repo"], str):
node["repo"] = REDACT_REPO
for value in node.values():
_redact_repos_in_place(value)
elif isinstance(node, list):
for item in node:
_redact_repos_in_place(item)