Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when parsing <constructor> element #3709

Open
daniellockyer opened this issue May 6, 2024 · 0 comments
Open

TypeError when parsing <constructor> element #3709

daniellockyer opened this issue May 6, 2024 · 0 comments

Comments

@daniellockyer
Copy link

Basic info:

  • Node.js version: v18
  • jsdom version: 23 & 24

General

We use jsdom within Ghost and noticed that some inputs are erroring out within jsdom. After investigation, it seems to be due to the use of <constructor> within the body.

Minimal reproduction case

const { JSDOM } = require("jsdom");

let dom = new JSDOM(`<constructor>`);

I get this stack trace:

..../node_modules/jsdom/lib/jsdom/living/helpers/create-element.js:277
    result = elementInterface.createImpl(_globalObject, [], {
                              ^

TypeError: Cannot read properties of undefined (reading 'createImpl')
    at createElement (..../node_modules/jsdom/lib/jsdom/living/helpers/create-element.js:277:31)
    at JSDOMParse5Adapter.createElement (..../node_modules/jsdom/lib/jsdom/browser/parser/html.js:78:21)
    at Parser._insertElement (..../node_modules/parse5/dist/cjs/parser/index.js:267:42)
    at genericStartTagInBody (..../node_modules/parse5/dist/cjs/parser/index.js:1854:7)
    at startTagInBody (..../node_modules/parse5/dist/cjs/parser/index.js:2068:13)
    at Parser._startTagOutsideForeignContent (..../node_modules/parse5/dist/cjs/parser/index.js:823:17)
    at Parser._processStartTag (..../node_modules/parse5/dist/cjs/parser/index.js:793:18)
    at Parser.onStartTag (..../node_modules/parse5/dist/cjs/parser/index.js:773:14)
    at Tokenizer.emitCurrentTagToken (..../node_modules/parse5/dist/cjs/tokenizer/index.js:393:26)
    at Tokenizer._stateTagName (..../node_modules/parse5/dist/cjs/tokenizer/index.js:1084:22)

Node.js v18.20.2

Whereas using an element of a different name, <abc>, works.

How does similar code behave in browsers?

I would expect the code not to crash on this element 🙂

daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
fix https://linear.app/tryghost/issue/SLO-87/cannot-read-properties-of-undefined-reading-createimpl-an-unexpected
refs jsdom/jsdom#3709

- in the event we are given some HTML to parse, and that fails, we
  currently return a HTTP 500 because it's unhandled
- the instance we saw was due to `<constructor>` crashing jsdom, we've
  opened an issue for that
- in terms of handling the error gracefully, we can surround the code
  in a try-catch and return a more suitable error. I've gone for a
  ValidationError for now - you could debate whether a different one is
  more appropriate
- also added Sentry error capturing so we're not blind to these,
  ultimately we should make sure the parser can handle all
  user-submitted data
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
fix https://linear.app/tryghost/issue/SLO-87/cannot-read-properties-of-undefined-reading-createimpl-an-unexpected
refs jsdom/jsdom#3709

- in the event we are given some HTML to parse, and that fails, we
  currently return a HTTP 500 because it's unhandled
- the instance we saw was due to `<constructor>` crashing jsdom, we've
  opened an issue for that
- in terms of handling the error gracefully, we can surround the code
  in a try-catch and return a more suitable error. I've gone for a
  ValidationError for now - you could debate whether a different one is
  more appropriate
- also added Sentry error capturing so we're not blind to these,
  ultimately we should make sure the parser can handle all
  user-submitted data
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
fix https://linear.app/tryghost/issue/SLO-87/cannot-read-properties-of-undefined-reading-createimpl-an-unexpected
refs jsdom/jsdom#3709

- in the event we are given some HTML to parse, and that fails, we
  currently return a HTTP 500 because it's unhandled
- the instance we saw was due to `<constructor>` crashing jsdom, we've
  opened an issue for that
- in terms of handling the error gracefully, we can surround the code
  in a try-catch and return a more suitable error. I've gone for a
  ValidationError for now - you could debate whether a different one is
  more appropriate
- also added Sentry error capturing so we're not blind to these,
  ultimately we should make sure the parser can handle all
  user-submitted data
daniellockyer added a commit to TryGhost/Ghost that referenced this issue May 7, 2024
fix https://linear.app/tryghost/issue/SLO-87/cannot-read-properties-of-undefined-reading-createimpl-an-unexpected
refs jsdom/jsdom#3709

- in the event we are given some HTML to parse, and that fails, we
  currently return a HTTP 500 because it's unhandled
- the instance we saw was due to `<constructor>` crashing jsdom, we've
  opened an issue for that
- in terms of handling the error gracefully, we can surround the code
  in a try-catch and return a more suitable error. I've gone for a
  ValidationError for now - you could debate whether a different one is
  more appropriate
- also added Sentry error capturing so we're not blind to these,
  ultimately we should make sure the parser can handle all
  user-submitted data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant