Skip to content

Commit b954b91

Browse files
committed
🔒️ fix CVE-2011-4969
1 parent ee931ed commit b954b91

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ var jQuery = window.jQuery = window.$ = function( selector, context ) {
2020
};
2121

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

2626
// Is it a simple selector
2727
isSimple = /^.[^:#\[\.]*$/,
@@ -54,13 +54,13 @@ jQuery.fn = jQuery.prototype = {
5454

5555
// HANDLE: $("#id")
5656
else {
57-
var elem = document.getElementById( match[3] );
57+
var elem = document.getElementById( match[2] );
5858

5959
// Make sure an element was located
6060
if ( elem ){
6161
// Handle the case where IE and Opera return items
6262
// by name instead of ID
63-
if ( elem.id != match[3] )
63+
if ( elem.id != match[2] )
6464
return jQuery().find( selector );
6565

6666
// Otherwise, we inject the element directly into the jQuery object

test/unit/core.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,29 @@ test("$('html', context)", function() {
226226
equals($span.length, 1, "Verify a span created with a div context works, #1763");
227227
});
228228

229+
test("XSS via location.hash", function() {
230+
expect(1);
231+
232+
stop();
233+
jQuery._check9521 = function(x){
234+
ok( x, "script called from #id-like selector with inline handler" );
235+
jQuery("#check9521").remove();
236+
delete jQuery._check9521;
237+
};
238+
239+
var $eCheck9521 = jQuery( '#<img id="check9521" src="no-such-.gif" onerror="jQuery._check9521(false)">' );
240+
241+
if($eCheck9521.length) {
242+
$eCheck9521.appendTo("#main");
243+
}
244+
else {
245+
jQuery._check9521(true);
246+
}
247+
248+
start();
249+
250+
});
251+
229252
if ( !isLocal ) {
230253
test("$(selector, xml).text(str) - Loaded via XML document", function() {
231254
expect(2);

0 commit comments

Comments
 (0)