Skip to content

Commit 8228b77

Browse files
mikewestChromium LUCI CQ
authored andcommitted
[Origin API] .from() should throw on <a> and <area>.
When extracting an origin from `<a>` and `<area>` elements without an `href` attribute, we're currently returning an opaque `Origin`. We should throw instead, as there's not an origin to extract from these elements (see [1]). Thanks to @annevk for pointing this out in [2]. [1]: https://html.spec.whatwg.org/multipage/links.html#api-for-a-and-area-elements:extract-an-origin [2]: whatwg/url#892 (comment) Bug: 434131026 Change-Id: I136cc0ff24355e29418e060ab384938f3615a60e Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7231346 Commit-Queue: Mike West <[email protected]> Reviewed-by: Antonio Sartori <[email protected]> Cr-Commit-Position: refs/heads/main@{#1554582}
1 parent 6d4aeea commit 8228b77

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

third_party/blink/renderer/core/html/html_anchor_element.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,10 @@ void HTMLAnchorElementBase::SetURL(const KURL& url) {
380380
DOMOrigin* HTMLAnchorElementBase::GetDOMOrigin(LocalDOMWindow*) const {
381381
// No access check is necessary, as anchor elements are not accessible
382382
// cross-origin.
383-
return DOMOrigin::Create(SecurityOrigin::Create(Url()));
383+
if (FastHasAttribute(html_names::kHrefAttr)) {
384+
return DOMOrigin::Create(SecurityOrigin::Create(Url()));
385+
}
386+
return nullptr;
384387
}
385388

386389
String HTMLAnchorElementBase::Input() const {

third_party/blink/web_tests/external/wpt/html/browsers/origin/api/origin-from-htmlhyperlinkelementutils.window.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
// META: title=`Origin.from(HTMLHyperlinkElementUtils)`
22
// META: script=resources/serializations.js
33

4+
test(t => {
5+
const invalid = document.createElement("a");
6+
assert_throws_js(TypeError, _ => Origin.from(invalid));
7+
}, `Origin.from(<a>) throws.`);
8+
9+
test(t => {
10+
const invalid = document.createElement("area");
11+
assert_throws_js(TypeError, _ => Origin.from(invalid));
12+
}, `Origin.from(<area>) throws.`);
13+
414
for (const opaque of urls.opaque) {
515
// <a>
616
test(t => {

0 commit comments

Comments
 (0)