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 %}