diff --git a/.changeset/estonian-future-birth-date.md b/.changeset/estonian-future-birth-date.md new file mode 100644 index 0000000..fea76e9 --- /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 0619d24..26ab520 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 fe47e49..00c46c3 100644 --- a/crates/stdnum-core/src/lib.rs +++ b/crates/stdnum-core/src/lib.rs @@ -808,32 +808,32 @@ 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) +} + +/// 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 +2611,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 aaac24c..6e177f7 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); } diff --git a/package.json b/package.json index 378bd39..e01820f 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", @@ -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 7650e38..6d46b3a 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",