Skip to content

Redact Chinese resident ID and bank card numbers out of the box - #244

Merged
fylorn merged 4 commits into
mainfrom
redact-personal-ids
Oct 1, 2026
Merged

fylorn merged 4 commits into
mainfrom
redact-personal-ids

Conversation

@fylorn

@fylorn fylorn commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What

Two built-in redaction rules, on by default, in a new personal kind (Kind::Personal). Like the credential rules, they only take what can be checked by structure, so they meet the same "rather miss than nag" bar:

Rule Matches
cn-resident-id An 18-character resident ID number: first two digits a GB/T 2260 province-level code (11–15, 21–23, 31–37, 41–46, 50–54, 61–65, 71, 81, 82), digits 7–14 a real date between 1900-01-01 and today (Beijing time, leap years included), last character the ISO 7064 MOD 11-2 check character (0–9, X/x). The 15-digit format is not matched.
bank-card A number that passes Luhn and whose prefix and length belong to UnionPay (62, 16–19), Visa (4, 16/19), Mastercard (51–55, 2221–2720, 16), American Express (34/37, 15), JCB (3528–3589, 16), Discover (6011, 644–649, 65, 16) or Diners Club (300–305, 36, 38, 14). Written as one run of digits, or in groups of four separated by a single space or a single hyphen, last group 1–4 digits (American Express also 4-6-5, Diners Club also 4-6-4). One separator per number.

Details:

  • Whole tokens only, using the existing token boundaries (JSON escapes are separators), so a valid number inside a longer digit run, a hex string or base64 is left alone, while \n6222…, 身份证号:110… and :110… are found. A trailing . does not stop a match.
  • Only inside JSON strings. The scan runs on the raw body, and unlike the credential rules the number rules could match a bare JSON number (a tool call's "input":{"order_id":6222…}, a seed); replacing it with an unquoted placeholder would leave the body invalid JSON. The tokenizer now tracks whether a token is inside a string (unescaped quotes), and for a JSON document the personal rules only look there. Non-JSON text scanned directly is looked at as before.
  • Space-grouped cards are several tokens; they are joined only across exactly one space, and the whole maximal run must be a card (no windows cut out of a table of 4-digit numbers). If it is not and its last group is shorter than four digits, that group is dropped and the rest checked once more, so 6222 0212 3456 7894 12/28 still finds the card.
  • Public test cards are excluded (TEST_CARDS, 62 numbers, next to the network table): Stripe's card-brand, co-branded and declined-payment tables plus three 3DS cards, Braintree's valid and unsuccessful-verification tables, Adyen's 3D Secure 2 table, all checked against the pages on 2026-10-01. A test pins that every listed number is one the rule would otherwise match (Stripe's 16-digit Diners 3056930009020004 is therefore not listed: Diners is 14 digits here, so it never matches anyway).
  • A token that is a valid ID number is never handed to the card rule, even when the ID rule is off (some Gansu numbers start with 62 and pass Luhn); same principle as sk-ant-… not falling through to the OpenAI rule.
  • Placeholders carry labels: <<TW_ID_NUMBER_1>>, <<TW_CARD_NUMBER_1>>. Built-in rules can now declare a placeholder label (Builtin.label, filled into Hit.label by one helper); numbering is per label and restoration, streaming included, already works per label (unit and end-to-end tests).
  • Masked excerpts (security log, events, the UI test box) keep only the last four characters for personal (…002X, …7894, separators skipped), since "first 5, last 4" shows the region code of an ID and the issuer of a card.
  • ThinkWatch Enterprise builds its rule set from RuleSet::none() plus its own patterns, so it is unaffected (test added); the tw-guard API it uses is unchanged.

A second commit removes the per-token allocations in the existing scan (looks_like_jwt collected a Vec, internal_token lowercased every token, is_rfc1918 collected twice). On a 2 MB request body the default scan goes from about 10.0 to 6.1 ms/MB (all built-ins: 12.0 → 6.9), so the default scan is faster than before these two rules.

Config / API

  • New built-in ids cn-resident-id, bank-card (both on; can be listed in security.redact.disable). Docs: generated tables regenerated, prose added to docs/config.md / docs/config.zh-CN.md.
  • Protocol: Matcher gains cn-resident-id and bank-card — Matcher::CnResidentId { born_since }, Matcher::BankCard { networks } with new CardNetwork { name, prefixes, lengths } and CardPrefix { from, to }; SecretKind gains personal. CONTROL_API_VERSION and crate versions are deliberately not bumped here; the version PR for this release should note this in the version comment.

Tests

