Skip to content

Commit d591bdd

Browse files
authored
Widget: Don't let widget name affect $.ui prototype & constructor
This is an edge case and it only affects code accepting untrusted input as a widget name, but it's still technically correct to filter these out. Closes gh-2310
1 parent 85bed8d commit d591bdd

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

tests/unit/widget/core.js

+22
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,28 @@ QUnit.test( "error handling", function( assert ) {
242242
$.error = error;
243243
} );
244244

245+
QUnit.test( "Prototype pollution", function( assert ) {
246+
assert.expect( 3 );
247+
248+
var elem = $( "<div>" );
249+
250+
$.widget( "ui.testWidget", {} );
251+
252+
elem.testWidget();
253+
254+
try {
255+
$.widget( "ui.__proto__", {} );
256+
} catch ( _e ) {}
257+
try {
258+
$.widget( "ui.constructor", {} );
259+
} catch ( _e ) {}
260+
261+
assert.strictEqual( Object.getPrototypeOf( $.ui ), Object.prototype,
262+
"$.ui constructor not modified" );
263+
assert.ok( $.ui instanceof Object, "$.ui is an Object instance" );
264+
assert.notOk( $.ui instanceof Function, "$.ui is not a Function instance" );
265+
} );
266+
245267
QUnit.test( "merge multiple option arguments", function( assert ) {
246268
assert.expect( 1 );
247269
$.widget( "ui.testWidget", {

ui/widget.js

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ $.widget = function( name, base, prototype ) {
5656

5757
var namespace = name.split( "." )[ 0 ];
5858
name = name.split( "." )[ 1 ];
59+
if ( name === "__proto__" || name === "constructor" ) {
60+
return $.error( "Invalid widget name: " + name );
61+
}
5962
var fullName = namespace + "-" + name;
6063

6164
if ( !prototype ) {

0 commit comments

Comments
 (0)