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.
This commit is contained in:
dotCooCoo committed 2026-11-22 10:44:46 -08:00
1 parent 4a51aa4d48
commit 72c4875990
4 files changed
+38 -8

No files matched your search

+25
View File
@@ -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.
+4 -3
View File
@@ -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
+4 -1
View File
@@ -203,11 +203,13 @@ fn slot(i: &Interp, o: ObjId, name: &str) -> JsResult<Value> {
})
}
/// 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<Valu
(WRITEABLE, Value::Bool(field(i, "writeable")?.truthy())),
(SIDECAR, Value::Bool(sidecar)),
(CHAR_FORM, field(i, "charForm")?),
(PENDING, Value::Undefined),
];
for (name, value) in fields {
set_slot(i, o, name, value)?;
+5 -4
View File
@@ -3063,10 +3063,11 @@ rules for these objects:
functions `XMPMeta.registerNamespace`, `getNamespacePrefix` and `getNamespaceURI`. A path is a property name,
`prefix:name`, or either with `[n]` (from 1) or `[last()]` for an array item. Arrays hold simple items; a structure
or a value with qualifiers that a packet holds is kept as it was written, and `getProperty` gives its options.
Namespaces a packet declares are registered with the prefix it gives them, or `prefix_1_` when that prefix names
another namespace. At most 4,096 namespaces, holding at most 1 MiB together, are registered. A namespace URI over
1,024 bytes, a prefix over 128 bytes, and a namespace past either limit throw `… the XMP data is too large`, both
from `registerNamespace` and from a packet that declares the namespace.
The namespaces that a packet's property, element and attribute names use are registered with the prefix the packet
gives them, or `prefix_1_` when that prefix names another namespace; a namespace the packet declares but no name
uses is not registered. At most 4,096 namespaces, holding at most 1 MiB together, are registered. A namespace URI
over 1,024 bytes, a prefix over 128 bytes, and a namespace past either limit throw `… the XMP data is too large`,
both from `registerNamespace` and from a packet that uses the namespace.
- `XMPFile(path, format, openFlags)` reads a file through the same places as `File`, so a path outside them, a
missing file and a file over 8 MiB throw the `File` messages (`Script file access denied`, `Script file does not
exist`, `Script file exceeds 8 MiB`). A `.xmp` file is read whole as its packet (a link counts by the file it leads to,