mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 05:22:58 -05:00
Scripts: read a bracketed global key in full before forgetting its variable
The script checker stored a bracketed global key such as $.global['name']
cut to 256 characters, so a longer key forgot a different variable and a
later own saveAs call on the real one was not reported. Keys are now
decoded in full. A key whose escape does not decode to one exact name
(\u{63}, a short \x or \u, a lone surrogate) forgets every variable's
own methods instead of one name.
This commit is contained in:
1 parent
dbe8120de7
commit
a9c8f36345
4 files changed
+77
-14
No files matched your search
@@ -27,7 +27,7 @@ use crate::catalog::{self, FeatureId};
|
|||||||
use crate::directives;
|
use crate::directives;
|
||||||
use crate::lex::{Lexer, T, Token, expr_after};
|
use crate::lex::{Lexer, T, Token, expr_after};
|
||||||
use crate::report::{self, Acc, OutsideId};
|
use crate::report::{self, Acc, OutsideId};
|
||||||
use sig::{COMPOUND, LineBreaks, NEWER, Read, Sig, ends_value, for_in, goes_on_after, goes_on_with, is, member_mark, newer_number, unquote, word};
|
use sig::{COMPOUND, LineBreaks, NEWER, Read, Sig, ends_value, exact_unquote, for_in, goes_on_after, goes_on_with, is, member_mark, newer_number, unquote, word};
|
||||||
|
|
||||||
mod declared;
|
mod declared;
|
||||||
mod features;
|
mod features;
|
||||||
@@ -717,8 +717,9 @@ struct Analyzer<'s, 'a, 'l> {
|
|||||||
/// The last token was the `]` that closes an index of the global object.
|
/// The last token was the `]` that closes an index of the global object.
|
||||||
global_index_closed: bool,
|
global_index_closed: bool,
|
||||||
/// The offset of the last `]` that closes an index of the global object by a string literal
|
/// 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']`).
|
/// alone, with the variable name it reads (`$.global['local']`, `this['local']`), or `None` when
|
||||||
global_bracket: Option<(usize, String)>,
|
/// the literal does not decode to one exact name.
|
||||||
|
global_bracket: Option<(usize, Option<String>)>,
|
||||||
/// The offset of the last name read as a property of the global object (`local` in
|
/// The offset of the last name read as a property of the global object (`local` in
|
||||||
/// `$.global.local`, `globalThis.local` or `this.local`).
|
/// `$.global.local`, `globalThis.local` or `this.local`).
|
||||||
global_member: Option<usize>,
|
global_member: Option<usize>,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
//! The platform features, menu commands and later syntax the analysis recognises.
|
//! The platform features, menu commands and later syntax the analysis recognises.
|
||||||
|
|
||||||
use super::{Analyzer, COMPOUND, FeatureId, NEWER, OutsideId, Pending, Sig, T, catalog, is, member_mark, newer_number, report, unquote, word};
|
use super::{Analyzer, COMPOUND, FeatureId, NEWER, OutsideId, Pending, Sig, T, catalog, exact_unquote, is, member_mark, newer_number, report, unquote, word};
|
||||||
|
|
||||||
/// Words that start or continue a statement or an expression, never a variable's name.
|
/// Words that start or continue a statement or an expression, never a variable's name.
|
||||||
const RESERVED: [&str; 24] = [
|
const RESERVED: [&str; 24] = [
|
||||||
@@ -592,7 +592,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
|||||||
&& is(h1, "[")
|
&& is(h1, "[")
|
||||||
&& let Some(key) = h0.filter(|s| s.kind == T::Str)
|
&& let Some(key) = h0.filter(|s| s.kind == T::Str)
|
||||||
{
|
{
|
||||||
self.global_bracket = Some((t.start, unquote(key.text, 256)));
|
self.global_bracket = Some((t.start, exact_unquote(key.text)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let changes = t.kind == T::Punct && (t.text == "=" || COMPOUND.contains(&t.text) || matches!(t.text, "++" | "--"));
|
let changes = t.kind == T::Punct && (t.text == "=" || COMPOUND.contains(&t.text) || matches!(t.text, "++" | "--"));
|
||||||
@@ -673,15 +673,20 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
|||||||
let global = if is(h1, ".") && h0.is_some_and(|m| m.kind == T::Ident) && write_key(h0) {
|
let global = if is(h1, ".") && h0.is_some_and(|m| m.kind == T::Ident) && write_key(h0) {
|
||||||
bracketed(h2)
|
bracketed(h2)
|
||||||
} else if is(h0, "]") && is(h2, "[") && h1.is_some_and(|m| m.kind == T::Str) && write_key(h1) {
|
} 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()))
|
bracketed(h3).or_else(|| h3.filter(|s| s.kind == T::Ident && self.global_member == Some(s.start)).map(|s| Some(s.text.to_string())))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
if let Some(name) = global {
|
match global {
|
||||||
// The global variable's value may be saved under a body's or a `catch`'s own variable of
|
// 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.
|
// that name, so forget every value of the name.
|
||||||
self.forget_writer(&name);
|
Some(Some(name)) => {
|
||||||
self.share_early_as(&name, true);
|
self.forget_writer(&name);
|
||||||
|
self.share_early_as(&name, true);
|
||||||
|
}
|
||||||
|
// A key that does not decode to one exact name may name any variable.
|
||||||
|
Some(None) => self.forget_writers(),
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// A name read in a function body that the body does not declare, as a receiver or otherwise. A
|
// A name read in a function body that the body does not declare, as a receiver or otherwise. A
|
||||||
@@ -701,20 +706,21 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> {
|
|||||||
// be the global object (`this`, `globalThis`, `$.global`, or any object holding it, as
|
// be the global object (`this`, `globalThis`, `$.global`, or any object holding it, as
|
||||||
// `holder.this`), so the variable of that name may get another value.
|
// `holder.this`), so the variable of that name may get another value.
|
||||||
let rebound = if is(h1, ".") {
|
let rebound = if is(h1, ".") {
|
||||||
h0.filter(|s| s.kind == T::Ident).map(|s| s.text.to_string())
|
h0.filter(|s| s.kind == T::Ident).map(|s| Some(s.text.to_string()))
|
||||||
} else if is(h0, "]") && is(h2, "[") && h1.is_some_and(|s| s.kind == T::Str) {
|
} else if is(h0, "]") && is(h2, "[") && h1.is_some_and(|s| s.kind == T::Str) {
|
||||||
h1.filter(|s| s.kind == T::Str).map(|s| unquote(s.text, 256))
|
h1.filter(|s| s.kind == T::Str).map(|s| exact_unquote(s.text))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
if let Some(name) = rebound {
|
if let Some(Some(name)) = rebound {
|
||||||
// In a function body: the function may run after a later literal gives the name methods.
|
// In a function body: the function may run after a later literal gives the name methods.
|
||||||
if !self.scopes.is_empty() {
|
if !self.scopes.is_empty() {
|
||||||
self.reassigned_later(&name);
|
self.reassigned_later(&name);
|
||||||
}
|
}
|
||||||
self.forget_writer(&name);
|
self.forget_writer(&name);
|
||||||
} else if global_index_closed {
|
} else if global_index_closed || rebound.is_some() {
|
||||||
// `$.global[key] = …`: the key may name any of them.
|
// `$.global[key] = …`, or a key that does not decode to one exact name: the key may
|
||||||
|
// name any of them.
|
||||||
self.forget_writers();
|
self.forget_writers();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -135,6 +135,34 @@ pub(super) fn newer_number(text: &str) -> bool {
|
|||||||
text.contains('_') || (b.len() > 1 && b.last() == Some(&b'n')) || matches!((b.first(), b.get(1)), (Some(b'0'), Some(b'b' | b'B' | b'o' | b'O')))
|
text.contains('_') || (b.len() > 1 && b.last() == Some(&b'n')) || matches!((b.first(), b.get(1)), (Some(b'0'), Some(b'b' | b'B' | b'o' | b'O')))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A string literal's whole value ([`unquote`] without a length limit), or `None` when an escape
|
||||||
|
/// does not decode to exactly one character: a `\x` without two hex digits, a `\u` without four
|
||||||
|
/// (`\u{63}` included), or a code unit that is not a character on its own.
|
||||||
|
pub(super) fn exact_unquote(text: &str) -> Option<String> {
|
||||||
|
let b = text.as_bytes();
|
||||||
|
let mut i = 0;
|
||||||
|
while i < b.len() {
|
||||||
|
if b[i] == b'\\' {
|
||||||
|
let digits = match b.get(i + 1) {
|
||||||
|
Some(b'x') => 2,
|
||||||
|
Some(b'u') => 4,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
if digits > 0 {
|
||||||
|
let hex = b.get(i + 2..i + 2 + digits).filter(|d| d.iter().all(u8::is_ascii_hexdigit))?;
|
||||||
|
// A surrogate code unit (`\uD800` to `\uDFFF`) is not a character on its own.
|
||||||
|
if digits == 4 && matches!(hex[0], b'd' | b'D') && matches!(hex[1], b'8'..=b'9' | b'a'..=b'f' | b'A'..=b'F') {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i += 2;
|
||||||
|
} else {
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(unquote(text, text.chars().count()))
|
||||||
|
}
|
||||||
|
|
||||||
/// A string literal's value, its quotes removed and its escapes undone (`\n`, `\xHH`, `\uHHHH`,
|
/// A string literal's value, its quotes removed and its escapes undone (`\n`, `\xHH`, `\uHHHH`,
|
||||||
/// octal escapes, a backslash before a line break), up to `max` characters.
|
/// octal escapes, a backslash before a line break), up to `max` characters.
|
||||||
pub(super) fn unquote(text: &str, max: usize) -> String {
|
pub(super) fn unquote(text: &str, max: usize) -> String {
|
||||||
|
|||||||
@@ -1209,6 +1209,34 @@ fn a_property_assignment_in_a_loop_ends_the_own_save_as() {
|
|||||||
"for (var i = 0; i < 2; i++) { local.saveAs(f); $.global['lo\\u0063al'] = app.activeDocument; }\n",
|
"for (var i = 0; i < 2; i++) { local.saveAs(f); $.global['lo\\u0063al'] = app.activeDocument; }\n",
|
||||||
));
|
));
|
||||||
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
|
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
|
||||||
|
// Hex escapes and a line continuation in the key.
|
||||||
|
let r = report(concat!(
|
||||||
|
"var f = new File(p);\n",
|
||||||
|
"var local = {saveAs: function () {}};\n",
|
||||||
|
"for (var i = 0; i < 2; i++) { local.saveAs(f); this['lo\\x63\\\n",
|
||||||
|
"al'] = app.activeDocument; }\n",
|
||||||
|
));
|
||||||
|
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
|
||||||
|
// A key longer than 256 characters names the variable of that whole name.
|
||||||
|
let long = "v".repeat(300);
|
||||||
|
let r = report(&format!(
|
||||||
|
"var f = new File(p);\nvar {long} = {{saveAs: function () {{}}}};\nfor (var i = 0; i < 2; i++) {{ {long}.saveAs(f); $.global['{long}'] = app.activeDocument; }}\n"
|
||||||
|
));
|
||||||
|
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..]));
|
||||||
|
let r = report(&format!("var f = new File(p);\nvar {long} = {{saveAs: function () {{}}}};\n$.global['{long}'].saveAs = w;\n{long}.saveAs(f);\n"));
|
||||||
|
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4][..]));
|
||||||
|
// A key whose escape does not decode to one exact name may name any variable.
|
||||||
|
let r = report(concat!(
|
||||||
|
"var f = new File(p);\n",
|
||||||
|
"var local = {saveAs: function () {}};\n",
|
||||||
|
"local.saveAs(f);\n",
|
||||||
|
"this['lo\\u{63}al'] = app.activeDocument;\n",
|
||||||
|
"local.saveAs(f);\n",
|
||||||
|
"var kept = {saveAs: function () {}};\n",
|
||||||
|
"$.global['k\\uD800'].saveAs = w;\n",
|
||||||
|
"kept.saveAs(f);\n",
|
||||||
|
));
|
||||||
|
assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 8][..]));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A function that replaces a method through the global object's property (`$.global.local.saveAs`)
|
/// A function that replaces a method through the global object's property (`$.global.local.saveAs`)
|
||||||
|
|||||||
Reference in new issue
Block a user