Repository navigation
Redact Chinese resident ID and bank card numbers out of the box - #244
Merged
Merged
Conversation
…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>
This was referenced Oct 1, 2026
Merged
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Two built-in redaction rules, on by default, in a new
personalkind (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:cn-resident-id0–9,X/x). The 15-digit format is not matched.bank-cardDetails:
\n6222…,身份证号:110…and:110…are found. A trailing.does not stop a match."input":{"order_id":6222…}, aseed); 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.6222 0212 3456 7894 12/28still finds the card.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 Diners3056930009020004is therefore not listed: Diners is 14 digits here, so it never matches anyway).sk-ant-…not falling through to the OpenAI rule.<<TW_ID_NUMBER_1>>,<<TW_CARD_NUMBER_1>>. Built-in rules can now declare a placeholder label (Builtin.label, filled intoHit.labelby one helper); numbering is per label and restoration, streaming included, already works per label (unit and end-to-end tests).personal(…002X,…7894, separators skipped), since "first 5, last 4" shows the region code of an ID and the issuer of a card.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_jwtcollected a Vec,internal_tokenlowercased every token,is_rfc1918collected 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
cn-resident-id,bank-card(both on; can be listed insecurity.redact.disable). Docs: generated tables regenerated, prose added todocs/config.md/docs/config.zh-CN.md.Matcher::CnResidentId { born_since },Matcher::BankCard { networks }with newCardNetwork { name, prefixes, lengths }andCardPrefix { from, to };SecretKindgainspersonal.CONTROL_API_VERSIONand 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_plainspans 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; theborn_sinceboundary). Gateway end-to-end test: labeled placeholders go upstream, a one-character-per-frame SSE echo comes back whole, the event reportspersonalwith…002X/…7894.Locally:
cargo fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspace(1940 passed), thetsbinding steps includingtsc, andscripts/smoke.sh(63 passed).🤖 Generated with Claude Code