mirror of
https://github.com/storytold/gridcraft.git
synced 2026-11-03 08:51:30 -06:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9664c01289 |
@@ -755,10 +755,10 @@ thread_local! {
|
||||
/// While one is alive, [`read_file`] and [`write_file`] on this thread refuse every path: a
|
||||
/// stored script's steps run inside one (#2178), so no command reaches the file system from a
|
||||
/// script whatever its parameters are called. Dropping it (also on unwind) lifts the refusal.
|
||||
pub(crate) struct ScriptSandbox(());
|
||||
pub struct ScriptSandbox(());
|
||||
|
||||
impl ScriptSandbox {
|
||||
pub(crate) fn enter() -> Self {
|
||||
pub fn enter() -> Self {
|
||||
let _ = SCRIPT_SANDBOX.try_with(|c| c.set(c.get().saturating_add(1)));
|
||||
ScriptSandbox(())
|
||||
}
|
||||
|
||||
+24
-46
@@ -142,22 +142,13 @@ pub fn read(cx: &mut Ctx<'_>, workbook_rels: &[Rel]) -> Result<Vec<Option<Arc<Ce
|
||||
let Some(image_rel_list) = read_xml(cx, &image_rels_path, "richValueRels")? else { return Ok(vec![]) };
|
||||
let image_rels = cx.pkg.rels(&image_rels_path)?;
|
||||
let structures = entries(cx, &structures, "s")?;
|
||||
// Each structure's key layout, worked out once: every value would otherwise walk its
|
||||
// structure's keys again. Only local-image structures matter here.
|
||||
let layouts: Vec<Option<Layout>> = structures.iter().map(|s| Layout::of(s)).collect();
|
||||
let image_rel_list = entries(cx, &image_rel_list, "rel")?;
|
||||
// Relationships by id, a repeated id recorded as invalid (`None`), so a value's lookup is
|
||||
// one probe rather than a scan of the whole table for duplicates.
|
||||
let mut rels_by_id: HashMap<&str, Option<&Rel>> = HashMap::new();
|
||||
for rel in &image_rels {
|
||||
rels_by_id.entry(rel.id.as_str()).and_modify(|r| *r = None).or_insert(Some(rel));
|
||||
}
|
||||
let values = entries(cx, &data, "rv")?;
|
||||
let mut pictures = Vec::with_capacity(values.len());
|
||||
let mut cache = HashMap::new();
|
||||
let mut total = 0usize;
|
||||
for value in values {
|
||||
let picture = read_value(cx, value, &layouts, &image_rel_list, &rels_by_id, &mut cache, &mut total)?;
|
||||
let picture = read_value(cx, value, &structures, &image_rel_list, &image_rels, &mut cache, &mut total)?;
|
||||
if picture.is_none() {
|
||||
cx.warn("some rich values are unsupported or malformed; their scalar fallbacks were retained (cell pictures may be missing)");
|
||||
}
|
||||
@@ -197,56 +188,43 @@ pub fn read(cx: &mut Ctx<'_>, workbook_rels: &[Rel]) -> Result<Vec<Option<Arc<Ce
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Where a `_localImage` structure keeps the values a picture needs.
|
||||
struct Layout {
|
||||
keys: usize,
|
||||
/// Index of the one `_rvRel:LocalImageIdentifier` key of type `i`, if exactly one.
|
||||
image: Option<usize>,
|
||||
/// Index of the one `Text` key of type `s`, if exactly one.
|
||||
text: Option<usize>,
|
||||
}
|
||||
|
||||
impl Layout {
|
||||
fn of(structure: &El) -> Option<Layout> {
|
||||
if structure.attr("t") != Some("_localImage") {
|
||||
return None;
|
||||
}
|
||||
let keys: Vec<&El> = structure.kids("k").collect();
|
||||
let one = |name: &str, ty: &str| {
|
||||
let mut matches = keys.iter().enumerate().filter(|(_, k)| k.attr("n") == Some(name));
|
||||
let (i, key) = matches.next()?;
|
||||
(matches.next().is_none() && key.attr("t") == Some(ty)).then_some(i)
|
||||
};
|
||||
Some(Layout { keys: keys.len(), image: one("_rvRel:LocalImageIdentifier", "i"), text: one("Text", "s") })
|
||||
}
|
||||
}
|
||||
|
||||
fn read_value(
|
||||
cx: &mut Ctx<'_>,
|
||||
value: &El,
|
||||
layouts: &[Option<Layout>],
|
||||
structures: &[&El],
|
||||
image_rel_list: &[&El],
|
||||
rels_by_id: &HashMap<&str, Option<&Rel>>,
|
||||
image_rels: &[Rel],
|
||||
cache: &mut HashMap<(String, String), Arc<CellPicture>>,
|
||||
total: &mut usize,
|
||||
) -> Result<Option<Arc<CellPicture>>, IoError> {
|
||||
let Some(Some(layout)) = value.attr_u32("s").and_then(|n| layouts.get(n as usize)) else { return Ok(None) };
|
||||
let values: Vec<_> = value.kids("v").collect();
|
||||
if layout.keys != values.len() {
|
||||
let Some(structure) = value.attr_u32("s").and_then(|n| structures.get(n as usize)) else { return Ok(None) };
|
||||
if structure.attr("t") != Some("_localImage") {
|
||||
return Ok(None);
|
||||
}
|
||||
let lookup = |slot: Option<usize>| slot.and_then(|i| values.get(i)).map(|v| v.text.as_str());
|
||||
let Some(rel_id) =
|
||||
lookup(layout.image).and_then(|v| v.trim().parse::<usize>().ok()).and_then(|n| image_rel_list.get(n)).and_then(|rel| rel.attr("id"))
|
||||
let keys: Vec<_> = structure.kids("k").collect();
|
||||
let values: Vec<_> = value.kids("v").collect();
|
||||
if keys.len() != values.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let lookup = |name: &str, ty: &str| {
|
||||
let mut matches = keys.iter().zip(&values).filter(|(k, _)| k.attr("n") == Some(name));
|
||||
let (key, value) = matches.next()?;
|
||||
(matches.next().is_none() && key.attr("t") == Some(ty)).then_some(value.text.as_str())
|
||||
};
|
||||
let Some(rel_id) = lookup("_rvRel:LocalImageIdentifier", "i")
|
||||
.and_then(|v| v.trim().parse::<usize>().ok())
|
||||
.and_then(|n| image_rel_list.get(n))
|
||||
.and_then(|rel| rel.attr("id"))
|
||||
else {
|
||||
return Ok(None);
|
||||
};
|
||||
let Some(found) = rels_by_id.get(rel_id) else { return Ok(None) };
|
||||
let Some(rel) = found.filter(|rel| rel.kind == "image" && !rel.external) else {
|
||||
let mut matches = image_rels.iter().filter(|rel| rel.id == rel_id);
|
||||
let Some(rel) = matches.next() else { return Ok(None) };
|
||||
if matches.next().is_some() || rel.kind != "image" || rel.external {
|
||||
cx.warn("a cell picture has an external or invalid image relationship; kept its scalar fallback");
|
||||
return Ok(None);
|
||||
};
|
||||
let alt = lookup(layout.text).unwrap_or("").to_string();
|
||||
}
|
||||
let alt = lookup("Text", "s").unwrap_or("").to_string();
|
||||
let key = (normalize(&rel.target), alt.clone());
|
||||
if let Some(picture) = cache.get(&key) {
|
||||
return Ok(Some(picture.clone()));
|
||||
|
||||
@@ -270,27 +270,3 @@ fn picture_metadata_limits_and_relationship_read_errors_do_not_block_workbooks()
|
||||
assert!(report.warnings.iter().any(|w| w.contains("cell-picture data could not be read")), "{:?}", report.warnings);
|
||||
}
|
||||
}
|
||||
|
||||
/// Many values over one wide structure, and a repeated relationship id: the structure's layout
|
||||
/// is worked out once and relationships are indexed, so the import is linear in the records;
|
||||
/// the repeated id is invalid and keeps the scalar fallback.
|
||||
#[test]
|
||||
fn wide_structures_and_repeated_relationship_ids_are_resolved_through_indexes() {
|
||||
let mut parts = independent_parts(false);
|
||||
// 200,000 keys in one structure and 50,000 short values that name it: rebuilding the key
|
||||
// list per value would be ten billion steps.
|
||||
let padding = r#"<k n="pad" t="s"/>"#.repeat(200_000);
|
||||
replace(&mut parts, "rich/structures.xml", r#"<k n="CalcOrigin" t="i"/>"#, &format!(r#"<k n="CalcOrigin" t="i"/>{padding}"#));
|
||||
let values = r#"<rv s="1"><v/></rv>"#.repeat(50_000);
|
||||
replace(&mut parts, "rich/values.xml", r#"<rv s="1"><v>green & <image></v><v>1</v><v>5</v></rv>"#, &values);
|
||||
let (wb, report) = read_xlsx(&zip(&parts)).unwrap();
|
||||
assert!(!wb.sheet(0).unwrap().cell_pictures.contains_key(&at("A1")));
|
||||
assert!(report.warnings.iter().any(|w| w.contains("picture")), "{:?}", report.warnings);
|
||||
// The same id twice makes every reference to it invalid.
|
||||
let mut parts = independent_parts(false);
|
||||
let dup = r#"<Relationship Id="opaqueImageId" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/original.bin"/>"#;
|
||||
replace(&mut parts, "rich/_rels/images.xml.rels", dup, &dup.repeat(2));
|
||||
let (wb, report) = read_xlsx(&zip(&parts)).unwrap();
|
||||
assert!(!wb.sheet(0).unwrap().cell_pictures.contains_key(&at("A1")));
|
||||
assert!(report.warnings.iter().any(|w| w.contains("invalid image relationship")), "{:?}", report.warnings);
|
||||
}
|
||||
|
||||
+43
-9
@@ -77,6 +77,8 @@ fn probe_live_commands(calls: &[(String, String)]) -> Probe {
|
||||
let params = if params.is_empty() { serde_json::json!({}) } else { serde_json::from_str(params).unwrap_or_default() };
|
||||
let mut s = Session::new();
|
||||
s.new_workbook();
|
||||
// The JSON picks the commands and their params: none of them may touch the file system.
|
||||
let _sandbox = gridcraft_engine::io::ScriptSandbox::enter();
|
||||
let status = match s.execute(id, params.clone()) {
|
||||
Ok(_) => {
|
||||
if s.journal.iter().any(|(jid, _)| jid == id) {
|
||||
@@ -181,15 +183,16 @@ fn parse_and_probe(source: &str) -> Result<(Vec<Entry>, Probe), String> {
|
||||
}
|
||||
}
|
||||
}
|
||||
entries.push(Entry {
|
||||
seq,
|
||||
kind,
|
||||
target,
|
||||
pending,
|
||||
params,
|
||||
excel: e.get("excel").and_then(|v| v.as_str()).unwrap_or("").to_string(),
|
||||
note: e.get("note").and_then(|v| v.as_str()).unwrap_or("").to_string(),
|
||||
});
|
||||
let excel = e.get("excel").and_then(|v| v.as_str()).unwrap_or("").to_string();
|
||||
let note = e.get("note").and_then(|v| v.as_str()).unwrap_or("").to_string();
|
||||
// These are written into generated Rust: a target inside string literals and format
|
||||
// strings, the annotations after `//`. Refuse anything that could end either.
|
||||
for (field, text) in [("target", &target), ("excel", &excel), ("note", ¬e)] {
|
||||
if text.chars().any(|c| c.is_control() || matches!(c, '"' | '\\' | '{' | '}')) {
|
||||
return Err(format!("{at}: `{field}` must not hold quotes, backslashes, braces or control characters: {text:?}"));
|
||||
}
|
||||
}
|
||||
entries.push(Entry { seq, kind, target, pending, params, excel, note });
|
||||
}
|
||||
// JSON order is authoritative and curated: the first entry for a tab is its canonical
|
||||
// badge (legacy aliases come later), and groups stay together. Do not sort.
|
||||
@@ -434,3 +437,34 @@ fn reset(h: &mut Harness) {
|
||||
key(h, Key::Escape); // leave keytip mode entirely
|
||||
}
|
||||
"#;
|
||||
|
||||
#[cfg(all(test, feature = "engine"))]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Text that would end a comment or string literal in the generated Rust is refused.
|
||||
#[test]
|
||||
fn annotations_and_targets_cannot_escape_generated_code() {
|
||||
for entry in [
|
||||
r#"{"seq": "Q", "kind": "command", "target": "edit.copy", "note": "x\nfn evil() {}"}"#,
|
||||
r#"{"seq": "Q", "kind": "command", "target": "edit.copy", "excel": "a\rb"}"#,
|
||||
r#"{"seq": "Q", "kind": "dialog", "target": "x\"); evil(); (\""}"#,
|
||||
r#"{"seq": "Q", "kind": "control", "target": "{0}"}"#,
|
||||
] {
|
||||
let doc = format!(r#"{{"sequences": [{entry}]}}"#);
|
||||
assert!(parse_and_probe(&doc).is_err(), "{entry}");
|
||||
}
|
||||
assert!(parse_and_probe(r#"{"sequences": [{"seq": "Q", "kind": "control", "target": "fontSize", "note": "Font Size box"}]}"#).is_ok());
|
||||
}
|
||||
|
||||
/// A probed command may not write files, whatever its params say.
|
||||
#[test]
|
||||
fn probes_cannot_touch_the_file_system() {
|
||||
let path = std::env::temp_dir().join(format!("keytips-probe-{}.pdf", std::process::id()));
|
||||
let _ = fs::remove_file(&path);
|
||||
let calls = vec![("file.exportPdf".to_string(), serde_json::json!({ "path": path.to_string_lossy() }).to_string())];
|
||||
let probe = probe_live_commands(&calls);
|
||||
assert_eq!(probe.values().copied().collect::<Vec<_>>(), [2]);
|
||||
assert!(!path.exists());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user