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:
1 parent
36316cf67f
commit
20d03cf7fd
9 files changed
+275
-75
No files matched your search
@@ -7,7 +7,7 @@ use serde::Serialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use crate::domain::js::parse_json;
|
||||
use crate::domain::js::{JsonReadError, parse_json};
|
||||
use crate::domain::sheet::{BundledSheetData, LoadError, LoadedSheet};
|
||||
use crate::domain::{ReportZone, SheetRef};
|
||||
use crate::json::{pretty_js_round_trip, to_js_compact};
|
||||
@@ -65,9 +65,21 @@ fn unknown(op: &str) -> Failure {
|
||||
Failure::new("unknown_op", format!("no operation named {op}"))
|
||||
}
|
||||
|
||||
/// A JSON text the core cannot read: `lone_surrogate` when reading it would alter a string
|
||||
/// (L1, refused whole), `invalid_json` otherwise.
|
||||
impl From<JsonReadError> for Failure {
|
||||
fn from(error: JsonReadError) -> Self {
|
||||
let kind = match error {
|
||||
JsonReadError::LoneSurrogate { .. } => "lone_surrogate",
|
||||
JsonReadError::Syntax(_) => "invalid_json",
|
||||
};
|
||||
Self::new(kind, error)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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))?;
|
||||
let value = parse_json(input)?;
|
||||
T::deserialize(&value).map_err(|error| Failure::new("invalid_shape", error))
|
||||
}
|
||||
|
||||
@@ -118,6 +130,13 @@ mod tests {
|
||||
assert!(outcome.contains(r#""kind":"invalid_json""#));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lone_surrogate_input_is_a_closed_kind_never_a_replacement() {
|
||||
let outcome = call("report_zone", r#"{"annotationId":"a\ud800","viewId":"v"}"#);
|
||||
assert!(outcome.contains(r#""kind":"lone_surrogate""#), "{outcome}");
|
||||
assert!(!outcome.contains('\u{FFFD}'));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_zone_round_trip_keeps_the_declared_order() {
|
||||
let outcome = call(
|
||||
|
||||
Reference in new issue
Block a user