Scripts: refuse unsafe close saves and refused multi-file writes up front

- A save of several files whose file access refuses one of them returns
  the refusal before any file goes to the writer, so a writer that does
  not check the file access writes nothing.
- close(SaveOptions.SAVECHANGES) throws before writing, and leaves the
  document open, when the document's last save named some of its
  artboards, since saving with those options again could leave changes
  out of the file.
- The script check no longer treats an object literal's saveAs or
  exportFile as the script's own when a function body assigns the
  variable without declaring it, or when the script assigns it as a
  property of this (the global object in global code and in a function
  called without a receiver).
- The close-with-SAVECHANGES format test reads the saved PDF and checks
  that it holds the change made after the first save.
- docs/mcp.md limits the "none of them is written" sentence for SVG
  linked images to refusals found before the first write, and documents
  the new close refusal.
This commit is contained in:
dotCooCoo committed 2026-11-28 03:15:22 -08:00
1 parent 887c67a8f1
commit 39c5e98a3e
7 files changed
+168 -14

No files matched your search

+39
View File
@@ -647,6 +647,12 @@ struct Analyzer<'s, 'a, 'l> {
/// What the receiver before the last `[` read defines in [`Analyzer::writers`], 0 for a receiver
/// that is not such a variable.
index_receiver: u8,
/// The variables that may get another value when code read elsewhere runs: assigned inside a
/// function body that does not declare them, or as a property of `this` ([`Analyzer::reassigned_later`]).
/// None of them joins [`Analyzer::writers`].
later: HashSet<String>,
/// `later` ran out of room: no variable joins [`Analyzer::writers`].
later_full: bool,
/// This file or what the scan started with gives names values the analysis doesn't read
/// ([`Declared::unsure`]): no object counts as a Folder object whatever the path names.
unsure: bool,
@@ -727,6 +733,8 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
objects: vec![],
closed: None,
index_receiver: 0,
later: HashSet::new(),
later_full: false,
unsure: known.unsure || declared.unsure,
own_names: HashSet::new(),
scopes: vec![],
@@ -1417,9 +1425,18 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
// A name after `.`, `..`, `.@`, `::` or `@` is a property's or an attribute's; any other
// name before `=` is a variable, at the start of a statement or inside an expression.
if !member_mark(h1) && !is(h1, "@") {
if !self.scopes.is_empty() && !self.declared_in_body(name.text) {
self.reassigned_later(name.text);
}
self.assign = Some(Assign { name: name.text.to_string(), after_new: false });
}
}
if let Some((name, _)) = self.updated(t)
&& !self.scopes.is_empty()
&& !self.declared_in_body(name)
{
self.reassigned_later(name);
}
match self.updated(t) {
// The value on the right is read at this depth, and the name holds nothing the check
// follows once that value ends ([`Analyzer::settle_groups`]).
@@ -1435,12 +1452,34 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
fn settle_literal(&mut self, t: &Sig<'s>) {
if let Some((name, bits)) = self.closed.take()
&& (t.is(";") || t.is(",") || (t.nl && !goes_on_with(t)))
&& !self.later_full
&& !self.later.contains(&name)
&& (self.writers.contains_key(&name) || self.acc.room(self.writers.len()))
{
self.writers.insert(name, bits);
}
}
/// `name` may get another value when code read elsewhere runs: a function body assigns it without
/// declaring it, and the function may be called before or after any line, or it is assigned as a
/// property of `this`. Its object literal's methods are no longer known, now or later.
fn reassigned_later(&mut self, name: &str) {
self.writers.remove(name);
if !self.later.contains(name) {
if self.acc.room(self.later.len()) {
self.later.insert(name.to_string());
} else {
self.later_full = true;
}
}
}
/// Whether `name` is declared in a function body this token is inside (`var name`, `function
/// name`, a parameter).
fn declared_in_body(&self, name: &str) -> bool {
self.scopes.iter().any(|s| s.saved.get(name).is_some_and(|v| v.local))
}
/// `t` in an object literal assigned to a variable, `before` the bracket depth before `t`: a single
/// function expression directly inside its braces as the whole value of a key that names a
/// document method writing a file (`{saveAs: function … {…}, …}`, `{'exportFile': function … {…}}`)
+16 -1
View File
@@ -376,9 +376,24 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
/// `local[k] = w`, `delete local.saveAs`).
fn writer_use(&mut self, t: &Sig<'s>) {
let [h0, h1, h2, h3] = self.h;
let writer = |s: Option<Sig<'s>>, before: Option<Sig<'s>>| s.filter(|s| self.writer_bits(Some(*s), before) != 0).map(|s| s.text);
let changes = t.kind == T::Punct && (t.text == "=" || COMPOUND.contains(&t.text) || matches!(t.text, "++" | "--"));
let key = t.is(":") && (is(h1, "{") || is(h1, ","));
// `this.local = …` or `this['local'] = …`: `this` is the global object in the global code and in a
// function called without a receiver (ECMA-262 3rd edition, sections 10.2.1 and 10.2.3), so the
// global variable of that name may get another value.
if changes {
let global = if is(h1, ".") && word(h2, "this") {
h0.filter(|s| s.kind == T::Ident).map(|s| s.text.to_string())
} else if is(h0, "]") && is(h2, "[") && word(h3, "this") {
h1.filter(|s| s.kind == T::Str).map(|s| unquote(s.text, 64))
} else {
None
};
if let Some(name) = global {
self.reassigned_later(&name);
}
}
let writer = |s: Option<Sig<'s>>, before: Option<Sig<'s>>| s.filter(|s| self.writer_bits(Some(*s), before) != 0).map(|s| s.text);
let escaped = if word(h0, "delete") {
// `delete local…`
writer(Some(*t), h0)
+40
View File
@@ -664,6 +664,46 @@ fn only_a_whole_var_value_makes_save_as_the_scripts_own() {
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 9, 11, 13, 14, 17][..]));
}
/// A function body read before the literal that assigns its variable without declaring it may run
/// after the literal, so the literal's `saveAs` keeps reporting the write; a body that declares the
/// name assigns its own variable.
#[test]
fn a_function_read_earlier_that_assigns_the_variable_keeps_save_as_writing() {
let r = report(concat!(
"var f = new File(p);\n",
"function choose() { d = app.activeDocument; }\n",
"var d = {saveAs: function () {}};\n",
"choose(); d.saveAs(f);\n",
"function bump() { e++; }\n",
"var e = {saveAs: function () {}};\n",
"e.saveAs(f);\n",
"function own() { var g; g = app.activeDocument; }\n",
"var g = {saveAs: function () {}};\n",
"g.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7][..]));
}
/// Assigning a property of `this`, the global object in the global code and in a function called
/// without a receiver, may give the global variable of that name another value, before or after
/// its literal.
#[test]
fn assigning_a_property_of_this_keeps_save_as_writing() {
let r = report(concat!(
"var f = new File(p);\n",
"var d = {saveAs: function () {}};\n",
"this.d = app.activeDocument;\n",
"d.saveAs(f);\n",
"function swap() { this['e'] = app.activeDocument; }\n",
"var e = {saveAs: function () {}};\n",
"swap(); e.saveAs(f);\n",
"var g = {saveAs: function () {}};\n",
"this.other = 1;\n",
"g.saveAs(f);\n",
));
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7][..]));
}
/// A `saveAs` key's value is a method of the script's own only when the whole value is one function
/// expression: a value that goes on after the function's body, and a literal inside another, leave
/// `saveAs` writing the file. A literal given to a function body's own variable at the end of the body