-
-
Notifications
You must be signed in to change notification settings - Fork 81
feat(privacy): add watcher-side privacy filter (drop/redact before send) #135
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
Open
TimeToBuildBob
wants to merge
4
commits into
ActivityWatch:master
Choose a base branch
from
TimeToBuildBob:feat/privacy-filter-watcher-side
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
34fe0d4
feat(privacy): add watcher-side privacy filter (drop/redact before send)
TimeToBuildBob 9176770
fix(privacy): skip non-string regex patterns
TimeToBuildBob 1bdacd8
ci: update codeql-action v2→v3 and checkout v3→v4
TimeToBuildBob 1d68a6b
fix(privacy): fail closed on swift and stop silent config drops
TimeToBuildBob 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,133 @@ | ||
| """ | ||
| Watcher-side privacy filter: drop or redact sensitive events before sending. | ||
|
|
||
| Mirrors the server-side ``privacy_filters`` engine in aw-server-rust so that | ||
| sensitive data never leaves the machine in the first place. | ||
|
|
||
| Configure rules in ``~/.config/activitywatch/aw-watcher-window/aw-watcher-window.toml``: | ||
|
|
||
| .. code-block:: toml | ||
|
|
||
| # Drop events whose title matches a private-browsing pattern | ||
| [[aw-watcher-window.privacy_filter]] | ||
| pattern = "(?i)private browsing|incognito" | ||
| action = "drop" | ||
|
|
||
| # Redact window titles containing a banking domain | ||
| [[aw-watcher-window.privacy_filter]] | ||
| pattern = "(?i)bank\\.example\\.com" | ||
| action = "redact" | ||
| replacement = "REDACTED" # optional; defaults to "excluded" | ||
|
|
||
| Rule fields: | ||
| pattern -- Python ``re`` regex applied to the target field value. | ||
| field -- Window-event field to match against (default: ``"title"``). | ||
| action -- ``"drop"`` (discard the event) or ``"redact"`` (replace the value). | ||
| replacement -- Replacement string for ``"redact"`` action (default: ``"excluded"``). | ||
|
|
||
| macOS note: the default ``swift`` strategy cannot apply these rules (it | ||
| sends heartbeats from a separate binary). The watcher refuses to start | ||
| under ``--strategy swift`` when any rule compiled, so titles are not | ||
| leaked. Use ``--strategy jxa`` or ``--strategy applescript`` on macOS. | ||
| """ | ||
|
|
||
| import logging | ||
| import re | ||
| from typing import Optional | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _VALID_ACTIONS = frozenset({"drop", "redact"}) | ||
|
|
||
|
|
||
| def compile_privacy_rules(raw_rules: list) -> list: | ||
| """Validate and compile regex patterns in privacy filter rules. | ||
|
|
||
| Returns a list of compiled rule dicts ready for :func:`apply_privacy_filters`. | ||
| Invalid rules (bad regex or unknown action) are skipped with an error log. | ||
| """ | ||
| compiled = [] | ||
| for raw in raw_rules: | ||
| if not isinstance(raw, dict): | ||
| logger.error("privacy_filter rule is not a table: %r — skipped", raw) | ||
| continue | ||
|
|
||
| pattern_str = raw.get("pattern", "") | ||
| if not pattern_str: | ||
| logger.error("privacy_filter rule missing 'pattern' — skipped: %r", raw) | ||
| continue | ||
|
|
||
| try: | ||
| pattern = re.compile(pattern_str) | ||
| except (re.error, TypeError) as exc: | ||
| logger.error( | ||
| "privacy_filter: invalid regex %r — %s — rule skipped", pattern_str, exc | ||
| ) | ||
| continue | ||
|
|
||
| action = raw.get("action", "redact") | ||
| if action not in _VALID_ACTIONS: | ||
| logger.error( | ||
| "privacy_filter: unknown action %r (expected 'drop' or 'redact') — rule skipped", | ||
| action, | ||
| ) | ||
| continue | ||
|
|
||
| field = raw.get("field", "title") | ||
| if not isinstance(field, str) or not field: | ||
| logger.error( | ||
| "privacy_filter: 'field' must be a non-empty string — rule skipped: %r", | ||
| raw, | ||
| ) | ||
| continue | ||
|
|
||
| replacement = raw.get("replacement", "excluded") | ||
| if not isinstance(replacement, str): | ||
| logger.error( | ||
| "privacy_filter: 'replacement' must be a string — rule skipped: %r", | ||
| raw, | ||
| ) | ||
| continue | ||
|
|
||
| compiled.append( | ||
| { | ||
| "pattern": pattern, | ||
| "field": field, | ||
| "action": action, | ||
| "replacement": replacement, | ||
| } | ||
| ) | ||
| return compiled | ||
|
|
||
|
|
||
| def apply_privacy_filters(window: dict, rules: list) -> Optional[dict]: | ||
| """Apply compiled privacy filter rules to a window event. | ||
|
|
||
| Returns ``None`` when the event should be dropped entirely, otherwise a | ||
| (possibly modified) copy of the window dict. Never mutates the input. | ||
| """ | ||
| if not rules: | ||
| return window | ||
|
|
||
| result = dict(window) | ||
| for rule in rules: | ||
| field = rule["field"] | ||
| value = result.get(field) | ||
| if not isinstance(value, str): | ||
| continue | ||
| if rule["pattern"].search(value): | ||
| if rule["action"] == "drop": | ||
| logger.debug( | ||
| "privacy_filter: dropping event (field=%r matched %r)", | ||
| field, | ||
| rule["pattern"].pattern, | ||
| ) | ||
| return None | ||
| else: # redact | ||
| logger.debug( | ||
| "privacy_filter: redacting field=%r matched %r", | ||
| field, | ||
| rule["pattern"].pattern, | ||
| ) | ||
| result[field] = rule["replacement"] | ||
| return result |
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.