Script check: a function that sets saveAs or exportFile keeps the write reported

A function declared before an object literal could replace the
literal's saveAs or exportFile method when it ran later, and the check
still treated the method as the script's own and left the write out of
filesWritten. When any function body assigns a property of that name,
no variable's method of that name counts as the script's own.
This commit is contained in:
dotCooCoo committed 2026-11-28 00:49:55 -08:00
1 parent c58763daf6
commit b086db0fbb
3 files changed
+27 -6

No files matched your search

+5 -2
View File
@@ -1441,8 +1441,10 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
/// closes a function body, so a literal given to the body's own variable stays inside the body.
/// No variable joins when code the scan does not follow at that point may give it another value:
/// a function body that assigns its name ([`Declared::body_assigned`]), a member assignment of a
/// property of that name, which may be the global object's ([`Declared::rebound`]), code given as
/// text or a `with` statement ([`Analyzer::unsure`]), and an included file ([`Analyzer::others`]).
/// property of that name, which may be the global object's ([`Declared::rebound`]), a function
/// body that assigns a `saveAs` or `exportFile` property of any object
/// ([`Declared::body_writer_assigned`]), code given as text or a `with` statement
/// ([`Analyzer::unsure`]), and an included file ([`Analyzer::others`]).
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)))
@@ -1450,6 +1452,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
&& !self.others
&& !self.declared.rebound.contains(&name)
&& !self.declared.body_assigned.contains(name.as_str())
&& !self.declared.body_writer_assigned
&& (self.writers.contains_key(&name) || self.acc.room(self.writers.len()))
{
self.writers.insert(name, bits);
@@ -45,6 +45,11 @@ pub(crate) struct Declared<'s> {
/// also before its body in the text when it is a declaration, so a variable of that name may lose
/// the value an assignment the analysis read gave it.
pub(super) body_assigned: HashSet<&'s str>,
/// A function body assigns a property named as one of [`catalog::DOM_FILE_WRITES`]
/// (`o.saveAs = …`, `this['exportFile'] = …`). The function may run at any time after it is
/// defined, and the object may be any variable's, so no variable's method of that name stays
/// the script's own.
pub(super) body_writer_assigned: bool,
/// The file has an `#include` line: the included file's code may give its variables values.
pub(super) includes: bool,
/// The file gives names values the analysis doesn't read in other ways ([`UNREAD_BINDINGS`] or
@@ -245,6 +250,9 @@ impl<'s> Declarations<'s, '_, '_> {
None
};
if let Some(name) = property {
if (!self.bodies.is_empty() || self.untracked.is_some()) && catalog::DOM_FILE_WRITES.contains(&name.as_str()) {
self.out.body_writer_assigned = true;
}
if self.out.rebound.contains(&name) || self.acc.room(self.out.rebound.len()) {
self.out.rebound.insert(name);
} else {
+14 -4
View File
@@ -670,12 +670,13 @@ fn only_a_whole_var_value_makes_save_as_the_scripts_own() {
/// A literal's `saveAs` stays the script's own only while no code the check does not follow may give
/// the variable another value: a declaration after a label, which `if` may skip, a function body that
/// assigns the variable (its own method's, or a declaration's that runs before its text), a member
/// assignment of a property of that name, which may be the global object's, code given as text and an
/// included file each leave the write reported. A function that declares its own variable of the name
/// leaves it the script's own.
/// assignment of a property of that name, which may be the global object's, a function body that
/// assigns a `saveAs` or `exportFile` property of any object, code given as text, a `with` statement
/// and an included file each leave the write reported. A function that declares its own variable of the
/// name, or assigns another property of it, leaves it the script's own.
#[test]
fn a_save_as_other_code_may_replace_writes_a_file() {
let cases: [(&str, &[usize]); 9] = [
let cases: [(&str, &[usize]); 12] = [
("var f = new File(p);\nvar d = app.activeDocument;\nif (flag) label:\nvar d = {saveAs: function () {}};\nd.saveAs(f);\n", &[5]),
(
"var f = new File(p);\nvar d = {\n saveAs: function () {},\n useDocument: function () {\n d = null;\n d = app.activeDocument;\n }\n};\nd.useDocument();\nd.saveAs(f);\n",
@@ -687,6 +688,15 @@ fn a_save_as_other_code_may_replace_writes_a_file() {
("#include \"other.jsx\"\nvar f = new File(p);\nvar d = {saveAs: function () {}};\nd.saveAs(f);\n", &[4]),
("var f = new File(p);\nvar d = {saveAs: function () {}};\n#include \"other.jsx\"\nd.saveAs(f);\n", &[4]),
("var f = new File(p);\nvar d = {saveAs: function () {}};\nwith ({d: app.activeDocument}) {\n d.saveAs(f);\n}\n", &[4]),
(
"var f = new File(p);\nfunction replace() { d.saveAs = app.activeDocument.saveAs; }\nvar d = {saveAs: function () {}};\nreplace();\nd.saveAs(f);\n",
&[5],
),
(
"var f = new File(p);\nfunction replace(o) { o['exportFile'] = w; }\nvar d = {exportFile: function () {}};\nreplace(d);\nd.exportFile(f);\n",
&[5],
),
("var f = new File(p);\nfunction note() { d.n = 1; }\nvar d = {saveAs: function () {}};\nnote();\nd.saveAs(f);\n", &[]),
("var f = new File(p);\nvar d = {saveAs: function () {}};\nfunction g() { var d = app.activeDocument; d = 1; }\nd.saveAs(f);\n", &[]),
];
for (src, lines) in cases {