Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions packages/flow/chrome_extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,66 @@
const SECRET_NAME =
/pass(word|wd|phrase)?\b|passwd|pwd|secret|private[_ -]?key|api[_ -]?key|apikey|token|card[_ -]?(number|num|no)|cc[-_ ]?num(ber)?|cc[-_ ]?csc|cc[-_ ]?exp|(^|[_ -])ssn($|[_ -])|user[_ -]?ssn|social[-_ ]?security|(^|[_ -])cvv($|[_ -])|(^|[_ -])cvc($|[_ -])|security[-_ ]?code|^pin$|(^|[_ -])pin($|[_ -])|^otp$|(^|[_ -])otp($|[_ -])|^code$|sms[-_ ]?code|one[-_ ]?time[-_ ]?code|mfa|2fa|totp|authenticator|passkey|webauthn|duo|verification[-_ ]?code/i;
const SECRET_AUTOCOMPLETE = /(^|\s)(current-password|new-password|one-time-code|cc-number|cc-csc|cc-exp|cc-exp-month|cc-exp-year)(\s|$)/;
// Token-based secret field names, shared with the engine. Identifiers are
// split at camelCase / PascalCase boundaries and non-alphanumerics, then
// lowercased; a phrase must appear as whole consecutive tokens, so
// patientSsn and "Card CVV" match but pinnedItems does not.
// secret-field-vocabulary:begin (copy of openadapt_flow/secret_field_vocabulary.json)
const SECRET_FIELD_PHRASES = [
"password",
"passwd",
"pwd",
"passphrase",
"passcode",
"pin",
"ssn",
"social security",
"sin",
"tax id",
"cvv",
"cvv2",
"cvc",
"cvc2",
"csc",
"card number",
"cc number",
"iban",
"account number",
"routing",
"otp",
"one time code",
"verification code",
"mfa",
"totp",
"2fa",
"sms code",
"api key",
"access token",
"refresh token",
"secret",
"auth"
];
// secret-field-vocabulary:end
const SECRET_PHRASE_TOKENS = SECRET_FIELD_PHRASES.map((p) => p.split(' '));

function fieldTokens(value) {
return String(value == null ? '' : value)
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter(Boolean);
}

function namesSecretField(value) {
const tokens = fieldTokens(value);
return SECRET_PHRASE_TOKENS.some((phrase) => {
for (let start = 0; start + phrase.length <= tokens.length; start++) {
if (phrase.every((word, i) => tokens[start + i] === word)) return true;
}
return false;
});
}

let recording = false;
let authPaused = false;
Expand All @@ -28,12 +88,17 @@
// a secret or MFA attribute, and every field MFA_ATTR_RE flags is secret.
for (const attr of ['name', 'id', 'aria-label']) {
const candidate = el.getAttribute(attr);
if (candidate && (SECRET_NAME.test(String(candidate)) || MFA_ATTR_RE.test(String(candidate)))) {
if (
candidate &&
(SECRET_NAME.test(String(candidate)) ||
MFA_ATTR_RE.test(String(candidate)) ||
namesSecretField(candidate))
) {
return true;
}
}
const autocomplete = String(el.getAttribute('autocomplete') || '').toLowerCase();
if (SECRET_AUTOCOMPLETE.test(autocomplete)) return true;
if (SECRET_AUTOCOMPLETE.test(autocomplete) || namesSecretField(autocomplete)) return true;
return false;
}

Expand Down
63 changes: 62 additions & 1 deletion packages/flow/chrome_extension/network_redact.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,66 @@
].join('|'),
'i'
);
// Token-based secret field names, shared with the engine. Identifiers are
// split at camelCase / PascalCase boundaries and non-alphanumerics, then
// lowercased; a phrase must appear as whole consecutive tokens, so
// patientSsn and "Card CVV" match but pinnedItems does not.
// secret-field-vocabulary:begin (copy of openadapt_flow/secret_field_vocabulary.json)
const SECRET_FIELD_PHRASES = [
"password",
"passwd",
"pwd",
"passphrase",
"passcode",
"pin",
"ssn",
"social security",
"sin",
"tax id",
"cvv",
"cvv2",
"cvc",
"cvc2",
"csc",
"card number",
"cc number",
"iban",
"account number",
"routing",
"otp",
"one time code",
"verification code",
"mfa",
"totp",
"2fa",
"sms code",
"api key",
"access token",
"refresh token",
"secret",
"auth"
];
// secret-field-vocabulary:end
const SECRET_PHRASE_TOKENS = SECRET_FIELD_PHRASES.map((p) => p.split(' '));

function fieldTokens(value) {
return String(value == null ? '' : value)
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1 $2')
.toLowerCase()
.split(/[^a-z0-9]+/)
.filter(Boolean);
}

function namesSecretField(value) {
const tokens = fieldTokens(value);
return SECRET_PHRASE_TOKENS.some((phrase) => {
for (let start = 0; start + phrase.length <= tokens.length; start++) {
if (phrase.every((word, i) => tokens[start + i] === word)) return true;
}
return false;
});
}
const SECRET_VALUE = [
/^\s*(bearer|basic|digest|token)\s+\S+/i,
/eyJ[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]{4,}\.[A-Za-z0-9_-]*/, // JWT
Expand Down Expand Up @@ -252,7 +312,8 @@
}

function isSecretKey(key) {
return SECRET_KEY.test(String(key || ''));
const text = String(key || '');
return SECRET_KEY.test(text) || namesSecretField(text);
}

function headerName(name) {
Expand Down
10 changes: 9 additions & 1 deletion packages/flow/docs/CHROME_EXTENSION_EVIDENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ keeps only typed `frame` keys again (defense in depth).
on ingest. A field is secret when its `type` is `password`, it carries
`data-openadapt-secret`, its `autocomplete` is `current-password`,
`new-password` or `one-time-code`, or its `name` **or** `id` (each checked on
its own) matches the secret pattern.
its own) matches the secret pattern. The content script also checks
`aria-label` and `autocomplete`.
- Field names are also split into lowercase tokens (camelCase and PascalCase
boundaries, `_`, `-`, `.`, spaces) and matched as whole tokens against one
shared list, `openadapt_flow/secret_field_vocabulary.json` (ssn, cvv, pin,
otp, card number, ...). So `patientSsn`, `PatientSSN`, `card.cvv` and
"Card CVV" are secret, while `pinnedItems` and `productCode` are not.
`content.js` and `network_redact.js` embed the same list, and a test fails
when a copy differs.
- Local native messaging only; no remote endpoints.
- Missing evidence file → empty capture (degrade cleanly).

Expand Down
5 changes: 4 additions & 1 deletion packages/flow/docs/CHROME_EXTENSION_NETWORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ and in the engine on ingest, on write and on load.
recorded.
- **Body.** Kept only when it parses as JSON (raw body with a JSON or absent
content type) or form data, under 64 KiB, at most 32 levels deep and 5000
nodes. Values under a secret-looking key (the whole subtree) and
nodes. A key is secret-looking when it matches the key pattern or contains
a phrase from `openadapt_flow/secret_field_vocabulary.json` as whole tokens
after camelCase splitting (`userPin`, `otpCode`, `cardCvv`). Values under a
secret-looking key (the whole subtree) and
credential-shaped values (bearer/basic schemes, JWTs, known key prefixes,
long opaque tokens) become `<secret>`. Any other body (multipart, file
uploads, text, binary, oversized) is dropped; only
Expand Down
5 changes: 4 additions & 1 deletion packages/flow/openadapt_flow/extension_evidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from urllib.parse import urlsplit

from openadapt_flow.extension_network import NETWORK_SAVE_KIND, redact_network_save
from openadapt_flow.secret_field_names import names_secret_field