Unit tests: valid IDs and cards replaced and restored; wrong check digit, impossible dates (1900-02-29, 02-30, month 13, day 0, before 1900, after today), unknown province codes, wrong Luhn, wrong length or prefix left alone; longer digit runs, hex and base64 left alone; JSON escapes and Chinese colons; bare JSON numbers left alone with the body still valid JSON; space- and hyphen-grouped cards as one value; irregular or mixed grouping rejected; the public test cards (contiguous and grouped) not matched; timestamps, snowflake IDs, order numbers, phone numbers and UUIDs not matched; switching either rule off; masking; labels with per-label numbering and a char-by-char streaming restore; scan_plain spans for the UI. The description test now ties both matchers to the scan (every prefix-range endpoint × every length matches, one digit shorter or longer does not; the born_since boundary). Gateway end-to-end test: labeled placeholders go upstream, a one-character-per-frame SSE echo comes back whole, the event reports personal with …002X / …7894.

Locally: cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace (1940 passed), the ts binding steps including tsc, and scripts/smoke.sh (63 passed).

🤖 Generated with Claude Code

fylorn and others added 4 commits October 1, 2026 17:56
…the box

Two built-in redaction rules, both on by default, in a new `personal`
kind. Like the credential rules, they only take what can be checked by
structure:

- `cn-resident-id`: an 18-character resident ID number whose first two
  digits are a GB/T 2260 province-level code, whose digits 7-14 are a real
  date between 1900-01-01 and today (Beijing time), and whose last
  character is the ISO 7064 MOD 11-2 check character. The 15-digit format
  is not matched.
- `bank-card`: a number that passes the Luhn check and whose prefix and
  length belong to UnionPay, Visa, Mastercard, American Express, JCB,
  Discover or Diners Club. Written as one run of digits, or in groups of
  four separated by a single space or a single hyphen (American Express
  also 4-6-5, Diners Club also 4-6-4). A short trailing group that breaks
  the number (an expiry month, a security code) is dropped and the rest
  checked again. The public test card numbers of Stripe, Braintree and
  Adyen are excluded; the list lives next to the network table.

Both match whole tokens only, so a valid number inside a longer digit run
or an encoded string is left alone. A token that is a valid ID number is
never handed to the card rule, even when the ID rule is off.

Their placeholders say what was there (`<<ID_NUMBER_1>>`,
`<<CARD_NUMBER_1>>`): built-in rules can now carry a placeholder label
(`Builtin.label`), and restoration, streaming included, already works per
label. Reported excerpts keep only the last four characters.

Protocol: `Matcher` gains `cn-resident-id` (`born_since`) and `bank-card`
(`networks`, with `CardNetwork` and `CardPrefix`); `SecretKind` gains
`personal`.

Rule sets built from `RuleSet::none()` (ThinkWatch Enterprise) are
unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every token of every request body went through `looks_like_jwt`, which
collected its dot-separated parts into a Vec, and `internal_token`, which
lowercased the token into a new String even with the internal-domain rule
off. `is_rfc1918` collected twice more when the internal-ip rule was on.
The checks now count dots, compare suffixes case-insensitively in place and
read the octets into a fixed array; what they match is unchanged, and a test
now pins the internal-domain rule, which had none.

On a 2 MB request body (code, Chinese prose, base64, UUIDs), the scan with
the default rules goes from about 10.0 to 6.1 ms per MB, and with every
built-in rule on from 12.0 to 6.9 ms per MB, so the two new personal-number
rules leave the default scan faster than it was before them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The scan runs on the raw JSON body. The credential rules never matched
outside a string, since nothing there contains letters, but the number
rules do: a tool call's input (`"input":{"order_id":6222021234567894}`) or
a `seed` that happens to be a valid card number was replaced with an
unquoted placeholder, and the request body was no longer JSON.

The tokenizer now tells whether a token sits inside a JSON string (by
counting unescaped quotes), and when the text is a JSON document the two
personal rules only look inside strings. Text that is not JSON, such as a
sample scanned directly, is looked at as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every placeholder the desktop gateway writes starts with TW_
(<<TW_SECRET_1>>), so a reader of the answer, or of the request detail,
can tell the gateway's placeholders from text the model wrote. The two
new labels now follow suit: <<TW_ID_NUMBER_1>> and <<TW_CARD_NUMBER_1>>.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@fylorn
fylorn merged commit 58663a4 into main Oct 1, 2026
4 checks passed
@fylorn
fylorn deleted the redact-personal-ids branch October 1, 2026 11:03
fylorn added a commit that referenced this pull request Oct 1, 2026
Release for #244, #245 and #246.

- Bump the workspace version to 0.57.0
- Control-plane protocol 30 -> 31, with the version comment for the
  upstream check-up, history search and the personal-number matchers
- release-notes/0.57.0.md

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant