Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
mgol committed Oct 28, 2024
commit 177a35d2136d3dc415f281cf5bf2b55dc62831c4
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