Skip to content

\p{…} accepts Object.prototype names such as \p{constructor} #71

Closed

Description

@ajmtrz

\p{…} / \P{…} names are looked up in LazyMap (src/UnicodeTables.js) with key in this.initializer and this.initializer[key] on a plain object literal, so names inherited from Object.prototype are treated as Unicode classes. RE2 (and an unknown name such as \p{NoSuchClass}) gives a parse error instead.

Reproduction (re2js 2.9.1, Node 26)

import { RE2JS } from 're2js'

for (const p of ['\\p{NoSuchClass}', '\\p{constructor}', '\\p{isPrototypeOf}', '\\P{constructor}',
                 '\\p{toString}', '\\p{valueOf}', '\\p{hasOwnProperty}', '\\p{__proto__}']) {
  try { console.log(p, 'compiled; test("abc") =', RE2JS.compile(p).test('abc')) }
  catch (e) { console.log(p, `${e.constructor.name}: ${e.message}`) }
}

Output:

\p{NoSuchClass}   RE2JSSyntaxException: error parsing regexp: invalid character class range: `\p{NoSuchClass}`
\p{constructor}   compiled; test("abc") = false
\p{isPrototypeOf} compiled; test("abc") = false
\P{constructor}   compiled; test("abc") = true
\p{toString}      TypeError: table.getLo is not a function
\p{valueOf}       TypeError: Cannot convert undefined or null to object
\p{hasOwnProperty} TypeError: Cannot convert undefined or null to object
\p{__proto__}     TypeError: fn is not a function

Expected

All of them should throw RE2JSSyntaxException (invalid character class range), as \p{NoSuchClass} does and as RE2 does.

Impact

A pattern with such a name either compiles and silently matches nothing (or everything, for \P{…}), or fails with an internal TypeError instead of a syntax error. This matters when patterns come from configuration and a compile error is the signal that a pattern is invalid.

Possible fix

Use own-property checks in LazyMap, e.g.

has(key) {
  return Object.hasOwn(this.initializer, key)
}
get(key) {
  if (this.cache.has(key)) return this.cache.get(key)
  const fn = Object.hasOwn(this.initializer, key) ? this.initializer[key] : undefined
  const val = fn ? fn() : null
  this.cache.set(key, val)
  return val
}

(or create the initializer objects with a null prototype).

Activity

  1. added a commit that references this issue on Nov 10, 2026
  2. le0pard commented on Nov 10, 2026

    @le0pard
    Owner

    Fixed and published in 2.10.0 Please check

  3. ajmtrz commented on Nov 10, 2026

    @ajmtrz
    Author

    Confirmed fixed in 2.10.0: all of \p{constructor}, \p{isPrototypeOf}, \P{constructor}, \p{toString}, \p{valueOf}, \p{hasOwnProperty}, \p{__proto__} and [\p{constructor}] now throw RE2JSSyntaxException: invalid character class range, the same as \p{NoSuchClass}. Thanks for the quick fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions