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.
385 lines
14 KiB
Rust
385 lines
14 KiB
Rust
//! JavaScript and Zod semantics the ported contracts depend on (R2 plan P1, P2).
|
||
//!
|
||
//! The TypeScript domain reads JSON with `JSON.parse` and validates it with Zod 3: a duplicate
|
||
//! key keeps its last value at its first position, `.int()` accepts any number with an
|
||
//! integer value (`2.0`, `1e20`), string lengths count UTF-16 code units, and the patterns are
|
||
//! JavaScript regular expressions whose `$` only matches at the end of the input. The helpers
|
||
//! below reproduce exactly that, so a document the old code accepted is accepted here and
|
||
//! prints the same. One declared exception: a lone surrogate escape, which `JSON.parse` keeps
|
||
//! and a Rust string cannot hold, refuses the text instead of being altered (L1).
|
||
|
||
use std::fmt;
|
||
|
||
use serde::de::Error as _;
|
||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||
use serde_json::Value;
|
||
|
||
/// Why a JSON text cannot be read.
|
||
#[derive(Debug)]
|
||
pub enum JsonReadError {
|
||
/// The text escapes a lone UTF-16 surrogate (`\uD800`–`\uDFFF` outside a high + low
|
||
/// pair). `JSON.parse` keeps it; reading it here would replace it, so the whole text is
|
||
/// refused and the caller keeps it verbatim (L1: user data is never altered silently).
|
||
LoneSurrogate { offset: usize },
|
||
/// Not JSON.
|
||
Syntax(serde_json::Error),
|
||
}
|
||
|
||
impl fmt::Display for JsonReadError {
|
||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||
match self {
|
||
Self::LoneSurrogate { offset } => {
|
||
write!(f, "lone UTF-16 surrogate escape at byte {offset}")
|
||
}
|
||
Self::Syntax(error) => error.fmt(f),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Reads JSON text as `JSON.parse` does, as far as a Rust string allows. serde_json refuses
|
||
/// a lone surrogate escape like any other error; the refusal is then classified so callers
|
||
/// can name it (the scan runs on failures only).
|
||
pub fn parse_json(text: &str) -> Result<Value, JsonReadError> {
|
||
serde_json::from_str(text).map_err(|error| match find_lone_surrogate(text) {
|
||
Some(offset) => JsonReadError::LoneSurrogate { offset },
|
||
None => JsonReadError::Syntax(error),
|
||
})
|
||
}
|
||
|
||
/// Byte offset of the first `\uD800`–`\uDFFF` escape that is not a high + low pair.
|
||
/// Escapes are consumed left to right, so `\\u…` (an escaped backslash) is skipped.
|
||
pub fn find_lone_surrogate(text: &str) -> Option<usize> {
|
||
let bytes = text.as_bytes();
|
||
let mut i = 0;
|
||
while i < bytes.len() {
|
||
if bytes[i] != b'\\' {
|
||
i += 1;
|
||
continue;
|
||
}
|
||
let Some(unit) = escape_unit(bytes, i) else {
|
||
i += 2;
|
||
continue;
|
||
};
|
||
let is_pair = (0xD800..=0xDBFF).contains(&unit)
|
||
&& escape_unit(bytes, i + 6).is_some_and(|low| (0xDC00..=0xDFFF).contains(&low));
|
||
if is_pair {
|
||
i += 12;
|
||
continue;
|
||
}
|
||
if (0xD800..=0xDFFF).contains(&unit) {
|
||
return Some(i);
|
||
}
|
||
i += 6;
|
||
}
|
||
None
|
||
}
|
||
|
||
/// The code unit of a `\uXXXX` escape starting at `at`, if there is one.
|
||
fn escape_unit(bytes: &[u8], at: usize) -> Option<u32> {
|
||
let escape = bytes.get(at..at + 6)?;
|
||
if escape[0] != b'\\' || escape[1] != b'u' || !escape[2..].iter().all(u8::is_ascii_hexdigit) {
|
||
return None;
|
||
}
|
||
let digits = std::str::from_utf8(&escape[2..]).ok()?;
|
||
u32::from_str_radix(digits, 16).ok()
|
||
}
|
||
|
||
/// Length of a string as JavaScript's `.length` counts it (UTF-16 code units).
|
||
pub fn utf16_len(text: &str) -> usize {
|
||
text.encode_utf16().count()
|
||
}
|
||
|
||
/// An integer as Zod's `.int()` accepts it: any finite number with no fraction, kept as the
|
||
/// JavaScript number it is and printed as JavaScript prints it.
|
||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
|
||
pub struct JsInt(f64);
|
||
|
||
impl JsInt {
|
||
pub fn new(value: f64) -> Option<Self> {
|
||
(value.is_finite() && value.fract() == 0.0).then_some(Self(value))
|
||
}
|
||
|
||
pub fn get(self) -> f64 {
|
||
self.0
|
||
}
|
||
}
|
||
|
||
impl From<u32> for JsInt {
|
||
fn from(value: u32) -> Self {
|
||
Self(f64::from(value))
|
||
}
|
||
}
|
||
|
||
impl Serialize for JsInt {
|
||
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||
// Exact below 2^53: printed as an integer by every formatter; beyond, the float
|
||
// goes through `js_number` like JavaScript.
|
||
if self.0.abs() < 9_007_199_254_740_992.0 {
|
||
#[allow(clippy::cast_possible_truncation)]
|
||
return serializer.serialize_i64(self.0 as i64);
|
||
}
|
||
serializer.serialize_f64(self.0)
|
||
}
|
||
}
|
||
|
||
impl<'de> Deserialize<'de> for JsInt {
|
||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||
let value = f64::deserialize(deserializer)?;
|
||
Self::new(value).ok_or_else(|| D::Error::custom("expected an integer"))
|
||
}
|
||
}
|
||
|
||
/// `.optional()`: absent is fine, an explicit `null` is refused (serde alone would read it
|
||
/// as absent). Used with `#[serde(default, deserialize_with = "js::present")]`.
|
||
pub fn present<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
|
||
where
|
||
D: Deserializer<'de>,
|
||
T: Deserialize<'de>,
|
||
{
|
||
T::deserialize(deserializer).map(Some)
|
||
}
|
||
|
||
/// `.nullable()` without a default: the key is required, `null` is a value. Used with
|
||
/// `#[serde(deserialize_with = "js::nullable")]` (no `default`, so a missing key fails).
|
||
pub fn nullable<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
|
||
where
|
||
D: Deserializer<'de>,
|
||
T: Deserialize<'de>,
|
||
{
|
||
Option::<T>::deserialize(deserializer)
|
||
}
|
||
|
||
/// `z.string().min(1)`.
|
||
pub fn non_empty(text: &str) -> bool {
|
||
!text.is_empty()
|
||
}
|
||
|
||
/// `/^[a-z0-9][a-z0-9_-]{0,63}$/` (stable ids).
|
||
pub fn is_stable_id(text: &str) -> bool {
|
||
let bytes = text.as_bytes();
|
||
!bytes.is_empty()
|
||
&& bytes.len() <= 64
|
||
&& matches!(bytes[0], b'a'..=b'z' | b'0'..=b'9')
|
||
&& bytes[1..]
|
||
.iter()
|
||
.all(|b| matches!(b, b'a'..=b'z' | b'0'..=b'9' | b'_' | b'-'))
|
||
}
|
||
|
||
/// `/^[A-Za-z0-9][A-Za-z0-9_-]{0,15}$/` (vertebral region codes).
|
||
pub fn is_region_code(text: &str) -> bool {
|
||
let bytes = text.as_bytes();
|
||
!bytes.is_empty()
|
||
&& bytes.len() <= 16
|
||
&& bytes[0].is_ascii_alphanumeric()
|
||
&& bytes[1..]
|
||
.iter()
|
||
.all(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'-')
|
||
}
|
||
|
||
/// `/^#[0-9a-fA-F]{6}$/`.
|
||
pub fn is_hex_color(text: &str) -> bool {
|
||
let bytes = text.as_bytes();
|
||
bytes.len() == 7 && bytes[0] == b'#' && bytes[1..].iter().all(u8::is_ascii_hexdigit)
|
||
}
|
||
|
||
/// `/^[0-9a-f]{64}$/`.
|
||
pub fn is_sha256_hex(text: &str) -> bool {
|
||
text.len() == 64 && text.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
|
||
}
|
||
|
||
/// `/^[A-Za-z0-9+/]*={0,2}$/`.
|
||
pub fn is_base64(text: &str) -> bool {
|
||
let body = text.trim_end_matches('=');
|
||
text.len() - body.len() <= 2
|
||
&& body
|
||
.bytes()
|
||
.all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'/')
|
||
}
|
||
|
||
/// Zod 3's `.uuid()` pattern: 8-4-4-4-12 hexadecimal digits, any case, any version.
|
||
pub fn is_uuid(text: &str) -> bool {
|
||
let bytes = text.as_bytes();
|
||
bytes.len() == 36
|
||
&& bytes.iter().enumerate().all(|(index, byte)| match index {
|
||
8 | 13 | 18 | 23 => *byte == b'-',
|
||
_ => byte.is_ascii_hexdigit(),
|
||
})
|
||
}
|
||
|
||
/// Zod's `.url()`: `new URL(text)` succeeds (WHATWG parser).
|
||
pub fn is_url(text: &str) -> bool {
|
||
url::Url::parse(text).is_ok()
|
||
}
|
||
|
||
/// A string literal field (the tag of a discriminated union, a format name): reads only
|
||
/// that literal and prints it, at the position the Zod schema declares it.
|
||
macro_rules! literal {
|
||
($name:ident, $value:literal) => {
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||
pub struct $name;
|
||
|
||
impl serde::Serialize for $name {
|
||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||
serializer.serialize_str($value)
|
||
}
|
||
}
|
||
|
||
impl<'de> serde::Deserialize<'de> for $name {
|
||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||
use serde::de::Error as _;
|
||
let text = <String as serde::Deserialize>::deserialize(deserializer)?;
|
||
if text == $value {
|
||
Ok($name)
|
||
} else {
|
||
Err(D::Error::custom(concat!("expected \"", $value, "\"")))
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(feature = "ts")]
|
||
impl ts_rs::TS for $name {
|
||
type WithoutGenerics = Self;
|
||
type OptionInnerType = Self;
|
||
fn name(_: &ts_rs::Config) -> String {
|
||
format!("\"{}\"", $value)
|
||
}
|
||
fn inline(cfg: &ts_rs::Config) -> String {
|
||
Self::name(cfg)
|
||
}
|
||
}
|
||
};
|
||
}
|
||
|
||
/// A number literal field (a document version): reads any JSON number equal to it (`2.0`
|
||
/// included, as `z.literal(2)` after `JSON.parse`) and prints the integer.
|
||
macro_rules! number_literal {
|
||
($name:ident, $value:literal) => {
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||
pub struct $name;
|
||
|
||
impl serde::Serialize for $name {
|
||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||
serializer.serialize_u32($value)
|
||
}
|
||
}
|
||
|
||
impl<'de> serde::Deserialize<'de> for $name {
|
||
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||
use serde::de::Error as _;
|
||
if <f64 as serde::Deserialize>::deserialize(deserializer)? == f64::from($value) {
|
||
Ok($name)
|
||
} else {
|
||
Err(D::Error::custom(concat!("expected ", $value)))
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(feature = "ts")]
|
||
impl ts_rs::TS for $name {
|
||
type WithoutGenerics = Self;
|
||
type OptionInnerType = Self;
|
||
fn name(_: &ts_rs::Config) -> String {
|
||
stringify!($value).to_owned()
|
||
}
|
||
fn inline(cfg: &ts_rs::Config) -> String {
|
||
Self::name(cfg)
|
||
}
|
||
}
|
||
};
|
||
}
|
||
pub(crate) use {literal, number_literal};
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn lone_surrogate_escapes_are_found_and_pairs_are_not() {
|
||
assert_eq!(find_lone_surrogate("\"a\\ud800b\""), Some(2));
|
||
assert_eq!(find_lone_surrogate("\"\\udc00\""), Some(1));
|
||
assert_eq!(find_lone_surrogate("\"\\uDBFF\""), Some(1));
|
||
assert_eq!(find_lone_surrogate("\"\\ud800\\ud800\""), Some(1));
|
||
assert_eq!(find_lone_surrogate("\"\\ud800\\n\""), Some(1));
|
||
assert_eq!(find_lone_surrogate("\"\\ud83d\\ude00\\udc00\""), Some(13));
|
||
assert_eq!(find_lone_surrogate("\"x\\ud800"), Some(2));
|
||
assert_eq!(find_lone_surrogate("\"\\ud83d\\ude00\""), None);
|
||
assert_eq!(find_lone_surrogate("\"\\\\ud800\""), None);
|
||
assert_eq!(find_lone_surrogate("\"\\u+800 \\u00e9 é\""), None);
|
||
assert_eq!(find_lone_surrogate("plain"), None);
|
||
}
|
||
|
||
#[test]
|
||
fn a_lone_surrogate_refuses_the_text_and_is_never_replaced() {
|
||
for text in [
|
||
"{\"t\":\"x\\ud800\"}",
|
||
"{\"t\":\"\\udfff\"}",
|
||
"{\"t\":[\"ok\",\"\\ud83d\"]}",
|
||
"{\"\\ud800\":1}",
|
||
] {
|
||
assert!(
|
||
matches!(parse_json(text), Err(JsonReadError::LoneSurrogate { .. })),
|
||
"{text}"
|
||
);
|
||
}
|
||
assert!(matches!(
|
||
parse_json("{ not json"),
|
||
Err(JsonReadError::Syntax(_))
|
||
));
|
||
}
|
||
|
||
#[test]
|
||
fn normal_text_reads_unchanged() {
|
||
let Ok(value) = parse_json("{\"t\":\"é \\u00e9 \\ud83d\\ude00 😀 \\\\ud800 \\uFFFD\"}")
|
||
else {
|
||
panic!("parse failed");
|
||
};
|
||
assert_eq!(value["t"], "é é 😀 😀 \\ud800 \u{FFFD}");
|
||
}
|
||
|
||
#[test]
|
||
fn duplicate_keys_keep_the_last_value_at_the_first_position() {
|
||
let Ok(value) = parse_json(r#"{"a":1,"b":2,"a":3}"#) else {
|
||
panic!("parse failed");
|
||
};
|
||
assert_eq!(value.to_string(), r#"{"a":3,"b":2}"#);
|
||
}
|
||
|
||
#[test]
|
||
fn js_int_accepts_integer_valued_numbers_only() {
|
||
let parsed: Vec<Option<JsInt>> = ["2", "2.0", "1e20", "-0", "2.5"]
|
||
.iter()
|
||
.map(|t| serde_json::from_str::<JsInt>(t).ok())
|
||
.collect();
|
||
assert_eq!(parsed[0].map(JsInt::get), Some(2.0));
|
||
assert_eq!(parsed[1].map(JsInt::get), Some(2.0));
|
||
assert_eq!(parsed[2].map(JsInt::get), Some(1e20));
|
||
assert!(parsed[3].is_some());
|
||
assert!(parsed[4].is_none());
|
||
let printed = crate::json::to_js_pretty(&[JsInt(2.0), JsInt(1e20), JsInt(-0.0)]);
|
||
assert_eq!(
|
||
printed.ok().as_deref(),
|
||
Some("[\n 2,\n 100000000000000000000,\n 0\n]")
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn utf16_lengths_count_surrogate_pairs_twice() {
|
||
assert_eq!(utf16_len("é"), 1);
|
||
assert_eq!(utf16_len("😀"), 2);
|
||
}
|
||
|
||
#[test]
|
||
fn patterns_match_the_zod_regular_expressions() {
|
||
assert!(is_stable_id("view-main") && is_stable_id(&"a".repeat(64)));
|
||
assert!(!is_stable_id("_x") && !is_stable_id("A") && !is_stable_id(&"a".repeat(65)));
|
||
assert!(!is_stable_id("a\n"));
|
||
assert!(is_region_code("Cd") && !is_region_code("-C") && !is_region_code(""));
|
||
assert!(is_hex_color("#a0B1c2") && !is_hex_color("#a0B1c"));
|
||
assert!(is_sha256_hex(&"0".repeat(64)) && !is_sha256_hex(&"A".repeat(64)));
|
||
assert!(is_base64("") && is_base64("QUJD==") && !is_base64("QUJD===") && !is_base64("a=b"));
|
||
assert!(is_uuid("123e4567-E89B-12d3-a456-426614174000"));
|
||
assert!(!is_uuid("123e4567e89b12d3a456426614174000"));
|
||
assert!(is_url("https://example.org/a?b#c") && !is_url("not a url") && !is_url("http://"));
|
||
}
|
||
}
|