Repository navigation
PII: obfuscate patterns #2
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
5 commits
Select commit
Hold shift + click to select a range
26192a5
fix(dbt): attribute relationships tests to the source node, not the F…
david-whimsical f9610d9
feat(render): obfuscate embedded PII shapes in sample-row cells
david-whimsical 44e69ec
docs(template): show Stripe ID + UUID rules as obf. examples
david-whimsical 5c54733
chore: bump version to 0.2.0
david-whimsical 3c7ce42
fix(obfuscate): split phone rule to stop matching non-ISO dates
david-whimsical 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
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,85 @@ | ||
| """Value-level PII obfuscation for rendered sample-row cells. | ||
|
|
||
| Complement to render/pii.py (which redacts whole columns by name). This | ||
| module runs after column-level redaction and scrubs PII shapes embedded | ||
| inside otherwise-innocuous values — e.g. an email pasted into a workspace | ||
| name, or a UUID inside a description field. | ||
|
|
||
| Defaults cover only the shapes that are always problematic: email, | ||
| phone, IP. URLs and UUIDs are deliberately excluded — UUIDs are warehouse | ||
| primary keys everywhere, URLs are not always PII, and scrubbing either | ||
| costs more in readability than it gains. Projects can append extra | ||
| patterns (including UUID or URL rules) via `output.obfuscate` in | ||
| wlens.yml. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import re | ||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ObfuscationRule: | ||
| name: str | ||
| pattern: re.Pattern[str] | ||
| replacement: str | ||
|
|
||
|
|
||
| # IP before phone: `1.2.3.4` should render as `<ip>`, not be partially matched. | ||
| DEFAULT_RULES: tuple[ObfuscationRule, ...] = ( | ||
| ObfuscationRule( | ||
| name="email", | ||
| pattern=re.compile(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+"), | ||
| replacement="<email>", | ||
| ), | ||
| ObfuscationRule( | ||
| name="ip", | ||
| pattern=re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b"), | ||
| replacement="<ip>", | ||
| ), | ||
| # Phone is split into two rules so neither matches DD-MM-YYYY / MM-DD-YYYY | ||
| # date strings. Real phones have ≥10 digits; common date formats have ≤8. | ||
| ObfuscationRule( | ||
| # International with explicit +country-code: `+1 555-123-4567`, | ||
| # `+44 20 7946 0958`. The leading `+` is the disambiguator. | ||
| name="phone_intl", | ||
| pattern=re.compile( | ||
| r"\+\d{1,3}[\s.-]?\d{1,4}[\s.-]?\d{2,9}[\s.-]?\d{2,9}" | ||
| ), | ||
| replacement="<phone>", | ||
| ), | ||
| ObfuscationRule( | ||
| # North American 3-3-4 shape: `(555) 123-4567`, `555-123-4567`, | ||
| # `555.123.4567`. The strict 3-3-4 grouping rules out date formats, | ||
| # whose leading day/month group is 1-2 digits. | ||
| name="phone_na", | ||
| pattern=re.compile(r"\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}"), | ||
| replacement="<phone>", | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def compile_rules(extras: list[dict] | None) -> tuple[ObfuscationRule, ...]: | ||
| """Return DEFAULT_RULES plus any user-supplied extras (appended).""" | ||
| rules: list[ObfuscationRule] = list(DEFAULT_RULES) | ||
| for i, extra in enumerate(extras or []): | ||
| if not isinstance(extra, dict) or "pattern" not in extra or "replacement" not in extra: | ||
| raise ValueError( | ||
| f"output.obfuscate[{i}] must have `pattern` and `replacement`. Got: {extra!r}" | ||
| ) | ||
| rules.append( | ||
| ObfuscationRule( | ||
| name=str(extra.get("name") or f"extra_{i}"), | ||
| pattern=re.compile(str(extra["pattern"])), | ||
| replacement=str(extra["replacement"]), | ||
| ) | ||
| ) | ||
| return tuple(rules) | ||
|
|
||
|
|
||
| def obfuscate(value: str, rules: tuple[ObfuscationRule, ...] = DEFAULT_RULES) -> str: | ||
| """Apply rules in order; return the scrubbed string.""" | ||
| for rule in rules: | ||
| value = rule.pattern.sub(rule.replacement, value) | ||
| return value | ||
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
Oops, something went wrong.
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.