EVIDENCE_FILE = "extension_evidence.jsonl"
SCHEMA = "openadapt.extension-evidence/1"
Expand Down Expand Up @@ -110,7 +111,9 @@ def is_secret_field(
if (input_type or "").lower() == "password":
return True
for candidate in (name, field_id):
if candidate and SECRET_NAME.search(str(candidate)):
if candidate and (
SECRET_NAME.search(str(candidate)) or names_secret_field(candidate)
):
return True
_ = tag # reserved for future tag-based rules
return False
Expand Down
5 changes: 4 additions & 1 deletion packages/flow/openadapt_flow/extension_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
from typing import Any, Iterable, Mapping, Optional
from urllib.parse import parse_qsl, quote, unquote, unquote_plus, urlsplit

from openadapt_flow.secret_field_names import names_secret_field

NETWORK_SAVE_KIND = "network_save"
PLACEHOLDER = "<secret>"
SAVE_METHODS = frozenset({"POST", "PUT", "PATCH", "DELETE"})
Expand Down Expand Up @@ -197,7 +199,8 @@ def is_secret_value(value: object) -> bool:
def is_secret_key(key: object) -> bool:
"""True when a query, form or JSON key names a secret."""

return bool(SECRET_KEY.search(str(key or "")))
text = str(key or "")
return bool(SECRET_KEY.search(text)) or names_secret_field(text)


def is_secret_header(name: object) -> bool:
Expand Down
52 changes: 52 additions & 0 deletions packages/flow/openadapt_flow/secret_field_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Token-based secret field-name matching shared by the extension layers.

A field identifier (``name``, ``id``, ``aria-label``, ``autocomplete`` or a
JSON key) is split into lowercase tokens, then matched as whole consecutive
tokens against ``secret_field_vocabulary.json``. So ``patientSsn``,
``PatientSSN``, ``card_cvv``, ``card-cvv``, ``card.cvv`` and "Card CVV" all
match, while ``pinnedItems``, ``spinnerState`` and ``opinionText`` do not.

``chrome_extension/content.js`` and ``chrome_extension/network_redact.js``
implement the same ``fieldTokens`` split and embed the same phrase list; keep
the three in step (a test compares the lists).
"""

from __future__ import annotations

import json
import re
from pathlib import Path
from typing import Final

_VOCABULARY_PATH: Final = Path(__file__).with_name("secret_field_vocabulary.json")

SECRET_FIELD_PHRASES: Final[tuple[str, ...]] = tuple(
json.loads(_VOCABULARY_PATH.read_text(encoding="utf-8"))["phrases"]
)
_PHRASE_TOKENS: Final[tuple[tuple[str, ...], ...]] = tuple(
tuple(phrase.split()) for phrase in SECRET_FIELD_PHRASES
)

_LOWER_UPPER = re.compile(r"([a-z0-9])([A-Z])")
_ACRONYM_WORD = re.compile(r"([A-Z]+)([A-Z][a-z])")
_SEPARATORS = re.compile(r"[^a-z0-9]+")


def field_tokens(value: object) -> list[str]:
"""Lowercase tokens of an identifier: ``"PatientSSNLast4"`` -> patient ssn last4."""

text = _LOWER_UPPER.sub(r"\1 \2", str(value or ""))
text = _ACRONYM_WORD.sub(r"\1 \2", text).lower()
return [token for token in _SEPARATORS.split(text) if token]


def names_secret_field(value: object) -> bool:
"""True when the identifier contains a secret phrase as whole tokens."""

tokens = field_tokens(value)
for phrase in _PHRASE_TOKENS:
width = len(phrase)
for start in range(len(tokens) - width + 1):
if tuple(tokens[start : start + width]) == phrase:
return True
return False
38 changes: 38 additions & 0 deletions packages/flow/openadapt_flow/secret_field_vocabulary.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"schema": "openadapt.secret-field-vocabulary/1",
"description": "Secret field-name phrases for the Chrome extension redaction layers (content.js, network_redact.js, extension_evidence.py, extension_network.py). A field name, id, aria-label, autocomplete value or JSON key is split into lowercase tokens (camelCase and PascalCase boundaries, then any run of non-alphanumeric characters) and is secret when a phrase appears in it as whole consecutive tokens. content.js and network_redact.js embed an identical copy of this list; tests/test_extension_secret_field_names.py fails when they differ. 'code' alone is deliberately absent: only an identifier that is exactly 'code' is secret.",
"phrases": [
"password",
"passwd",
"pwd",
"passphrase",
"passcode",
"pin",
"ssn",
"social security",
"sin",
"tax id",
"cvv",
"cvv2",
"cvc",
"cvc2",
"csc",
"card number",
"cc number",
"iban",
"account number",
"routing",
"otp",
"one time code",
"verification code",
"mfa",
"totp",
"2fa",
"sms code",
"api key",
"access token",
"refresh token",
"secret",
"auth"
]
}
1 change: 1 addition & 0 deletions packages/flow/public-artifacts.json
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,7 @@
"openadapt_flow/policies/permissive.yaml",
"openadapt_flow/policies/record-write.yaml",
"openadapt_flow/registry_trusted_root.json",
"openadapt_flow/secret_field_vocabulary.json",
"openadapt_flow/taskgen/profiles/mockmed.yaml",
"openadapt_flow/visualize/static/program_graph.css",
"openadapt_flow/visualize/static/program_graph.js",
Expand Down
Loading