mirror of
https://github.com/narrrl/narl-workspace.git
synced 2026-11-18 18:35:04 +01:00
vault: files sealed under a password that is never stored
Argon2id turns the vault password into a key that seals a random master key; only the sealed copy is kept. Each file gets its own key, sealed under the master key in its header with its media id, and its bytes are AES-256-GCM STREAM records, so a swapped, cut or altered file refuses to open. Uploads with ?encrypt=true are sealed as they stream in. :vault, :unlock, :lock, :passwd, :encrypt, :decrypt and :upload! drive it. The vault locks after WORKSPACE_VAULT_IDLE_MIN idle minutes, on logout and on restart; while locked, sealed files answer 423, share links never serve them, and :export! refuses rather than leave them out. Sealing an existing file rewrites it under a new id and retires the old one: the mirror deletes the plaintext from Proton Drive outright, trash included, whatever WORKSPACE_BACKUP_PRUNE says, and drops it from the readable files/ view. The mirror now compares on-disk sizes, so sealed files are not re-hashed every run.
This commit is contained in:
1 parent
364ba61963
commit
a641d9b6ba
33 files changed
+1573
-54
No files matched your search
+34
-7
@@ -13,7 +13,11 @@ use sqlx::Row;
|
||||
use tokio_util::io::ReaderStream;
|
||||
use zip::{write::SimpleFileOptions, CompressionMethod, ZipWriter};
|
||||
|
||||
use crate::{auth::Session, error::AppResult, state::AppState};
|
||||
use crate::{
|
||||
auth::Session,
|
||||
error::{AppError, AppResult},
|
||||
state::AppState,
|
||||
};
|
||||
|
||||
/// One node, flattened out of the database ahead of the blocking zip work.
|
||||
struct ExportNode {
|
||||
@@ -159,12 +163,27 @@ pub async fn export(_: Session, State(state): State<AppState>) -> AppResult<Resp
|
||||
})
|
||||
.collect();
|
||||
|
||||
let media: Vec<(String, String)> = sqlx::query("SELECT id, filename FROM media")
|
||||
let rows = sqlx::query("SELECT id, filename, encrypted FROM media")
|
||||
.fetch_all(&state.db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.await?;
|
||||
let media: Vec<(String, String)> = rows
|
||||
.iter()
|
||||
.map(|row| (row.get("id"), row.get("filename")))
|
||||
.collect();
|
||||
let sealed: std::collections::HashSet<String> = rows
|
||||
.iter()
|
||||
.filter(|row| row.get::<bool, _>("encrypted"))
|
||||
.map(|row| row.get("id"))
|
||||
.collect();
|
||||
|
||||
// The archive is meant to be read without this application, so sealed
|
||||
// files go in opened — which needs the vault open. Leaving them out
|
||||
// instead would make an export that silently is not complete.
|
||||
let key = if sealed.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(state.vault.key().ok_or(AppError::Locked)?)
|
||||
};
|
||||
|
||||
let uploads = state.config.uploads_dir();
|
||||
|
||||
@@ -243,11 +262,19 @@ pub async fn export(_: Session, State(state): State<AppState>) -> AppResult<Resp
|
||||
.chain(in_tree.iter().map(|(id, path)| (id, path.clone())));
|
||||
for (id, entry) in entries {
|
||||
// A row whose file has gone missing must not fail the whole backup.
|
||||
let Ok(mut source) = std::fs::File::open(uploads.join(id)) else {
|
||||
continue;
|
||||
let path = uploads.join(id);
|
||||
let source: Box<dyn std::io::Read> = match (&key, sealed.contains(id)) {
|
||||
(Some(key), true) => match crate::vault::Reader::open(key, id, &path) {
|
||||
Ok(reader) => Box::new(reader),
|
||||
Err(_) => continue,
|
||||
},
|
||||
_ => match std::fs::File::open(&path) {
|
||||
Ok(file) => Box::new(file),
|
||||
Err(_) => continue,
|
||||
},
|
||||
};
|
||||
zip.start_file(entry, blob)?;
|
||||
std::io::copy(&mut source, &mut zip)?;
|
||||
std::io::copy(&mut { source }, &mut zip)?;
|
||||
}
|
||||
|
||||
let mut file = zip.finish()?;
|
||||
|
||||
@@ -252,7 +252,7 @@ async fn collect(
|
||||
// The archive itself is only passing through: it lands under a
|
||||
// dotted name no media row can carry, and goes once it is unpacked.
|
||||
let id = format!(".import-{}", Uuid::new_v4());
|
||||
let size = media::store_field(state, &mut field, &id, &mut budget).await?;
|
||||
let size = media::store_field(state, &mut field, &id, &mut budget, None).await?;
|
||||
if size == 0 {
|
||||
skipped.push(name);
|
||||
continue;
|
||||
@@ -309,7 +309,7 @@ async fn collect(
|
||||
|
||||
let mime = media::mime_of(&path, field.content_type());
|
||||
let id = Uuid::new_v4().to_string();
|
||||
let size = media::store_field(state, &mut field, &id, &mut budget).await?;
|
||||
let size = media::store_field(state, &mut field, &id, &mut budget, None).await?;
|
||||
if size == 0 {
|
||||
skipped.push(path);
|
||||
continue;
|
||||
|
||||
+92
-13
@@ -2,13 +2,13 @@ use std::{collections::BTreeSet, sync::LazyLock};
|
||||
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{multipart::Field, Multipart, Path, State},
|
||||
extract::{multipart::Field, Multipart, Path, Query, State},
|
||||
http::header,
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use regex::Regex;
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Row, SqlitePool};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio_util::io::ReaderStream;
|
||||
@@ -19,6 +19,7 @@ use crate::{
|
||||
error::{AppError, AppResult},
|
||||
routes::nodes,
|
||||
state::AppState,
|
||||
vault,
|
||||
};
|
||||
|
||||
/// Media referenced from a document body always looks like `/api/media/<uuid>`,
|
||||
@@ -69,6 +70,8 @@ pub struct Media {
|
||||
pub size: i64,
|
||||
pub created_at: i64,
|
||||
pub url: String,
|
||||
/// Sealed in the vault: its bytes on disk are ciphertext.
|
||||
pub encrypted: bool,
|
||||
/// Every document that embeds this file, which may be none or several.
|
||||
pub node_ids: Vec<i64>,
|
||||
}
|
||||
@@ -91,6 +94,7 @@ fn row_to_media(row: &sqlx::sqlite::SqliteRow) -> Media {
|
||||
mime: row.get("mime"),
|
||||
size: row.get("size"),
|
||||
created_at: row.get("created_at"),
|
||||
encrypted: row.try_get("encrypted").unwrap_or(false),
|
||||
node_ids,
|
||||
}
|
||||
}
|
||||
@@ -118,17 +122,29 @@ pub(crate) fn mime_of(filename: &str, declared: Option<&str>) -> String {
|
||||
/// field has arrived whole, so a dropped connection never leaves a truncated
|
||||
/// file under a name the database might come to know. Returns the size; an
|
||||
/// empty field writes nothing and returns 0.
|
||||
///
|
||||
/// With `seal`, the bytes are encrypted as they pass, so the plaintext never
|
||||
/// touches the disk at all; the size returned is still the plaintext's.
|
||||
pub(crate) async fn store_field(
|
||||
state: &AppState,
|
||||
field: &mut Field<'_>,
|
||||
id: &str,
|
||||
budget: &mut u64,
|
||||
seal: Option<&vault::Key>,
|
||||
) -> AppResult<u64> {
|
||||
let path = state.config.uploads_dir().join(id);
|
||||
let partial = path.with_extension("part");
|
||||
|
||||
let result = async {
|
||||
let mut file = tokio::fs::File::create(&partial).await?;
|
||||
let mut sealer = match seal {
|
||||
Some(key) => {
|
||||
let (sealer, header) = vault::Sealer::new(key, id)?;
|
||||
file.write_all(&header).await?;
|
||||
Some(sealer)
|
||||
}
|
||||
None => None,
|
||||
};
|
||||
let mut size = 0u64;
|
||||
while let Some(chunk) = field
|
||||
.chunk()
|
||||
@@ -139,7 +155,13 @@ pub(crate) async fn store_field(
|
||||
if size > *budget {
|
||||
return Err(too_large(state));
|
||||
}
|
||||
file.write_all(&chunk).await?;
|
||||
match sealer.as_mut() {
|
||||
Some(sealer) => file.write_all(&sealer.push(&chunk)?).await?,
|
||||
None => file.write_all(&chunk).await?,
|
||||
}
|
||||
}
|
||||
if let Some(sealer) = sealer {
|
||||
file.write_all(&sealer.finish()?).await?;
|
||||
}
|
||||
file.flush().await?;
|
||||
Ok(size)
|
||||
@@ -186,7 +208,7 @@ pub async fn upload(
|
||||
let mime = mime_of(&filename, field.content_type());
|
||||
|
||||
let id = Uuid::new_v4().to_string();
|
||||
let size = store_field(&state, &mut field, &id, &mut budget).await?;
|
||||
let size = store_field(&state, &mut field, &id, &mut budget, None).await?;
|
||||
if size == 0 {
|
||||
continue;
|
||||
}
|
||||
@@ -214,15 +236,29 @@ pub async fn upload(
|
||||
Ok(Json(uploaded))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UploadQuery {
|
||||
/// Seal the files into the vault as they arrive.
|
||||
#[serde(default)]
|
||||
pub encrypt: bool,
|
||||
}
|
||||
|
||||
/// `POST /api/nodes/{id}/files` — every file in the body becomes a file node
|
||||
/// in that space or folder, under the name it was uploaded with.
|
||||
/// `?encrypt=true` seals them on the way in, which needs the vault open.
|
||||
pub async fn upload_into(
|
||||
_: Session,
|
||||
State(state): State<AppState>,
|
||||
Path(parent): Path<i64>,
|
||||
Query(query): Query<UploadQuery>,
|
||||
mut multipart: Multipart,
|
||||
) -> AppResult<Json<Vec<nodes::Node>>> {
|
||||
nodes::container(&state.db, parent).await?;
|
||||
let key = if query.encrypt {
|
||||
Some(state.vault.key().ok_or(AppError::Locked)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut created = Vec::new();
|
||||
let mut budget = state.config.max_upload_bytes as u64;
|
||||
@@ -242,12 +278,12 @@ pub async fn upload_into(
|
||||
let mime = mime_of(&filename, field.content_type());
|
||||
|
||||
let id = Uuid::new_v4().to_string();
|
||||
let size = store_field(&state, &mut field, &id, &mut budget).await?;
|
||||
let size = store_field(&state, &mut field, &id, &mut budget, key.as_deref()).await?;
|
||||
if size == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
match insert_file(&state.db, parent, &id, &filename, &mime, size).await {
|
||||
match insert_file(&state.db, parent, &id, &filename, &mime, size, key.is_some()).await {
|
||||
Ok(node) => created.push(node),
|
||||
Err(e) => {
|
||||
let _ = tokio::fs::remove_file(state.config.uploads_dir().join(&id)).await;
|
||||
@@ -277,19 +313,22 @@ async fn insert_file(
|
||||
filename: &str,
|
||||
mime: &str,
|
||||
size: u64,
|
||||
encrypted: bool,
|
||||
) -> AppResult<i64> {
|
||||
let slug = nodes::unique_slug(db, Some(parent), &nodes::slugify(filename), None).await?;
|
||||
let now = crate::now();
|
||||
let mut tx = db.begin().await?;
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO media (id, filename, mime, size, created_at) VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
"INSERT INTO media (id, filename, mime, size, created_at, encrypted)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
)
|
||||
.bind(media_id)
|
||||
.bind(filename)
|
||||
.bind(mime)
|
||||
.bind(size as i64)
|
||||
.bind(now)
|
||||
.bind(encrypted)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
@@ -319,7 +358,7 @@ async fn insert_file(
|
||||
|
||||
pub async fn list(_: Session, State(state): State<AppState>) -> AppResult<Json<Vec<Media>>> {
|
||||
let rows = sqlx::query(
|
||||
"SELECT m.id, m.filename, m.mime, m.size, m.created_at,
|
||||
"SELECT m.id, m.filename, m.mime, m.size, m.created_at, m.encrypted,
|
||||
(SELECT group_concat(nm.node_id)
|
||||
FROM node_media nm WHERE nm.media_id = m.id) AS node_ids
|
||||
FROM media m ORDER BY m.created_at DESC LIMIT 500",
|
||||
@@ -334,7 +373,7 @@ pub async fn serve(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<String>,
|
||||
) -> AppResult<Response> {
|
||||
stream_media(&state, &id).await
|
||||
stream_media(&state, &id, true).await
|
||||
}
|
||||
|
||||
pub async fn remove(
|
||||
@@ -347,9 +386,11 @@ pub async fn remove(
|
||||
Ok(Json(serde_json::json!({ "ok": true })))
|
||||
}
|
||||
|
||||
/// Shared by the private and the public-share media handlers.
|
||||
pub async fn stream_media(state: &AppState, id: &str) -> AppResult<Response> {
|
||||
let row = sqlx::query("SELECT mime, filename, size FROM media WHERE id = ?1")
|
||||
/// Shared by the private and the public-share media handlers. A sealed file
|
||||
/// is only ever served to the owner, and only while the vault is open — a
|
||||
/// share link is a door for strangers, and the vault is not behind it.
|
||||
pub async fn stream_media(state: &AppState, id: &str, private: bool) -> AppResult<Response> {
|
||||
let row = sqlx::query("SELECT mime, filename, size, encrypted FROM media WHERE id = ?1")
|
||||
.bind(id)
|
||||
.fetch_optional(&state.db)
|
||||
.await?
|
||||
@@ -357,14 +398,25 @@ pub async fn stream_media(state: &AppState, id: &str) -> AppResult<Response> {
|
||||
|
||||
let filename: String = row.get("filename");
|
||||
let size: i64 = row.get("size");
|
||||
let encrypted: bool = row.get("encrypted");
|
||||
// Re-checked on the way out, not just on the way in, so rows written before
|
||||
// the allowlist existed cannot be served as something executable either.
|
||||
let mime = sanitize_mime(&row.get::<String, _>("mime"));
|
||||
let disposition = if is_inline(&mime) { "inline" } else { "attachment" };
|
||||
|
||||
let key = match (encrypted, private) {
|
||||
(false, _) => None,
|
||||
(true, false) => return Err(AppError::NotFound),
|
||||
(true, true) => Some(state.vault.key().ok_or(AppError::Locked)?),
|
||||
};
|
||||
|
||||
let file = tokio::fs::File::open(state.config.uploads_dir().join(id))
|
||||
.await
|
||||
.map_err(|_| AppError::NotFound)?;
|
||||
let body = match key {
|
||||
None => Body::from_stream(ReaderStream::new(file)),
|
||||
Some(key) => Body::from_stream(open_stream(file, &key, id).await?),
|
||||
};
|
||||
|
||||
Ok((
|
||||
[
|
||||
@@ -386,11 +438,38 @@ pub async fn stream_media(state: &AppState, id: &str) -> AppResult<Response> {
|
||||
"sandbox; default-src 'none'".to_string(),
|
||||
),
|
||||
],
|
||||
Body::from_stream(ReaderStream::new(file)),
|
||||
body,
|
||||
)
|
||||
.into_response())
|
||||
}
|
||||
|
||||
/// A sealed file as a stream of its plaintext, a record at a time. The
|
||||
/// header is checked before the first byte goes out, so a file that will not
|
||||
/// open is an error response rather than a download that breaks off.
|
||||
async fn open_stream(
|
||||
mut file: tokio::fs::File,
|
||||
key: &vault::Key,
|
||||
id: &str,
|
||||
) -> AppResult<impl futures_util::Stream<Item = std::io::Result<axum::body::Bytes>>> {
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
let len = file.metadata().await?.len();
|
||||
let mut header = [0u8; vault::HEADER];
|
||||
file.read_exact(&mut header).await?;
|
||||
let opener = vault::Opener::new(key, id, &header, len)?;
|
||||
|
||||
Ok(futures_util::stream::try_unfold((file, opener), |(mut file, mut opener)| async move {
|
||||
let len = opener.next_len();
|
||||
if len == 0 {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut record = vec![0u8; len];
|
||||
file.read_exact(&mut record).await?;
|
||||
let plain = opener.open(&record).map_err(std::io::Error::other)?;
|
||||
Ok(Some((axum::body::Bytes::from(plain), (file, opener))))
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn delete_media(state: &AppState, id: &str) -> AppResult<()> {
|
||||
sqlx::query("DELETE FROM media WHERE id = ?1")
|
||||
.bind(id)
|
||||
|
||||
@@ -4,9 +4,11 @@ pub mod nodes;
|
||||
pub mod export;
|
||||
pub mod import;
|
||||
pub mod media;
|
||||
pub mod seal;
|
||||
pub mod session;
|
||||
pub mod share;
|
||||
pub mod ssh_keys;
|
||||
pub mod vault;
|
||||
|
||||
use axum::{
|
||||
routing::{delete, get, post, put},
|
||||
@@ -39,6 +41,11 @@ pub fn api_router() -> Router<AppState> {
|
||||
.route("/nodes/{id}/move", post(nodes::move_node))
|
||||
.route("/nodes/{id}/import", post(import::import))
|
||||
.route("/nodes/{id}/files", post(media::upload_into))
|
||||
.route("/nodes/{id}/encrypt", post(seal::encrypt))
|
||||
.route("/nodes/{id}/decrypt", post(seal::decrypt))
|
||||
.route("/vault", get(vault::status).post(vault::setup).put(vault::change_password))
|
||||
.route("/vault/unlock", post(vault::unlock))
|
||||
.route("/vault/lock", post(vault::lock))
|
||||
.route("/nodes/{id}/share", post(share::enable))
|
||||
.route("/nodes/{id}/share", delete(share::disable))
|
||||
.route("/media", post(media::upload).get(media::list))
|
||||
|
||||
+5
-1
@@ -35,6 +35,8 @@ pub struct FileInfo {
|
||||
pub mime: String,
|
||||
pub size: i64,
|
||||
pub url: String,
|
||||
/// Sealed in the vault: it opens only while the vault does.
|
||||
pub encrypted: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -158,7 +160,8 @@ const SUMMARY_COLUMNS: &str = "n.id, n.parent_id, n.kind, n.name, n.slug, n.posi
|
||||
n.created_at, n.updated_at, n.share_token, n.media_id,
|
||||
(SELECT COUNT(*) FROM nodes c WHERE c.parent_id = n.id) AS child_count,
|
||||
(SELECT m.mime FROM media m WHERE m.id = n.media_id) AS file_mime,
|
||||
(SELECT m.size FROM media m WHERE m.id = n.media_id) AS file_size";
|
||||
(SELECT m.size FROM media m WHERE m.id = n.media_id) AS file_size,
|
||||
(SELECT m.encrypted FROM media m WHERE m.id = n.media_id) AS file_encrypted";
|
||||
|
||||
/// A short, plain-ish preview of the body for the list pane. `head` is only the
|
||||
/// leading `HEAD_CHARS` of the document, so truncation is inferred from its
|
||||
@@ -286,6 +289,7 @@ fn row_to_summary(row: &sqlx::sqlite::SqliteRow, head: &str) -> NodeSummary {
|
||||
media_id,
|
||||
mime: row.get::<Option<String>, _>("file_mime").unwrap_or_default(),
|
||||
size: row.get::<Option<i64>, _>("file_size").unwrap_or_default(),
|
||||
encrypted: row.get::<Option<bool>, _>("file_encrypted").unwrap_or_default(),
|
||||
});
|
||||
NodeSummary {
|
||||
id: row.get("id"),
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
//! Moving files that already exist into the vault, and back out of it.
|
||||
//!
|
||||
//! `POST /api/nodes/{id}/encrypt` seals a file node, or every file node under
|
||||
//! a folder or space; `/decrypt` does the opposite. Either needs the vault open.
|
||||
//!
|
||||
//! A file is re-written under a **new** media id rather than in place. The
|
||||
//! mirror treats `uploads/<uuid>` as immutable, so rewriting one would leave
|
||||
//! the old bytes on Drive as the previous revision of the same file — the
|
||||
//! plaintext, in the case that matters. Under a new id the sealed copy is a new
|
||||
//! file, and the old id is put on `backup_retired`, which the mirror takes off
|
||||
//! Drive for good whatever `WORKSPACE_BACKUP_PRUNE` says. Documents that embed
|
||||
//! the file have their links moved to the new id in the same transaction.
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
Json,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use sqlx::Row;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
auth::Session,
|
||||
error::{AppError, AppResult},
|
||||
routes::nodes,
|
||||
state::AppState,
|
||||
vault,
|
||||
};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Resealed {
|
||||
/// How many files changed.
|
||||
pub files: usize,
|
||||
/// Of what was asked about, how many were already that way.
|
||||
pub unchanged: usize,
|
||||
}
|
||||
|
||||
pub async fn encrypt(
|
||||
_: Session,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<i64>,
|
||||
) -> AppResult<Json<Resealed>> {
|
||||
reseal(&state, id, true).await.map(Json)
|
||||
}
|
||||
|
||||
pub async fn decrypt(
|
||||
_: Session,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<i64>,
|
||||
) -> AppResult<Json<Resealed>> {
|
||||
reseal(&state, id, false).await.map(Json)
|
||||
}
|
||||
|
||||
struct Target {
|
||||
node_id: i64,
|
||||
media_id: String,
|
||||
encrypted: bool,
|
||||
}
|
||||
|
||||
async fn reseal(state: &AppState, id: i64, seal: bool) -> AppResult<Resealed> {
|
||||
let key = state.vault.key().ok_or(AppError::Locked)?;
|
||||
|
||||
let subtree = nodes::descendants(&state.db, id).await?;
|
||||
if subtree.is_empty() {
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
let list = subtree.iter().map(i64::to_string).collect::<Vec<_>>().join(",");
|
||||
let targets: Vec<Target> = sqlx::query(&format!(
|
||||
"SELECT n.id, n.media_id, m.encrypted
|
||||
FROM nodes n JOIN media m ON m.id = n.media_id
|
||||
WHERE n.kind = 'file' AND n.id IN ({list})"
|
||||
))
|
||||
.fetch_all(&state.db)
|
||||
.await?
|
||||
.iter()
|
||||
.map(|row| Target {
|
||||
node_id: row.get("id"),
|
||||
media_id: row.get("media_id"),
|
||||
encrypted: row.get("encrypted"),
|
||||
})
|
||||
.collect();
|
||||
|
||||
if targets.is_empty() {
|
||||
return Err(AppError::BadRequest("there are no files there".into()));
|
||||
}
|
||||
|
||||
let mut files = 0;
|
||||
let mut unchanged = 0;
|
||||
for target in targets {
|
||||
if target.encrypted == seal {
|
||||
unchanged += 1;
|
||||
continue;
|
||||
}
|
||||
rewrite(state, &key, &target, seal).await?;
|
||||
files += 1;
|
||||
}
|
||||
|
||||
if files > 0 {
|
||||
state.backup.signal();
|
||||
}
|
||||
Ok(Resealed { files, unchanged })
|
||||
}
|
||||
|
||||
/// Re-write one file sealed or opened under a new id, then point everything
|
||||
/// at the new id and retire the old one.
|
||||
async fn rewrite(state: &AppState, key: &vault::Key, target: &Target, seal: bool) -> AppResult<()> {
|
||||
let uploads = state.config.uploads_dir();
|
||||
let old = target.media_id.clone();
|
||||
let new = Uuid::new_v4().to_string();
|
||||
let source = uploads.join(&old);
|
||||
let partial = uploads.join(format!("{new}.part"));
|
||||
|
||||
let written = {
|
||||
let (key, old, new, source, partial) =
|
||||
(key.clone(), old.clone(), new.clone(), source.clone(), partial.clone());
|
||||
tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
|
||||
if seal {
|
||||
let mut plain = std::fs::File::open(&source)?;
|
||||
vault::seal_file(&key, &new, &mut plain, &partial)?;
|
||||
} else {
|
||||
let mut opened = vault::Reader::open(&key, &old, &source)?;
|
||||
let mut out = std::fs::File::create(&partial)?;
|
||||
std::io::copy(&mut opened, &mut out)?;
|
||||
out.sync_all()?;
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
.map_err(anyhow::Error::from)
|
||||
.and_then(|done| done)
|
||||
};
|
||||
if let Err(e) = written {
|
||||
let _ = tokio::fs::remove_file(&partial).await;
|
||||
return Err(e.into());
|
||||
}
|
||||
tokio::fs::rename(&partial, uploads.join(&new)).await?;
|
||||
|
||||
let committed = async {
|
||||
let mut tx = state.db.begin().await?;
|
||||
sqlx::query(
|
||||
"INSERT INTO media (id, filename, mime, size, created_at, encrypted)
|
||||
SELECT ?1, filename, mime, size, created_at, ?2 FROM media WHERE id = ?3",
|
||||
)
|
||||
.bind(&new)
|
||||
.bind(seal)
|
||||
.bind(&old)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query("UPDATE nodes SET media_id = ?1 WHERE media_id = ?2")
|
||||
.bind(&new)
|
||||
.bind(&old)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
// Every document that embeds the file keeps showing it.
|
||||
sqlx::query(
|
||||
"UPDATE nodes SET body = replace(body, '/api/media/' || ?2, '/api/media/' || ?1)
|
||||
WHERE kind = 'document' AND id IN (SELECT node_id FROM node_media WHERE media_id = ?2)",
|
||||
)
|
||||
.bind(&new)
|
||||
.bind(&old)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query("UPDATE node_media SET media_id = ?1 WHERE media_id = ?2")
|
||||
.bind(&new)
|
||||
.bind(&old)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query("INSERT OR IGNORE INTO backup_retired (path) VALUES ('uploads/' || ?1)")
|
||||
.bind(&old)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
sqlx::query("DELETE FROM media WHERE id = ?1")
|
||||
.bind(&old)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
AppResult::Ok(())
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Err(e) = committed {
|
||||
let _ = tokio::fs::remove_file(uploads.join(&new)).await;
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
// The database no longer knows the old bytes; they go from the disk too.
|
||||
let _ = tokio::fs::remove_file(&source).await;
|
||||
tracing::info!(node = target.node_id, from = %old, to = %new, sealed = seal, "file re-written");
|
||||
Ok(())
|
||||
}
|
||||
@@ -58,6 +58,8 @@ pub async fn login(
|
||||
}
|
||||
|
||||
pub async fn logout(State(state): State<AppState>) -> impl IntoResponse {
|
||||
// Leaving is the moment the vault should not outlive.
|
||||
state.vault.lock();
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(header::SET_COOKIE, auth::clear_cookie(&state.config))],
|
||||
|
||||
+1
-1
@@ -160,7 +160,7 @@ pub async fn serve_media(
|
||||
if !key_matches(stored.as_deref(), &key) {
|
||||
return Err(AppError::NotFound);
|
||||
}
|
||||
media::stream_media(&state, &id).await
|
||||
media::stream_media(&state, &id, false).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
//! `/api/vault`: set the vault up, open it, close it, change its password.
|
||||
|
||||
use axum::{extract::State, Json};
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
auth::Session,
|
||||
error::{AppError, AppResult},
|
||||
state::AppState,
|
||||
vault::{Status, Unlock, MIN_PASSWORD},
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PasswordInput {
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct ChangeInput {
|
||||
pub current: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
fn strong_enough(password: &str) -> AppResult<()> {
|
||||
if password.chars().count() < MIN_PASSWORD {
|
||||
return Err(AppError::BadRequest(format!(
|
||||
"a vault password needs at least {MIN_PASSWORD} characters"
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn status(_: Session, State(state): State<AppState>) -> AppResult<Json<Status>> {
|
||||
Ok(Json(state.vault.status().await?))
|
||||
}
|
||||
|
||||
/// `POST /api/vault` — make the vault. Only once.
|
||||
pub async fn setup(
|
||||
_: Session,
|
||||
State(state): State<AppState>,
|
||||
Json(input): Json<PasswordInput>,
|
||||
) -> AppResult<Json<Status>> {
|
||||
strong_enough(&input.password)?;
|
||||
if !state.vault.setup(input.password).await? {
|
||||
return Err(AppError::BadRequest(
|
||||
"there is a vault already — :passwd changes its password".into(),
|
||||
));
|
||||
}
|
||||
tracing::info!("vault set up");
|
||||
state.backup.signal();
|
||||
Ok(Json(state.vault.status().await?))
|
||||
}
|
||||
|
||||
/// Wrong guesses are slowed down exactly like logins. Argon2 already makes
|
||||
/// each one cost a fraction of a second; this makes a run of them cost minutes.
|
||||
fn throttle(state: &AppState) -> AppResult<()> {
|
||||
state.vault_throttle.check().map_err(|wait| {
|
||||
tracing::warn!("vault unlock refused, {wait}s of backoff remaining");
|
||||
AppError::TooManyRequests(wait)
|
||||
})
|
||||
}
|
||||
|
||||
fn answer(state: &AppState, outcome: Unlock) -> AppResult<()> {
|
||||
match outcome {
|
||||
Unlock::Opened => {
|
||||
state.vault_throttle.record_success();
|
||||
Ok(())
|
||||
}
|
||||
Unlock::WrongPassword => {
|
||||
state.vault_throttle.record_failure();
|
||||
Err(AppError::BadRequest("wrong vault password".into()))
|
||||
}
|
||||
Unlock::NotConfigured => Err(AppError::BadRequest(
|
||||
"there is no vault yet — :vault sets one up".into(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn unlock(
|
||||
_: Session,
|
||||
State(state): State<AppState>,
|
||||
Json(input): Json<PasswordInput>,
|
||||
) -> AppResult<Json<Status>> {
|
||||
throttle(&state)?;
|
||||
answer(&state, state.vault.unlock(input.password).await?)?;
|
||||
Ok(Json(state.vault.status().await?))
|
||||
}
|
||||
|
||||
pub async fn lock(_: Session, State(state): State<AppState>) -> AppResult<Json<Status>> {
|
||||
state.vault.lock();
|
||||
Ok(Json(state.vault.status().await?))
|
||||
}
|
||||
|
||||
pub async fn change_password(
|
||||
_: Session,
|
||||
State(state): State<AppState>,
|
||||
Json(input): Json<ChangeInput>,
|
||||
) -> AppResult<Json<Status>> {
|
||||
throttle(&state)?;
|
||||
strong_enough(&input.password)?;
|
||||
answer(&state, state.vault.change_password(input.current, input.password).await?)?;
|
||||
tracing::info!("vault password changed");
|
||||
state.backup.signal();
|
||||
Ok(Json(state.vault.status().await?))
|
||||
}
|
||||
Reference in new issue
Block a user