mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 13:32:45 -05:00
Scripts: report saveAs replaced through bracketed global members
The checker treated `$.global.local.saveAs = …` as replacing the variable's own saveAs, but missed the bracket forms `$.global['local'].saveAs = …`, `this['local'].saveAs = …`, `globalThis['local']['saveAs'] = …` and `$.global.local['saveAs'] = …`, so a script could rebind a document write method that way and its later writes were not reported. These forms now end the variable's own saveAs, including when a function body declares a variable of the same name.
This commit is contained in:
1 parent
891d24f822
commit
dbe8120de7
3 files changed
+70
No files matched your search
@@ -716,6 +716,9 @@ struct Analyzer<'s, 'a, 'l> {
|
||||
global_indexes: Vec<usize>,
|
||||
/// The last token was the `]` that closes an index of the global object.
|
||||
global_index_closed: bool,
|
||||
/// The offset of the last `]` that closes an index of the global object by a string literal
|
||||
/// alone, with the variable name it reads (`$.global['local']`, `this['local']`).
|
||||
global_bracket: Option<(usize, String)>,
|
||||
/// The offset of the last name read as a property of the global object (`local` in
|
||||
/// `$.global.local`, `globalThis.local` or `this.local`).
|
||||
global_member: Option<usize>,
|
||||
@@ -822,6 +825,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
index_stable: false,
|
||||
global_indexes: vec![],
|
||||
global_index_closed: false,
|
||||
global_bracket: None,
|
||||
global_member: None,
|
||||
with_head: None,
|
||||
withs: vec![],
|
||||
|
||||
@@ -587,6 +587,13 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
let depth = self.depth();
|
||||
self.global_index_closed = self.global_indexes.last() == Some(&(depth + 1));
|
||||
self.global_indexes.retain(|d| *d <= depth);
|
||||
// `$.global['local']` or `this['local']`: the `]` reads the global variable `local`.
|
||||
if self.global_index_closed
|
||||
&& is(h1, "[")
|
||||
&& let Some(key) = h0.filter(|s| s.kind == T::Str)
|
||||
{
|
||||
self.global_bracket = Some((t.start, unquote(key.text, 256)));
|
||||
}
|
||||
}
|
||||
let changes = t.kind == T::Punct && (t.text == "=" || COMPOUND.contains(&t.text) || matches!(t.text, "++" | "--"));
|
||||
let key = t.is(":") && (is(h1, "{") || is(h1, ","));
|
||||
@@ -657,6 +664,26 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
||||
if let Some(name) = early {
|
||||
self.share_early(name);
|
||||
}
|
||||
// A document write method of a global variable replaced through a bracket form:
|
||||
// `$.global['local'].saveAs = …`, `this['local']['saveAs'] = …` or `$.global.local['saveAs'] = …`.
|
||||
if changes {
|
||||
let write_key = |s: Option<Sig<'s>>| s.is_some_and(|m| catalog::DOM_FILE_WRITES.contains(&unquote(m.text, 32).as_str()));
|
||||
let bracketed =
|
||||
|s: Option<Sig<'s>>| s.and_then(|s| self.global_bracket.as_ref().filter(|(at, _)| *at == s.start).map(|(_, name)| name.clone()));
|
||||
let global = if is(h1, ".") && h0.is_some_and(|m| m.kind == T::Ident) && write_key(h0) {
|
||||
bracketed(h2)
|
||||
} else if is(h0, "]") && is(h2, "[") && h1.is_some_and(|m| m.kind == T::Str) && write_key(h1) {
|
||||
bracketed(h3).or_else(|| h3.filter(|s| s.kind == T::Ident && self.global_member == Some(s.start)).map(|s| s.text.to_string()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(name) = global {
|
||||
// The global variable's value may be saved under a body's or a `catch`'s own variable of
|
||||
// that name, so forget every value of the name.
|
||||
self.forget_writer(&name);
|
||||
self.share_early_as(&name, true);
|
||||
}
|
||||
}
|
||||
// A name read in a function body that the body does not declare, as a receiver or otherwise. A
|
||||
// receiver is settled once its member is read: assigning a property that is not one of the
|
||||
// document methods that write a file (`d.n = 1`) leaves its methods as they are.
|
||||
|
||||
@@ -1239,6 +1239,45 @@ fn a_method_replaced_through_the_global_object_ends_the_own_save_as() {
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 9, 12, 16][..]));
|
||||
}
|
||||
|
||||
/// Bracket forms of the global object's property (`$.global['local'].saveAs`, `this['local'].saveAs`,
|
||||
/// `$.global.local['saveAs']`) change the variable's methods, as the dot forms do.
|
||||
#[test]
|
||||
fn a_method_replaced_through_a_bracketed_global_member_ends_the_own_save_as() {
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"function install() {\n",
|
||||
" $.global['local'].saveAs = function (f) {\n",
|
||||
" app.activeDocument.saveAs(f);\n",
|
||||
" };\n",
|
||||
"}\n",
|
||||
"var local = {saveAs: function () {}};\n",
|
||||
"install();\n",
|
||||
"local.saveAs(f);\n",
|
||||
"var mine = {saveAs: function () {}};\n",
|
||||
"this['mine'].saveAs = w;\n",
|
||||
"mine.saveAs(f);\n",
|
||||
"var third = {saveAs: function () {}};\n",
|
||||
"$.global.third['saveAs'] = w;\n",
|
||||
"third.saveAs(f);\n",
|
||||
"var fourth = {saveAs: function () {}};\n",
|
||||
"globalThis['fourth']['saveAs'] = w;\n",
|
||||
"fourth.saveAs(f);\n",
|
||||
"var safe = {saveAs: function () {}};\n",
|
||||
"holder.global['safe'].saveAs = w;\n",
|
||||
"safe.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 9, 12, 15, 18][..]));
|
||||
// A body's own variable of the same name does not keep the global variable's methods.
|
||||
let r = report(concat!(
|
||||
"var f = new File(p);\n",
|
||||
"var local = {saveAs: function () {}};\n",
|
||||
"function g() { var local = {saveAs: function () {}}; $.global['local'].saveAs = w; }\n",
|
||||
"g();\n",
|
||||
"local.saveAs(f);\n",
|
||||
));
|
||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5][..]));
|
||||
}
|
||||
|
||||
/// Only a `var` statement directly in a function body or the global code that gives its first name
|
||||
/// the literal as its whole value makes the literal's `saveAs` the script's own, from the end of the
|
||||
/// statement. A literal given in a branch, one the value goes on from, one given by an assignment
|
||||
|
||||
Reference in new issue
Block a user