Skip to content

Commit bcc037a

Browse files
domeniccodex
andcommitted
Honor script type and legacy event attributes
Strip only leading and trailing ASCII whitespace from script type attributes. JavaScript's trim() also removes non-ASCII whitespace, incorrectly allowing scripts with otherwise unrecognized types to execute. When both for and event attributes are present, restrict classic script execution to for="window" with event="onload" or event="onload()", using ASCII case-insensitive comparisons after stripping ASCII whitespace. Scripts with other combinations must not run. Follow https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element. Co-authored-by: codex <[email protected]>
1 parent faa5c4f commit bcc037a

2 files changed

Lines changed: 11 additions & 5 deletions

File tree

‎lib/jsdom/living/nodes/HTMLScriptElement-impl.js‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const { serializeURL } = require("whatwg-url");
77
const HTMLElementImpl = require("./HTMLElement-impl").implementation;
88
const reportException = require("../helpers/runtime-script-errors");
99
const { domSymbolTree } = require("../helpers/internal-constants");
10-
const { asciiLowercase } = require("../helpers/strings");
10+
const { asciiLowercase, stripLeadingAndTrailingASCIIWhitespace } = require("../helpers/strings");
1111
const { childTextContent } = require("../helpers/text");
1212
const { isShadowRoot } = require("../helpers/shadow-dom");
1313
const nodeTypes = require("../node-type");
@@ -177,6 +177,15 @@ class HTMLScriptElementImpl extends HTMLElementImpl {
177177

178178
// TODO: implement nomodule here, **but only after we support modules**.
179179

180+
// https://html.spec.whatwg.org/multipage/scripting.html#prepare-the-script-element
181+
if (this.hasAttributeNS(null, "event") && this.hasAttributeNS(null, "for")) {
182+
const forValue = asciiLowercase(stripLeadingAndTrailingASCIIWhitespace(this.getAttributeNS(null, "for")));
183+
const event = asciiLowercase(stripLeadingAndTrailingASCIIWhitespace(this.getAttributeNS(null, "event")));
184+
if (forValue !== "window" || (event !== "onload" && event !== "onload()")) {
185+
return;
186+
}
187+
}
188+
180189
// At this point we completely depart from the spec.
181190

182191
if (this.hasAttributeNS(null, "src")) {
@@ -209,7 +218,7 @@ class HTMLScriptElementImpl extends HTMLElementImpl {
209218
}
210219

211220
if (typeAttr !== null) {
212-
return typeAttr.trim();
221+
return stripLeadingAndTrailingASCIIWhitespace(typeAttr);
213222
}
214223

215224
if (langAttr !== null) {

‎test/web-platform-tests/to-run.yaml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,8 +2143,6 @@ muted-errors.sub.html: [fail, Muted errors not implemented]
21432143
nomodule-**: [timeout, Not implemented]
21442144
script-crossorigin-network.sub.html: [fail, fix https://github.com/zjffun/jsdom/commits/feature/crossorigin-attr]
21452145
script-crossorigin.html: [fail, Unknown]
2146-
script-for-event-xhtml.xhtml: [fail, Not implemented]
2147-
script-for-event.html: [fail, Not implemented]
21482146
script-not-executed-after-shutdown.html: [fail-slow, Unknown]
21492147
script-onerror-insertion-point-1.html: [timeout, Unknown]
21502148
script-onerror-insertion-point-2.html: [fail-slow, Unknown]
@@ -2154,7 +2152,6 @@ script-supports.html: [fail, Not implemented]
21542152
script-text-modifications-csp.html: [fail, Not implemented]
21552153
script-text-modifications.html: [fail, Unknown]
21562154
script-type-and-language-js-svg.svg: [fail, Our script execution timing is off; see discussion in https://github.com/jsdom/jsdom/pull/1406]
2157-
script-type-whitespace.html: [fail, Unknown]
21582155
text-module/**: [fail-slow, Unknown]
21592156

21602157
---

0 commit comments

Comments
 (0)