Dates: four-digit years from 1900 to 2100
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:
1 parent
38070ee135
commit
d543c929e9
6 files changed
+147
-10
No files matched your search
@@ -5,6 +5,23 @@ use sqlx::PgPool;
|
||||
|
||||
use crate::common::{assert_unique_ids, local};
|
||||
|
||||
/// Every date input carries `min` and `max` from `visa::dates`: without a
|
||||
/// `max`, Chromium lets its year part take six digits (`202611-12-05`).
|
||||
/// Returns how many date inputs the page has.
|
||||
fn assert_date_limits(html: &str, page: &str) -> usize {
|
||||
let min = format!("min=\"{}-01-01\"", visa::dates::FIRST_YEAR);
|
||||
let max = format!("max=\"{}-12-31\"", visa::dates::LAST_YEAR);
|
||||
let mut n = 0;
|
||||
for tag in html.split("<input").skip(1) {
|
||||
let tag = &tag[..tag.find('>').unwrap_or(tag.len())];
|
||||
if tag.contains("type=\"date\"") {
|
||||
assert!(tag.contains(&min) && tag.contains(&max), "{page}: {tag}");
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
n
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn every_page_renders_with_demo_data(pool: PgPool) {
|
||||
assert!(visa::seed::demo(&pool, visa::dates::today()).await.unwrap());
|
||||
@@ -116,6 +133,7 @@ async fn every_page_renders_with_demo_data(pool: PgPool) {
|
||||
(format!("/activity?person={erika}"), "MUSTERMANN, Erika"),
|
||||
];
|
||||
pages.extend(older.into_iter().map(|(p, e)| (p, e.to_string())));
|
||||
let mut date_inputs = 0;
|
||||
for (path, expect) in &pages {
|
||||
let r = app.get(path).await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{path}: {}", r.body);
|
||||
@@ -125,7 +143,9 @@ async fn every_page_renders_with_demo_data(pool: PgPool) {
|
||||
);
|
||||
assert_unique_ids(&r.body, path);
|
||||
assert_eq!(r.headers["cache-control"], "no-store", "{path}");
|
||||
date_inputs += assert_date_limits(&r.body, path);
|
||||
}
|
||||
assert!(date_inputs >= 20, "only {date_inputs} date inputs seen");
|
||||
|
||||
// warnings from the demo dates: Jane's passport runs out in 95 days, she
|
||||
// has no TM.30 receipt, Tomas's passport has under a year
|
||||
|
||||
@@ -164,6 +164,77 @@ async fn validation_and_edits_with_changed_fields(pool: PgPool) {
|
||||
assert_eq!(audit_rows(&pool, id).await.last().unwrap().0, "view");
|
||||
}
|
||||
|
||||
/// Chromium sends `202611-12-05` when the year part took six digits, and
|
||||
/// `0026-...` for a two-digit year; neither may reach the database.
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn dates_need_a_four_digit_year_from_1900_to_2100(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
let r = app
|
||||
.post(
|
||||
"/people",
|
||||
&[
|
||||
("surname", "Doe"),
|
||||
("first_name", "Jane"),
|
||||
("date_of_birth", "0026-11-12"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(
|
||||
r.text().contains("Enter a year from 1900 to 2100."),
|
||||
"{}",
|
||||
r.text()
|
||||
);
|
||||
|
||||
let r = app
|
||||
.post("/people", &[("surname", "Doe"), ("first_name", "Jane")])
|
||||
.await;
|
||||
let id = id_from(r.location());
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/people/{id}/passports"),
|
||||
&[
|
||||
("number", "X1234567"),
|
||||
("issuing_country", "USA"),
|
||||
("issued_on", "202611-12-05"),
|
||||
("expires_on", "2036-11-11"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
let t = r.text();
|
||||
assert!(t.contains("Enter a date such as 2026-11-17."), "{t}");
|
||||
assert!(
|
||||
r.body.contains("value=\"202611-12-05\""),
|
||||
"the typed value stays"
|
||||
);
|
||||
|
||||
let r = app
|
||||
.post(
|
||||
&format!("/people/{id}/arrivals"),
|
||||
&[
|
||||
("arrived_on", "0026-09-01"),
|
||||
("admission", "exempt"),
|
||||
("permitted_until", "2026-10-30"),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
|
||||
assert!(
|
||||
r.text().contains("Enter a year from 1900 to 2100."),
|
||||
"{}",
|
||||
r.text()
|
||||
);
|
||||
|
||||
let saved: i64 = sqlx::query_scalar(
|
||||
"SELECT (SELECT count(*) FROM passports) + (SELECT count(*) FROM arrivals)",
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(saved, 0);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
|
||||
async fn archive_and_restore(pool: PgPool) {
|
||||
let app = local(pool.clone());
|
||||
|
||||
Reference in new issue
Block a user