From d294eea2035803bcc55c1d1e9f4a77cfe4bfb301 Mon Sep 17 00:00:00 2001 From: jan-kubica Date: Wed, 18 Nov 2026 12:55:41 +0100 Subject: [PATCH 1/3] fix(ee): reject personal ID codes with a future birth date ee.ik accepted an isikukood whose encoded birth date has not happened yet. stdnum-js 1.16 now rejects these, as dk.cpr and no.fodselsnummer already do here, so the oracle gate reported disagreements once the comparator was updated. The birth date is compared against the shared current_date() clock; the date logic takes "today" as a parameter so the boundary tests are deterministic. A future date reports an invalid component. Bumps the stdnum-js oracle to 1.16.0 in the same change so the gate checks the new rule, and moves python-stdnum:ee.ik to survey-only since python-stdnum still accepts future dates. --- .changeset/estonian-future-birth-date.md | 5 ++ bun.lock | 4 +- crates/stdnum-core/src/lib.rs | 84 ++++++++++++++----- .../src/validators/legacy_specs.rs | 5 ++ package.json | 2 +- scripts/oracle.ts | 3 + 6 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 .changeset/estonian-future-birth-date.md diff --git a/.changeset/estonian-future-birth-date.md b/.changeset/estonian-future-birth-date.md new file mode 100644 index 00000000..fea76e94 --- /dev/null +++ b/.changeset/estonian-future-birth-date.md @@ -0,0 +1,5 @@ +--- +"@stll/stdnum": patch +--- + +Reject Estonian personal ID codes (isikukood) whose birth date is in the future, as Danish CPR and Norwegian birth numbers already are. diff --git a/bun.lock b/bun.lock index 0619d24b..26ab5208 100644 --- a/bun.lock +++ b/bun.lock @@ -23,7 +23,7 @@ "oxlint": "1.93.0", "oxlint-tsgolint": "7.0.2006", "rut.js": "^2.1.0", - "stdnum": "^1.15.0", + "stdnum": "^1.16.0", "tsdown": "0.23.7", "validate-polish": "^2.1.40", "vite": "8.3.7", @@ -465,7 +465,7 @@ "source-map-js": ["source-map-js@1.2.2", "", {}, "sha512-KGj/8Y43x35aZVDtt+J4mK1hoLGHULMYfSkODJNQjNDC3oW1PqPoxMwo0pLUsWM/UEGzON/NxeHywEfNXNP3Vw=="], - "stdnum": ["stdnum@1.15.0", "", {}, "sha512-ke7nTSVB0B8XLTzLAnRzvOlfk69QCJYDhwK0Vv4/kYWH9dSDRW9yHIomPCVyCNd3agCzfnEguldaFwYqe4p9jA=="], + "stdnum": ["stdnum@1.16.0", "", {}, "sha512-Kdb2KqJT9Sxh6XORFGP0LHYGker7CG3isOroR3C2PwH0LPuYL/0LtexT2JI7oIelMv5OtOwiqLSo+u5tkLKKuA=="], "tinyexec": ["tinyexec@1.3.1", "", {}, "sha512-GCvB3aoys96IuDFBMcTB46JOR6mdMtAToqwiW8JlWhsoh1mhHi/xn9ss/Dg7N555GiJyEt2qzoG/NHCwM6h1EA=="], diff --git a/crates/stdnum-core/src/lib.rs b/crates/stdnum-core/src/lib.rs index fe47e49e..4b82ad4f 100644 --- a/crates/stdnum-core/src/lib.rs +++ b/crates/stdnum-core/src/lib.rs @@ -808,32 +808,38 @@ fn validate_dk_vat(value: &str) -> bool { } fn validate_ee_ik(value: &str) -> bool { + ee_ik_valid_on(value, current_date()) +} + +/// Whether `value` is a valid isikukood on `today`: a person cannot be +/// registered before their birth date. +fn ee_ik_valid_on(value: &str, today: (u32, u32, u32)) -> bool { + ee_ik_birth_date(value).is_some_and(|birth_date| birth_date <= today) +} + +/// Whether `value` is a well-formed isikukood whose birth date comes after +/// the current date. +pub(crate) fn ee_ik_birth_date_is_future(value: &str) -> bool { + ee_ik_birth_date(value).is_some_and(|birth_date| birth_date > current_date()) +} + +/// Birth date of a structurally valid isikukood with a correct check digit. +fn ee_ik_birth_date(value: &str) -> Option<(u32, u32, u32)> { let compact = compact_without(value, &[' ', '-']); - let Ok(digits) = <[u32; 11]>::try_from(decimal_digits_strict(&compact)) - else { - return false; - }; - let Some(gender_century) = digits.first().copied() else { - return false; - }; - let century: u32 = match gender_century { + let digits = <[u32; 11]>::try_from(decimal_digits_strict(&compact)).ok()?; + let century: u32 = match digits.first().copied()? { 1 | 2 => 1800, 3 | 4 => 1900, 5 | 6 => 2000, - _ => return false, - }; - let Some(yy) = two_digit_number(&digits, 1) else { - return false; - }; - let Some(month) = two_digit_number(&digits, 3) else { - return false; - }; - let Some(day) = two_digit_number(&digits, 5) else { - return false; + _ => return None, }; - valid_date(century.saturating_add(yy), month, day) + let year = century.saturating_add(two_digit_number(&digits, 1)?); + let month = two_digit_number(&digits, 3)?; + let day = two_digit_number(&digits, 5)?; + (valid_date(year, month, day) && two_pass_personal_check(digits.get(..10).unwrap_or(&[])) - == digits.get(10).copied() + == digits.get(10).copied()) + .then_some((year, month, day)) } fn validate_ee_vat(value: &str) -> bool { @@ -2611,6 +2617,44 @@ mod tests { )); } + #[test] + fn estonian_personal_id_rejects_future_birth_dates() { + use super::ee_ik_valid_on; + use crate::{ValidationError, validators::ee::ik}; + + let today = (2026, 11, 18); + // Born today (century digit 6: 2000s) is valid, tomorrow is not. + assert!(ee_ik_valid_on("62611180000", today)); + assert!(ee_ik_valid_on("52611180003", today)); + assert!(!ee_ik_valid_on("62611190007", today)); + assert!(!ee_ik_valid_on("52611190006", today)); + assert!(ee_ik_valid_on("62611190007", (2026, 11, 19))); + // Later years of the current century are in the future. + assert!(!ee_ik_valid_on("62701010000", today)); + assert!(!ee_ik_valid_on("69912310002", today)); + // Earlier centuries are always in the past. + assert!(ee_ik_valid_on("19912310008", today)); + assert!(ee_ik_valid_on("40001010005", today)); + assert!(ee_ik_valid_on("36805280109", today)); + assert!(ee_ik_valid_on("50002290002", today)); + // A future date does not rescue a bad check digit. + assert!(!ee_ik_valid_on("19912310009", today)); + + // 2094-03-06 with a correct check digit. + assert_eq!( + ik::validate("59403068580"), + Err(ValidationError::InvalidComponent( + "Estonian personal ID contains a birth date in the future", + )) + ); + assert!(!validate_named_id("ee.ik", "59403068580")); + assert_eq!(ik::validate("36805280109"), Ok("36805280109".to_owned())); + assert!(matches!( + ik::validate("59403068581"), + Err(ValidationError::InvalidChecksum(_)) + )); + } + #[test] fn rejects_unknown_validators() { assert!(!validate_named_id("unknown", "111000025")); diff --git a/crates/stdnum-core/src/validators/legacy_specs.rs b/crates/stdnum-core/src/validators/legacy_specs.rs index aaac24ca..52c10c0a 100644 --- a/crates/stdnum-core/src/validators/legacy_specs.rs +++ b/crates/stdnum-core/src/validators/legacy_specs.rs @@ -1563,6 +1563,11 @@ fn validate_for(id: &str, value: &str) -> ValidationResult { "identifier contains an invalid component", )); } + if id == "ee.ik" && crate::ee_ik_birth_date_is_future(&compact) { + return Err(ValidationError::InvalidComponent( + "Estonian personal ID contains a birth date in the future", + )); + } if compact.is_empty() || !compact .chars() diff --git a/package.json b/package.json index 378bd39a..f44a48af 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,7 @@ "oxlint": "1.93.0", "oxlint-tsgolint": "7.0.2006", "rut.js": "^2.1.0", - "stdnum": "^1.15.0", + "stdnum": "^1.16.0", "tsdown": "0.23.7", "validate-polish": "^2.1.40", "vite": "8.3.7" diff --git a/scripts/oracle.ts b/scripts/oracle.ts index 7650e38a..6d46b3af 100644 --- a/scripts/oracle.ts +++ b/scripts/oracle.ts @@ -1058,6 +1058,9 @@ const SURVEY_ONLY_ENTRIES = new Set([ "python-stdnum:br.cpf", "python-stdnum:cn.ric", "python-stdnum:cz.rc", + // python-stdnum accepts isikukood birth dates after + // the current date; we reject them, as stdnum-js does. + "python-stdnum:ee.ik", "python-stdnum:do_.rnc", "python-stdnum:id.npwp", "python-stdnum:lv.vat", From 754d68168cdcd22caf9e5ef243be6a36d140bfa8 Mon Sep 17 00:00:00 2001 From: jan-kubica Date: Wed, 18 Nov 2026 12:59:27 +0100 Subject: [PATCH 2/3] fix(ee): keep the future-date helper private --- crates/stdnum-core/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/stdnum-core/src/lib.rs b/crates/stdnum-core/src/lib.rs index 4b82ad4f..3763522b 100644 --- a/crates/stdnum-core/src/lib.rs +++ b/crates/stdnum-core/src/lib.rs @@ -819,7 +819,7 @@ fn ee_ik_valid_on(value: &str, today: (u32, u32, u32)) -> bool { /// Whether `value` is a well-formed isikukood whose birth date comes after /// the current date. -pub(crate) fn ee_ik_birth_date_is_future(value: &str) -> bool { +fn ee_ik_birth_date_is_future(value: &str) -> bool { ee_ik_birth_date(value).is_some_and(|birth_date| birth_date > current_date()) } From 13cfd2293074801c99d1192ebb57ecfc5528a8df Mon Sep 17 00:00:00 2001 From: jan-kubica Date: Wed, 18 Nov 2026 13:07:14 +0100 Subject: [PATCH 3/3] fix(ee): classify future dates from one clock reading; run the oracle in UTC --- crates/stdnum-core/src/lib.rs | 6 ------ .../stdnum-core/src/validators/legacy_specs.rs | 17 ++++++++++++----- package.json | 6 +++--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/crates/stdnum-core/src/lib.rs b/crates/stdnum-core/src/lib.rs index 3763522b..00c46c39 100644 --- a/crates/stdnum-core/src/lib.rs +++ b/crates/stdnum-core/src/lib.rs @@ -817,12 +817,6 @@ fn ee_ik_valid_on(value: &str, today: (u32, u32, u32)) -> bool { ee_ik_birth_date(value).is_some_and(|birth_date| birth_date <= today) } -/// Whether `value` is a well-formed isikukood whose birth date comes after -/// the current date. -fn ee_ik_birth_date_is_future(value: &str) -> bool { - ee_ik_birth_date(value).is_some_and(|birth_date| birth_date > current_date()) -} - /// Birth date of a structurally valid isikukood with a correct check digit. fn ee_ik_birth_date(value: &str) -> Option<(u32, u32, u32)> { let compact = compact_without(value, &[' ', '-']); diff --git a/crates/stdnum-core/src/validators/legacy_specs.rs b/crates/stdnum-core/src/validators/legacy_specs.rs index 52c10c0a..6e177f70 100644 --- a/crates/stdnum-core/src/validators/legacy_specs.rs +++ b/crates/stdnum-core/src/validators/legacy_specs.rs @@ -1533,6 +1533,18 @@ fn validate_for(id: &str, value: &str) -> ValidationResult { } } let compact = compact_for(id, value); + if id == "ee.ik" { + // Decide acceptance and the error kind from one date reading. + if let Some(birth_date) = crate::ee_ik_birth_date(&compact) { + return if birth_date <= crate::current_date() { + Ok(compact) + } else { + Err(ValidationError::InvalidComponent( + "Estonian personal ID contains a birth date in the future", + )) + }; + } + } if raw_is_valid(id, &compact) { return Ok(compact); } @@ -1563,11 +1575,6 @@ fn validate_for(id: &str, value: &str) -> ValidationResult { "identifier contains an invalid component", )); } - if id == "ee.ik" && crate::ee_ik_birth_date_is_future(&compact) { - return Err(ValidationError::InvalidComponent( - "Estonian personal ID contains a birth date in the future", - )); - } if compact.is_empty() || !compact .chars() diff --git a/package.json b/package.json index f44a48af..e01820fa 100644 --- a/package.json +++ b/package.json @@ -24,9 +24,9 @@ "performance:oracles": "node scripts/oracle-performance-gate.mjs", "codegen": "bun scripts/generate-public-surface.ts", "codegen:check": "bun scripts/generate-public-surface.ts --check", - "oracle": "bun scripts/oracle.ts --mode=gate", - "oracle:gate": "bun scripts/oracle.ts --mode=gate", - "oracle:survey": "bun scripts/oracle.ts --mode=survey", + "oracle": "TZ=UTC bun scripts/oracle.ts --mode=gate", + "oracle:gate": "TZ=UTC bun scripts/oracle.ts --mode=gate", + "oracle:survey": "TZ=UTC bun scripts/oracle.ts --mode=survey", "rust:fmt": "cargo ci-fmt", "rust:fmt:check": "cargo ci-fmt", "rust:lint": "cargo ci-clippy",