Preserve LightCraft zero-padded entities with linear scanning

Reuse LightCraft f786b87533e58c6d31c7f9f2897814904448f39a entity_end implementation and regression. XML numeric references are not arbitrarily length limited.
This commit is contained in:
Dylan M. Taylor committed 2026-11-15 11:21:36 -05:00
1 parent f53ed0c505
commit c17840e099
2 files changed
+9 -2

No files matched your search

+1 -1
View File
@@ -14,6 +14,6 @@ Source: `storytold/craft-anything` at `88d1d9cac852aec1c7fd97886db451e005e49d2a`
`parse_xml` and `parse_xml_with_limits` now read UTF-8 and BOM-tagged UTF-16 XML into the same `Value` tree. XML input/decoded-text size, tokens, values, nesting and cumulative key/leaf text are bounded. The independent token ceiling is eight times the smaller node/object allowance plus eight; unusually fragmented text can exhaust it before the value limit. This is not a precise RSS limit. The decoded-XML limit also includes markup; `<data>` consumes its encoded text allowance, including whitespace. XML `max_objects` and `max_nodes` both cap values.
The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning is bounded before searching for a terminator. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added.
The attributed Craft Anything reader includes its complete-document, declaration-skipping (no external entity fetching), quoted-tag and normalized-line-ending behavior. Nonfinite reals are refused using contributor PR #1905. Unknown or malformed character references retain legacy literal behavior; scanning stops at the next non-entity byte, preserving LightCraft's valid zero-padded numeric references without suffix rescans. Base64 retains that reader's legacy optional-padding/ASCII-whitespace behavior, including unused trailing bits; it is not a signature/key-validation API. No writer, JSON projection, keyed-archive interpretation or app policy is added.
XML source: Craft Anything `8f3b775157d331dd31cfa4da911e26d8856b8c77`, plus #1905 `d85e104f093a334961048881bf003351e1ac4992`. The import credits Brandon Thomas and the existing hardening contributors; #1905 is authored by gmrmad1 and the quoted-tag/line-ending tests by RRFZ. Consumer cutovers require their own policy/parity tests, especially LightCraft's preset mapping and Craft Anything's projections.
+8 -1
View File
@@ -56,6 +56,12 @@ enum Tok {
Text(String),
}
// Ported from LightCraft f786b875: scan one entity-name run, never the remaining document.
fn entity_end(rest: &str) -> Option<usize> {
let n = rest.bytes().skip(1).take_while(|b| b.is_ascii_alphanumeric() || *b == b'#').count();
(rest.as_bytes().get(n + 1) == Some(&b';')).then_some(n + 1)
}
fn decode_entities(s: &str) -> String {
if !s.contains('&') {
return s.to_string();
@@ -65,7 +71,7 @@ fn decode_entities(s: &str) -> String {
while let Some(p) = rest.find('&') {
out.push_str(rest.get(..p).unwrap_or(""));
let after = rest.get(p..).unwrap_or("");
let Some(end) = after.as_bytes().iter().take(13).position(|b| *b == b';') else {
let Some(end) = entity_end(after) else {
out.push('&');
rest = after.get(1..).unwrap_or("");
continue;
@@ -478,6 +484,7 @@ mod tests {
assert_eq!(decode_entities(&(text.clone() + ";")), text + ";");
}
assert_eq!(decode_entities("é&missing; &amp; &#13; &#x41;"), "é&missing; & \r A");
assert_eq!(decode_entities("C&#0000000097;non &#x0000041;"), "Canon A");
}
#[test]
fn caller_limits_refuse_before_unbounded_tree_growth() {