From 430e2f1523ecf0562cd33a515cb8b4390095d9f6 Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 24 Nov 2026 19:58:42 +0800 Subject: [PATCH 1/4] engine: tests for a task file's requires checked on import (#2818) --- crates/engine/src/tests.rs | 3 + crates/engine/src/tests/task_files.rs | 136 ++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 crates/engine/src/tests/task_files.rs diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 30bdc16be1..67091eb998 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -63,6 +63,9 @@ mod project_crs; #[path = "tests/task_reentry.rs"] mod task_reentry; +#[path = "tests/task_files.rs"] +mod task_files; + #[path = "tests/credential_output.rs"] mod credential_output; diff --git a/crates/engine/src/tests/task_files.rs b/crates/engine/src/tests/task_files.rs new file mode 100644 index 0000000000..fc0809d302 --- /dev/null +++ b/crates/engine/src/tests/task_files.rs @@ -0,0 +1,136 @@ +//! Task files (`.rgistask`) are read with the project's header check, so an older RustGIS than the +//! file refuses what it can't keep, naming it (#2818). + +use crate::{Project, Session}; +use serde_json::{Value, json}; + +/// A task file as every release so far writes it: no `format`, no `requires`. +fn bare() -> Value { + json!({"name": "Lesson", "description": "", "steps": [{"title": "Zoom", "instructions": "", "command": "map.zoomIn", "params": {}}]}) +} + +/// `bare` with `key` set to `value`. +fn with(key: &str, value: Value) -> String { + let mut v = bare(); + v[key] = value; + v.to_string() +} + +fn import(s: &mut Session, text: &str) -> Result { + s.run("task.import", &json!({"text": text})) +} + +/// What the project's own reader says for the same header, said of a task. +fn as_a_project_says(text: &str) -> String { + crate::project::check_format(text, "project").unwrap_err().replace("this project", "this task") +} + +#[test] +fn a_task_file_needing_an_unknown_capability_is_refused_by_name() { + let mut s = Session::new(Project::default()); + let text = with("requires", json!(["label-weight", "future-step-key", "future-step-key"])); + let err = import(&mut s, &text).unwrap_err(); + assert_eq!(err, "this task needs what this RustGIS can't read: future-step-key. Update RustGIS to open it."); + assert_eq!(err, as_a_project_says(&text)); + assert!(s.project.tasks.is_empty(), "nothing imported"); + + // Read from a path and from a base64 file, as from text. + let path = std::env::temp_dir().join(format!("task-requires-2818-{}.rgistask", std::process::id())); + std::fs::write(&path, &text).unwrap(); + let from_path = s.run("task.import", &json!({"path": path.to_string_lossy()})).unwrap_err(); + std::fs::remove_file(&path).unwrap(); + assert_eq!(from_path, err); + use base64::Engine; + let data = base64::engine::general_purpose::STANDARD.encode(&text); + assert_eq!(s.run("task.import", &json!({"data": data})).unwrap_err(), err); + assert!(s.project.tasks.is_empty(), "nothing imported"); + + // Told to update, not that a step is forbidden, when the file is both. + let mut both = bare(); + both["requires"] = json!(["future-step-key"]); + both["steps"] = json!([{"title": "Add", "command": "layer.addData", "params": {"path": "/data/roads.geojson"}}]); + let err = import(&mut s, &both.to_string()).unwrap_err(); + assert!(err.contains("future-step-key") && !err.contains("reads or writes files"), "{err}"); +} + +#[test] +fn a_task_file_of_a_newer_format_is_refused() { + let mut s = Session::new(Project::default()); + let text = with("format", json!(crate::project::READ_FORMAT + 1)); + let err = import(&mut s, &text).unwrap_err(); + assert_eq!(err, "this task was saved by a newer RustGIS (format 3). Update RustGIS to open it."); + assert!(s.project.tasks.is_empty(), "nothing imported"); +} + +#[test] +fn a_task_file_with_a_hostile_requires_is_refused_as_a_project_is() { + let mut s = Session::new(Project::default()); + let many: Vec = (0..=crate::project::MAX_REQUIRES).map(|i| format!("future-{i}")).collect(); + for requires in [ + json!(5), + json!("label-weight"), + json!({"a": 1}), + json!(["label-weight", 1]), + json!([["nested"]]), + json!(many), + json!(["x".repeat(1 << 20)]), + json!(["line\nbreak\u{202e}name"]), + ] { + let text = with("requires", requires.clone()); + let err = import(&mut s, &text).unwrap_err(); + assert_eq!(err, as_a_project_says(&text), "{}", err.chars().take(120).collect::()); + assert!(err.len() < 1024, "the refusal stays short for a huge name: {} bytes", err.len()); + assert!(!err.contains('\n') && !err.contains('\u{202e}'), "{err}"); + } + assert!(s.project.tasks.is_empty(), "nothing imported"); +} + +/// Not text at all, or a list, is still `not a RustGIS task`, as before. +#[test] +fn a_task_file_that_is_no_task_is_still_said_so() { + let mut s = Session::new(Project::default()); + for text in ["[]", "5", "{}", "{\"name\": \"x\"}", "not json"] { + let err = import(&mut s, text).unwrap_err(); + assert!(err.starts_with("not a RustGIS task"), "{text}: {err}"); + } +} + +#[test] +fn task_files_without_a_header_or_with_known_capabilities_import() { + let mut s = Session::new(Project::default()); + // A file as every release so far writes it. + import(&mut s, &bare().to_string()).unwrap(); + // A header listing nothing, and one listing what this RustGIS knows. + import(&mut s, &with("requires", json!([]))).unwrap(); + import(&mut s, &with("requires", json!(["label-weight", "dggs-system", "table-export-order"]))).unwrap(); + import(&mut s, &with("format", json!(crate::project::READ_FORMAT))).unwrap(); + assert_eq!(s.project.tasks.len(), 4); + assert!(s.project.tasks.iter().all(|t| t.steps.len() == 1 && t.steps[0].command == "map.zoomIn")); +} + +/// A recorded `task.import` keeps the document's header when its text is cleaned of credentials, +/// so replaying it refuses what the first run would have (`roles::document`). +#[test] +fn a_recorded_task_import_keeps_the_check_when_its_text_is_cleaned() { + const SECRET: &str = "RGSYNTH_task_requires_20261124"; + let mut task = bare(); + task["requires"] = json!(["future-step-key"]); + task["steps"] = + json!([{"title": "Add", "command": "layer.addUrl", "params": {"url": format!("https://h.invalid/a.geojson?token={SECRET}")}}]); + let text = task.to_string(); + let (clean, report) = crate::roles::command("task.import", &json!({"text": text}), 0); + let clean = clean.expect("the credential is cleaned out of the text"); + let cleaned = clean["text"].as_str().unwrap(); + assert!(!cleaned.contains(SECRET), "{cleaned}"); + assert_eq!(report.unchecked, 0); + let kept: Value = serde_json::from_str(cleaned).unwrap(); + assert_eq!(kept["requires"], json!(["future-step-key"]), "{cleaned}"); + assert_eq!((kept["name"].as_str(), kept["steps"].as_array().map(Vec::len)), (Some("Lesson"), Some(1))); + let mut s = Session::new(Project::default()); + let err = import(&mut s, cleaned).unwrap_err(); + assert!(err.contains("future-step-key"), "{err}"); + + // A text that needs no cleaning is kept as written. + let (clean, report) = crate::roles::command("task.import", &json!({"text": bare().to_string()}), 0); + assert!(clean.is_none() && report.unchecked == 0); +} From 722dc978506a86aff531892df2fa227aab5a67de Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 24 Nov 2026 20:20:45 +0800 Subject: [PATCH 2/4] engine: a task file's requires is checked on import (#2818) --- crates/engine/src/cmd/items.rs | 4 ++++ crates/engine/src/roles.rs | 21 ++++++++++++++++++-- crates/engine/src/tests/credential_output.rs | 4 ++-- docs/maintaining.md | 3 ++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/items.rs b/crates/engine/src/cmd/items.rs index 45ce47278f..1d606fb9ce 100644 --- a/crates/engine/src/cmd/items.rs +++ b/crates/engine/src/cmd/items.rs @@ -403,6 +403,10 @@ pub fn commands() -> Vec { .params(r#"{"task"?: name, "path": "*.rgistask"}"#), C::new("task.import", "Import Task", "Analyze › Tasks", |s, p| { let text = read_text(p)?; + // A newer format, or a capability this release doesn't know, is refused by name rather + // than read without what it adds (#2818); before the steps are read, so a newer file is + // told to update RustGIS. A file without a header is a legacy task. + crate::project::check_format(&text, "task")?; let t: Task = serde_json::from_str(&text).map_err(|e| format!("not a RustGIS task: {e}"))?; // A task from a file is someone else's: it may not read or write files, or run scripts. if let Some((i, st)) = t.steps.iter().enumerate().find(|(_, st)| touches_files(s, &st.command, &st.params)) { diff --git a/crates/engine/src/roles.rs b/crates/engine/src/roles.rs index 6e68a33d81..6261890d38 100644 --- a/crates/engine/src/roles.rs +++ b/crates/engine/src/roles.rs @@ -385,7 +385,9 @@ fn document(kind: Document, v: &Value, depth: usize, report: &mut Report) -> Opt let Some(text) = v.as_str().filter(|s| s.len() as u64 <= crate::project::max_bytes()) else { return invalid(v, report) }; // One this RustGIS wouldn't read, or would rewrite without what it doesn't know, stays as // written. Only project and map readers read format 2 and refuse a document for what it - // requires; the others keep format 1's bound and don't look at `requires`. + // requires; the others keep format 1's bound. A task's and a model's readers do look at + // `requires`, so their cleaned copies keep the document's other keys (`task_document`, + // `model_document`). if crate::project::kept_as_written(text, matches!(kind, Document::Project | Document::Map)) { report.unchecked = report.unchecked.saturating_add(1); return None; @@ -399,7 +401,7 @@ fn document(kind: Document, v: &Value, depth: usize, report: &mut Report) -> Opt text_document::(text, depth) } } - Document::Task => text_document::(text, depth), + Document::Task => task_document(text, depth), Document::Presentation => text_document::(text, depth), Document::Notebook => crate::cmd::notebook::durable_document(text, depth), Document::Model => model_document(text, depth), @@ -422,6 +424,21 @@ fn text_document(text: &str, depth: u }) } +/// A task document keeps its other keys as written, `requires` among them (`task.import` refuses +/// what it lists that this RustGIS doesn't know), and cleans only its steps. +fn task_document(text: &str, depth: usize) -> Result<(String, Report), String> { + let mut v: Value = serde_json::from_str(text).map_err(|e| e.to_string())?; + let mut report = Report::default(); + let Some(obj) = v.as_object_mut() else { return Err("not a task".into()) }; + let Some(field) = obj.get("steps") else { return Err("not a task".into()) }; + if let Some(clean) = typed_value::(field, depth, &mut report) { + obj.insert("steps".into(), clean); + Ok((v.to_string(), report)) + } else { + Ok((text.into(), report)) + } +} + /// Model import has two envelopes; keeps the envelope's other fields and cleans only its model. fn model_document(text: &str, depth: usize) -> Result<(String, Report), String> { let mut v: Value = serde_json::from_str(text).map_err(|e| e.to_string())?; diff --git a/crates/engine/src/tests/credential_output.rs b/crates/engine/src/tests/credential_output.rs index 69c69bcd32..c5a9baa5e4 100644 --- a/crates/engine/src/tests/credential_output.rs +++ b/crates/engine/src/tests/credential_output.rs @@ -519,8 +519,8 @@ fn durable_future_documents_keep_their_exact_text_unchecked() { let kept = clean.map_or(text.clone(), |c| c["text"].as_str().unwrap_or_default().to_string()); assert_eq!(kept.contains(SECRET), unchecked == 1, "{kept}"); } - // A task document's reader doesn't look at `requires`, so listing one doesn't keep it unchecked; - // a future format stays as written because its typed copy could lose new fields. + // A task document's reader looks at `requires`, and its cleaned copy keeps the list (so listing one + // doesn't keep it unchecked); a future format stays as written because the reader refuses it. for (task, unchecked) in [ (json!({"name": "t", "requires": ["z-values"], "steps": []}), 0), (json!({"format": crate::project::READ_FORMAT + 1, "name": "t", "steps": []}), 1), diff --git a/docs/maintaining.md b/docs/maintaining.md index 316d103338..092ce28bd0 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -76,7 +76,8 @@ same files. New and changed code follows these rules; not every existing reader writes it. - A project, map or layer file, a model or a web tool lists in `requires` each capability it needs that an older reader would lose or misread: a reader refuses one it doesn't know, naming it. - Task, presentation and notebook readers, and models inside a task's steps, don't check + `task.import` reads a task file this way (#2818), though `task.save` doesn't list anything in one + yet. Presentation and notebook readers, and models inside a task's steps, don't check `requires` yet. A release reads `.rgis` format 2 before any writes it (`READ_FORMAT` and `CAPABILITIES` in `crates/engine/src/project.rs`), so what is added to format 2 after that comes with a capability From c143e593e3bb871180ea6c02a21a5503185577bc Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 24 Nov 2026 22:15:13 +0800 Subject: [PATCH 3/4] engine: a recorded task import keeps only its envelope beside the typed clean (#2818) --- crates/engine/src/project.rs | 6 ++-- crates/engine/src/roles.rs | 34 ++++++++++-------- crates/engine/src/tests/task_files.rs | 52 ++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 20 deletions(-) diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 90993f023d..1581aa0005 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1215,8 +1215,10 @@ fn capability_said(name: &str) -> String { /// rewrite through RustGIS's types. For a kind whose reader looks at `requires` and reads format 2 /// (`reads_format_2`: projects and maps): of a format past [`READ_FORMAT`], or requiring anything, /// as the typed copy wouldn't keep the list, even of capabilities it knows. For the others (tasks, -/// presentations, notebooks, models), whose typed copies drop what they don't know: of a format -/// past [`FORMAT`]. A list (a legacy style file), or text whose `format` isn't a number, isn't one. +/// presentations, notebooks, models): of a format past [`FORMAT`], which the reader refuses; a +/// task's and a model's cleaned copy keeps `requires` itself (`roles::task_document`, +/// `roles::model_document`). A list (a legacy style file), or text whose `format` isn't a number, +/// isn't one. pub(crate) fn kept_as_written(text: &str, reads_format_2: bool) -> bool { if text.trim_start().starts_with('[') { return false; diff --git a/crates/engine/src/roles.rs b/crates/engine/src/roles.rs index 6261890d38..14d552092a 100644 --- a/crates/engine/src/roles.rs +++ b/crates/engine/src/roles.rs @@ -385,9 +385,9 @@ fn document(kind: Document, v: &Value, depth: usize, report: &mut Report) -> Opt let Some(text) = v.as_str().filter(|s| s.len() as u64 <= crate::project::max_bytes()) else { return invalid(v, report) }; // One this RustGIS wouldn't read, or would rewrite without what it doesn't know, stays as // written. Only project and map readers read format 2 and refuse a document for what it - // requires; the others keep format 1's bound. A task's and a model's readers do look at - // `requires`, so their cleaned copies keep the document's other keys (`task_document`, - // `model_document`). + // requires; for the others it is a format past `FORMAT` (also 2 now). A task's and a model's + // readers do look at `requires`, so their cleaned copies keep it (`task_document` puts it and + // `format` back; `model_document` keeps its envelope). if crate::project::kept_as_written(text, matches!(kind, Document::Project | Document::Map)) { report.unchecked = report.unchecked.saturating_add(1); return None; @@ -424,19 +424,25 @@ fn text_document(text: &str, depth: u }) } -/// A task document keeps its other keys as written, `requires` among them (`task.import` refuses -/// what it lists that this RustGIS doesn't know), and cleans only its steps. +/// A task document is cleaned through the typed task, as it always was (a key the task doesn't +/// have is not kept), and then `format` and `requires` are put back from the document: `task.import` +/// reads them (it refuses a newer format and what `requires` lists that this RustGIS doesn't know), +/// and the typed copy has neither. fn task_document(text: &str, depth: usize) -> Result<(String, Report), String> { - let mut v: Value = serde_json::from_str(text).map_err(|e| e.to_string())?; - let mut report = Report::default(); - let Some(obj) = v.as_object_mut() else { return Err("not a task".into()) }; - let Some(field) = obj.get("steps") else { return Err("not a task".into()) }; - if let Some(clean) = typed_value::(field, depth, &mut report) { - obj.insert("steps".into(), clean); - Ok((v.to_string(), report)) - } else { - Ok((text.into(), report)) + let (clean, report) = text_document::(text, depth)?; + if clean == text { + return Ok((clean, report)); + } + let original: Value = serde_json::from_str(text).map_err(|e| e.to_string())?; + let mut rewritten: Value = serde_json::from_str(&clean).map_err(|e| e.to_string())?; + if let (Some(from), Some(to)) = (original.as_object(), rewritten.as_object_mut()) { + for key in ["format", "requires"] { + if let Some(value) = from.get(key) { + to.insert(key.into(), value.clone()); + } + } } + Ok((rewritten.to_string(), report)) } /// Model import has two envelopes; keeps the envelope's other fields and cleans only its model. diff --git a/crates/engine/src/tests/task_files.rs b/crates/engine/src/tests/task_files.rs index fc0809d302..36c90f9ebc 100644 --- a/crates/engine/src/tests/task_files.rs +++ b/crates/engine/src/tests/task_files.rs @@ -16,6 +16,15 @@ fn with(key: &str, value: Value) -> String { v.to_string() } +/// A file removed when the test ends, whether or not an assertion failed first. +struct TempFile(std::path::PathBuf); + +impl Drop for TempFile { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } +} + fn import(s: &mut Session, text: &str) -> Result { s.run("task.import", &json!({"text": text})) } @@ -35,10 +44,9 @@ fn a_task_file_needing_an_unknown_capability_is_refused_by_name() { assert!(s.project.tasks.is_empty(), "nothing imported"); // Read from a path and from a base64 file, as from text. - let path = std::env::temp_dir().join(format!("task-requires-2818-{}.rgistask", std::process::id())); - std::fs::write(&path, &text).unwrap(); - let from_path = s.run("task.import", &json!({"path": path.to_string_lossy()})).unwrap_err(); - std::fs::remove_file(&path).unwrap(); + let file = TempFile(std::env::temp_dir().join(format!("task-requires-2818-{}.rgistask", std::process::id()))); + std::fs::write(&file.0, &text).unwrap(); + let from_path = s.run("task.import", &json!({"path": file.0.to_string_lossy()})).unwrap_err(); assert_eq!(from_path, err); use base64::Engine; let data = base64::engine::general_purpose::STANDARD.encode(&text); @@ -93,6 +101,15 @@ fn a_task_file_that_is_no_task_is_still_said_so() { let err = import(&mut s, text).unwrap_err(); assert!(err.starts_with("not a RustGIS task"), "{text}: {err}"); } + // A `format` that isn't a whole number is read as the project reader reads it (it used to be + // ignored here, as a task file never had one): not a task this RustGIS can read. + for format in [json!("2"), json!(2.5), json!(-1), json!(null), json!([2])] { + let text = with("format", format.clone()); + let err = import(&mut s, &text).unwrap_err(); + assert!(err.starts_with("not a RustGIS task"), "{format}: {err}"); + assert_eq!(err, crate::project::check_format(&text, "task").unwrap_err()); + } + assert!(s.project.tasks.is_empty(), "nothing imported"); } #[test] @@ -134,3 +151,30 @@ fn a_recorded_task_import_keeps_the_check_when_its_text_is_cleaned() { let (clean, report) = crate::roles::command("task.import", &json!({"text": bare().to_string()}), 0); assert!(clean.is_none() && report.unchecked == 0); } + +/// A recorded `task.import` is cleaned through the typed task as it always was: a key the task +/// doesn't have, with a credential in it, isn't kept beside the cleaned steps. Only `format` and +/// `requires` are put back from the document. +#[test] +fn a_recorded_task_import_keeps_only_its_envelope_beside_the_typed_clean() { + const SECRET: &str = "RGSYNTH_stray_key_20261124"; + let mut task = bare(); + task["requires"] = json!(["label-weight"]); + task["format"] = json!(crate::project::READ_FORMAT); + task["token"] = json!(SECRET); + task["extra"] = json!({"password": SECRET}); + // A credential in a step makes the document change, so it is written again from the typed task. + task["steps"] = + json!([{"title": "Add", "command": "layer.addUrl", "params": {"url": format!("https://h.invalid/a.geojson?token={SECRET}")}}]); + let (clean, report) = crate::roles::command("task.import", &json!({"text": task.to_string()}), 0); + let cleaned = clean.expect("the credential is cleaned out of the text"); + let cleaned = cleaned["text"].as_str().unwrap(); + assert!(!cleaned.contains(SECRET), "{cleaned}"); + assert_eq!(report.unchecked, 0); + let kept: Value = serde_json::from_str(cleaned).unwrap(); + let mut keys: Vec<&String> = kept.as_object().unwrap().keys().collect(); + keys.sort(); + assert_eq!(keys, ["description", "format", "name", "requires", "steps"], "{cleaned}"); + assert_eq!(kept["requires"], json!(["label-weight"])); + assert_eq!(kept["format"], json!(crate::project::READ_FORMAT)); +} From c39093672df122c1a6753e9e534ba9432a15f90b Mon Sep 17 00:00:00 2001 From: choupeanut Date: Tue, 24 Nov 2026 23:53:32 +0800 Subject: [PATCH 4/4] engine: a recorded task import keeps its requires only when it carries no credential (#2818) --- crates/engine/src/roles.rs | 13 ++++++-- crates/engine/src/tests/task_files.rs | 45 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/crates/engine/src/roles.rs b/crates/engine/src/roles.rs index 14d552092a..d8d1798f41 100644 --- a/crates/engine/src/roles.rs +++ b/crates/engine/src/roles.rs @@ -427,16 +427,23 @@ fn text_document(text: &str, depth: u /// A task document is cleaned through the typed task, as it always was (a key the task doesn't /// have is not kept), and then `format` and `requires` are put back from the document: `task.import` /// reads them (it refuses a newer format and what `requires` lists that this RustGIS doesn't know), -/// and the typed copy has neither. +/// and the typed copy has neither. Neither is a place for a credential, so a document whose header +/// holds one is withheld whole (`[parameters withheld]`, which no replay reads as a task), as a +/// document that doesn't read is; dropping only the header would let a replay skip the check. fn task_document(text: &str, depth: usize) -> Result<(String, Report), String> { + let original: Value = serde_json::from_str(text).map_err(|e| e.to_string())?; + let header = ["format", "requires"]; + if header.iter().filter_map(|key| original.get(key)).any(suspicious) { + let report = Report { withheld: 1, ..Report::default() }; + return Ok((WITHHELD.into(), report)); + } let (clean, report) = text_document::(text, depth)?; if clean == text { return Ok((clean, report)); } - let original: Value = serde_json::from_str(text).map_err(|e| e.to_string())?; let mut rewritten: Value = serde_json::from_str(&clean).map_err(|e| e.to_string())?; if let (Some(from), Some(to)) = (original.as_object(), rewritten.as_object_mut()) { - for key in ["format", "requires"] { + for key in header { if let Some(value) = from.get(key) { to.insert(key.into(), value.clone()); } diff --git a/crates/engine/src/tests/task_files.rs b/crates/engine/src/tests/task_files.rs index 36c90f9ebc..f6033895f4 100644 --- a/crates/engine/src/tests/task_files.rs +++ b/crates/engine/src/tests/task_files.rs @@ -178,3 +178,48 @@ fn a_recorded_task_import_keeps_only_its_envelope_beside_the_typed_clean() { assert_eq!(kept["requires"], json!(["label-weight"])); assert_eq!(kept["format"], json!(crate::project::READ_FORMAT)); } + +/// What a recorded `task.import` keeps of `requires` and `format` is only what carries no credential: +/// one that does is withheld with the whole text, which replays as no task, so a replay can't skip +/// the capability check by losing the list. A clean list is still kept, rewritten or not. +#[test] +fn a_recorded_task_import_keeps_its_requires_only_when_it_carries_no_credential() { + const SECRET: &str = "RGSYNTH_requires_20261124"; + let secret_step = + json!([{"title": "Add", "command": "layer.addUrl", "params": {"url": format!("https://h.invalid/a.geojson?token={SECRET}")}}]); + let clean_step = bare()["steps"].clone(); + for (key, value) in [ + ("requires", json!(["future-step-key", format!("https://h.invalid/x?token={SECRET}")])), + ("requires", json!({"token": SECRET})), + ("requires", json!([{"headers": {"Authorization": format!("Bearer {SECRET}")}}])), + ("format", json!({"token": SECRET})), + ] { + // Rewritten (a credential in a step) and kept as written (none). + for steps in [&secret_step, &clean_step] { + let mut task = bare(); + task[key] = value.clone(); + task["steps"] = steps.clone(); + let (clean, report) = crate::roles::command("task.import", &json!({"text": task.to_string()}), 0); + let params = clean.unwrap_or_else(|| panic!("{key} {value}: kept as written, credential and all")); + let cleaned = params["text"].as_str().unwrap(); + assert!(!cleaned.contains(SECRET), "{key} {value}: {cleaned}"); + assert_eq!(report.withheld, 1, "{key} {value}"); + // The replay is refused: nothing is imported without the check. + let mut s = Session::new(Project::default()); + assert!(import(&mut s, cleaned).is_err(), "{key} {value}: {cleaned}"); + assert!(s.project.tasks.is_empty()); + } + } + // A clean list is kept, so the replay is checked. + for steps in [&secret_step, &clean_step] { + let mut task = bare(); + task["requires"] = json!(["future-step-key"]); + task["steps"] = steps.clone(); + let (clean, report) = crate::roles::command("task.import", &json!({"text": task.to_string()}), 0); + let kept = clean.map_or(task.to_string(), |p| p["text"].as_str().unwrap().to_string()); + assert_eq!(serde_json::from_str::(&kept).unwrap()["requires"], json!(["future-step-key"]), "{kept}"); + assert_eq!(report.withheld, 0); + let mut s = Session::new(Project::default()); + assert!(import(&mut s, &kept).unwrap_err().contains("future-step-key")); + } +}