fix(anatomy): R5.4 field segments and code spans on the generator alphabet, wiki and liquid brackets refused in list items, linear guard
This commit is contained in:
1 parent
1b12d90325
commit
aeb5050502
10 files changed
+325
-48
No files matched your search
@@ -101,13 +101,16 @@ pub fn split_field(field: &str) -> Result<Vec<&str>, PipelineError> {
|
||||
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()) || key.contains('`'))
|
||||
{
|
||||
// A segment reaches the reports' code spans through the record: the alphabet of every
|
||||
// settable path (`name.fr`, `view_box.min_x`, `count_range.min`), nothing a Markdown parser
|
||||
// could pair with the text around the span (a `]` closed an author's `[` there).
|
||||
let id_alphabet = |key: &str| {
|
||||
key.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || "_-".contains(c))
|
||||
};
|
||||
if !path.iter().all(|key| id_alphabet(key)) {
|
||||
return Err(fail(format!(
|
||||
"field {field:?} holds a control character or a backtick"
|
||||
"field {field:?} holds a segment outside [A-Za-z0-9_-]"
|
||||
)));
|
||||
}
|
||||
// A JavaScript assignment through `__proto__` never wrote a key: it set a prototype,
|
||||
|
||||
@@ -357,9 +357,15 @@ fn intro() -> String {
|
||||
|
||||
/// A control character (a line break, a tab, a carriage return…) in an authored text the
|
||||
/// report writes in a list item would break the item, and Prettier re-indents what follows:
|
||||
/// refused (the TypeScript wrote it raw).
|
||||
/// refused (the TypeScript wrote it raw). A list-item field shares its line with the other fields of the change and the generated
|
||||
/// code spans: no control character, and no wiki-link or liquid bracket (`[[`, `]]`, `{%`, `%}`),
|
||||
/// which Prettier pairs across the whole line (a `[[` in a rationale closed by the question's
|
||||
/// `]]`, the code spans between them notwithstanding).
|
||||
fn assert_single_line(text: &str) -> Result<(), PipelineError> {
|
||||
if text.chars().any(char::is_control) {
|
||||
let across = ["[[", "]]", "{%", "%}"]
|
||||
.iter()
|
||||
.any(|bracket| text.contains(bracket));
|
||||
if across || text.chars().any(char::is_control) {
|
||||
return Err(PipelineError::UnsupportedMarkdown(text.to_owned()));
|
||||
}
|
||||
assert_authored(text)
|
||||
|
||||
@@ -51,12 +51,16 @@ const LEGACY_ENTITIES: [&str; 106] = [
|
||||
/// HTML tag or an autolink (`<` before a letter, `/`, `!` or `?`), a character reference
|
||||
/// (`&name;`, `&#n;`); a bare `&` is ruled in `admitted_char`.
|
||||
fn rewritten_construct(plain: &str) -> bool {
|
||||
// A `[[` padded inside with a `]]` after it, or a `]]` padded inside with a `[[` before it.
|
||||
// A `[[` padded inside with a `]]` after it, or a `]]` padded inside with a `[[` before it
|
||||
// (the last `]]` and the first `[[` found once: linear).
|
||||
let last_close = plain.rfind("]]");
|
||||
let first_open = plain.find("[[");
|
||||
let padded_wiki = plain.match_indices("[[").any(|(at, _)| {
|
||||
plain[at + 2..].starts_with(char::is_whitespace) && plain[at + 2..].contains("]]")
|
||||
}) || plain
|
||||
.match_indices("]]")
|
||||
.any(|(at, _)| plain[..at].ends_with(char::is_whitespace) && plain[..at].contains("[["));
|
||||
plain[at + 2..].starts_with(char::is_whitespace) && last_close.is_some_and(|c| c > at + 1)
|
||||
}) || plain.match_indices("]]").any(|(at, _)| {
|
||||
plain[..at].ends_with(char::is_whitespace) && first_open.is_some_and(|o| o + 1 < at)
|
||||
});
|
||||
let liquid = plain.contains("{%") || plain.contains("%}");
|
||||
let tag = plain
|
||||
.split('<')
|
||||
.skip(1)
|
||||
@@ -75,6 +79,7 @@ fn rewritten_construct(plain: &str) -> bool {
|
||||
|| plain.contains("][")
|
||||
|| plain.contains("![")
|
||||
|| padded_wiki
|
||||
|| liquid
|
||||
|| tag
|
||||
|| reference
|
||||
}
|
||||
@@ -111,22 +116,29 @@ fn starts_with(chars: &[char], from: usize, text: &str) -> bool {
|
||||
.all(|(index, c)| chars.get(index) == Some(&c))
|
||||
}
|
||||
|
||||
/// Whether position `i` lies in a bare URL token: the run of non-space characters around it
|
||||
/// starts with a URL literal.
|
||||
fn in_url_token(chars: &[char], i: usize) -> bool {
|
||||
let start = chars[..i]
|
||||
.iter()
|
||||
.rposition(|c| c.is_whitespace())
|
||||
.map_or(0, |p| p + 1);
|
||||
(start..i).any(|k| URL_STARTS.iter().any(|url| starts_with(chars, k, url)))
|
||||
/// For each position, whether it lies in a bare URL token (a run of non-space characters
|
||||
/// holding a URL literal before it), computed in one pass.
|
||||
fn url_tokens(chars: &[char]) -> Vec<bool> {
|
||||
let mut inside = Vec::with_capacity(chars.len());
|
||||
let mut seen = false;
|
||||
for (k, c) in chars.iter().enumerate() {
|
||||
if c.is_whitespace() {
|
||||
seen = false;
|
||||
}
|
||||
inside.push(seen);
|
||||
if URL_STARTS.iter().any(|url| starts_with(chars, k, url)) {
|
||||
seen = true;
|
||||
}
|
||||
}
|
||||
inside
|
||||
}
|
||||
|
||||
/// An `&` Prettier writes as is: before a space, or a URL's query separator (`&name=`, the name
|
||||
/// not starting with a legacy reference). Anything else may start a character reference.
|
||||
fn admitted_ampersand(chars: &[char], i: usize) -> bool {
|
||||
fn admitted_ampersand(chars: &[char], urls: &[bool], i: usize) -> bool {
|
||||
match chars.get(i + 1) {
|
||||
Some(' ') => true,
|
||||
Some(c) if c.is_ascii_lowercase() && in_url_token(chars, i) => {
|
||||
Some(c) if c.is_ascii_lowercase() && urls[i] => {
|
||||
let name: String = chars[i + 1..]
|
||||
.iter()
|
||||
.take_while(|c| c.is_ascii_alphanumeric())
|
||||
@@ -140,13 +152,13 @@ fn admitted_ampersand(chars: &[char], i: usize) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn admitted_char(chars: &[char], i: usize) -> bool {
|
||||
fn admitted_char(chars: &[char], urls: &[bool], 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(' '),
|
||||
'&' => admitted_ampersand(chars, i),
|
||||
'&' => admitted_ampersand(chars, urls, i),
|
||||
'_' => {
|
||||
previous.is_some_and(|c| c.is_ascii_alphanumeric())
|
||||
&& at(i + 1).is_some_and(|c| c.is_ascii_alphanumeric())
|
||||
@@ -165,7 +177,19 @@ fn admitted_plain(plain: &str) -> bool {
|
||||
return false;
|
||||
};
|
||||
let chars: Vec<char> = plain.chars().collect();
|
||||
(0..chars.len()).all(|i| admitted_char(&chars, i))
|
||||
let urls = url_tokens(&chars);
|
||||
(0..chars.len()).all(|i| admitted_char(&chars, &urls, i))
|
||||
}
|
||||
|
||||
/// A code span as the generator writes one: ids, targets, field names, file paths and the
|
||||
/// template's `pnpm import:equine` (the alphabet `[A-Za-z0-9_:/. -]`), never empty, no outer space.
|
||||
fn generated_code(code: &str) -> bool {
|
||||
!code.is_empty()
|
||||
&& !code.starts_with(' ')
|
||||
&& !code.ends_with(' ')
|
||||
&& code
|
||||
.chars()
|
||||
.all(|c| c.is_ascii_alphanumeric() || "_:/. -".contains(c))
|
||||
}
|
||||
|
||||
/// A text outside table delimiters: its code spans and its plain parts.
|
||||
@@ -176,7 +200,7 @@ fn guard_text(text: &str) -> bool {
|
||||
}
|
||||
let admitted = parts.iter().enumerate().all(|(index, part)| {
|
||||
if index % 2 == 1 {
|
||||
!part.is_empty() && !part.starts_with(' ') && !part.ends_with(' ')
|
||||
generated_code(part)
|
||||
} else {
|
||||
admitted_plain(part)
|
||||
}
|
||||
|
||||
@@ -72,12 +72,13 @@ const EQUINE_SAFE: &str = "[]&#{}=<>\"':;()/%?-.,«»…≠°—+";
|
||||
|
||||
fn equine_safe(probe: &str) -> bool {
|
||||
// A leading or trailing space becomes two spaces once the probe sits in its sentence; the
|
||||
// inline constructs (links, wiki links, autolinks, character references) are probed apart.
|
||||
// inline constructs (links, wiki links, autolinks, character references) are probed apart;
|
||||
// the list-item fields refuse the wiki and liquid brackets whole (none in the equine ones).
|
||||
let tag = probe.match_indices('<').any(|(at, _)| {
|
||||
probe[at + 1..].starts_with(|c: char| c.is_ascii_alphabetic() || "/!?".contains(c))
|
||||
});
|
||||
let construct = tag
|
||||
|| ["](", "][", "![", "[[ ", " ]]", "&", "`"]
|
||||
|| ["](", "][", "![", "[[", "]]", "{%", "%}", "&", "`"]
|
||||
.iter()
|
||||
.any(|m| probe.contains(m));
|
||||
!construct
|
||||
@@ -145,6 +146,6 @@ fn every_probe_is_formatted_as_the_typescript_or_refused() {
|
||||
"silent differences for {} probes: {silent:?}",
|
||||
silent.len()
|
||||
);
|
||||
assert_eq!(same + refused, 790);
|
||||
assert_eq!(same + refused, 798);
|
||||
println!("{same} identical, {refused} refused");
|
||||
}
|
||||
@@ -467,6 +467,23 @@ fn ids_and_field_segments_that_could_forge_report_structure_are_refused() {
|
||||
apply_changeset(&frozen["baseline"], &references, &changeset.changes).unwrap_err();
|
||||
assert!(error.to_string().starts_with("CHG-001 op 0:"), "{error}");
|
||||
}
|
||||
// A segment outside the paths' alphabet: a `]` in a code span closed an author's `[`
|
||||
// (security gate R5.4 round 5).
|
||||
for field in ["name.x]", "name.a b", "name.x*", "name.é"] {
|
||||
let file = serde_json::json!({ "changes": [{
|
||||
"id": "CHG-001", "level": "A", "kind": "technical",
|
||||
"title": { "fr": "Essai [", "en": "Test" }, "rationale": "Essai", "reference_ids": [],
|
||||
"ops": [{ "op": "set", "target": "finding:hip/hip-dysfunctions/mobility",
|
||||
"field": field, "value": "a" }],
|
||||
}]});
|
||||
let changeset = parse_changeset(&to_js_compact(&file).unwrap()).unwrap();
|
||||
let error =
|
||||
apply_changeset(&frozen["baseline"], &references, &changeset.changes).unwrap_err();
|
||||
assert!(
|
||||
error.to_string().contains("segment outside [A-Za-z0-9_-]"),
|
||||
"{field}: {error}"
|
||||
);
|
||||
}
|
||||
let deep = format!(
|
||||
"<svg xmlns=\"http://www.w3.org/2000/svg\">{}{}</svg>",
|
||||
"<g>".repeat(100_000),
|
||||
|
||||
@@ -1 +1 @@
|
||||
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}],"legacy_id_map":{"views":{"head":"head"},"annotations":{"legacy-hip":"hip","legacy-neck":"neck"},"categories":{},"finding_nodes":{},"assets":{}},"cases":[{"probe":"*","reference_id":"probe_ref","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *","en":"Title"},"rationale":"Raison * fin.","reference_ids":[],"question":"Question * ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe_ref","type":"web","citation":"Citation *","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe_ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*"}]}]},"changelog_raw_sha256":"77926ee5c69c45be2f9576e04a7e3430b45c203bc667c5f1b51ac3a21e7ae4d9","changelog_sha256":"a84644e09477bf50e413ebe1e8240ccc04657488381b093755d349a35e494843","review_raw_sha256":"a3fc194f01e61c8ae010271894c962b40b8ef4b23987adb9d7712190839734df","review_sha256":"6b2208027f21d2b663a202a8d39accb640f9cae26bbceaa701b2754bdd6f8342"},{"probe":"*x","reference_id":"probe_ref","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *x","en":"Title"},"rationale":"Raison *x fin.","reference_ids":[],"question":"Question *x ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe_ref","type":"web","citation":"Citation *x","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe_ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *x"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*x"}]}]},"changelog_raw_sha256":"a29af5d1b46d52b0de4a334e31e16aa4765c976865fee8ed0b2e00b7f60af4f4","changelog_sha256":"b150e9617d14894c9c33a2c95feab51b4b59245b74a78d48015569ffc300dec3","review_raw_sha256":"0e6605a31ba79a30d66e8ab8fe3858b4c305a816ea7b1e130201e8300611e218","review_sha256":"8693621d140233c9c7708c943621e61586289d0f8a72b93ca2e071eb58a5d45f"},{"probe":"x*","reference_id":"probe_ref","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre x*","en":"Title"},"rationale":"Raison x* fin.","reference_ids":[],"question":"Question x* ?","ops":[{"op":"add","target":"document","field":"references","valueLine truncated
|
||||
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}],"legacy_id_map":{"views":{"head":"head"},"annotations":{"legacy-hip":"hip","legacy-neck":"neck"},"categories":{},"finding_nodes":{},"assets":{}},"cases":[{"probe":"*","reference_id":"probe_ref","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *","en":"Title"},"rationale":"Raison * fin.","reference_ids":[],"question":"Question * ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe_ref","type":"web","citation":"Citation *","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe_ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*"}]}]},"changelog_raw_sha256":"77926ee5c69c45be2f9576e04a7e3430b45c203bc667c5f1b51ac3a21e7ae4d9","changelog_sha256":"a84644e09477bf50e413ebe1e8240ccc04657488381b093755d349a35e494843","review_raw_sha256":"a3fc194f01e61c8ae010271894c962b40b8ef4b23987adb9d7712190839734df","review_sha256":"6b2208027f21d2b663a202a8d39accb640f9cae26bbceaa701b2754bdd6f8342"},{"probe":"*x","reference_id":"probe_ref","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *x","en":"Title"},"rationale":"Raison *x fin.","reference_ids":[],"question":"Question *x ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe_ref","type":"web","citation":"Citation *x","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe_ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *x"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*x"}]}]},"changelog_raw_sha256":"a29af5d1b46d52b0de4a334e31e16aa4765c976865fee8ed0b2e00b7f60af4f4","changelog_sha256":"b150e9617d14894c9c33a2c95feab51b4b59245b74a78d48015569ffc300dec3","review_raw_sha256":"0e6605a31ba79a30d66e8ab8fe3858b4c305a816ea7b1e130201e8300611e218","review_sha256":"8693621d140233c9c7708c943621e61586289d0f8a72b93ca2e071eb58a5d45f"},{"probe":"x*","reference_id":"probe_ref","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre x*","en":"Title"},"rationale":"Raison x* fin.","reference_ids":[],"question":"Question x* ?","ops":[{"op":"add","target":"document","field":"references","valueLine truncated
|
||||
Reference in new issue
Block a user