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

+85
View File
@@ -0,0 +1,85 @@
{#- Documents: the upload with what the files are, and the list. A person's
Documents tab and a company's page use them. -#}
{% import "_fields.html" as fm %}
{#- The record a person's document belongs to, by kind of record. -#}
{% macro belongs(value, groups, err="") -%}
<div class="field{% if err %} has-err{% endif %}">
<label for="f-link">Belongs to</label>
<select id="f-link" name="link"{% if err %} aria-invalid="true" aria-describedby="e-link"{% endif %}>
<option value="">The person, no record</option>
{%- for g in groups %}
<optgroup label="{{ g.title }}">
{%- for o in g.options %}
<option value="{{ o[0] }}"{% if o[0] == value %} selected{% endif %}>{{ o[1] }}</option>
{%- endfor %}
</optgroup>
{%- endfor %}
</select>
{%- if err %}
<p class="err" id="e-link">{{ err }}</p>
{%- endif %}
</div>
{%- endmacro %}
{#- The upload: a one-line strip that takes dropped files (the file input
covers it) and the fields that say what they are. The form token comes
first: the server checks it before it reads any file. -#}
{% macro upload(u, csrf, person=true) -%}
{%- if u.files_on %}
<form method="post" action="{{ u.action }}" enctype="multipart/form-data" class="upload" id="upload">
<input type="hidden" name="csrf" value="{{ csrf }}">
{%- if u.errors %}
<div class="notice warn">Nothing was uploaded. Correct what is marked and choose the files again.</div>
{%- endif %}
<div class="drop{% if u.errors.file %} has-err{% endif %}" data-max="{{ u.file_limit }}">
<input type="file" id="f-file" name="file" multiple required accept="application/pdf,image/jpeg,image/png,image/webp" aria-describedby="h-file{% if u.errors.file %} e-file{% endif %}">
<span class="drop-icon" aria-hidden="true">+</span>
<label for="f-file"><b>Choose files</b> or drop them here</label>
<span class="drop-names" data-drop-names></span>
<span class="hint" id="h-file">PDF, JPEG, PNG or WebP, up to {{ u.file_limit_mb }} MB each, {{ u.max_files }} at once</span>
</div>
{%- if u.errors.file %}
<p class="err" id="e-file">{{ u.errors.file }}</p>
{%- endif %}
<div class="upload-row{% if not person %} company{% endif %}">
{{ fm.select("kind", "What it is", u.values.kind, u.kinds, err=u.errors.kind, blank="Choose", req=true) }}
{%- if person %}
{{ belongs(u.values.link, u.groups, err=u.errors.link) }}
{%- endif %}
{{ fm.text("dated_on", "Date on the document", u.values.dated_on, err=u.errors.dated_on, type="date") }}
{{ fm.text("note", "Note", u.values.note, err=u.errors.note, ph="page 2, certified copy") }}
<div class="upload-go"><button type="submit" class="btn pri" id="upload-btn">Upload</button></div>
</div>
</form>
{%- else %}
<div class="notice warn">Documents are switched off on this server: it has no files directory and file key (VISA_FILES_DIR and VISA_FILES_KEY_FILE).</div>
{%- endif %}
{%- endmacro %}
{#- The list. On phones the date, type and size move under the name. -#}
{% macro table(docs, person=true) -%}
<div class="tblwrap bleed">
<table class="tbl docs" id="doc-list">
<thead><tr><th>Document</th>{% if person %}<th class="hs">Belongs to</th>{% endif %}<th class="hs">Dated</th><th class="hs">File</th><th class="hs">Added</th><th><span class="vh">Actions</span></th></tr></thead>
<tbody>
{%- for d in docs %}
<tr id="doc-{{ d.id }}">
<td class="docname">
<a class="strong" href="{{ d.href }}" target="_blank" rel="noopener">{{ d.kind_label }}</a>
{%- if d.note %}<div class="muted small">{{ d.note }}</div>{% endif %}
<div class="muted small ps">{% if d.dated_on %}{{ d.dated_on }} · {% endif %}{{ d.type_label }}, {{ d.size }}</div>
</td>
{%- if person %}
<td class="hs">{% if d.link_label %}<a href="{{ d.link_href }}">{{ d.link_label }}</a>{% else %}<span class="muted">the person</span>{% endif %}</td>
{%- endif %}
<td class="hs nowrap">{{ d.dated_on }}</td>
<td class="hs"><span class="ftype">{{ d.type_label }}</span> <span class="muted">{{ d.size }}</span><div class="muted small fname" title="{{ d.file_name }}">{{ d.file_name }}</div></td>
<td class="hs nowrap">{{ d.uploaded }}<div class="muted small">{{ d.uploaded_by }}</div></td>
<td class="acts"><a href="{{ d.href }}" target="_blank" rel="noopener">View</a><a href="{{ d.href }}?download=1">Download</a><a href="{{ d.edit }}">Edit</a></td>
</tr>
{%- endfor %}
</tbody>
</table>
</div>
{%- endmacro %}
+2 -1
View File
@@ -19,8 +19,9 @@
{%- if p.employer %}<span class="sep">·</span><a href="/companies/{{ p.employer_id }}">{{ p.employer }}</a>{% endif %}
</p>
<nav class="tabs" aria-label="About {{ p.name }}">
<a href="/people/{{ p.id }}"{% if tab != "stay" and tab != "activity" %} class="on" aria-current="page"{% endif %}>Overview</a>
<a href="/people/{{ p.id }}"{% if tab not in ["stay", "documents", "activity"] %} class="on" aria-current="page"{% endif %}>Overview</a>
<a href="/people/{{ p.id }}/stay"{% if tab == "stay" %} class="on" aria-current="page"{% endif %}>Visa and stay</a>
<a href="/people/{{ p.id }}/documents"{% if tab == "documents" %} class="on" aria-current="page"{% endif %}>Documents</a>
<a href="/people/{{ p.id }}/activity"{% if tab == "activity" %} class="on" aria-current="page"{% endif %}>Activity</a>
</nav>
</div>
+2 -2
View File
@@ -4,7 +4,7 @@
<h1>Activity{% if person %}: <a href="/people/{{ person.id }}">{{ person.name }}</a>{% endif %}</h1>
{% if person %}<div class="actions"><a class="btn sm ghost" href="/activity">Everyone</a></div>{% endif %}
</div>
<p class="muted">Every view and change of a person's record, newest first: who, when and which fields changed (never their values). The log cannot be edited.</p>
<p class="muted">Every view and change of a record or document, newest first: who, when and which fields changed (never their values). The log cannot be edited.</p>
{% if items %}
<div class="card flush">
<div class="tblwrap">
@@ -16,7 +16,7 @@
<td class="mono">{{ e.at }}</td>
<td>{{ e.actor }}</td>
<td>{{ e.action }}</td>
<td>{% if e.person_id %}<a href="/people/{{ e.person_id }}">{{ e.person_name or ("person " ~ e.person_id) }}</a> <span class="muted">{{ e.what }}</span>{% elif e.company_id %}<a href="/companies/{{ e.company_id }}">{{ e.company_name or ("company " ~ e.company_id) }}</a> <span class="muted">company</span>{% else %}<span class="muted">{{ e.what }}</span>{% endif %}</td>
<td>{% if e.person_id %}<a href="/people/{{ e.person_id }}">{{ e.person_name or ("person " ~ e.person_id) }}</a> <span class="muted">{{ e.what }}</span>{% elif e.company_id %}<a href="/companies/{{ e.company_id }}">{{ e.company_name or ("company " ~ e.company_id) }}</a> <span class="muted">{{ e.what or "company" }}</span>{% else %}<span class="muted">{{ e.what }}</span>{% endif %}</td>
<td class="hs muted">{{ e.fields }}</td>
</tr>
{% endfor %}
+10
View File
@@ -1,4 +1,5 @@
{% extends "base.html" %}
{% import "_documents.html" as dm %}
{% block main %}
<div class="phead">
<div class="crumbs"><a href="/companies">Companies</a></div>
@@ -66,5 +67,14 @@
</form>
{% endif %}
</section>
<section class="card span2" id="documents">
<header><h2>Company documents</h2><span class="muted small">for the extension packs of the people employed here</span></header>
{{ dm.upload(upload, csrf, person=false) }}
{%- if docs %}
{{ dm.table(docs, person=false) }}
{%- else %}
<p class="empty-inline">No company documents yet: the company certificate and the list of shareholders (certified within six months), the tax and social security returns, a map and photos of the office.</p>
{%- endif %}
</section>
</div>
{% endblock %}
+52
View File
@@ -0,0 +1,52 @@
{% extends "base.html" %}
{% import "_fields.html" as fm %}
{% import "_documents.html" as dm %}
{% block main %}
{% if p %}
{% include "_person_head.html" %}
{% else %}
<div class="phead">
<div class="crumbs"><a href="/companies">Companies</a> / <a href="/companies/{{ company.id }}">{{ company.name }}</a></div>
<div class="pagehead"><h1>{{ doc.kind_label }}</h1></div>
</div>
{% endif %}
<div class="cols">
<form method="post" action="{{ action }}" class="form card" id="document-form">
<input type="hidden" name="csrf" value="{{ csrf }}">
<header><h2>{% if p %}{{ doc.kind_label }}{% else %}About the document{% endif %}</h2></header>
<div class="grid">
{{ fm.select("kind", "What it is", values.kind, kinds, err=errors.kind, blank="Choose", req=true) }}
{%- if p %}
{{ dm.belongs(values.link, groups, err=errors.link) }}
{%- endif %}
{{ fm.text("dated_on", "Date on the document", values.dated_on, err=errors.dated_on, type="date", hint="The issue, certification or filing date.") }}
{{ fm.text("note", "Note", values.note, err=errors.note, cls="wide") }}
</div>
<div class="formactions">
<button type="submit" class="btn pri" id="save">Save</button>
<a class="btn ghost" href="{{ cancel }}">Cancel</a>
</div>
</form>
<section class="card" id="file">
<header><h2>The file</h2><div class="actions"><a class="btn sm" href="{{ doc.href }}" target="_blank" rel="noopener">View</a><a class="btn sm ghost" href="{{ doc.href }}?download=1">Download</a></div></header>
{%- if doc.image %}
<a class="preview" href="{{ doc.href }}" target="_blank" rel="noopener"><img src="{{ doc.href }}" alt="{{ doc.kind_label }}"></a>
{%- endif %}
<dl class="kv">
<dt>Name</dt><dd>{{ doc.file_name }}</dd>
<dt>Type and size</dt><dd>{{ doc.type_label }}, {{ doc.size }}</dd>
<dt>Added</dt><dd>{{ doc.uploaded }} by {{ doc.uploaded_by }}</dd>
{%- if doc.updated %}
<dt>Changed</dt><dd>{{ doc.updated }}</dd>
{%- endif %}
<dt>SHA-256</dt><dd class="mono small">{{ doc.sha256 }}</dd>
</dl>
<p class="hint">Encrypted on the server. Each view and download is in the activity log.</p>
</section>
</div>
<form method="post" action="{{ action }}/delete" class="dangerzone" data-confirm="Delete this document and its file from the server?">
<input type="hidden" name="csrf" value="{{ csrf }}">
<button type="submit" class="btn sm danger">Delete this document</button>
<span class="muted small">The file is deleted from the server. The activity log keeps a note that it existed.</span>
</form>
{% endblock %}
+14
View File
@@ -0,0 +1,14 @@
{% extends "base.html" %}
{% import "_documents.html" as dm %}
{% block main %}
{% include "_person_head.html" %}
<section class="card" id="documents">
<header><h2>Documents</h2>{% if docs %}<span class="muted small">{{ docs|length }} {% if docs|length == 1 %}document{% else %}documents{% endif %}, encrypted on the server</span>{% endif %}</header>
{{ dm.upload(upload, csrf) }}
{%- if docs %}
{{ dm.table(docs) }}
{%- else %}
<p class="empty-inline">No documents yet. Scans or photos of the passport pages, the TDAC and the receipts go here, each with the record it belongs to.</p>
{%- endif %}
</section>
{% endblock %}
+2 -2
View File
@@ -35,7 +35,7 @@
<dt>Valid until</dt><dd>{{ pp.expires_on }}{% if pp.status %} <span class="chip {{ pp.status.level }}">{{ pp.status.label }}</span>{% endif %}</dd>
<dt>Zone</dt><dd>{% if pp.has_mrz %}read, check digits matched{% else %}<span class="muted">typed in by hand</span>{% endif %}</dd>
</dl>
<div class="rowactions"><a href="/people/{{ p.id }}/passports/{{ pp.id }}/edit">Edit</a></div>
<div class="rowactions"><a href="/people/{{ p.id }}/passports/{{ pp.id }}/edit">Edit</a>{% for d in docs["passports:" ~ pp.id] %}<a class="doclink" href="{{ d.href }}" target="_blank" rel="noopener">{{ d.label }}</a>{% endfor %}<a href="/people/{{ p.id }}/documents?for=passports:{{ pp.id }}#upload">+ Document</a></div>
</div>
{% else %}
<p class="empty-inline">No passport recorded yet. Add it with + New passport; the two lines at the bottom of the photo page fill the form in.</p>
@@ -56,7 +56,7 @@
<dt>TM.30</dt><dd>{% if a.tm30_reported_on %}reported {{ a.tm30_reported_on }}{% if a.tm30_receipt_no %}, receipt <span class="mono">{{ a.tm30_receipt_no }}</span>{% endif %}{% else %}<span class="muted">no receipt recorded</span>{% endif %}
{% if a.tm30 %}<div><span class="chip {{ a.tm30.level }}">{{ a.tm30.label }}</span></div>{% endif %}</dd>
</dl>
<div class="rowactions"><a href="/people/{{ p.id }}/addresses/{{ a.id }}/edit">Edit</a></div>
<div class="rowactions"><a href="/people/{{ p.id }}/addresses/{{ a.id }}/edit">Edit</a>{% for d in docs["addresses:" ~ a.id] %}<a class="doclink" href="{{ d.href }}" target="_blank" rel="noopener">{{ d.label }}</a>{% endfor %}<a href="/people/{{ p.id }}/documents?for=addresses:{{ a.id }}#upload">+ Document</a></div>
</div>
{% else %}
<p class="empty-inline">No address recorded yet. The TM.30 receipt of the current address goes with it.</p>
+1 -1
View File
@@ -19,7 +19,7 @@
{%- endfor %}
</dl>
{% endif %}
<div class="rowactions"><a href="/people/{{ p.id }}/{{ s.slug }}/{{ r.id }}/edit">Edit</a></div>
<div class="rowactions"><a href="/people/{{ p.id }}/{{ s.slug }}/{{ r.id }}/edit">Edit</a>{% for d in docs[s.slug ~ ":" ~ r.id] %}<a class="doclink" href="{{ d.href }}" target="_blank" rel="noopener">{{ d.label }}</a>{% endfor %}<a href="/people/{{ p.id }}/documents?for={{ s.slug }}:{{ r.id }}#upload">+ Document</a></div>
</div>
{% else %}
<p class="empty-inline">No {{ s.one }} recorded yet.</p>