Skip to content

Commit

Permalink
capture: fix possible error of invalid custom element name
Browse files Browse the repository at this point in the history
  • Loading branch information
danny0838 committed Sep 14, 2024
1 parent a5bb2ed commit 5859ede
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/capturer/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@
['origin-when-crossorigin', 'origin-when-cross-origin'],
]);

const CUSTOM_ELEMENT_NAME_PATTERN = /^[a-z](.+)-(.+)$/;

// ref: https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
const CUSTOM_ELEMENT_NAME_FORBIDDEN = new Set([
"annotation-xml",
"color-profile",
"font-face",
"font-face-src",
"font-face-uri",
"font-face-format",
"font-face-name",
"missing-glyph",
]);

const REWRITABLE_SPECIAL_OBJECTS = new Set([false, 'adoptedStyleSheet']);

const REMOVE_HIDDEN_EXCLUDE_HTML = new Set(["html", "head", "title", "meta", "link", "style", "script", "body", "noscript", "template", "source", "track"]);
Expand Down Expand Up @@ -2770,7 +2784,7 @@
// record custom elements
{
const nodeName = elem.nodeName.toLowerCase();
if (nodeName.includes('-')) {
if (CUSTOM_ELEMENT_NAME_PATTERN.test(nodeName) && !CUSTOM_ELEMENT_NAME_FORBIDDEN.has(nodeName)) {
customElementNames.add(nodeName);
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/t/capture_custom_elements/bad.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test bad custom elements</title>
</head>
<body>
<blockquote></blockquote>
<blockquote><annotation-xml>something</annotation-xml></blockquote>
<blockquote><color-profile>something</color-profile></blockquote>
<blockquote><font-face>something</font-face></blockquote>
<blockquote><font-face-src>something</font-face-src></blockquote>
<blockquote><font-face-uri>something</font-face-uri></blockquote>
<blockquote><font-face-format>something</font-face-format></blockquote>
<blockquote><font-face-name>something</font-face-name></blockquote>
<blockquote><missing-glyph>something</missing-glyph></blockquote>
</body>
<script>
var wrapper = document.querySelectorAll('blockquote')[0];
var elem = wrapper.appendChild(document.createElement('_bad-elem'));
elem.textContent = 'something';
</script>
</html>
17 changes: 17 additions & 0 deletions test/test_capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -13552,6 +13552,23 @@ it('test_capture_custom_elements', async function () {
assert(value.match(rawRegex`${'^'}(function (names) {${'.+'}})(["custom-subelem","custom-elem"])${'$'}`));
});

it('test_capture_custom_elements_bad', async function () {
/* capture.script = remove */
var options = {
"capture.script": "remove",
};
var blob = await capture({
url: `${localhost}/capture_custom_elements/bad.html`,
options: Object.assign({}, baseOptions, options),
});

var zip = await new JSZip().loadAsync(blob);
var indexFile = zip.file('index.html');
var indexBlob = new Blob([await indexFile.async('blob')], {type: "text/html"});
var doc = await readFileAsDocument(indexBlob);
assert(!doc.querySelector(`script[data-scrapbook-elem="custom-elements-loader"]`));
});

/**
* Check if option works
*
Expand Down

0 comments on commit 5859ede

Please sign in to comment.