From 72c4875990a8b01d04d147c7fccc2a7a95a5f4ca Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 22 Nov 2026 10:44:46 -0800 Subject: [PATCH] Scripts: a script cannot change the state an XMPFile keeps An XMPFile kept its path, open flags and pending packet in writable, configurable properties, so a script could assign a packet to a file opened for reading and closeFile wrote it without checking the update flag or reading the packet. - The properties holding an XMPFile's state are not writable, enumerable or configurable, and the pending packet is one of them from the start. - The XMPMeta docs say that only the namespaces a packet's names use are registered, which is what reading a packet does. Test: a_script_cannot_change_the_state_of_an_xmp_file in crates/engine/src/tests/script_xmp.rs. Docs: docs/mcp.md. --- crates/engine/src/tests/script_xmp.rs | 25 +++++++++++++++++++++++++ crates/js/src/xmp.rs | 7 ++++--- crates/js/src/xmp_classes.rs | 5 ++++- docs/mcp.md | 9 +++++---- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/tests/script_xmp.rs b/crates/engine/src/tests/script_xmp.rs index b2c68e2b95..b839da8893 100644 --- a/crates/engine/src/tests/script_xmp.rs +++ b/crates/engine/src/tests/script_xmp.rs @@ -161,6 +161,31 @@ fn a_scanned_sidecar_is_read_only() { let _ = std::fs::remove_dir_all(base); } +/// The state an `XMPFile` keeps cannot be changed by the script: a sidecar opened for reading is +/// not written by `closeFile` after the script assigns, defines or deletes the properties that +/// hold the packet, the path and the open flags. +#[test] +fn a_script_cannot_change_the_state_of_an_xmp_file() { + let base = folder("state"); + let mut s = with_data(&base); + let log = logged( + &mut s, + &base, + &format!( + "var p = Folder.userData.fsName + '/r.xmp'; var f = new File(p); f.open('w'); f.write({PACKET:?}); f.close();\n\ + var x = new XMPFile(p, XMPConst.FILE_UNKNOWN, XMPConst.OPEN_FOR_READ); var out = [];\n\ + x.__vcXmpPending = 'not a packet'; x.__vcXmpFlags = XMPConst.OPEN_FOR_UPDATE;\n\ + try {{ Object.defineProperty(x, '__vcXmpPending', {{ value: 'not a packet' }}); out.push('defined'); }} catch (e) {{ out.push(e.name); }}\n\ + out.push(typeof x.__vcXmpPending, x.getFileInfo().openFlags == XMPConst.OPEN_FOR_READ);\n\ + x.closeFile(); out.push(delete x.__vcXmpPath); vc.log(out); vc.log(p);" + ), + ); + assert_eq!(log[0], json!(["TypeError", "undefined", true, false])); + let text = std::fs::read_to_string(log[1].as_str().unwrap()).unwrap(); + assert_eq!(text, PACKET); + let _ = std::fs::remove_dir_all(base); +} + /// A path outside the script's places, a missing file and a file opened for update where the run /// may not write are refused with the file access errors, and a malformed or huge packet with an /// error, never a fault. diff --git a/crates/js/src/xmp.rs b/crates/js/src/xmp.rs index d4973603d5..0348da2e16 100644 --- a/crates/js/src/xmp.rs +++ b/crates/js/src/xmp.rs @@ -10,9 +10,10 @@ //! a simple value (text, or a URI in `rdf:resource`), an array of simple items (`rdf:Bag`, //! `rdf:Seq` or `rdf:Alt`, each item with an optional `xml:lang`), or any other value (a structure, //! a value with qualifiers, an array of structures), which is kept as the XML it was written in and -//! written back with the namespace declarations it uses. Namespaces a packet declares are added to -//! the realm's [`Registry`] with the prefix the packet gives them, or a new prefix when that one is -//! taken. +//! written back with the namespace declarations it uses. The namespaces of the property, element and +//! attribute names under `rdf:Description` are added to the realm's [`Registry`] with the prefix the +//! packet gives them, or a new prefix when that one is taken; a namespace the packet declares and no +//! such name uses is not added. //! //! VectorCraft's own rules: a packet or a written packet holds at most [`MAX_BYTES`] bytes, //! elements nest at most 64 deep, a child of `rdf:RDF` other than `rdf:Description` makes the diff --git a/crates/js/src/xmp_classes.rs b/crates/js/src/xmp_classes.rs index aadfb83fcb..5733d28e9a 100644 --- a/crates/js/src/xmp_classes.rs +++ b/crates/js/src/xmp_classes.rs @@ -203,11 +203,13 @@ fn slot(i: &Interp, o: ObjId, name: &str) -> JsResult { }) } +/// Sets the slot `name` of `o` as a property that is not writable, enumerable or configurable, so +/// a script cannot assign, redefine or delete it. fn set_slot(i: &mut Interp, o: ObjId, name: &str, value: Value) -> JsResult<()> { if let Value::String(s) = &value { i.rooted(std::slice::from_ref(&value), |i| i.reserve_heap(PROP_BYTES + string_bytes(s.len())))?; } - i.define(o, key(name), Prop::Data { value, flags: HIDDEN }) + i.define(o, key(name), Prop::Data { value, flags: 0 }) } /// A string value, charged to the heap. @@ -983,6 +985,7 @@ fn file_ctor(i: &mut Interp, _: Value, args: &[Value], _: bool) -> JsResult