Skip to content

Commit 67970b0

Browse files
committed
Bug 1806964 - Restrict SVG <use> to prevent usage of data: URLs. r=longsonr
Differential Revision: https://phabricator.services.mozilla.com/D193414
1 parent b24a3dd commit 67970b0

File tree

10 files changed

+72
-11
lines changed

10 files changed

+72
-11
lines changed

dom/base/crashtests/crashtests.list

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ load 637214-1.svg
110110
load 637214-2.svg
111111
pref(extensions.InstallTrigger.enabled,true) pref(extensions.InstallTriggerImpl.enabled,true) load 642022-1.html
112112
load 646184.html
113-
load 658845-1.svg
113+
pref(svg.use-element.data-url-href.allowed,true) load 658845-1.svg
114114
load 666869.html
115115
load 667336-1.html
116116
load 675516.xhtml

dom/svg/SVGUseElement.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,16 +543,30 @@ void SVGUseElement::LookupHref() {
543543
return;
544544
}
545545

546-
nsCOMPtr<nsIURI> originURI =
547-
mOriginal ? mOriginal->GetBaseURI() : GetBaseURI();
548-
nsCOMPtr<nsIURI> baseURI =
549-
nsContentUtils::IsLocalRefURL(href)
550-
? SVGObserverUtils::GetBaseURLForLocalRef(this, originURI)
551-
: originURI;
546+
if (nsContentUtils::IsLocalRefURL(href)) {
547+
// Use the original <use>, if it exists, because the #ref might be local
548+
// the original's document.
549+
RefPtr<SVGUseElement> elem = mOriginal ? mOriginal.get() : this;
550+
RefPtr<nsAtom> idAtom = NS_AtomizeMainThread(Substring(href, 1));
551+
mReferencedElementTracker.ResetWithID(*elem, idAtom);
552+
return;
553+
}
552554

555+
nsCOMPtr<nsIURI> baseURI = mOriginal ? mOriginal->GetBaseURI() : GetBaseURI();
553556
nsCOMPtr<nsIURI> targetURI;
554557
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI), href,
555558
GetComposedDoc(), baseURI);
559+
if (!targetURI) {
560+
return;
561+
}
562+
563+
// Don't allow <use href="data:...">. Using "#ref" inside a data: document is
564+
// handled above.
565+
if (targetURI->SchemeIs("data") &&
566+
!StaticPrefs::svg_use_element_data_url_href_allowed()) {
567+
return;
568+
}
569+
556570
nsIReferrerInfo* referrer =
557571
OwnerDoc()->ReferrerInfoForInternalCSSAndSVGResources();
558572
mReferencedElementTracker.ResetToURIFragmentID(this, targetURI, referrer);

layout/reftests/svg/reftest.list

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ pref(layout.css.devPixelsPerPx,"1.0") == svg-blurry-with-subpixel-position.html
564564
== use-02.svg pass.svg
565565
== use-01-extref.svg pass.svg
566566
== use-02-extref.svg use-02-extref-ref.svg
567-
== use-extref-dataURI-01.svg pass.svg
567+
pref(svg.use-element.data-url-href.allowed,true) == use-extref-dataURI-01.svg pass.svg
568568
== use-children.svg pass.svg
569569

570570
test-pref(svg.use-element.graphics-element-restrictions,0) == use-restrictions.svg use-restrictions-not-restricted-ref.svg

layout/svg/crashtests/crashtests.list

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ load 709920-2.svg
128128
load 713413-1.svg
129129
load 722003-1.svg
130130
load 725918-1.svg
131-
load 732836-1.svg
131+
pref(svg.use-element.data-url-href.allowed,true) load 732836-1.svg
132132
load 740627-1.svg
133133
load 740627-2.svg
134134
load 743469.svg

modules/libpref/init/StaticPrefList.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14386,6 +14386,12 @@
1438614386
value: 8
1438714387
mirror: always
1438814388

14389+
# Whether <svg:use> with a data: URL as href is allowed
14390+
- name: svg.use-element.data-url-href.allowed
14391+
type: bool
14392+
value: false
14393+
mirror: always
14394+
1438914395
#---------------------------------------------------------------------------
1439014396
# Prefs starting with "telemetry."
1439114397
#---------------------------------------------------------------------------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
prefs: [svg.use-element.data-url-href.allowed:false]

testing/web-platform/meta/svg/struct/reftests/use-data-url.tentative.svg.ini

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!doctype HTML>
2+
<meta charset="utf-8">
3+
<title>Testcase for changing a valid &#x3c;use&#x3e; to a data URL:</title>
4+
<link rel="help" href="https://svgwg.org/svg2-draft/struct.html#UseElementHrefAttribute">
5+
<link rel="match" href="reference/green-100x100.html">
6+
<script>
7+
function go() {
8+
const use = document.querySelector("use");
9+
use.setAttribute("href", "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogIDxyZWN0IGlkPSJvcmFuZ2UtcmVjdCIgd2lkdGg9IjEwMCIgaGVpZ2h0PSIxMDAiIGZpbGw9Im9yYW5nZSIvPgo8L3N2Zz4=#orange-rect");
10+
}
11+
</script>
12+
<body onload="go()">
13+
<svg id="mySVG">
14+
<rect id="red" width="100" height="100" fill="red"></rect>
15+
<rect id="green" width="100" height="100" fill="green"></rect>
16+
<use x="100" y="0" href="#red"></use>
17+
</svg>
18+
</body>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype HTML>
2+
<meta charset="utf-8">
3+
<title>Testcase for where SVG loaded via data: uses #ref</title>
4+
<link rel="help" href="https://svgwg.org/svg2-draft/struct.html#UseElementHrefAttribute">
5+
<link rel="match" href="reference/green-100x100.html">
6+
<body>
7+
<!-- base64 SVG source:
8+
<svg xmlns="http://www.w3.org/2000/svg">
9+
<rect id="green-rect" width="100" height="100" fill="green"/>
10+
<rect width="100" height="100" fill="red"/>
11+
<use href="#green-rect"/>
12+
</svg>
13+
-->
14+
<img src="data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KICA8cmVjdCBpZD0iZ3JlZW4tcmVjdCIgd2lkdGg9IjEwMCIgaGVpZ2h0PSIxMDAiIGZpbGw9ImdyZWVuIi8+DQogIDxyZWN0IHdpZHRoPSIxMDAiIGhlaWdodD0iMTAwIiBmaWxsPSJyZWQiLz4NCiAgPHVzZSBocmVmPSIjZ3JlZW4tcmVjdCIvPg0KPC9zdmc+">
15+
</body>

0 commit comments

Comments
 (0)