fix(anatomy): R5.4 report guard as an allowlist (report_guard.rs), sweep of 762 probes incl. seeded random strings, NUL refused in field segments

This commit is contained in:
bchanot committed 2026-10-23 12:39:17 +02:00
1 parent 077d91bdff
commit 124d954a0d
10 files changed
+246 -195

No files matched your search

+4 -4
View File
@@ -98,18 +98,18 @@ pub fn split_field(field: &str) -> Result<Vec<&str>, PipelineError> {
"field \"{field}\" has more than {MAX_FIELD_SEGMENTS} segments"
)));
}
if let Some(key) = path.iter().find(|key| key.starts_with('\0')) {
return Err(fail(format!("reserved key {key:?} in field \"{field}\"")));
}
// A control character or a backtick would reach the reports' code spans through the record.
if path
.iter()
.any(|key| key.chars().any(|c| c.is_control() && c != '\0') || key.contains('`'))
.any(|key| key.chars().any(|c| c.is_control()) || key.contains('`'))
{
return Err(fail(format!(
"field {field:?} holds a control character or a backtick"
)));
}
if let Some(key) = path.iter().find(|key| key.starts_with('\0')) {
return Err(fail(format!("reserved key {key:?} in field \"{field}\"")));
}
// A JavaScript assignment through `__proto__` never wrote a key: it set a prototype,
// polluted `Object.prototype` or did nothing. No faithful reading exists, so it is refused.
if path.contains(&"__proto__") {
+5 -6
View File
@@ -242,12 +242,6 @@ fn add_destination(doc: &mut Value, op: &AddOp) -> Result<Destination, PipelineE
})
}
/// A `children` or `categories` of an added value must be a list (a later add would otherwise
/// replace the author's value by `[]`, as the TypeScript's `childrenOf` did), and each entry an
/// object. The
/// TypeScript met a null or a primitive there inconsistently (a TypeError when it walked it,
/// silently skipped when a rename walked past it, refused by the gate when it stayed); the
/// port refuses it at the add (RUST.md §11.12). Walked with an explicit stack.
/// An id an op writes must already be what the final document requires (a stable id; a region
/// code for a region): an id that passed only because a later op removed or renamed its item
/// would still reach the records, and from them the reports' code spans (a line break there
@@ -277,6 +271,11 @@ fn js_string_value(value: &Value) -> String {
super::records::js_string(Some(value))
}
/// A `children` or `categories` of an added value must be a list (a later add would otherwise
/// replace the author's value by `[]`, as the TypeScript's `childrenOf` did), each entry an
/// object with a stable id. The TypeScript met a null or a primitive there inconsistently (a
/// TypeError when it walked it, silently skipped when a rename walked past it, refused by the
/// gate when it stayed); the port refuses it at the add (RUST.md §11.12). Explicit stack.
fn assert_object_children(value: &Value) -> Result<(), PipelineError> {
let mut pending = vec![value];
while let Some(item) = pending.pop() {
+3 -3
View File
@@ -30,8 +30,8 @@ use crate::curation::PipelineError;
use crate::curation::ids::{asset_id_from_sha256, sha256_hex};
use crate::curation::legacy::types::{Anomaly, AssetRecord, ImportResult};
use crate::curation::legacy::{HEAD_VIEW_ID, MAIN_VIEW_ID};
use crate::curation::markdown::format_markdown;
use crate::curation::prettier_json::format_json;
use crate::curation::report_guard::format_report;
use crate::domain::sheet::{AnatomicalSheetDocument, Reference, SheetIssue, ViewBox};
pub const DRAFT_SEED_SQL_FILE: &str = "equine_draft.sql";
@@ -270,8 +270,8 @@ pub fn render_draft_artifacts(
(MIGRATION_MAP_FILE.to_owned(), format_json(&map)?),
];
files.extend(review_drawings(draft)?);
files.push(("CHANGELOG.md".to_owned(), format_markdown(&changelog)?));
files.push(("REVIEW-REPORT.md".to_owned(), format_markdown(&review)?));
files.push(("CHANGELOG.md".to_owned(), format_report(&changelog)?));
files.push(("REVIEW-REPORT.md".to_owned(), format_report(&review)?));
files.push((DRAFT_SEED_SQL_FILE.to_owned(), seed));
Ok(files)
}
+3 -149
View File
@@ -67,158 +67,12 @@ pub fn code(value: Option<&str>) -> String {
value.map_or_else(|| "-".to_owned(), |text| format!("`{text}`"))
}
/// The plain text of a raw line Prettier would rewrite, found in its parts outside code spans
/// (`oracles/r5/markdown-sweep.json`): `*` outside a `**strong**` span; an underscore not between two letters or digits; a
/// backslash before a space or at the end, or an authored one before the cell escape `\|`
/// (`\"` in a JSON value is kept as written); two spaces in a row, a tab, a carriage return;
/// the inline constructs of `rewritten_construct`.
/// The parts of plain text outside the `**strong**` spans Prettier keeps as written (the
/// opening and closing pairs bounded by a non-word character or the line's edge, the inside
/// non-empty, without `*` and without an outer space). `None` when a `*` is left outside
/// such a span.
fn outside_strong(plain: &str) -> Option<String> {
let mut out = String::new();
let mut rest = plain;
let edge = |c: Option<char>| c.is_none_or(|c| !c.is_alphanumeric());
while let Some(open) = rest.find("**") {
let inside = &rest[open + 2..];
let close = inside.find("**")?;
let content = &inside[..close];
let after = inside[close + 2..].chars().next();
let valid = !content.is_empty()
&& !content.contains('*')
&& !content.starts_with(' ')
&& !content.ends_with(' ')
&& edge(rest[..open].chars().next_back())
&& edge(after);
if !valid {
return None;
}
out.push_str(&rest[..open]);
out.push_str(content);
rest = &inside[close + 2..];
}
out.push_str(rest);
(!out.contains('*')).then_some(out)
}
/// Inline constructs Prettier re-prints (the sweep's link, wiki-link, autolink and reference
/// probes): a link, image or reference (`](`, `][`), a wiki link with inner spaces, an autolink
/// (`<scheme:…>`, `<…@…>`), a character reference (`&name;`, `&#n;`). The last three are refused
/// whole although Prettier keeps some of them: a loud refusal, never met by the equine data.
fn rewritten_construct(plain: &str) -> bool {
// `[[ a ]]` loses its inner spaces; `[[a]]` and a polygon's `[[10,10],…]` stay as written.
let padded_wiki = plain.split("[[").skip(1).any(|after| {
let inside = after.split("]]").next().unwrap_or_default();
after.contains("]]")
&& (inside.starts_with(char::is_whitespace) || inside.ends_with(char::is_whitespace))
});
if plain.contains("](") || plain.contains("][") || padded_wiki {
return true;
}
let autolink = plain.split('<').skip(1).any(|after| {
let inside = after.split('>').next().unwrap_or_default();
let closed = after.contains('>');
let scheme = inside.split(':').next().unwrap_or_default();
let is_scheme = inside.contains(':')
&& (2..=32).contains(&scheme.len())
&& scheme
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
&& scheme
.chars()
.all(|c| c.is_ascii_alphanumeric() || "+.-".contains(c));
closed && !inside.contains(' ') && (is_scheme || inside.contains('@'))
});
let reference = plain.split('&').skip(1).any(|after| {
let name = after.split(';').next().unwrap_or_default();
after.contains(';')
&& !name.is_empty()
&& name.len() <= 32
&& name
.trim_start_matches('#')
.chars()
.all(|c| c.is_ascii_alphanumeric())
});
autolink || reference
}
fn plain_rewrite(plain: &str) -> bool {
if rewritten_construct(plain) {
return true;
}
let Some(plain) = outside_strong(plain) else {
return true;
};
let chars: Vec<char> = plain.chars().collect();
// An underscore Prettier keeps sits between two ASCII letters or digits: the sweep shows it
// escapes one next to an accented letter, a CJK, kana or Hangul character.
let word = |i: Option<usize>| {
i.and_then(|i| chars.get(i))
.is_some_and(char::is_ascii_alphanumeric)
};
chars.iter().enumerate().any(|(i, &c)| match c {
'*' | '\t' | '\r' => true,
'_' => !(word(i.checked_sub(1)) && word(Some(i + 1))),
'\\' => match chars.get(i + 1) {
None | Some(' ') => true,
Some('|') => i.checked_sub(1).and_then(|p| chars.get(p)) == Some(&'\\'),
Some(_) => false,
},
' ' => chars.get(i + 1) == Some(&' '),
_ => false,
})
}
/// Refuses a raw line holding Markdown the port does not reproduce, cell by cell in a table
/// row: an odd number of backticks, an empty code span or one with an outer space, or plain
/// text Prettier would rewrite (`plain_rewrite`). The report is then not written at all.
fn guard_line(line: &str) -> Result<(), PipelineError> {
if line.starts_with('|') {
// A table row: each cell as GFM reads it (the padding around the pipes is layout).
for cell in split_row(line) {
guard_text(&cell).map_err(|_| PipelineError::UnsupportedMarkdown(line.to_owned()))?;
}
return Ok(());
}
guard_text(line)
}
fn guard_text(line: &str) -> Result<(), PipelineError> {
let parts: Vec<&str> = line.split('`').collect();
let refuse = || Err(PipelineError::UnsupportedMarkdown(line.to_owned()));
// No carriage return or tab anywhere, code spans included (a CR there broke a table row;
// the other control characters align as Prettier aligns them, `oracles/r5/markdown-tables.json`).
if line.contains(['\r', '\t']) {
return refuse();
}
if parts.len().is_multiple_of(2) {
return refuse();
}
for (index, part) in parts.iter().enumerate() {
let code = index % 2 == 1;
let bad = if code {
part.is_empty() || part.starts_with(' ') || part.ends_with(' ')
} else {
plain_rewrite(part)
};
if bad {
return refuse();
}
}
Ok(())
}
/// What `formatMarkdown` does to a report written by `table()`: every table aligned (cells
/// padded to the widest of their column, three at least, separator dashes to the same
/// width), every other line kept. A text with Markdown Prettier would rewrite otherwise is
/// refused (`guard_line`).
/// width), every other line kept. Prettier's other rewrites of authored text are not
/// reproduced: the draft's reports go through `report_guard::format_report`, which refuses them.
pub fn format_markdown(raw: &str) -> Result<String, PipelineError> {
let lines: Vec<&str> = raw.split('\n').collect();
for line in &lines {
guard_line(line)?;
}
let mut out: Vec<String> = Vec::with_capacity(lines.len());
let mut index = 0;
while index < lines.len() {
@@ -254,7 +108,7 @@ fn table_end(lines: &[&str], start: usize) -> usize {
}
/// The cells of a row: split on the pipes no backslash escapes, trimmed as GFM trims them.
fn split_row(line: &str) -> Vec<String> {
pub(crate) fn split_row(line: &str) -> Vec<String> {
let inner = line.trim_end_matches([' ', '\t']);
let inner = inner.strip_prefix('|').unwrap_or(inner);
let inner = inner.strip_suffix('|').unwrap_or(inner);
+1
View File
@@ -14,6 +14,7 @@ pub mod ids;
pub mod legacy;
pub mod markdown;
pub mod prettier_json;
pub mod report_guard;
pub mod text;
/// Why the pipeline refuses to produce a file. The TypeScript accepted some of these inputs
+140
View File
@@ -0,0 +1,140 @@
//! The guard of the draft's two Markdown reports (CHANGELOG.md, REVIEW-REPORT.md). The port
//! reproduces only Prettier's table alignment (`markdown::format_markdown`); Prettier also
//! rewrites Markdown in authored text (escapes, emphasis, whitespace, links). Rather than
//! chase every such rewrite, the guard admits an allowlist proven identical by the TypeScript
//! sweep (`oracles/r5/markdown-sweep.json`, hand-picked and seeded random probes) and by the
//! equine reports, and refuses everything else with `UnsupportedMarkdown`: a report is either
//! byte for byte the TypeScript's or not written.
//!
//! Admitted outside code spans: letters and digits, single spaces (none at a line end), the
//! punctuation of `PUNCTUATION`, an underscore between two ASCII letters or digits and not
//! glued to a URL, a backslash before `"` or `|` (a JSON quote, the cell escape), `**word**`
//! (one run of letters between spaces or line edges, the import report's form), no inline
//! construct (`rewritten_construct`). Inside code spans (the generated ids and targets):
//! anything but a carriage return or a tab, never empty, no outer space.
use super::PipelineError;
use super::markdown::{format_markdown, split_row};
/// Punctuation Prettier writes as is in the probed positions (the sweep's characters and the
/// equine reports'); any other character outside letters and digits is refused.
const PUNCTUATION: &str = ".,;:!?'\"«»()[]{}/-–—…%°≠=+#&<>’‘“”±→×≤≥|";
/// The starts of a bare URL literal Prettier links.
const URL_STARTS: [&str; 3] = ["http://", "https://", "www."];
fn refuse(line: &str) -> PipelineError {
PipelineError::UnsupportedMarkdown(line.to_owned())
}
/// Inline constructs Prettier re-prints: a link, image or reference (`](`, `][`), a wiki link
/// with inner spaces, an HTML tag or an autolink (`<` before a letter, `/`, `!` or `?`), a
/// character reference (`&name;`, `&#n;`).
fn rewritten_construct(plain: &str) -> bool {
let padded_wiki = plain.split("[[").skip(1).any(|after| {
let inside = after.split("]]").next().unwrap_or_default();
after.contains("]]")
&& (inside.starts_with(char::is_whitespace) || inside.ends_with(char::is_whitespace))
});
let tag = plain
.split('<')
.skip(1)
.any(|after| after.starts_with(|c: char| c.is_ascii_alphabetic() || "/!?".contains(c)));
let reference = plain.split('&').skip(1).any(|after| {
let name = after.split(';').next().unwrap_or_default();
after.contains(';')
&& !name.is_empty()
&& name.len() <= 32
&& name
.trim_start_matches('#')
.chars()
.all(|c| c.is_ascii_alphanumeric())
});
plain.contains("](") || plain.contains("][") || padded_wiki || tag || reference
}
/// The `**word**` spans removed (their word kept); `None` when a `*` stays outside one.
fn outside_strong(plain: &str) -> Option<String> {
let mut out = String::new();
let mut rest = plain;
while let Some(open) = rest.find("**") {
let inside = &rest[open + 2..];
let close = inside.find("**")?;
let word = &inside[..close];
let before = rest[..open].chars().next_back();
let after = inside[close + 2..].chars().next();
let valid = !word.is_empty()
&& word.chars().all(char::is_alphabetic)
&& before.is_none_or(|c| c == ' ')
&& after.is_none_or(|c| c == ' ');
if !valid {
return None;
}
out.push_str(&rest[..open]);
out.push_str(word);
rest = &inside[close + 2..];
}
out.push_str(rest);
(!out.contains('*')).then_some(out)
}
fn admitted_char(chars: &[char], i: usize) -> bool {
let at = |k: usize| chars.get(k).copied();
let previous = i.checked_sub(1).and_then(at);
match chars[i] {
c if c.is_alphanumeric() => true,
' ' => at(i + 1) != Some(' '),
'_' => {
let rest: String = chars[i + 1..].iter().collect();
previous.is_some_and(|c| c.is_ascii_alphanumeric())
&& at(i + 1).is_some_and(|c| c.is_ascii_alphanumeric())
&& !URL_STARTS.iter().any(|start| rest.starts_with(start))
}
'\\' => matches!(at(i + 1), Some('"' | '|')) && previous != Some('\\'),
c => PUNCTUATION.contains(c),
}
}
fn admitted_plain(plain: &str) -> bool {
if rewritten_construct(plain) {
return false;
}
let Some(plain) = outside_strong(plain) else {
return false;
};
let chars: Vec<char> = plain.chars().collect();
(0..chars.len()).all(|i| admitted_char(&chars, i))
}
/// A text outside table delimiters: its code spans and its plain parts.
fn guard_text(text: &str) -> bool {
let parts: Vec<&str> = text.split('`').collect();
if parts.len().is_multiple_of(2) || text.contains(['\r', '\t']) || text.ends_with(' ') {
return false;
}
parts.iter().enumerate().all(|(index, part)| {
if index % 2 == 1 {
!part.is_empty() && !part.starts_with(' ') && !part.ends_with(' ')
} else {
admitted_plain(part)
}
})
}
/// Refuses a raw report line outside the allowlist, cell by cell in a table row.
fn guard_line(line: &str) -> Result<(), PipelineError> {
let admitted = if line.starts_with('|') {
split_row(line).iter().all(|cell| guard_text(cell))
} else {
guard_text(line)
};
if admitted { Ok(()) } else { Err(refuse(line)) }
}
/// Every line of a raw report checked, then the tables aligned.
pub fn format_report(raw: &str) -> Result<String, PipelineError> {
for line in raw.split('\n') {
guard_line(line)?;
}
format_markdown(raw)
}