Truly independent web browser
LibJS: Match escaped __proto__ keys in the duplicate __proto__ check
Shannon Booth616ca4b7cd56
diff --git a/Libraries/LibJS/Rust/src/ast.rs b/Libraries/LibJS/Rust/src/ast.rsindex 4340c97dd5a..847efdb970b 100644--- a/Libraries/LibJS/Rust/src/ast.rs+++ b/Libraries/LibJS/Rust/src/ast.rs@@ -719,10 +719,7 @@ impl SyntaxTree {fn is_proto_key(&self, key: u32) -> bool {match self.tag(key) {tag::IDENT => self.a(key) == N_PROTO,- tag::STR => {- self.string_units(key).length_in_code_units() >= 9- && self.string_value(key).to_utf16().as_ref() == utf16!("__proto__")- }+ tag::STR => literals::string_literal_text(self.string_units(key)).view() == "__proto__",_ => false,}}diff --git a/Libraries/LibJS/Rust/src/bytecode/codegen/nodes.rs b/Libraries/LibJS/Rust/src/bytecode/codegen/nodes.rsindex 500d016a820..10828904151 100644--- a/Libraries/LibJS/Rust/src/bytecode/codegen/nodes.rs+++ b/Libraries/LibJS/Rust/src/bytecode/codegen/nodes.rs@@ -22,6 +22,8 @@ use crate::parser::columns::NONE;use crate::parser::columns::tag;use crate::parser::literals;pub(super) use crate::parser::literals::EscapeMode;+pub(super) use crate::parser::literals::StringLiteralText;+use crate::parser::literals::has_escape_sequence;use crate::parser::token::T;use crate::utf16::Utf16View;use ak::Utf16String;@@ -151,22 +153,6 @@ pub(super) struct TryData {pub(super) finalizer: Option<NodeId>,}-/// The text of a string literal: a view of the source if it has no escape sequences, which most do not.-pub(super) enum StringLiteralText<'t> {- Source(Utf16View<'t>),- Processed(Utf16String),-}--impl StringLiteralText<'_> {- #[inline(always)]- pub(super) fn view(&self) -> Utf16View<'_> {- match self {- StringLiteralText::Source(view) => *view,- StringLiteralText::Processed(string) => Utf16View::of_string(string),- }- }-}-impl SyntaxTree {#[inline(always)]pub(super) fn node_tag(&self, node: NodeId) -> u8 {@@ -254,12 +240,7 @@ impl SyntaxTree {/// The value of a string literal node, without a copy if that is possible.#[inline(always)]pub(super) fn string_literal_text(&self, node: NodeId) -> StringLiteralText<'_> {- let units = self.string_literal_units(node);- if has_escape_sequence(units) {- StringLiteralText::Processed(self.string_literal_value(node))- } else {- StringLiteralText::Source(units)- }+ literals::string_literal_text(self.string_literal_units(node))}/// The value of a string literal node.@@ -453,15 +434,6 @@ impl SyntaxTree {}}-/// Whether the text of a string literal or a template has an escape sequence in it.-#[inline(always)]-fn has_escape_sequence(units: Utf16View<'_>) -> bool {- match units {- Utf16View::Ascii(units) | Utf16View::Latin1(units) => units.contains(&b'\\'),- Utf16View::Utf16(units) => units.contains(&u16::from(b'\\')),- }-}-#[derive(Clone, Copy)]pub(super) enum BinaryOrLogical {Binary(BinaryOp),diff --git a/Libraries/LibJS/Rust/src/parser/expr.rs b/Libraries/LibJS/Rust/src/parser/expr.rsindex 7d2d0e3ada4..ec5c080d0fd 100644--- a/Libraries/LibJS/Rust/src/parser/expr.rs+++ b/Libraries/LibJS/Rust/src/parser/expr.rs@@ -1070,8 +1070,10 @@ impl<U: Unit> Parser<'_, U> {match key_kind {T::Name => key_name == N_PROTO,T::Str => {- let start = self.ast.pos[key as usize] as usize;- key_end as usize - start == 11 && (0..9).all(|i| self.at(start + 1 + i) == b"__proto__"[i] as u32)+ // NB: The name of the property is the value of the string, which escape sequences spell out as well.+ let start = self.ast.pos[key as usize] as usize + 1;+ let units = self.source_view(start, key_end as usize - 1);+ super::literals::string_literal_text(units).view() == "__proto__"}_ => false,}diff --git a/Libraries/LibJS/Rust/src/parser/lexer.rs b/Libraries/LibJS/Rust/src/parser/lexer.rsindex 0be03c5010f..d7002fe4809 100644--- a/Libraries/LibJS/Rust/src/parser/lexer.rs+++ b/Libraries/LibJS/Rust/src/parser/lexer.rs@@ -11,6 +11,7 @@use super::base::Parser;use super::token::*;use super::unit::*;+use crate::utf16::Utf16View;const fn keyword_kinds() -> [T; 36] {let mut kinds = [T::Eof; 36];@@ -57,6 +58,13 @@ impl<U: Unit> Parser<'_, U> {unsafe { (*self.src.add(index)).code() }}+ /// The source text from `start` up to `end`.+ pub(crate) fn source_view(&self, start: usize, end: usize) -> Utf16View<'_> {+ debug_assert!(start <= end && end <= self.len as usize);+ // SAFETY: The range is within the source.+ U::view(unsafe { ak::slice_from_raw_parts(self.src.add(start), end - start) })+ }+#[inline(always)]fn block(&self, index: usize) -> Block {// SAFETY: As for at().diff --git a/Libraries/LibJS/Rust/src/parser/literals.rs b/Libraries/LibJS/Rust/src/parser/literals.rsindex 7752bfe0628..c621bba445e 100644--- a/Libraries/LibJS/Rust/src/parser/literals.rs+++ b/Libraries/LibJS/Rust/src/parser/literals.rs@@ -28,6 +28,41 @@ pub(crate) struct EscapeResult {pub(crate) failed: bool,}+/// The text of a string literal: a view of the source if it has no escape sequences, which most do not.+pub(crate) enum StringLiteralText<'t> {+ Source(Utf16View<'t>),+ Processed(Utf16String),+}++impl StringLiteralText<'_> {+ #[inline(always)]+ pub(crate) fn view(&self) -> Utf16View<'_> {+ match self {+ StringLiteralText::Source(view) => *view,+ StringLiteralText::Processed(string) => Utf16View::of_string(string),+ }+ }+}++/// Whether the text of a string literal or a template has an escape sequence in it.+#[inline(always)]+pub(crate) fn has_escape_sequence(units: Utf16View<'_>) -> bool {+ match units {+ Utf16View::Ascii(units) | Utf16View::Latin1(units) => units.contains(&b'\\'),+ Utf16View::Utf16(units) => units.contains(&u16::from(b'\\')),+ }+}++/// The value of a string literal from the text between its quotes, without a copy if that is possible.+#[inline(always)]+pub(crate) fn string_literal_text(units: Utf16View<'_>) -> StringLiteralText<'_> {+ if has_escape_sequence(units) {+ StringLiteralText::Processed(process_escape_sequences_impl(units, EscapeMode::StringLiteral).value)+ } else {+ StringLiteralText::Source(units)+ }+}+/// Unified escape sequence processor for both string and template literals.////// In `TaggedTemplate` mode, `failed` is true when an invalid escape isdiff --git a/Libraries/LibJS/Rust/src/parser/unit.rs b/Libraries/LibJS/Rust/src/parser/unit.rsindex 7a8bf1b9027..c23e6db0903 100644--- a/Libraries/LibJS/Rust/src/parser/unit.rs+++ b/Libraries/LibJS/Rust/src/parser/unit.rs@@ -12,11 +12,14 @@//!//! The source must be followed by at least 64 units of zero padding, so blocks may read past its end.+use crate::utf16::Utf16View;+pub trait Unit: Copy + Eq + 'static {const WIDE: bool;fn code(self) -> u32;/// Loads a block of units as bytes.unsafe fn load(p: *const Self) -> Block;+ fn view(units: &[Self]) -> Utf16View<'_>;}impl Unit for u8 {@@ -29,6 +32,10 @@ impl Unit for u8 {unsafe fn load(p: *const u8) -> Block {unsafe { block::load_narrow(p) }}+ #[inline(always)]+ fn view(units: &[u8]) -> Utf16View<'_> {+ Utf16View::Latin1(units)+ }}impl Unit for u16 {@@ -41,6 +48,10 @@ impl Unit for u16 {unsafe fn load(p: *const u16) -> Block {unsafe { block::load_wide(p) }}+ #[inline(always)]+ fn view(units: &[u16]) -> Utf16View<'_> {+ Utf16View::Utf16(units)+ }}#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]diff --git a/Tests/LibJS/Runtime/object-expression-__proto__.js b/Tests/LibJS/Runtime/object-expression-__proto__.jsindex bee5ccf99fd..55a7fdd8d4c 100644--- a/Tests/LibJS/Runtime/object-expression-__proto__.js+++ b/Tests/LibJS/Runtime/object-expression-__proto__.js@@ -4,3 +4,11 @@ test("__proto__ property", () => {expect(Object.getPrototypeOf({ "__proto__": Array.prototype })).toEqual(Array.prototype); // prettier-ignoreexpect(Object.getOwnPropertyNames({ __proto__: Array.prototype, test: 1 })).toEqual(["test"]);});++test("duplicate __proto__ properties are a syntax error however the key is spelled", () => {+ expect('({ __proto__: null, "__proto__": null })').not.toEval();+ expect('({ "\\x5f_proto__": null, __proto__: null })').not.toEval();+ expect('({ "\\u005f_proto__": null, "__proto__": null })').not.toEval();+ expect('({ "__proto__": null, ["__proto__"]: null })').toEval();+ expect("({ __proto__: null, __proto__ })").toEval();+});