From 51eef64acaad4c77fd7857f6a10d102b67a90a8d Mon Sep 17 00:00:00 2001 From: Shaurya Kesarwani Date: Tue, 13 Oct 2026 10:26:43 +0530 Subject: [PATCH 1/3] =?UTF-8?q?fix(safety):=20bound=20the=20international?= =?UTF-8?q?=20phone=20rule=20to=207=E2=80=9315=20digits?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The international phone pattern repeated over digits and separators alike, so its comment's promise of 7–15 digits held for neither end: `+1 2` matched on two digits and a run of spaces, and an 18-digit value matched on a prefix and was redacted in part. The rule now reads `+` and 7–15 digits with at most one space or hyphen between any two of them, and ends on a word boundary so an overlong run of digits is left alone rather than matched on its head. A validator pins the digit count the pattern promises, the way Luhn and the IPv4 octet check back the card and address rules. Closes #263 --- crates/tinymemory-api/src/safety/mod_tests.rs | 56 ++++++++++++++++++- crates/tinymemory-api/src/safety/rules.rs | 25 +++++++-- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/crates/tinymemory-api/src/safety/mod_tests.rs b/crates/tinymemory-api/src/safety/mod_tests.rs index d45a9a1c..bd053cad 100644 --- a/crates/tinymemory-api/src/safety/mod_tests.rs +++ b/crates/tinymemory-api/src/safety/mod_tests.rs @@ -9,7 +9,7 @@ //! * detection is deterministic + offline (no external egress on classify), //! * category identifiers serialize to stable snake_case strings. -use super::rules::{is_ipv4, is_luhn_valid}; +use super::rules::{has_international_phone_digit_count, is_ipv4, is_luhn_valid}; use super::*; // --------------------------------------------------------------------------- @@ -70,6 +70,52 @@ fn detects_phone_numbers_varied_formats() { } } +#[test] +fn international_phone_numbers_carry_seven_to_fifteen_digits() { + for raw in [ + "+1234567", + "+123456789012345", + "+44 20 7946 0958", + "+1-415-555-0100", + ] { + let r = scan(raw); + assert!(r.has_category(PiiCategory::PhoneNumber), "missed {raw:?}"); + } + for raw in [ + "+123456", + "+1234567890123456", + "+123456789012345678", + "+1 2", + ] { + let r = scan(raw); + assert!( + !r.has_category(PiiCategory::PhoneNumber), + "flagged {raw:?}: {r:?}" + ); + } +} + +#[test] +fn an_overlong_number_is_not_matched_on_a_prefix() { + // 18 digits: no 15-digit (or shorter) head of it may count as a phone + // number, so the value is reported clean and left as written. + let text = "ref +123456789012345678 end"; + assert_eq!(scan(text).level, RiskLevel::None); + assert_eq!(redact_identifiers(text), text); +} + +#[test] +fn international_phone_redaction_covers_the_whole_number() { + assert_eq!( + redact_identifiers("call +44 20 7946 0958 now"), + "call [redacted] now" + ); + assert_eq!( + redact_identifiers("call +123456789012345 now"), + "call [redacted] now" + ); +} + #[test] fn detects_ipv4_address() { let r = scan("client connected from 192.168.10.24 last night"); @@ -309,6 +355,14 @@ fn luhn_validator_accepts_known_good_and_rejects_bad() { assert!(!is_luhn_valid("12345")); // too short } +#[test] +fn international_phone_validator_bounds_digit_count() { + assert!(has_international_phone_digit_count("+1234567")); + assert!(has_international_phone_digit_count("+1 234 567 890 123 45")); + assert!(!has_international_phone_digit_count("+123456")); + assert!(!has_international_phone_digit_count("+1234567890123456")); +} + #[test] fn ipv4_validator_bounds_octets() { assert!(is_ipv4("192.168.0.1")); diff --git a/crates/tinymemory-api/src/safety/rules.rs b/crates/tinymemory-api/src/safety/rules.rs index 6cad3122..ac6167ce 100644 --- a/crates/tinymemory-api/src/safety/rules.rs +++ b/crates/tinymemory-api/src/safety/rules.rs @@ -3,8 +3,9 @@ //! Two flavours of rule, both compiled once into a process-wide static: //! //! * **Pattern rules** — a [`Regex`] plus an optional structural `validator` -//! (Luhn for cards, octet range for IPv4) that rejects shape-matches that -//! aren't actually valid identifiers. +//! (Luhn for cards, octet range for IPv4, digit count for international +//! phone numbers) that rejects shape-matches that aren't actually valid +//! identifiers. //! * **Keyword rules** — an alternation of domain terms wrapped in word //! boundaries, used for the topical categories (medical / legal / financial / //! family) where there is no single canonical value shape. @@ -86,6 +87,13 @@ pub(crate) fn is_luhn_valid(raw: &str) -> bool { sum.is_multiple_of(10) } +/// Digit-count bounds for an international phone number: 7–15 digits once +/// separators are removed. E.164 allows at most 15, and nothing shorter than +/// 7 is a dialable subscriber number anywhere. +pub(crate) fn has_international_phone_digit_count(raw: &str) -> bool { + (7..=15).contains(&raw.bytes().filter(u8::is_ascii_digit).count()) +} + /// Validate that a dotted-quad match is a real IPv4 address (each octet 0–255). pub(crate) fn is_ipv4(raw: &str) -> bool { let octets: Vec<&str> = raw.split('.').collect(); @@ -141,9 +149,16 @@ fn build_rules() -> Vec { r"\b(?:\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]\d{3}[-.\s]\d{4}\b", None, ), - // Legacy international phone-like pattern. Its repetition counts - // separators too; normalized digit bounds remain follow-up #263. - pattern_rule(PiiCategory::PhoneNumber, r"\+\d(?:[\d\s\-]{6,16}\d)", None), + // International phone number: `+` and 7–15 digits, with at most one + // space or hyphen between any two digits. The trailing boundary + // refuses a 15-digit prefix of a longer run of digits, so an overlong + // value is left alone rather than matched on its head, and the + // validator pins the digit count the pattern promises. + pattern_rule( + PiiCategory::PhoneNumber, + r"\+\d(?:[\s\-]?\d){6,14}\b", + Some(has_international_phone_digit_count), + ), // IPv4 address (octet-validated). pattern_rule( PiiCategory::IpAddress, From be43a4e18064ca36c0f3741904bb07d74ac4bf07 Mon Sep 17 00:00:00 2001 From: Shaurya Kesarwani Date: Wed, 14 Oct 2026 15:21:13 +0530 Subject: [PATCH 2/3] fix(safety): consume the whole international phone run before bounding it A separator after the fifteenth digit let the word boundary end the match early and accept the 15-digit head of a 16-digit value. The pattern now has no upper bound, so it consumes the whole digit-and-separator run and the validator rejects it as a unit. Separators are a literal space or hyphen; a newline or tab no longer joins two runs into one number. --- crates/tinymemory-api/src/safety/mod_tests.rs | 28 +++++++++++++++++++ crates/tinymemory-api/src/safety/rules.rs | 11 ++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/crates/tinymemory-api/src/safety/mod_tests.rs b/crates/tinymemory-api/src/safety/mod_tests.rs index bd053cad..2aa6a7ca 100644 --- a/crates/tinymemory-api/src/safety/mod_tests.rs +++ b/crates/tinymemory-api/src/safety/mod_tests.rs @@ -104,6 +104,34 @@ fn an_overlong_number_is_not_matched_on_a_prefix() { assert_eq!(redact_identifiers(text), text); } +#[test] +fn an_overlong_separated_number_is_not_matched_on_a_prefix() { + // 16 digits with a separator after the fifteenth: the rule must consume + // the whole run and reject it, not stop at the separator and accept the + // 15-digit head. + for text in [ + "ref +123456789012345-6 end", + "ref +123456789012345 6 end", + "ref +1-234-567-890-123-456 end", + ] { + assert_eq!(scan(text).level, RiskLevel::None, "flagged {text:?}"); + assert_eq!(redact_identifiers(text), text); + } +} + +#[test] +fn international_phone_separators_do_not_span_lines_or_tabs() { + // Only a space or a hyphen joins two digit groups. A newline or a tab + // ends the candidate, so two short runs on separate lines are not glued + // into one phone number. + for text in ["+1234\n567", "+1234\t567", "+123 456\n789 012"] { + assert!( + !scan(text).has_category(PiiCategory::PhoneNumber), + "flagged {text:?}" + ); + } +} + #[test] fn international_phone_redaction_covers_the_whole_number() { assert_eq!( diff --git a/crates/tinymemory-api/src/safety/rules.rs b/crates/tinymemory-api/src/safety/rules.rs index ac6167ce..e9bab467 100644 --- a/crates/tinymemory-api/src/safety/rules.rs +++ b/crates/tinymemory-api/src/safety/rules.rs @@ -150,13 +150,14 @@ fn build_rules() -> Vec { None, ), // International phone number: `+` and 7–15 digits, with at most one - // space or hyphen between any two digits. The trailing boundary - // refuses a 15-digit prefix of a longer run of digits, so an overlong - // value is left alone rather than matched on its head, and the - // validator pins the digit count the pattern promises. + // space or hyphen between any two digits. The pattern has no upper + // bound on purpose: it consumes the whole digit-and-separator run, and + // the validator then rejects any run outside 7–15 digits, so an + // overlong value is left alone rather than matched on a 15-digit head + // that happens to end at a separator. pattern_rule( PiiCategory::PhoneNumber, - r"\+\d(?:[\s\-]?\d){6,14}\b", + r"\+\d(?:[ -]?\d){6,}\b", Some(has_international_phone_digit_count), ), // IPv4 address (octet-validated). From fddaaa36df23658d3446fd142a92e02dcc205ea6 Mon Sep 17 00:00:00 2001 From: M3gA-Mind Date: Wed, 11 Nov 2026 15:10:15 +0530 Subject: [PATCH 3/3] fix(safety): count only ASCII digits in the international phone pattern \d is Unicode-aware in the regex crate, so a run mixing ASCII and other scripts' digits matched whole while the validator counted only its ASCII digits, letting a 16-digit run pass the 7-15 bound. Match [0-9], the set the validator counts. --- crates/tinymemory-api/src/safety/mod_tests.rs | 11 +++++++++++ crates/tinymemory-api/src/safety/rules.rs | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/crates/tinymemory-api/src/safety/mod_tests.rs b/crates/tinymemory-api/src/safety/mod_tests.rs index 8d0bffda..4eb216bd 100644 --- a/crates/tinymemory-api/src/safety/mod_tests.rs +++ b/crates/tinymemory-api/src/safety/mod_tests.rs @@ -171,6 +171,17 @@ fn an_overlong_separated_number_is_not_matched_on_a_prefix() { } } +#[test] +fn only_ascii_digits_make_up_an_international_phone_number() { + // Seven ASCII digits followed by nine Arabic-Indic ones: 16 digits in one + // run, past the 15-digit bound, so it is not a phone number. + let text = "ref +1234567\u{661}\u{662}\u{663}\u{664}\u{665}\u{666}\u{667}\u{668}\u{669} end"; + assert!( + !scan(text).has_category(PiiCategory::PhoneNumber), + "flagged {text:?}" + ); +} + #[test] fn international_phone_separators_do_not_span_lines_or_tabs() { // Only a space or a hyphen joins two digit groups. A newline or a tab diff --git a/crates/tinymemory-api/src/safety/rules.rs b/crates/tinymemory-api/src/safety/rules.rs index 124a0250..a036aa8f 100644 --- a/crates/tinymemory-api/src/safety/rules.rs +++ b/crates/tinymemory-api/src/safety/rules.rs @@ -199,10 +199,12 @@ fn build_rules() -> Vec { // bound on purpose: it consumes the whole digit-and-separator run, and // the validator then rejects any run outside 7–15 digits, so an // overlong value is left alone rather than matched on a 15-digit head - // that happens to end at a separator. + // that happens to end at a separator. Digits are ASCII only, the same + // set the validator counts; `\d` would also take other scripts' digits + // and let a run past 15 digits through. pattern_rule( PiiCategory::PhoneNumber, - r"\+\d(?:[ -]?\d){6,}\b", + r"\+[0-9](?:[ -]?[0-9]){6,}\b", Some(has_international_phone_digit_count), ), // IPv4 address (octet-validated).