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

HTML: forbid data: and javascript: URLs in the <base> element #41731

Merged
merged 3 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions html/semantics/document-metadata/the-base-element/base-data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!-- Please update base-javascript.html together with this -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does having a comment instead of doctype on the first line put this into quirks mode?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it doesn't. In IE6 it did.

<!DOCTYPE html>
<meta charset="utf-8">
<title>&lt;base> and data: URLs</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<base href="data:/,test">
<base href="https://example.com/">
<div id=log></div>
<script>
test(() => {
const link = document.createElement("a");
link.href = "blah";
assert_equals(link.href, new URL("blah", document.URL).href);
}, "First <base> has a data: URL so fallback is used");

test(() => {
document.querySelector("base").remove();
const link = document.createElement("a");
link.href = "blah";
assert_equals(link.href, new URL("blah", "https://example.com/").href);
}, "First <base> is removed so second is used");

test(() => {
const base = document.createElement("base");
base.href = "data:/,more-test";
document.head.prepend(base);
const link = document.createElement("a");
link.href = "blah";
assert_equals(link.href, new URL("blah", document.URL).href);
}, "Dynamically inserted first <base> has a data: URL so fallback is used");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!-- Please update base-data.html together with this -->
<!DOCTYPE html>
<meta charset="utf-8">
<title>&lt;base> and javascript: URLs</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<base href="javascript:/,test">
<base href="https://example.com/">
<div id=log></div>
<script>
test(() => {
const link = document.createElement("a");
link.href = "blah";
assert_equals(link.href, new URL("blah", document.URL).href);
}, "First <base> has a javascript: URL so fallback is used");

test(() => {
document.querySelector("base").remove();
const link = document.createElement("a");
link.href = "blah";
assert_equals(link.href, new URL("blah", "https://example.com/").href);
}, "First <base> is removed so second is used");

test(() => {
const base = document.createElement("base");
base.href = "javascript:/,more-test";
document.head.prepend(base);
const link = document.createElement("a");
link.href = "blah";
assert_equals(link.href, new URL("blah", document.URL).href);
}, "Dynamically inserted first <base> has a javascript: URL so fallback is used");
</script>
4 changes: 4 additions & 0 deletions url/resources/a-element-origin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function runURLTests(urlTests) {
if (expected.base === null && expected.input.startsWith("#"))
continue;

// HTML special cases data: and javascript: URLs in <base>
if (expected.base !== null && (expected.base.startsWith("data:") || expected.base.startsWith("javascript:")))
continue;

// We cannot use a null base for HTML tests
const base = expected.base === null ? "about:blank" : expected.base;

Expand Down
4 changes: 4 additions & 0 deletions url/resources/a-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function runURLTests(urlTests) {
if (expected.relativeTo === "any-base")
continue;

// HTML special cases data: and javascript: URLs in <base>
if (expected.base !== null && (expected.base.startsWith("data:") || expected.base.startsWith("javascript:")))
continue;

// We cannot use a null base for HTML tests
const base = expected.base === null ? "about:blank" : expected.base;

Expand Down