fix(domain): security audit follow-ups — deserialise JSON by reference, literal delete paths in the wasm build

The security gate (PASS, 0 blocking) reported LOW items: an imported
attachment's Value was copied for the wrapper and the snapshot gate (a
hostile 16 MiB attachment multiplied linear memory), and wasm-build.sh
deleted its outputs through a variable. Every JSON input is now read with
T::deserialize(&Value) (same errors, no copy); one deserialiser instead of
two also shrinks the shipped module from 1,180,281 to 1,046,498 bytes raw
(372,132 → 340,929 gzip). The two outputs are removed on their literal
paths. RUST.md records the numbers, the quarantine retention and the
base-image tag residual.
This commit is contained in:
bchanot committed 2026-10-14 01:02:30 +02:00
1 parent 8e8c58efca
commit e49009e363
10 files changed
+39 -29

No files matched your search

+4 -4
View File
@@ -4,7 +4,7 @@
//! wrapper is then validated whole or dropped with a reason. Accepted shapes: a bare Report v2
//! or v3, or an archive envelope of version 1.
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[cfg(feature = "ts")]
use ts_rs::TS;
@@ -95,8 +95,8 @@ fn read_envelope_v1(
data: &Value,
report: &Report,
) -> Result<ReportArchiveEnvelope, SnapshotRejection> {
let wrapper: EnvelopeWrapper =
serde_json::from_value(data.clone()).map_err(|_| SnapshotRejection::Schema)?;
// Deserialised by reference: an attachment can be megabytes, never copy the whole value.
let wrapper = EnvelopeWrapper::deserialize(data).map_err(|_| SnapshotRejection::Schema)?;
let snapshot = assert_snapshot(data.get("sheetSnapshot").unwrap_or(&Value::Null))?;
if !snapshot_matches(&snapshot, report) {
return Err(SnapshotRejection::SheetMismatch);
@@ -121,5 +121,5 @@ fn snapshot_matches(snapshot: &SheetSnapshot, report: &Report) -> bool {
/// `ReportArchiveEnvelopeSchema.parse`: the shape of a whole envelope (the snapshot's shape
/// only, no caps nor integrity), as the session keeps it.
pub fn parse_envelope(data: &Value) -> Result<ReportArchiveEnvelope, serde_json::Error> {
serde_json::from_value(data.clone())
ReportArchiveEnvelope::deserialize(data)
}
@@ -7,7 +7,7 @@
use std::collections::{HashMap, HashSet};
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[cfg(feature = "ts")]
use ts_rs::TS;
@@ -195,8 +195,7 @@ fn snapshot_assets(sheet: &LoadedSheet) -> Vec<SheetSnapshotAsset> {
/// caps, integrity rules and every view reaching a carried asset (`integrity`).
pub fn assert_snapshot(input: &Value) -> Result<SheetSnapshot, SnapshotRejection> {
assert_finding_depth(input)?;
let snapshot: SheetSnapshot =
serde_json::from_value(input.clone()).map_err(|_| SnapshotRejection::Schema)?;
let snapshot = SheetSnapshot::deserialize(input).map_err(|_| SnapshotRejection::Schema)?;
assert_caps(&snapshot)?;
assert_integrity(&snapshot)?;
Ok(snapshot)
+4 -5
View File
@@ -6,7 +6,7 @@
//! truthiness of the original is kept on purpose: an empty `detailSourceId` means the main
//! view, `requiresVetReferral: false` is dropped, `vertebrae: []` is kept.
use serde::Serialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[cfg(feature = "ts")]
use ts_rs::TS;
@@ -56,7 +56,7 @@ pub fn read_report_document(
) -> Result<MigrationOutcome, ReadFailure> {
let version = version_of(data);
if version == Some(3.0) {
let report: Report = serde_json::from_value(data.clone()).map_err(ReadFailure::invalid)?;
let report = Report::deserialize(data).map_err(ReadFailure::invalid)?;
return Ok(MigrationOutcome {
report,
unresolved_zones: 0,
@@ -64,8 +64,7 @@ pub fn read_report_document(
});
}
if version == Some(2.0) {
let report: ReportV2 =
serde_json::from_value(data.clone()).map_err(ReadFailure::invalid)?;
let report = ReportV2::deserialize(data).map_err(ReadFailure::invalid)?;
return migrate_report_v2_to_v3(&report, map);
}
Err(ReadFailure::UnsupportedVersion(version))
@@ -108,7 +107,7 @@ pub fn migrate_report_v2_to_v3(
/// `ReportSchema.parse` of a report built in code: every constraint checked again.
pub(crate) fn reparse(report: &Report) -> Result<Report, ReadFailure> {
let value = serde_json::to_value(report).map_err(ReadFailure::invalid)?;
serde_json::from_value(value).map_err(ReadFailure::invalid)
Report::deserialize(&value).map_err(ReadFailure::invalid)
}
fn unresolved_counts(report: &Report) -> (u32, u32) {
+1 -1
View File
@@ -20,7 +20,7 @@ pub struct SheetCheck {
/// Strict parse then rules; a document the shape refuses is an error, never a check.
pub fn check_sheet_document(value: Value) -> Result<SheetCheck, serde_json::Error> {
let document: AnatomicalSheetDocument = serde_json::from_value(value)?;
let document = AnatomicalSheetDocument::deserialize(&value)?;
let issues = validate_sheet_document(&document);
let publishable = is_publishable(&issues);
Ok(SheetCheck {
+2 -1
View File
@@ -1,6 +1,7 @@
//! Starting point of a new sheet (`anatomical-sheet/create.ts`): shape-valid so it can be
//! saved as a draft at once, but not publishable until the main view gets its SVG asset.
use serde::Deserialize;
use serde_json::json;
use super::schema::{AnatomicalSheetDocument, LocalizedText, SHEET_DOCUMENT_SCHEMA_VERSION};
@@ -13,7 +14,7 @@ pub fn create_empty_sheet_document(
species_id: &str,
name: &LocalizedText,
) -> Result<AnatomicalSheetDocument, serde_json::Error> {
serde_json::from_value(json!({
AnatomicalSheetDocument::deserialize(&json!({
"schema_version": SHEET_DOCUMENT_SCHEMA_VERSION,
"species_id": species_id,
"name": name,
+2 -2
View File
@@ -75,8 +75,8 @@ impl From<NoMainView> for LoadError {
impl LoadedSheet {
/// `parseBundledSheet` then `buildSheetIndex`.
pub fn from_bundle(data: BundledSheetData) -> Result<Self, LoadError> {
let document: AnatomicalSheetDocument = serde_json::from_value(data.document)?;
let migration_map: ReportMigrationMap = serde_json::from_value(data.migration_map)?;
let document = AnatomicalSheetDocument::deserialize(&data.document)?;
let migration_map = ReportMigrationMap::deserialize(&data.migration_map)?;
let index = SheetIndex::build(&document)?;
let r = data.reference;
Ok(Self {
+1 -1
View File
@@ -68,7 +68,7 @@ fn unknown(op: &str) -> Failure {
/// Parses the JSON text of an operation input into its contract type.
pub(crate) fn parse_input<T: DeserializeOwned>(input: &str) -> Result<T, Failure> {
let value = parse_json(input).map_err(|error| Failure::new("invalid_json", error))?;
serde_json::from_value(value).map_err(|error| Failure::new("invalid_shape", error))
T::deserialize(&value).map_err(|error| Failure::new("invalid_shape", error))
}
pub(crate) fn to_value<T: Serialize>(value: &T) -> Outcome {