Skip to content

Commit 062b1fb

Browse files
authored
fix(sanitize-html): apply allowedSchemesByTag to srcset URLs (#5576)
1 parent cf9ec7a commit 062b1fb

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sanitize-html": patch
3+
---
4+
5+
`allowedSchemesByTag` is now applied to `srcset` and `imagesrcset` URLs. Previously the per-tag lookup used the attribute name instead of the tag name, so these attributes always fell back to the global `allowedSchemes` and ignored a tag-specific scheme allowlist.

‎packages/sanitize-html/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ function sanitizeHtml(html, options, _recursing) {
478478
try {
479479
let parsed = parseSrcset(value);
480480
parsed.forEach(function(value) {
481-
if (naughtyHref(a, value.url)) {
481+
if (naughtyHref(name, value.url)) {
482482
value.evil = true;
483483
}
484484
});

‎packages/sanitize-html/test/test.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,17 @@ describe('sanitizeHtml', function() {
10251025
'<img src="fallback.jpg" srcset="/upload/f_auto,q_auto:eco,c_fit,w_1460,h_2191/abc.jpg 1460w, /upload/f_auto,q_auto:eco,c_fit,w_1360,h_2041/abc.jpg" />'
10261026
);
10271027
});
1028+
it('should apply allowedSchemesByTag restrictions to srcset URLs', function() {
1029+
assert.equal(
1030+
sanitizeHtml('<img src="https://example.com/fallback.jpg" srcset="https://example.com/foo.jpg 100w" />', {
1031+
allowedTags: sanitizeHtml.defaults.allowedTags.concat([ 'img' ]),
1032+
allowedAttributes: { img: [ 'src', 'srcset' ] },
1033+
allowedSchemes: [ 'http', 'https' ],
1034+
allowedSchemesByTag: { img: [] }
1035+
}),
1036+
'<img />'
1037+
);
1038+
});
10281039
it('should drop javascript: in form action', function() {
10291040
assert.equal(
10301041
sanitizeHtml('<form action="javascript:alert(1)"><button>x</button></form>', {

0 commit comments

Comments
 (0)