fix(domain): refuse lone UTF-16 surrogates instead of replacing them (L1)

serde_json already refuses a lone surrogate escape; the U+FFFD pre-pass
is gone and parse_json classifies the refusal (strict escape scan, on
failure only) so callers name it: closed kind lone_surrogate from
operation inputs, sheet.check, draft.restore and archive.import.
Oracles untouched: the two L1 cases and the Zod verdict now assert that
TypeScript kept the surrogate and Rust refuses the text.
This commit is contained in:
bchanot committed 2026-10-14 21:49:28 +02:00
1 parent 36316cf67f
commit 20d03cf7fd
9 files changed
+275 -75

No files matched your search

+94
View File
@@ -882,3 +882,97 @@ fn import_reads_and_rebases_or_names_the_failure_as_the_engine_does() {
);
}
}
// ---------------------------------------------------------------------------
// L1: a lone UTF-16 surrogate escape refuses the text whole, never replaced
/// `value` as JSON text with `MARK` swapped for the raw escape `escape` (serde_json cannot
/// write a lone surrogate itself; `JSON.stringify` writes exactly this).
fn text_with_escape(value: &Value, escape: &str) -> String {
serde_json::to_string(value)
.unwrap()
.replace("MARK", escape)
}
#[test]
fn a_draft_with_a_lone_surrogate_is_refused_as_content_never_rewritten() {
let f = EnvelopeFixture::new();
let report = with(report_value(&f.report), "conclusion", json!("aMARKb"));
for escape in ["\\ud800", "\\udfff", "\\ud83d\\ud83d"] {
let draft = text_with_escape(&report, escape);
assert!(
matches!(
restore_draft(&f.draft, &draft, None),
Err(RestoreFailure::LoneSurrogate(_))
),
"{escape}"
);
}
let v2 = common::repo_file("packages/domain/tests/fixtures/report-v2.full.json").replace(
"\"comment\": \"Réagit à la palpation du garrot.\"",
"\"comment\": \"Réagit \\udc00\"",
);
assert!(v2.contains("\\udc00"));
assert!(matches!(
restore_draft(&f.draft, &v2, None),
Err(RestoreFailure::LoneSurrogate(_))
));
}
#[test]
fn an_archive_with_a_lone_surrogate_is_set_aside_while_the_draft_restores() {
let f = EnvelopeFixture::new();
let draft = serde_json::to_string(&report_value(&f.report)).unwrap();
let mut envelope = f.envelope();
envelope["branding"]["poweredBy"]["fallbackText"] = json!("MARK");
let readable = serde_json::to_string(&envelope)
.unwrap()
.replace("MARK", "zen");
assert!(
!restore_draft(&f.draft, &draft, Some(&readable))
.unwrap()
.archive_rejected
);
let archive = text_with_escape(&envelope, "\\ud800");
let restored = restore_draft(&f.draft, &draft, Some(&archive)).unwrap();
assert!(restored.archive.is_none() && restored.archive_rejected);
assert_eq!(restored.rebase.report, f.report);
}
#[test]
fn an_attachment_with_a_lone_surrogate_is_refused_with_its_own_kind() {
let f = EnvelopeFixture::new();
let mut envelope = f.envelope();
envelope["report"]["conclusion"] = json!("MARK");
let text = text_with_escape(&envelope, "\\ud800");
assert!(matches!(
import_archive(&f.draft, &text),
Err(ImportFailure::LoneSurrogate(_))
));
let bare = text_with_escape(
&with(report_value(&f.report), "conclusion", json!("MARK")),
"\\udbff",
);
assert!(matches!(
import_archive(&f.draft, &bare),
Err(ImportFailure::LoneSurrogate(_))
));
}
#[test]
fn normal_text_with_pairs_and_escaped_backslashes_reads_unchanged() {
let f = EnvelopeFixture::new();
let kept = "😀 \\ud800 é \u{FFFD}";
let report = with(report_value(&f.report), "conclusion", json!(kept));
let text = serde_json::to_string(&report).unwrap();
assert!(text.contains("\\\\ud800"));
let restored = restore_draft(&f.draft, &text, None).unwrap();
assert_eq!(restored.rebase.report.conclusion, kept);
let mut envelope = f.envelope();
envelope["report"] = report.clone();
let paired = serde_json::to_string(&envelope)
.unwrap()
.replace('😀', "\\ud83d\\ude00");
let imported = import_archive(&f.draft, &paired).unwrap();
assert_eq!(imported.rebase.report.conclusion, kept);
}