Files
visa/AGENTS.md
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

85 lines
5.5 KiB
Markdown

# Working on this repository (for agents and people)
visa.buildfor.life holds passport data of real people. Read this file and
[docs/PLAN.md](docs/PLAN.md) before changing anything.
## Rules
- **No real personal data** in the repo, in tests, in demo data or in
screenshots. Demo people are invented (`src/seed.rs`) and carry the note
"Demo record, invented for development." Test passports use invented
numbers such as `C01X00T47`.
- **No secrets** in the repo. Secrets reach the app as files or systemd
credentials (`src/config.rs`); the pre-commit hook and CI run gitleaks.
- **The audit log stays complete.** Every handler that shows a person's record
writes a `view` row, and every change writes a row with the names of the
changed fields (`changed_fields!` in `src/people.rs`), never their values.
The table refuses `UPDATE` and `DELETE` (`migrations/0001_init.sql`).
- **Every POST** goes through the same-site check and the form token
(`src/web/mod.rs`); forms carry `<input type="hidden" name="csrf">`. An
upload (multipart) is the one exception in the guard: its handler checks
the token itself, which must be the form's first field, before it reads a
file (`src/web/documents.rs`).
- **Documents** are files the app encrypts before it stores them
(`src/files.rs`): in a bucket of the fleet's Garage on the server
(`VISA_S3_*`, `src/s3.rs`), in a directory in development
(`VISA_FILES_DIR`). Never write a document's file another way, never send
one unencrypted to the bucket, and never log its contents or its name.
Opening a file is a `view` of the document, saving it a `download`. Local
files, the dev Garage's key and the file key live in `.dev/`, which git
ignores.
- **No inline script or style.** The Content Security Policy refuses them;
`tests/pages.rs` checks the templates.
- **Migrations** are never edited once they are on `main`; add a new file.
- **A new kind of record** for a stay is a migration and one `Kind` in
`src/records.rs`; the routes, the form, the Stay tab and the audit entries
follow from it. A reference to another record is a selection (`Input::
Passport`, `Visa`, `Company`) whose choices are checked on the server.
- **Deadline rules** live in `src/deadlines.rs` with a unit test each, and in
docs/PLAN.md ("Deadline rules") with their sources. A date from a receipt
or a stamp wins over a calculated one.
- **Dates** are shown as "Nov 17, 2026" in Chiang Mai time (`src/dates.rs`);
countries are ISO 3166-1 alpha-3 codes as in the passport's zone
(`src/countries.rs`, generated by `scripts/gen_countries.py`).
- **Writing** for people (pages, docs) follows the house style: plain American
English in whole sentences, no em dashes, "Nov 17, 2026" in tables.
## Checks before a push
```sh
eval "$(bash scripts/dev-db.sh)"
cargo fmt --all --check
cargo clippy --all-targets -- -D warnings
cargo test
```
CI (`.gitea/workflows/ci.yml`) runs the same against PostgreSQL 18, plus the
secret scan over the whole history. For a page change, look at it in light
and dark, at desktop and phone width, with `scripts/dev.sh` running.
## Layout
| Path | What |
| --------------------- | --------------------------------------------------------------------- |
| `src/main.rs` | the commands `serve`, `migrate`, `seed-demo`, `files-check`, `files-migrate`, `files-mirror` |
| `src/config.rs` | settings from the environment and credentials |
| `src/oidc.rs` | OpenID Connect against Kanidm (PKCE, nonce, ES256), from the fleet overview |
| `src/session.rs` | sessions in PostgreSQL, keyed by the SHA-256 of the cookie |
| `src/audit.rs` | the activity log |
| `src/people.rs` | people, passports, addresses: validation and SQL |
| `src/records.rs` | the stay records and companies, one `Kind` each: fields, checks, SQL, display |
| `src/deadlines.rs` | the deadline rules as a pure function (`assess`), and loading their facts |
| `src/companies.rs` | what only companies need: list, archive, selections, employees |
| `src/mrz.rs` | reading the passport's machine-readable zone (ICAO 9303, TD3) |
| `src/files.rs` | the encrypted document store (a bucket or a directory): sealing, opening, the mirror for backups, checking a file's type |
| `src/s3.rs` | a small S3 client with SigV4 signing, for the bucket in Garage |
| `src/documents.rs` | documents of people and companies: kinds, records, SQL, `files-check`, `files-migrate` |
| `src/papers/` | the Immigration Bureau's forms filled in (`layout.rs` is generated by `scripts/gen_layout.py`) |
| `src/pack/` | the extension pack: its rules as a pure function (`assess`), and the pack as one PDF (`pdf.rs`) |
| `static/mrz-photo.js` | the passport photo reader in the browser (camera view, Tesseract.js from `static/vendor/tesseract/`, see its README.txt); the server's half is `mrz::from_ocr` |
| `src/web/` | routes, the login gate, pages |
| `templates/` | minijinja templates, compiled in by `build.rs` |
| `static/` | CSS and JavaScript, compiled in; the URL carries a content hash |
| `migrations/` | SQL migrations, compiled in |
| `tests/` | integration tests, one binary (`tests/main.rs` declares each file) |