feat: project safe content, verify load stability and cover scope escapes
- Expose ContentBlock/ContentLink projected from the analysis AST and source byte spans; raw HTML becomes an inert placeholder, links keep url/span for the Preview protocol allowlist. Components and scenario content units use it. - After building the graph, re-analyze every participating module and require the same source_digest; a mid-load edit returns Engine web.engine.stale and no partial model. - Security cases: symlink escape outside --root is rejected, absolute link targets are rejected, percent-encoded traversal stays unresolved, raw HTML is inert. - Tests grow to 18 real-CLI cases (content projection, html placeholder, symlink/absolute/encoded scope escapes, forged CLI output stale detection). - Evidence/docs record what remains: WEB-03 graph/slicing, component CI service, WEB-02 output budget/CSP, WEB-05 root integration. Refs cflmy/marqdo-web-design#1, cflmy/marqdo#6
This commit was merged in pull request #5.
This commit is contained in:
1 parent
9684e388ce
commit
495d2e4bf7
6 files changed
+252
-4
No files matched your search
@@ -522,3 +522,128 @@ fn enforces_per_file_and_slot_limits() {
|
||||
"web.layout.slots_per_row"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn projects_safe_content_blocks_for_components_and_scenarios() {
|
||||
let project = TempProject::new();
|
||||
project.write(
|
||||
"index.mq.md",
|
||||
"# 页面\n\n## @knowledge 工作台\n\n### 内容\n\n普通段落。\n\n- 一\n- 二\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [A](a.mq.md#甲) | auto |\n",
|
||||
);
|
||||
project.write(
|
||||
"a.mq.md",
|
||||
"# A 组件\n\n## @knowledge 甲\n\n### 内容\n\n这里是**正文**。\n\n### 场景\n\n| 状态 | 内容 | 初始 |\n| --- | --- | --- |\n| only | [样例](states.mq.md#空状态) | true |\n",
|
||||
);
|
||||
project.write("states.mq.md", STATES);
|
||||
let design = load(&project.config("index.mq.md", "工作台")).expect("content load");
|
||||
let root = &design.definitions["index.mq.md#工作台"];
|
||||
let kinds: Vec<&str> = root
|
||||
.content
|
||||
.iter()
|
||||
.map(|block| block.kind.as_str())
|
||||
.collect();
|
||||
assert!(kinds.contains(&"paragraph"));
|
||||
assert!(kinds.contains(&"list"));
|
||||
let a = &design.definitions["a.mq.md#甲"];
|
||||
assert!(a
|
||||
.scenarios
|
||||
.iter()
|
||||
.all(|scene| !scene.content_blocks.is_empty()));
|
||||
let text = handoff(&design);
|
||||
assert!(text.contains("scenario `only`"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn raw_html_is_exposed_as_an_inert_placeholder() {
|
||||
let project = TempProject::new();
|
||||
project.write(
|
||||
"index.mq.md",
|
||||
"# 页面\n\n## @knowledge 工作台\n\n### 内容\n\n<div onclick=\"alert(1)\">危险</div>\n",
|
||||
);
|
||||
let design = load(&project.config("index.mq.md", "工作台")).expect("html load");
|
||||
let block = design.definitions["index.mq.md#工作台"]
|
||||
.content
|
||||
.iter()
|
||||
.find(|block| block.kind == "placeholder")
|
||||
.expect("raw html block");
|
||||
assert!(block.text.contains("inert text"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_scope_escape_via_symlink_absolute_and_encoded_paths() {
|
||||
let project = TempProject::new();
|
||||
let outside_dir = project
|
||||
.path
|
||||
.parent()
|
||||
.unwrap()
|
||||
.join(format!("marqdo-web-design-outside-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&outside_dir).unwrap();
|
||||
let outside = outside_dir.join("outside.mq.md");
|
||||
std::fs::write(&outside, "# 外部\n\n## @knowledge 目标\n\n### 内容\n\nx\n").unwrap();
|
||||
project.write(
|
||||
"index.mq.md",
|
||||
"# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [外部](escape.mq.md#目标) | auto |\n",
|
||||
);
|
||||
match std::os::unix::fs::symlink(&outside, project.path.join("escape.mq.md")) {
|
||||
Ok(()) => {
|
||||
let error = load(&project.config("index.mq.md", "工作台")).unwrap_err();
|
||||
let code = match &error {
|
||||
Error::Design(items) | Error::Engine(items) => items[0].code,
|
||||
};
|
||||
assert_eq!(code, "web.link.outside_root", "{error:?}");
|
||||
}
|
||||
Err(error) => eprintln!("symlink unavailable, skipped: {error}"),
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(&outside_dir);
|
||||
|
||||
let absolute = TempProject::new();
|
||||
absolute.write(
|
||||
"index.mq.md",
|
||||
"# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [绝对](/tmp/x.mq.md#目标) | auto |\n",
|
||||
);
|
||||
let error = load(&absolute.config("index.mq.md", "工作台")).unwrap_err();
|
||||
let code = match &error {
|
||||
Error::Design(items) | Error::Engine(items) => items[0].code,
|
||||
};
|
||||
assert!(
|
||||
matches!(code, "web.link.candidate" | "web.link.absolute"),
|
||||
"{error:?}"
|
||||
);
|
||||
|
||||
let encoded = TempProject::new();
|
||||
encoded.write(
|
||||
"index.mq.md",
|
||||
"# 页面\n\n## @knowledge 工作台\n\n### 组件\n\n| 行 | 槽 | 组件 | 宽度 |\n| --- | --- | --- | --- |\n| 1 | a | [穿越](%2e%2e/outside.mq.md#目标) | auto |\n",
|
||||
);
|
||||
let error = load(&encoded.config("index.mq.md", "工作台")).unwrap_err();
|
||||
let code = match &error {
|
||||
Error::Design(items) | Error::Engine(items) => items[0].code,
|
||||
};
|
||||
assert!(
|
||||
matches!(code, "web.link.candidate" | "web.link.target_missing"),
|
||||
"{error:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_a_changed_source_digest_during_one_load() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let project = basic_project();
|
||||
let real_cli = cli();
|
||||
let wrapper = project.path.join("fake-cli.sh");
|
||||
let counter = project.path.join("calls.txt");
|
||||
std::fs::write(
|
||||
&wrapper,
|
||||
format!(
|
||||
"#!/bin/sh\ncount=0\n[ -f '{count}' ] && count=$(cat '{count}')\ncount=$((count+1))\necho \"$count\" > '{count}'\n\"{cli}\" \"$@\"\nstatus=$?\nif [ \"$count\" -ge 2 ] && [ \"$status\" -eq 0 ]; then\n emit=\"\"\n while [ \"$#\" -gt 0 ]; do\n if [ \"$1\" = \"--emit\" ]; then shift; emit=\"$1\"; fi\n shift\n done\n python3 - \"$emit\" <<'PY'\nimport json, sys\npath = sys.argv[1]\ndata = json.load(open(path))\ndata['modules'][0]['source_digest'] = '0' * 64\njson.dump(data, open(path, 'w'))\nPY\nfi\nexit \"$status\"\n",
|
||||
cli = real_cli.display(),
|
||||
count = counter.display(),
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&wrapper, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let mut config = project.config("index.mq.md", "工作台");
|
||||
config.cli = wrapper;
|
||||
let error = load(&config).unwrap_err();
|
||||
assert_eq!(engine_code(error), "web.engine.stale");
|
||||
}
|
||||
Reference in new issue
Block a user