Skip to content

Commit db9e023

Browse files
committed
Merge pull request jquery#474 from dmethvin/fix-9521-xss-hash
Fixes #9521. Prioritize #id over <tag> to avoid XSS via location.hash.
2 parents 84f2908 + 749dbad commit db9e023

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ var jQuery = function( selector, context ) {
1616
rootjQuery,
1717

1818
// A simple way to check for HTML strings or ID strings
19-
// (both of which we optimize for)
20-
quickExpr = /^(?:[^<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
19+
// Prioritize #id over <tag> to avoid XSS via location.hash (#9521)
20+
quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/,
2121

2222
// Check if a string has a non-whitespace character in it
2323
rnotwhite = /\S/,

test/unit/core.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,24 @@ test("isXMLDoc - HTML", function() {
467467
document.body.removeChild( iframe );
468468
});
469469

470+
test("XSS via location.hash", function() {
471+
expect(1);
472+
473+
stop();
474+
jQuery._check9521 = function(x){
475+
ok( x, "script called from #id-like selector with inline handler" );
476+
jQuery("#check9521").remove();
477+
delete jQuery._check9521;
478+
start();
479+
};
480+
try {
481+
// This throws an error because it's processed like an id
482+
jQuery( '#<img id="check9521" src="no-such-.gif" onerror="jQuery._check9521(false)">' ).appendTo("#qunit-fixture");
483+
} catch (err) {
484+
jQuery._check9521(true);
485+
};
486+
});
487+
470488
if ( !isLocal ) {
471489
test("isXMLDoc - XML", function() {
472490
expect(3);

0 commit comments

Comments
 (0)