feat(supabase): R5.5 upload-sheet-assets in Rust, R5.4 caps and FLOOR clarification

Core rules curation::upload (manifest, local destination, bytes through the SVG reader,
plan from observed state); binary upload-sheet-assets over ureq (handshake, no overwrite,
confirm after verify, bounded retries, key never printed); pnpm assets:upload repointed,
e2e:sheet-assets for the local stack. 4 MiB cap on changes.json and references.json.
FLOOR clarification for audit prose with its negative proof.
This commit is contained in:
bchanot committed 2026-10-23 21:15:22 +02:00
1 parent 637ea6334c
commit 2c445063a4
20 files changed
+1595 -21

No files matched your search

+76
View File
@@ -0,0 +1,76 @@
//! The resource caps and the stable-id acceptance of the curation inputs (R5.5 adjustments of
//! R5.4, owner 2026-10-23 §3 and §6): an authored file over 4 MiB is refused by name before any
//! parse allocation, the committed files keep a wide margin, and the stable-id grammar the
//! applier enforces is the sheet contract's, not an equine list.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
use faunosteo_core::curation::draft::schema::MAX_CURATION_FILE_BYTES;
use faunosteo_core::curation::draft::{parse_changeset, parse_references};
use faunosteo_core::domain::js::is_stable_id;
const CURATION: &str = "../../supabase/seed/anatomical-sheets/equine/curation";
fn committed(name: &str) -> String {
std::fs::read_to_string(format!("{CURATION}/{name}")).unwrap()
}
/// A valid-looking JSON text of exactly `len` bytes (spaces pad the tail).
fn padded(len: usize) -> String {
let mut text = String::from("{\"changes\":[]}");
text.push_str(&" ".repeat(len - text.len()));
text
}
#[test]
fn committed_files_keep_a_wide_margin_under_the_cap() {
for name in ["changes.json", "references.json"] {
let size = committed(name).len();
assert!(size * 10 < MAX_CURATION_FILE_BYTES, "{name}: {size} bytes");
}
}
#[test]
fn a_file_over_the_cap_is_refused_by_name_before_the_parse() {
let over = padded(MAX_CURATION_FILE_BYTES + 1);
let changes = parse_changeset(&over).unwrap_err().to_string();
assert!(
changes.starts_with("changes.json: larger than "),
"{changes}"
);
let references = parse_references(&over).unwrap_err().to_string();
assert!(
references.starts_with("references.json: larger than "),
"{references}"
);
}
#[test]
fn a_file_at_the_cap_reaches_the_parser() {
let at = padded(MAX_CURATION_FILE_BYTES);
assert!(parse_changeset(&at).is_ok());
}
#[test]
fn stable_ids_of_any_species_are_accepted_and_the_rest_refused() {
for id in [
"canine-skull",
"feline_thorax-2",
"0-bovine",
"a",
&"x".repeat(64),
] {
assert!(is_stable_id(id), "{id}");
}
for id in [
"",
"Equine",
"-x",
"a b",
"a/b",
"a.b",
"é",
&"x".repeat(65),
] {
assert!(!is_stable_id(id), "{id:?}");
}
}
+208
View File
@@ -0,0 +1,208 @@
//! The rules of the sheet-asset upload (R5.5): manifest, destination, bytes, plan. The tool
//! around them is exercised by `upload_tool.rs` against a stub of the local stack.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
use faunosteo_core::curation::ids::sha256_hex;
use faunosteo_core::curation::upload::{
AssetEntry, ObjectState, Plan, RowState, UploadKind, check_destination, parse_manifest,
plan_entry, verify_asset,
};
const SEED: &str = "../../supabase/seed/anatomical-sheets/equine/draft";
const UUID: &str = "7f767f74-3b36-51a7-8465-52dd7e2ead4a";
const SVG: &[u8] = b"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 10 10\"><g/></svg>";
fn entry_for(bytes: &[u8]) -> AssetEntry {
AssetEntry {
file: "a.svg".into(),
sha256: sha256_hex(bytes),
byte_size: bytes.len(),
svg_asset_id: UUID.into(),
}
}
fn manifest(entries: &[(&str, &str, usize, &str)]) -> String {
let items: Vec<String> = entries
.iter()
.map(|(file, sha, size, id)| {
format!("{{\"file\":\"{file}\",\"sha256\":\"{sha}\",\"byte_size\":{size},\"svg_asset_id\":\"{id}\"}}")
})
.collect();
format!("[{}]", items.join(","))
}
fn kind(
result: Result<impl std::fmt::Debug, faunosteo_core::curation::upload::UploadError>,
) -> UploadKind {
result.unwrap_err().kind
}
#[test]
fn the_committed_manifest_and_drawing_pass() {
let text = std::fs::read_to_string(format!("{SEED}/assets.json")).unwrap();
let entries = parse_manifest(&text).unwrap();
assert_eq!(entries.len(), 1);
let bytes = std::fs::read(format!("{SEED}/assets/{}", entries[0].file)).unwrap();
verify_asset(&entries[0], &bytes).unwrap();
assert_eq!(
entries[0].object_key(),
format!("{}.svg", entries[0].sha256)
);
}
#[test]
fn a_bad_manifest_is_refused_whole() {
let sha = "a".repeat(64);
let good = (&"a.svg", sha.as_str(), 5usize, UUID);
let cases = [
manifest(&[]),
"{}".into(),
"[{\"file\":\"a.svg\"}]".into(),
manifest(&[("../a.svg", &sha, 5, UUID)]),
manifest(&[(".a.svg", &sha, 5, UUID)]),
manifest(&[("a.png", &sha, 5, UUID)]),
manifest(&[("a.svg", &sha.to_uppercase(), 5, UUID)]),
manifest(&[("a.svg", "abc", 5, UUID)]),
manifest(&[("a.svg", &sha, 5, "not-a-uuid")]),
manifest(&[("a.svg", &sha, 0, UUID)]),
manifest(&[("a.svg", &sha, 17 * 1024 * 1024, UUID)]),
manifest(&[
(good.0, good.1, good.2, good.3),
(
"a.svg",
&"b".repeat(64),
5,
"11111111-1111-1111-1111-111111111111",
),
]),
manifest(&[
(good.0, good.1, good.2, good.3),
("b.svg", &sha, 5, "11111111-1111-1111-1111-111111111111"),
]),
manifest(&[
(good.0, good.1, good.2, good.3),
("b.svg", &"b".repeat(64), 5, UUID),
]),
"x".repeat(2 * 1024 * 1024),
];
for text in cases {
assert_eq!(
kind(parse_manifest(&text)),
UploadKind::Manifest,
"{}",
&text[..text.len().min(80)]
);
}
}
#[test]
fn a_manifest_with_an_unknown_key_is_refused() {
let text = manifest(&[("a.svg", &"a".repeat(64), 5, UUID)]).replace("}", ",\"x\":1}");
assert_eq!(kind(parse_manifest(&text)), UploadKind::Manifest);
}
#[test]
fn only_bytes_that_match_and_that_the_svg_reader_keeps_whole_are_uploaded() {
let entry = entry_for(SVG);
verify_asset(&entry, SVG).unwrap();
let mut other = SVG.to_vec();
other[5] = b'X';
assert_eq!(kind(verify_asset(&entry, &other)), UploadKind::Content);
let scripted: &[u8] = b"<svg xmlns=\"http://www.w3.org/2000/svg\"><script>x</script></svg>";
assert_eq!(
kind(verify_asset(&entry_for(scripted), scripted)),
UploadKind::Content
);
let not_svg: &[u8] = b"<html/>";
assert_eq!(
kind(verify_asset(&entry_for(not_svg), not_svg)),
UploadKind::Content
);
let binary: &[u8] = &[0xff, 0xfe, 0x00];
assert_eq!(
kind(verify_asset(&entry_for(binary), binary)),
UploadKind::Content
);
}
#[test]
fn only_a_local_stack_is_a_destination() {
for ok in [
"http://127.0.0.1:54321",
"http://localhost:54321",
"http://[::1]:54321",
"http://127.0.0.2:9",
] {
let destination = check_destination(ok, None).unwrap();
assert!(destination.origin().starts_with("http://"), "{ok}");
assert!(!destination.origin().ends_with('/'));
}
assert_eq!(
check_destination("http://LOCALHOST:1", None)
.unwrap()
.origin(),
"http://localhost:1"
);
for refused in [
"https://127.0.0.1:54321",
"http://127.0.0.1",
"http://127.0.0.1:80",
"http://user@127.0.0.1:54321",
"http://127.0.0.1:54321/x",
"http://127.0.0.1:54321/?a=1",
"http://127.0.0.1:54321/#f",
"http://10.1.2.3:54321",
"http://example.test:54321",
"http://abcd.supabase.co:54321",
"http://faunosteo.com:54321",
"http://0.0.0.0:54321",
"http://[2001:db8::1]:54321",
"127.0.0.1:54321",
"",
] {
assert_eq!(
kind(check_destination(refused, None)),
UploadKind::Destination,
"{refused}"
);
}
}
#[test]
fn the_dev_address_is_allowed_only_when_configured_and_private() {
let url = "http://192.168.1.100:54321";
assert!(check_destination(url, None).is_err());
assert!(check_destination(url, Some("192.168.1.101")).is_err());
assert!(check_destination(url, Some("192.168.1.100")).is_ok());
// A public address is never a DEV address, even when named as one.
assert!(check_destination("http://8.8.8.8:54321", Some("8.8.8.8")).is_err());
// A name is not resolved: it cannot be proven local.
assert!(check_destination("http://mybox:54321", Some("mybox")).is_err());
}
#[test]
fn the_plan_follows_what_the_destination_holds_and_never_overwrites() {
let entry = entry_for(SVG);
let plan = |object, row| plan_entry(&entry, &object, &row);
let go = |upload, confirm| Ok(Plan { upload, confirm });
assert_eq!(plan(ObjectState::Absent, RowState::Pending), go(true, true));
assert_eq!(
plan(ObjectState::Verified, RowState::Pending),
go(false, true)
);
assert_eq!(
plan(ObjectState::Verified, RowState::Confirmed),
go(false, false)
);
assert_eq!(
plan(ObjectState::Absent, RowState::Confirmed),
go(true, false)
);
for (object, row) in [
(ObjectState::Different("f".repeat(64)), RowState::Pending),
(ObjectState::Absent, RowState::Unknown),
(ObjectState::Verified, RowState::OtherHash("f".repeat(64))),
] {
assert_eq!(kind(plan(object, row)), UploadKind::State);
}
}
+316
View File
@@ -0,0 +1,316 @@
//! The `upload-sheet-assets` binary against an in-process stub of the local Supabase API
//! (R5.5): the plan of a run, what is written and what never is, resume after a partial
//! failure, refusals by category, network errors, and the key never printed. The stub listens
//! on loopback with a made-up key; the real local stack is exercised by
//! `scripts/sheet-assets-e2e.sh`, run by the owner.
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::process::{Command, Output};
use std::sync::{Arc, Mutex};
const KEY: &str = "stub-service-key-0123456789";
const DRAFT: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../supabase/seed/anatomical-sheets/equine/draft"
);
const SHA: &str = "0ea5202fa3b95bac167b87c12ee969ab0272e085b60c2d34dbca6202c5f08541";
#[derive(Default)]
struct State {
objects: HashMap<String, Vec<u8>>,
/// `None`: no row. `Some((sha256, confirmed))`.
row: Option<(String, bool)>,
bucket_status: u16,
fail_confirm: bool,
log: Vec<String>,
}
type Shared = Arc<Mutex<State>>;
fn horse() -> Vec<u8> {
std::fs::read(format!("{DRAFT}/assets/horse.svg")).unwrap()
}
fn read_request(stream: &mut TcpStream) -> (String, String, Vec<u8>) {
let mut reader = BufReader::new(stream.try_clone().unwrap());
let mut line = String::new();
reader.read_line(&mut line).unwrap();
let mut parts = line.split_whitespace();
let (method, path) = (
parts.next().unwrap().to_owned(),
parts.next().unwrap().to_owned(),
);
let mut length = 0;
loop {
let mut header = String::new();
reader.read_line(&mut header).unwrap();
if header.trim().is_empty() {
break;
}
if let Some(value) = header.to_ascii_lowercase().strip_prefix("content-length:") {
length = value.trim().parse().unwrap();
}
}
let mut body = vec![0; length];
reader.read_exact(&mut body).unwrap();
(method, path, body)
}
fn respond(stream: &mut TcpStream, status: u16, body: &[u8]) {
let head = format!(
"HTTP/1.1 {status} X\r\nContent-Length: {}\r\nConnection: close\r\nContent-Type: application/json\r\n\r\n",
body.len()
);
stream.write_all(head.as_bytes()).unwrap();
stream.write_all(body).unwrap();
}
fn route(state: &mut State, method: &str, path: &str, body: Vec<u8>) -> (u16, Vec<u8>) {
state.log.push(format!("{method} {path}"));
let object = path.strip_prefix("/storage/v1/object/anatomical-sheet-assets/");
match (method, object) {
("GET", None) if path == "/storage/v1/bucket/anatomical-sheet-assets" => (
state.bucket_status,
br#"{"name":"anatomical-sheet-assets","public":false}"#.to_vec(),
),
("GET", Some(key)) => match state.objects.get(key) {
Some(bytes) => (200, bytes.clone()),
None => (400, br#"{"statusCode":"404","error":"not_found"}"#.to_vec()),
},
("POST", Some(key)) if state.objects.contains_key(key) => {
(400, br#"{"statusCode":"409","error":"Duplicate"}"#.to_vec())
}
("POST", Some(key)) => {
state.objects.insert(key.to_owned(), body);
(200, b"{}".to_vec())
}
("GET", None) if path.starts_with("/rest/v1/anatomical_sheet_assets") => {
let rows = match &state.row {
None => "[]".to_owned(),
Some((sha, confirmed)) => {
let at = if *confirmed {
"\"2026-10-23T00:00:00Z\""
} else {
"null"
};
format!("[{{\"sha256\":\"{sha}\",\"uploaded_at\":{at}}}]")
}
};
(200, rows.into_bytes())
}
("POST", None) if path.ends_with("/rpc/confirm_sheet_asset_upload") => {
if state.fail_confirm {
return (500, b"{}".to_vec());
}
if let Some((_, confirmed)) = state.row.as_mut() {
*confirmed = true;
}
(200, b"\"id\"".to_vec())
}
_ => (404, b"{}".to_vec()),
}
}
fn serve(state: Shared) -> u16 {
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let port = listener.local_addr().unwrap().port();
std::thread::spawn(move || {
for stream in listener.incoming() {
let mut stream = stream.unwrap();
let (method, path, body) = read_request(&mut stream);
let (status, reply) = route(&mut state.lock().unwrap(), &method, &path, body);
respond(&mut stream, status, &reply);
}
});
port
}
/// A stub with the seed applied: the row exists, pending, the bucket answers 200.
fn stub() -> (Shared, String) {
let state: Shared = Arc::new(Mutex::new(State {
row: Some((SHA.to_owned(), false)),
bucket_status: 200,
..State::default()
}));
let port = serve(Arc::clone(&state));
(state, format!("http://127.0.0.1:{port}"))
}
fn run(url: &str, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_upload-sheet-assets"))
.args(args)
.env_clear()
.env("SUPABASE_URL", url)
.env("SUPABASE_SERVICE_ROLE_KEY", KEY)
.output()
.unwrap()
}
fn text(output: &Output) -> String {
format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
)
}
fn writes(state: &Shared) -> usize {
let log = &state.lock().unwrap().log;
log.iter().filter(|line| line.starts_with("POST")).count()
}
#[test]
fn check_reads_and_writes_nothing() {
let (state, url) = stub();
let output = run(&url, &["--check", DRAFT]);
let said = text(&output);
assert!(output.status.success(), "{said}");
assert!(said.contains("would upload, confirm"), "{said}");
assert_eq!(writes(&state), 0);
assert!(!said.contains(KEY));
}
#[test]
fn a_run_uploads_verifies_then_confirms_and_a_rerun_does_nothing() {
let (state, url) = stub();
let first = run(&url, &[DRAFT]);
assert!(first.status.success(), "{}", text(&first));
{
let held = state.lock().unwrap();
assert_eq!(held.objects.get(&format!("{SHA}.svg")), Some(&horse()));
assert_eq!(held.row, Some((SHA.to_owned(), true)));
}
let before = writes(&state);
let again = run(&url, &[DRAFT]);
let said = text(&again);
assert!(again.status.success(), "{said}");
assert!(said.contains("nothing to do"), "{said}");
assert_eq!(writes(&state), before);
}
#[test]
fn different_bytes_under_the_key_are_never_overwritten() {
let (state, url) = stub();
state
.lock()
.unwrap()
.objects
.insert(format!("{SHA}.svg"), b"other".to_vec());
let output = run(&url, &[DRAFT]);
let said = text(&output);
assert_eq!(output.status.code(), Some(1), "{said}");
assert!(
said.contains("State") && said.contains("other bytes"),
"{said}"
);
assert_eq!(writes(&state), 0);
assert_eq!(
state.lock().unwrap().objects[&format!("{SHA}.svg")],
b"other"
);
}
#[test]
fn a_missing_or_foreign_row_stops_the_run_before_any_write() {
for row in [None, Some(("f".repeat(64), false))] {
let (state, url) = stub();
state.lock().unwrap().row = row;
let output = run(&url, &[DRAFT]);
assert_eq!(output.status.code(), Some(1), "{}", text(&output));
assert_eq!(writes(&state), 0);
}
}
#[test]
fn a_refused_key_is_an_access_refusal_and_never_printed() {
for status in [401, 403] {
let (state, url) = stub();
state.lock().unwrap().bucket_status = status;
let output = run(&url, &[DRAFT]);
let said = text(&output);
assert_eq!(output.status.code(), Some(1), "{said}");
assert!(said.contains("Access") && !said.contains(KEY), "{said}");
assert_eq!(writes(&state), 0);
}
}
#[test]
fn a_failure_after_the_upload_resumes_with_the_confirmation_only() {
let (state, url) = stub();
state.lock().unwrap().fail_confirm = true;
let broken = run(&url, &[DRAFT]);
assert_eq!(broken.status.code(), Some(3), "{}", text(&broken));
assert!(
state
.lock()
.unwrap()
.objects
.contains_key(&format!("{SHA}.svg"))
);
assert_eq!(state.lock().unwrap().row, Some((SHA.to_owned(), false)));
{
let mut held = state.lock().unwrap();
held.fail_confirm = false;
held.log.clear();
}
let resumed = run(&url, &[DRAFT]);
assert!(resumed.status.success(), "{}", text(&resumed));
let log = state.lock().unwrap().log.clone();
assert!(
log.iter()
.all(|l| !l.contains("/storage/v1/object") || l.starts_with("GET"))
);
assert_eq!(state.lock().unwrap().row, Some((SHA.to_owned(), true)));
}
#[test]
fn an_unreachable_destination_is_a_network_error_after_bounded_retries() {
let free = TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let output = run(&format!("http://127.0.0.1:{free}"), &[DRAFT]);
let said = text(&output);
assert_eq!(output.status.code(), Some(3), "{said}");
assert!(said.contains("Network") && !said.contains(KEY), "{said}");
}
#[test]
fn destinations_that_are_not_a_local_stack_are_refused_before_any_request() {
for url in [
"https://127.0.0.1:54321",
"http://127.0.0.1",
"http://10.1.2.3:54321",
"http://example.test:54321",
"http://user:pw@127.0.0.1:54321",
"http://127.0.0.1:54321/rest/v1",
"",
] {
let output = run(url, &[DRAFT]);
let said = text(&output);
assert_eq!(output.status.code(), Some(1), "{url}: {said}");
assert!(said.contains("Destination"), "{url}: {said}");
assert!(!said.contains("pw"), "{url}: {said}");
}
}
#[test]
fn a_folder_outside_the_seed_is_refused() {
let (state, url) = stub();
let outside = concat!(env!("CARGO_MANIFEST_DIR"), "/src");
let output = run(&url, &["--check", outside]);
assert_eq!(output.status.code(), Some(1), "{}", text(&output));
assert!(state.lock().unwrap().log.is_empty());
}
#[test]
fn a_usage_error_names_the_usage() {
let (_, url) = stub();
let output = run(&url, &["--check"]);
assert_eq!(output.status.code(), Some(1));
assert!(text(&output).contains("usage"));
}