Skip to content

Commit

Permalink
HTML: forbid data: and javascript: URLs in the <base> element
Browse files Browse the repository at this point in the history
  • Loading branch information
annevk authored and pull[bot] committed Oct 6, 2023
1 parent 1dcb7f0 commit 2477746
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
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 -->
<!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

0 comments on commit 2477746

Please sign in to comment.