Skip to content

Commit

Permalink
Widget: Don't let widget name affect $.ui prototype & constructor
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mgol authored Oct 30, 2024
1 parent 85bed8d commit d591bdd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/unit/widget/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ QUnit.test( "error handling", function( assert ) {
$.error = error;
} );

QUnit.test( "Prototype pollution", function( assert ) {
assert.expect( 3 );

var elem = $( "<div>" );

$.widget( "ui.testWidget", {} );

elem.testWidget();

try {
$.widget( "ui.__proto__", {} );
} catch ( _e ) {}
try {
$.widget( "ui.constructor", {} );
} catch ( _e ) {}

assert.strictEqual( Object.getPrototypeOf( $.ui ), Object.prototype,
"$.ui constructor not modified" );
assert.ok( $.ui instanceof Object, "$.ui is an Object instance" );
assert.notOk( $.ui instanceof Function, "$.ui is not a Function instance" );
} );

QUnit.test( "merge multiple option arguments", function( assert ) {
assert.expect( 1 );
$.widget( "ui.testWidget", {
Expand Down
3 changes: 3 additions & 0 deletions ui/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ $.widget = function( name, base, prototype ) {

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

if ( !prototype ) {
Expand Down

0 comments on commit d591bdd

Please sign in to comment.