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:
cflmy committed 2026-11-27 08:47:56 +08:00
1 parent 9684e388ce
commit 495d2e4bf7
6 files changed
+252 -4

No files matched your search

+69
View File
@@ -84,6 +84,75 @@ fn text_at(module: &Module, span: (usize, usize)) -> String {
.to_string()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContentBlock {
pub kind: String,
pub text: String,
pub span: (usize, usize),
pub links: Vec<ContentLink>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ContentLink {
pub text: String,
pub url: String,
pub span: (usize, usize),
}
/// Safe projection of the analysis AST inside one source span.
///
/// Only structural kinds are exposed; raw HTML becomes an inert placeholder and
/// link targets remain data for the Preview's protocol allowlist. No Markdown is
/// re-parsed: block text is read from the analysis-provided source by span.
pub fn content_blocks(module: &Module, span: (usize, usize)) -> Vec<ContentBlock> {
let mut blocks = Vec::new();
let Some(children) = module.document["children"].as_array() else {
return blocks;
};
for child in children {
let Some((start, end)) = span_from(&child["span"]) else {
continue;
};
if start < span.0 || end > span.1 {
continue;
}
let kind = match child["kind"].as_str().unwrap_or("") {
"paragraph" => "paragraph",
"list" => "list",
"table" => "table",
"code_block" => "code",
"html" | "html_block" => "placeholder",
"heading" => "heading",
"block_quote" => "quote",
other => other,
};
let mut links = Vec::new();
walk_links(child, &mut links);
let links = links
.into_iter()
.filter_map(|link| {
let span = span_from(&link["span"])?;
Some(ContentLink {
text: text_at(module, span),
url: link["url"].as_str()?.to_string(),
span,
})
})
.collect();
let mut text = text_at(module, (start, end));
if kind == "placeholder" {
text = format!("[raw HTML as inert text] {text}");
}
blocks.push(ContentBlock {
kind: kind.to_string(),
text,
span: (start, end),
links,
});
}
blocks
}
/// Resolve one exact anchor to its unit without imposing the design-component kind.
pub fn any_unit_by_anchor(module: &Module, anchor: &str) -> Result<Unit, Diagnostic> {
let mut matches = Vec::new();
+47
View File
@@ -22,6 +22,7 @@ use diagnostic::{Diagnostic, LoadError, Span};
use document::{Cell, Link, Unit};
pub use diagnostic::{LoadError as Error, Severity};
pub use document::{ContentBlock, ContentLink};
/// Explicit inputs; there is no implicit CLI or project discovery.
#[derive(Debug, Clone)]
@@ -126,6 +127,8 @@ pub struct Scenario {
pub state: String,
/// Content unit (Knowledge H2/H3), never a design-component declaration.
pub content: SourceRef,
/// Safe structural projection of the content unit.
pub content_blocks: Vec<ContentBlock>,
pub initial: bool,
}
@@ -156,6 +159,8 @@ pub struct ComponentDef {
pub key: String,
pub source: SourceRef,
pub slots: Vec<ChildSlot>,
/// Safe structural projection of the direct `内容` section, if present.
pub content: Vec<ContentBlock>,
pub inputs: Vec<InputDecl>,
pub events: Vec<EventDecl>,
pub scenarios: Vec<Scenario>,
@@ -763,9 +768,16 @@ impl Loader {
initial += 1;
}
let content = source_ref(&target_module, &target_unit, target_unit.anchor_text());
let content_start = target_unit
.heading_span
.map(|(_, end)| end)
.unwrap_or(target_unit.span.0);
let content_blocks =
document::content_blocks(&target_module, (content_start, target_unit.span.1));
scenarios.push(Scenario {
state,
content,
content_blocks,
initial: is_initial,
});
}
@@ -920,6 +932,19 @@ impl Loader {
span: json_span(section.span),
});
}
let content = match document::direct_sections(module, unit)
.into_iter()
.find(|section| section.name == "内容")
{
Some(section) => {
let start = section
.heading_span
.map(|(_, end)| end)
.unwrap_or(section.span.0);
document::content_blocks(module, (start, section.span.1))
}
None => Vec::new(),
};
self.stack.pop();
if self.definitions.len() >= self.config.max_definitions {
return Err(design_error(
@@ -931,6 +956,7 @@ impl Loader {
key: key.clone(),
source: source_ref(module, unit, anchor),
slots,
content,
inputs,
events,
scenarios,
@@ -943,6 +969,26 @@ impl Loader {
}
}
impl Loader {
fn verify_stable(&mut self) -> Result<(), LoadError> {
let modules: Vec<Module> = self.modules.values().cloned().collect();
for module in modules {
let fresh = analysis::analyze(&self.config.cli, &module.path, &self.root)?;
if fresh.source_digest != module.source_digest {
return Err(LoadError::Engine(vec![Diagnostic::error(
"web.engine.stale",
format!(
"source {} changed during one design load ({} -> {})",
module.relative, module.source_digest, fresh.source_digest
),
)
.at(module.relative.clone(), None)]));
}
}
Ok(())
}
}
fn valid_state(state: &str) -> bool {
let mut chars = state.chars();
let Some(first) = chars.next() else {
@@ -1084,6 +1130,7 @@ pub fn load(config: &LoadConfig) -> Result<Design, LoadError> {
let unit = document::knowledge_h2(&module, &config.unit)?;
let root_key = loader.load_definition(&module, &unit, &config.unit, 0)?;
validate_design(&loader.definitions)?;
loader.verify_stable()?;
let mut instances = Vec::new();
collect_instances(&loader.definitions, &root_key, "root", &mut instances);
if instances.len() > config.max_instances {