Files
visa/tests/mrz_photo.rs
T
grabowski 1f54d93643
CI / secret scan (push) Successful in 6s
CI / fmt, clippy, tests (push) Successful in 28s
Passport photo reader: a camera view with a frame, read in the browser
On the new person and new passport forms, next to Read:

- "Take a photo" opens a camera view (getUserMedia, the back camera): a
  frame of the photo page's shape (125 by 88 mm), a band for the two lines,
  and a hint from four checks of the band four times a second: too dark,
  glare on the lines, no lines in the band, not sharp yet (variance of the
  Laplacian). Once the band has looked readable for a second it takes the
  photo itself; "Take the photo" and Cancel (or Escape) are there too.
  The band where the frame showed it is read first; when that holds no
  zone, the whole frame is searched.
- "Choose a picture" reads any image: EXIF orientation, the four quarter
  turns, a tilt of 7 degrees either way; a failed read says when there is
  glare in the picture.

Reading happens in the browser (static/mrz-photo.js): even light from a
local mean, the lines found with Tesseract's page layout, then the band cut
along their baselines, scaled and read as one block. Tesseract.js 7.0.0
and its core (SIMD and plain LSTM builds) with Doubango's MRZ model, all
served by the app from static/vendor/tesseract/ (README.txt: sources, npm
integrity, SHA-256, licenses Apache-2.0 and BSD-3), loaded at the first
photo. The photo never leaves the device: only the lines go into the zone's
field, and Read is pressed.

The server reads them like typed ones, through mrz::from_ocr when they are
no clean zone: the first line wherever it is, the second whose check digits
match best, a miscounted filler run cut or filled, K for < in the filler,
letters and digits where the zone has them, and look-alikes (O for 0, B for
8) swapped in the number fields only where a check digit asks for it,
preferring a digit. A photo's note asks to compare the names, which have no
check digit.

Only those two pages and the reader's files allow WebAssembly
('wasm-unsafe-eval', worker-src 'self', img-src blob:) and the camera
(Permissions-Policy camera=(self)); every other page keeps camera=().
Content types for .wasm and .gz.

Checks: fmt, clippy -D warnings, 99 unit and 50 integration tests (new:
from_ocr cases from real reads, tests/mrz_photo.rs for the markup, the two
policies, the files and a read from photo lines). In Edge (Playwright): 12
made-up phone photos and 16 specimen pages from Wikimedia Commons, every
zone that is readable read with all check digits matching (not: a hard
shadow across the lines; the specimens whose zone is blanked out), 1 to 5 s
each; the camera view with a fake camera: the glare and dark hints, and a
good picture taken by itself and the form filled in. Layout check 0
problems on both forms and a person, light and dark, desk and phone.

PLAN: task 7.3 done ahead of the client link, the decision recorded.
2026-11-20 16:47:40 +07:00

125 lines
4.2 KiB
Rust

//! The passport photo reader: its button on the forms, the policy that lets
//! its WebAssembly run there and nowhere else, its files, and the lines it
//! sends read like typed ones.
use axum::http::StatusCode;
use sqlx::PgPool;
use crate::common::local;
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_forms_offer_a_photo_and_allow_its_reader(pool: PgPool) {
let app = local(pool);
let r = app.get("/people/new").await;
assert_eq!(r.status, StatusCode::OK);
let t = r.text();
assert!(t.contains("data-mrz-camera>Take a photo</button>"), "{t}");
assert!(
t.contains(r#"<input type="file" accept="image/*" data-mrz-photo"#),
"{t}"
);
assert!(t.contains(r#"<input type="hidden" name="mrz_source" value="">"#));
assert!(t.contains("/static/mrz-photo.js?v="));
assert!(
r.header("content-security-policy")
.contains("'wasm-unsafe-eval'")
);
assert!(r.header("permissions-policy").contains("camera=(self)"));
// other pages keep the strict policy and no camera
let list = app.get("/people").await;
assert!(!list.header("content-security-policy").contains("wasm"));
assert!(list.header("permissions-policy").contains("camera=()"));
assert!(!list.text().contains("mrz-photo.js"));
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn the_reader_files_are_served_with_their_policy(pool: PgPool) {
let app = local(pool);
for (file, kind) in [
("mrz-photo.js", "text/javascript; charset=utf-8"),
(
"vendor/tesseract/tesseract.min.js",
"text/javascript; charset=utf-8",
),
(
"vendor/tesseract/worker.min.js",
"text/javascript; charset=utf-8",
),
(
"vendor/tesseract/tesseract-core-simd-lstm.js",
"text/javascript; charset=utf-8",
),
(
"vendor/tesseract/tesseract-core-simd-lstm.wasm",
"application/wasm",
),
(
"vendor/tesseract/tesseract-core-lstm.wasm",
"application/wasm",
),
("vendor/tesseract/mrz.traineddata.gz", "application/gzip"),
] {
let r = app.get(&format!("/static/{file}")).await;
assert_eq!(r.status, StatusCode::OK, "{file}");
assert_eq!(r.header("content-type"), kind, "{file}");
let csp = r.header("content-security-policy");
assert_eq!(
csp.contains("'wasm-unsafe-eval'"),
file.starts_with("vendor/"),
"{file}"
);
}
}
#[sqlx::test(migrator = "visa::db::MIGRATOR")]
async fn lines_from_a_photo_fill_in_the_form(pool: PgPool) {
let app = local(pool);
// as the reader sends them: the closer read (a 3 misread), then the
// first look, out of order; O for 0 in a date
let read = "3334013720GBR5802140M27O1013<<<<<<<<<<<<<<06\n\
P<GBROBRIEN<SMITH<<JOHN<PAUL<<<<<<<<<<<<<<<<\n\
5334013720GBR5802140M2701013<<<<<<<<<<<<<<06";
let r = app
.post(
"/people",
&[
("action", "read-mrz"),
("mrz", read),
("mrz_source", "photo"),
],
)
.await;
assert_eq!(r.status, StatusCode::OK);
let t = r.text();
assert!(
t.contains(
"P<GBROBRIEN<SMITH<<JOHN<PAUL<<<<<<<<<<<<<<<<\n5334013720GBR5802140M2701013&lt;"
) || t.contains("P&lt;GBROBRIEN&lt;SMITH&lt;&lt;JOHN&lt;PAUL"),
"{t}"
);
assert!(
t.contains("Read from the photo; every check digit matches."),
"{t}"
);
assert!(t.contains(r#"value="OBRIEN SMITH""#), "{t}");
assert!(t.contains(r#"value="1958-02-14""#), "{t}");
// what is no zone stays as typed, with the reason
let r = app
.post(
"/people",
&[
("action", "read-mrz"),
("mrz", "PASSPORT\nREISEPASS"),
("mrz_source", "photo"),
],
)
.await;
assert_eq!(r.status, StatusCode::UNPROCESSABLE_ENTITY);
let t = r.text();
assert!(
t.contains("a passport&#x27;s lines have 44") || t.contains("a passport's lines have 44"),
"{t}"
);
}