Scripts: DocumentPreset settings refuse a value of the wrong kind or size

Assigning a DocumentPreset setting stored any value until the run ended,
and the value was checked only when addDocument read it. A script could
keep gigabytes of strings in presets that no addDocument would accept.

The setter now checks the value before storing it: a value of the
setting's enumeration, a finite number, a title of at most 4096 bytes,
true or false for documentBleedLink, or four finite numbers for
documentBleedOffsetRect. undefined unsets any setting, and the three
settings addDocument does not apply yet also take null. A value of
another kind throws a TypeError, a longer title throws a RangeError, and
the setting keeps its old value. addDocument still checks the ranges.
This commit is contained in:
dotCooCoo committed 2026-11-27 20:16:03 -08:00
1 parent 5d70217e0c
commit 8ff8dbefc6
3 files changed
+89 -8

No files matched your search

+53 -2
View File
@@ -1180,8 +1180,58 @@ fn preset_of(h: &Host, d: &Data) -> Option<usize> {
(i < h.dom.presets.borrow().len()).then_some(i)
}
/// The getter and setter of the `DocumentPreset` setting `key`: an unset one reads `undefined`, and
/// a value is checked when `addDocument` uses it.
/// The enumeration whose values the `DocumentPreset` setting `key` takes, when it takes one.
fn preset_enum(key: &str) -> Option<&'static str> {
Some(match key {
"artboardLayout" => "DocumentArtboardLayout",
"colorMode" => "DocumentColorSpace",
"previewMode" => "DocumentPreviewMode",
"rasterResolution" => "DocumentRasterResolution",
"transparencyGrid" => "DocumentTransparencyGrid",
"units" => "RulerUnits",
_ => return None,
})
}
/// Checks that `value` is of the kind the `DocumentPreset` setting `key` holds: a value of its
/// enumeration, a finite number, a title of at most [`MAX_TEXT_BYTES`] bytes, `true` or `false` for
/// `documentBleedLink`, or four finite numbers for `documentBleedOffsetRect`. `undefined` unsets any
/// setting, and the three settings `addDocument` does not apply yet also take `null`.
fn check_preset_value(key: &str, value: &Data) -> std::result::Result<(), HostError> {
let wrong = |kind: &str| Err(HostError::Type(format!("DocumentPreset.{key} must be {kind}")));
match value {
Data::Undefined => return Ok(()),
Data::Null if matches!(key, "documentBleedLink" | "documentBleedOffsetRect" | "transparencyGrid") => return Ok(()),
_ => {}
}
if let Some(name) = preset_enum(key) {
let member = ENUMS.iter().find(|(n, _)| *n == name).map_or(&[][..], |(_, m)| *m);
return match value {
Data::String(s) if s.strip_prefix(name).and_then(|r| r.strip_prefix('.')).is_some_and(|m| member.contains(&m)) => Ok(()),
_ => wrong(&format!("a {name} value")),
};
}
match key {
"title" => match value {
Data::String(t) if t.len() > MAX_TEXT_BYTES => Err(HostError::Range("the title is too long".into())),
Data::String(_) => Ok(()),
_ => wrong("a string"),
},
"documentBleedLink" => match value {
Data::Bool(_) => Ok(()),
_ => wrong("true or false"),
},
"documentBleedOffsetRect" => match value {
Data::Array(r) if r.len() == 4 && r.iter().all(|n| matches!(n, Data::Number(n) if n.is_finite())) => Ok(()),
_ => wrong("an array of four finite numbers"),
},
_ => number(value, &format!("DocumentPreset.{key}")).map(|_| ()),
}
}
/// The getter and setter of the `DocumentPreset` setting `key`: an unset one reads `undefined`. The
/// setter checks the kind and size of a value ([`check_preset_value`]) and keeps the old value when
/// it throws; `addDocument` checks the ranges.
fn preset_field(link: &Link, key: &'static str) -> HostMember {
let not_preset = || HostError::Type("this is not a DocumentPreset".into());
let l = link.clone();
@@ -1201,6 +1251,7 @@ fn preset_field(link: &Link, key: &'static str) -> HostMember {
let value = arg(&a, 1).clone();
h.charge_call(16 + data_size(&value))?;
let i = preset_of(&h, arg(&a, 0)).ok_or_else(not_preset)?;
check_preset_value(key, &value)?;
let mut presets = h.dom.presets.borrow_mut();
let fields = &mut presets[i];
match fields.iter_mut().find(|(k, _)| k == key) {
+30 -4
View File
@@ -1178,12 +1178,12 @@ fn add_document_refuses_pixel_preview() {
#[test]
fn document_preset_values_count_toward_the_data_a_run_passes() {
// A 64 KiB title, set once, then read or handed to addDocument 32 times: 2 MiB, over a 1 MiB limit.
// A 4 KiB title, set once, then read or handed to addDocument 512 times: 2 MiB, over a 1 MiB limit.
crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(Some(1 << 20)));
let mut s = Session::new();
let set = "var p = new DocumentPreset(); p.title = new Array(65537).join('t');";
let read = failure(&mut s, &format!("{set} for (var i = 0; i < 32; i++) p.title;"));
let added = failure(&mut s, &format!("{set} for (var i = 0; i < 32; i++) try {{ app.documents.addDocument('Print', p, 5); }} catch (e) {{}}"));
let set = "var p = new DocumentPreset(); p.title = new Array(4097).join('t');";
let read = failure(&mut s, &format!("{set} for (var i = 0; i < 512; i++) p.title;"));
let added = failure(&mut s, &format!("{set} for (var i = 0; i < 512; i++) try {{ app.documents.addDocument('Print', p, 5); }} catch (e) {{}}"));
let within = run(&mut s, &format!("{set} for (var i = 0; i < 8; i++) p.title;"));
crate::script::TEST_BRIDGE_BYTES.with(|b| b.set(None));
for e in [read, added] {
@@ -1193,6 +1193,32 @@ fn document_preset_values_count_toward_the_data_a_run_passes() {
assert!(s.documents().is_empty());
}
#[test]
fn document_preset_setter_refuses_a_value_of_the_wrong_kind_or_size_and_keeps_the_old_one() {
let mut s = Session::new();
let out = log(
&mut s,
"var p = new DocumentPreset(); p.title = 'Flyer';\
var bad = [['title', new Array(4098).join('t')], ['title', 5], ['width', 'wide'], ['width', Infinity], ['height', null],\
['colorMode', 'RGB'], ['units', {}], ['artboardLayout', 'DocumentColorSpace.RGB'], ['documentBleedLink', 1],\
['documentBleedOffsetRect', [1, 2, 3]], ['documentBleedOffsetRect', [1, 2, 3, 'x']], ['transparencyGrid', 'TransparencyGridRed']];\
for (var i = 0; i < bad.length; i++) { try { p[bad[i][0]] = bad[i][1]; vc.log('kept ' + bad[i][0]); } catch (e) { vc.log(e.name); } }\
vc.log([p.title, p.width, p.height, p.colorMode === undefined, p.units === undefined, p.artboardLayout,\
p.documentBleedLink === undefined, p.documentBleedOffsetRect === undefined, p.transparencyGrid === undefined]);",
);
let mut names = vec![json!("RangeError")];
names.extend(std::iter::repeat_n(json!("TypeError"), 11));
assert_eq!(out[..12], names[..], "{out:?}");
assert_eq!(out[12], json!(["Flyer", 612, 792, true, true, "DocumentArtboardLayout.GridByRow", true, true, true]));
let out = log(
&mut s,
"var p = new DocumentPreset(); p.title = new Array(4097).join('t'); p.documentBleedLink = null; p.transparencyGrid = null;\
p.documentBleedOffsetRect = [0, 0, 0, 0]; p.width = undefined; vc.log([p.title.length, p.documentBleedLink, p.width === undefined]);\
p.documentBleedOffsetRect = undefined; vc.log(app.documents.addDocument('Print', p).typename);",
);
assert_eq!(out, vec![json!([4096, null, true]), json!("Document")]);
}
#[test]
fn wrappers_go_stale_after_delete_and_undo() {
let (mut s, _, _) = doc();
+6 -2
View File
@@ -2787,8 +2787,12 @@ other files through `$`:
`file.newPresets`, or `Basic CMYK` and `Basic RGB` for no profile), starting from the first preset
`file.newPresets` lists for that profile, and the settings of a `new DocumentPreset()`
(https://ai-scripting.docsforadobe.dev/jsobjref/DocumentPreset/) in place of the preset's. A `DocumentPreset`
starts with the defaults that page gives. Its `documentBleedLink`, `documentBleedOffsetRect` and
`transparencyGrid` can be assigned, but `addDocument` throws an error that says they are not available yet when
starts with the defaults that page gives. Assigning one of its settings checks the value's kind: a value of the
setting's enumeration, a finite number, a `title` string, `true` or `false` for `documentBleedLink`, or an array
of four finite numbers for `documentBleedOffsetRect`. Any setting takes `undefined`, which unsets it. A value of
another kind throws a `TypeError`, a `title` longer than 4096 bytes throws a `RangeError`, and the setting keeps
its old value. `addDocument` checks the ranges of the values. `documentBleedLink`, `documentBleedOffsetRect` and
`transparencyGrid` also take `null` and can be assigned, but `addDocument` throws an error that says they are not available yet when
the preset it is given holds one of them with a value other than `null`, and makes no document. A
`presetSettings` that is not a `DocumentPreset` (a plain object such as `{width: 500}` included) throws a
`TypeError`. A script's `file.new` reads the profile's file in the profiles folder as the person's does, under the