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

+18 -5
View File
@@ -19,6 +19,8 @@ pub enum Action {
Restore,
SignIn,
SignOut,
/// A document's file saved to the browser's disk (opening it is a view).
Download,
}
impl Action {
@@ -32,6 +34,7 @@ impl Action {
Action::Restore => "restore",
Action::SignIn => "sign-in",
Action::SignOut => "sign-out",
Action::Download => "download",
}
}
}
@@ -49,6 +52,7 @@ pub enum Entity {
ReentryPermit,
Report,
WorkPermit,
Document,
}
impl Entity {
@@ -65,6 +69,7 @@ impl Entity {
Entity::ReentryPermit => "reentry-permit",
Entity::Report => "report",
Entity::WorkPermit => "work-permit",
Entity::Document => "document",
}
}
}
@@ -75,6 +80,8 @@ pub struct Entry<'a> {
pub entity: Entity,
pub entity_id: Option<i64>,
pub person_id: Option<i64>,
/// The company a company's record or document belongs to.
pub company_id: Option<i64>,
pub fields: &'a [String],
}
@@ -85,14 +92,15 @@ where
E: Executor<'e, Database = Postgres>,
{
sqlx::query(
"INSERT INTO audit_log (actor, action, entity, entity_id, person_id, fields) \
VALUES ($1, $2, $3, $4, $5, $6)",
"INSERT INTO audit_log (actor, action, entity, entity_id, person_id, company_id, fields) \
VALUES ($1, $2, $3, $4, $5, $6, $7)",
)
.bind(e.actor)
.bind(e.action.as_str())
.bind(e.entity.as_str())
.bind(e.entity_id)
.bind(e.person_id)
.bind(e.company_id)
.bind(e.fields)
.execute(ex)
.await?;
@@ -109,6 +117,7 @@ pub async fn viewed(db: &PgPool, actor: &str, entity: Entity, id: i64, person: i
entity,
entity_id: Some(id),
person_id: Some(person),
company_id: None,
fields: &[],
},
)
@@ -124,6 +133,7 @@ struct Row {
entity: String,
entity_id: Option<i64>,
person_id: Option<i64>,
company_id: Option<i64>,
fields: Vec<String>,
person_name: Option<String>,
company_name: Option<String>,
@@ -156,12 +166,15 @@ fn noun(entity: &str) -> &str {
/// The newest entries first, for one person or for everyone.
pub async fn recent(db: &PgPool, person: Option<i64>, limit: i64) -> Result<Vec<Item>> {
let rows: Vec<Row> = sqlx::query_as(
"SELECT a.id, a.at, a.actor, a.action, a.entity, a.entity_id, a.person_id, a.fields, \
"SELECT a.id, a.at, a.actor, a.action, a.entity, a.entity_id, a.person_id, \
COALESCE(a.company_id, CASE WHEN a.entity = 'company' THEN a.entity_id END) \
AS company_id, a.fields, \
CASE WHEN p.id IS NULL THEN NULL \
ELSE upper(p.surname) || ', ' || trim(p.first_name || ' ' || p.middle_name) \
END AS person_name, c.name AS company_name \
FROM audit_log a LEFT JOIN people p ON p.id = a.person_id \
LEFT JOIN companies c ON a.entity = 'company' AND c.id = a.entity_id \
LEFT JOIN companies c \
ON c.id = COALESCE(a.company_id, CASE WHEN a.entity = 'company' THEN a.entity_id END) \
WHERE ($1::bigint IS NULL OR a.person_id = $1) \
ORDER BY a.id DESC LIMIT $2",
)
@@ -178,7 +191,7 @@ pub async fn recent(db: &PgPool, person: Option<i64>, limit: i64) -> Result<Vec<
(e, Some(id)) => format!("{} {id}", noun(e)),
(e, None) => noun(e).to_string(),
};
let company_id = (r.entity == "company").then_some(r.entity_id).flatten();
let company_id = r.company_id;
Item {
id: r.id,
at: dates::fmt_datetime(r.at),