Phase 4: documents, encrypted on the server
CI / secret scan (push) Successful in 5s
CI / fmt, clippy, tests (push) Successful in 53s

Scans and photos of a person's papers, and a company's documents for the
extension packs, uploaded on a dashed strip that takes dropped files: up to
10 files of 20 MB each, 64 MB in all; PDF, JPEG, PNG and WebP, told by their
first bytes.

- src/files.rs: one file per document as <id>.vdoc, AES-256-GCM under a key
  per file (HKDF-SHA256 from the file key and a random salt in the header);
  the document's id is authenticated with it. The key comes from
  VISA_FILES_KEY_FILE or the credential files-key; with the login on, the
  app does not start without it and VISA_FILES_DIR.
- migrations/0003: documents (a person's or a company's, the record it
  belongs to, kind, date on it, note, file name, type, size, SHA-256);
  audit_log gets company_id and the action download.
- A Documents tab per person, the documents on the company's page, and each
  record's documents next to its Edit link with "+ Document".
- An upload's form token is its first field and is checked before any file
  is read; the guard lets multipart through only to the two upload addresses.
- Files open under their own CSP and are never cached; a download is named
  after the person or company, the kind and the date. Every upload, view,
  download, change and deletion is in the audit log.
- visa files-check; demo documents in seed-demo; scripts/dev.sh keeps the
  local documents and their key in .dev/; /favicon.ico for image and PDF
  pages.

Tests: 57 unit and 33 integration (8 new for documents). Edge check of the
strip (choose, drop, too large), an upload, the PDF and image viewers under
the file policy: no problems, no console errors.
This commit is contained in:
grabowski committed 2026-11-18 10:23:41 +07:00
1 parent d543c929e9
commit 2061e3a024
35 files changed
+4256 -58

No files matched your search

+46 -4
View File
@@ -1,7 +1,8 @@
# visa.buildfor.life: plan
> Phases 1 to 3 built on November 17, 2026; the app is live at
> https://visa.buildfor.life. **Resume here: phase 4.**
> Phases 1 to 3 built on November 17, 2026, and phase 4's app side on
> November 18, 2026; the app is live at https://visa.buildfor.life. **Resume
> here: task 4.6, the fleet side of the documents, then phase 5.**
visa.buildfor.life keeps the immigration records of the foreigners that B4L
looks after: their passports, addresses in Thailand, arrivals with the Thailand
@@ -57,6 +58,15 @@ inline script or style.
(PDPA). Every view of a person and every change is written to `audit_log`
with the login, the record and the names of the changed fields, never their
values. The table refuses updates and deletes. The Activity page lists it.
- **Documents.** Scans and photos lie in a files directory on the server,
one file per document, encrypted by the app with AES-256-GCM. Each file has
a key of its own, derived with HKDF-SHA256 from the app's file key and a
random salt in the file's header, and the document's id is authenticated
with it, so a file renamed to another document's id does not open. The
file key is a systemd credential that is never in the database, so neither
the files nor their backups can be read without it. The database holds what
is known about each file: its kind, the record it belongs to, the date on
it, a note, its name, type, size and SHA-256.
- **Passport data entry.** The two lines of the machine-readable zone (MRZ)
at the bottom of the photo page fill the name, nationality, date of birth,
sex, passport number, issuing state and expiry date. Their check digits
@@ -81,7 +91,7 @@ made of.
| `reentry_permits` | TM.8 permits, single or multiple, valid until | 2 |
| `ninety_day_reports` | the report date, the way it was filed, the receipt and the next due date printed on it | 2 |
| `work_permits` | number, employer (a company), position, validity | 2 |
| `documents` | encrypted uploads tied to a person and a record | 4 |
| `documents` | uploads of a person (tied to one of their records, or none) or of a company: kind, date on the document, note, file name, type, size, SHA-256; the file itself is encrypted on disk | 4 |
## Deadline rules
@@ -158,7 +168,7 @@ dark at desktop and phone width, a commit and a push.
| 1 | App, Kanidm login, people, passports with MRZ reading, addresses in Thailand, audit log, demo data, CI | built Nov 17, 2026 |
| 2 | Companies; stays: visas, arrivals with TDAC, extensions, re-entry permits, 90-day reports, work permits; deadlines | built Nov 17, 2026 |
| 3 | On cnx01: flake package, fleet module, Kanidm client and group, bkk01 site, backups; real data from here on | live Nov 17, 2026 |
| 4 | Documents: encrypted uploads (passport pages, TDAC, receipts, photo) on the dataset `zroot/visa` with a key var, viewing and download, all audited | **next** |
| 4 | Documents: encrypted uploads (passport pages, TDAC, receipts, photo) on the dataset `zroot/visa` with a key var, viewing and download, all audited | app built Nov 18, 2026; fleet next |
| 5 | Filled-in forms: TM.7, STM.2, the overstay acknowledgment, TM.47, TM.8 and STM.1 as PDF | |
| 6 | The Non-B extension pack: B4L's company documents, the checklist, one PDF to print | |
| 7 | Reminders by Matrix room message and by email | |
@@ -219,6 +229,38 @@ one `Kind`.
web01, cnx01, bkk01 and control on November 17, 2026
(cnx-network-clan PR #121).
### Phase 4: documents
- [x] 4.1 Encrypted store (`src/files.rs`): one file per document,
`<id>.vdoc`, written to a part file and synced before it takes its
name, never replaced. The file key is 64 hex digits from
`VISA_FILES_KEY_FILE` or the credential `files-key`; with the login on,
the app does not start without it and `VISA_FILES_DIR`.
- [x] 4.2 Uploads on a new Documents tab of each person: a one-line dashed
strip that takes dropped files, up to 10 files of 20 MB each and 64 MB
in all. PDF, JPEG, PNG and WebP are taken, told by their first bytes.
The kinds follow the Non-B pack, and a document may belong to one of
the person's records (passport, visa, arrival, extension, re-entry
permit, 90-day report, address, work permit). The form token is the
upload's first field and is checked before any file is read; a file
that is there already for the person is refused.
- [x] 4.3 Company documents on the company's page: the certificate, BOJ.5,
the balance sheet with PND.50, PND.1, SPS 1-10, PP.30, a map and photos
of the office, each with the date on it for phase 6's six-month rule.
- [x] 4.4 Viewing and download: a file opens in the browser's image or PDF
viewer under its own Content Security Policy, in which nothing runs, and
is never cached. A download is named after the person or company, the
kind and the date. Each record lists its documents next to its Edit
link, with "+ Document" to add one.
- [x] 4.5 Audit: upload, view (opening the file), download (a new action),
change and deletion; a company's entries carry its id
(`audit_log.company_id`). `visa files-check` reads every file back and
lists damaged, missing and stray files. `visa seed-demo` adds demo
documents: PDFs marked as demos and PNGs of shapes.
- [ ] 4.6 Fleet: the dataset `zroot/visa` at `/var/lib/visa` with the files
in `files/`, the var `visa-files` with the key, the files in cnx01's
borg backups, and the VM test uploading a scan through the login.
## Open questions
1. B4L's BOI application: once B4L is promoted, its staff extend their stay