Dates: four-digit years from 1900 to 2100
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 20s

Chromium's date input lets the year part take six digits when the input
has no max, so typing 2026 and then 11 for the month gave 202611-12-05
(reported on the live app). Every date input now carries
min="1900-01-01" and max="2100-12-31", so the year part stops at four
digits and moves on to the month. Checked in Edge: typing 20261112 gives
2026-11-12, where it left the field empty before.

The server checks the same years for every date field
(dates::parse_form_date). Person, passport and address dates were only
parsed, so a two-digit year such as 0026-11-12 would have been saved;
stay records had the range already. A six-digit year never parsed, so no
such date can be in the database.

Tests: the parser's cases; the person, passport and arrival forms refuse
both years and save nothing; every date input on every page carries the
limits (pages.rs).
This commit is contained in:
grabowski committed 2026-11-17 21:15:14 +07:00
1 parent 38070ee135
commit d543c929e9
6 files changed
+147 -10

No files matched your search

+43
View File
@@ -29,6 +29,23 @@ pub fn parse_date(s: &str) -> Option<NaiveDate> {
NaiveDate::parse_from_str(s.trim(), "%Y-%m-%d").ok()
}
/// The years a date field accepts. Every date input carries the same limits
/// as `min` and `max` (`templates/_fields.html`); without a `max`, Chromium
/// lets the year part take six digits and never moves on to the month.
pub const FIRST_YEAR: i32 = 1900;
pub const LAST_YEAR: i32 = 2100;
/// A date typed into a form: a calendar date from `FIRST_YEAR` to
/// `LAST_YEAR`, or the message to show next to the field.
pub fn parse_form_date(s: &str) -> Result<NaiveDate, &'static str> {
let d = parse_date(s).ok_or("Enter a date such as 2026-11-17.")?;
if (FIRST_YEAR..=LAST_YEAR).contains(&d.year()) {
Ok(d)
} else {
Err("Enter a year from 1900 to 2100.")
}
}
/// Full years from `born` to `on`.
pub fn age(born: NaiveDate, on: NaiveDate) -> i32 {
let mut years = on.year() - born.year();
@@ -114,6 +131,32 @@ mod tests {
assert_eq!(parse_date(" 2026-02-30 "), None);
}
#[test]
fn form_dates_have_four_digit_years_in_range() {
assert_eq!(parse_form_date("2026-11-12"), Ok(d("2026-11-12")));
assert_eq!(parse_form_date("1900-01-01"), Ok(d("1900-01-01")));
assert_eq!(parse_form_date("2100-12-31"), Ok(d("2100-12-31")));
// what Chromium sends when the year part took six digits
assert_eq!(
parse_form_date("202611-12-05"),
Err("Enter a date such as 2026-11-17.")
);
// a two-digit year typed into the year part
assert_eq!(
parse_form_date("0026-11-12"),
Err("Enter a year from 1900 to 2100.")
);
assert_eq!(
parse_form_date("2101-01-01"),
Err("Enter a year from 1900 to 2100.")
);
let years = format!("{FIRST_YEAR} to {LAST_YEAR}");
assert!(
parse_form_date("1899-12-31").unwrap_err().contains(&years),
"the message names the limits"
);
}
#[test]
fn ages() {
assert_eq!(age(d("1984-08-12"), d("2026-08-11")), 41);
+6 -4
View File
@@ -280,11 +280,13 @@ fn optional_date(value: &str, field: &str, e: &mut Errors) -> Option<NaiveDate>
if value.is_empty() {
return None;
}
let d = dates::parse_date(value);
if d.is_none() {
err(e, field, "Enter a date (year-month-day).");
match dates::parse_form_date(value) {
Ok(d) => Some(d),
Err(msg) => {
err(e, field, msg);
None
}
}
d
}
pub async fn get_person(db: &PgPool, id: i64) -> Result<Option<Person>> {
+6 -5
View File
@@ -8,7 +8,7 @@
use std::collections::BTreeMap;
use anyhow::Result;
use chrono::{Datelike, NaiveDate, TimeDelta};
use chrono::{NaiveDate, TimeDelta};
use serde::Serialize;
use sqlx::postgres::PgRow;
use sqlx::{PgConnection, PgPool, Postgres, QueryBuilder, Row};
@@ -670,10 +670,11 @@ pub fn validate(kind: &Kind, v: &Values, opts: &Options) -> Errors {
continue;
}
match fl.input {
Input::Date => match dates::parse_date(value) {
Some(d) if (1900..=2100).contains(&d.year()) => {}
_ => err(&mut e, fl.name, "Enter a date such as 2026-11-17."),
},
Input::Date => {
if let Err(msg) = dates::parse_form_date(value) {
err(&mut e, fl.name, msg);
}
}
Input::Choice(list) => {
if label(list, value).is_none() {
err(&mut e, fl.name, "Choose from the list.");