@@ -315,6 +315,23 @@ describe('serialize( obj )', function () {
315315 strictEqual ( typeof serialize ( re ) , 'string' ) ;
316316 strictEqual ( serialize ( re ) , 'new RegExp("[\\u003C\\\\\\u002Fscript\\u003E\\u003Cscript\\u003Ealert(\'xss\')\\\\\\u002F\\\\\\u002F]", "")' ) ;
317317 } ) ;
318+
319+ it ( 'should sanitize RegExp.flags to prevent code injection' , function ( ) {
320+ // Object that passes instanceof RegExp with attacker-controlled .flags
321+ var fakeRegex = Object . create ( RegExp . prototype ) ;
322+ Object . defineProperty ( fakeRegex , 'source' , { get : function ( ) { return 'x' ; } } ) ;
323+ Object . defineProperty ( fakeRegex , 'flags' , {
324+ get : function ( ) { return '"+(global.__INJECTED_FLAGS="pwned")+"' ; }
325+ } ) ;
326+ fakeRegex . toJSON = function ( ) { return '@placeholder' ; } ;
327+ var output = serialize ( { re : fakeRegex } ) ;
328+ // Malicious flags must be stripped; only valid flag chars allowed
329+ strictEqual ( output . includes ( '__INJECTED_FLAGS' ) , false ) ;
330+ strictEqual ( output . includes ( 'pwned' ) , false ) ;
331+ var obj = eval ( 'obj = ' + output ) ;
332+ strictEqual ( global . __INJECTED_FLAGS , undefined ) ;
333+ delete global . __INJECTED_FLAGS ;
334+ } ) ;
318335 } ) ;
319336
320337 describe ( 'dates' , function ( ) {
@@ -345,6 +362,16 @@ describe('serialize( obj )', function () {
345362 strictEqual ( typeof serialize ( { t : [ d ] } ) , 'string' ) ;
346363 strictEqual ( serialize ( { t : [ d ] } ) , '{"t":[{"foo":new Date("2016-04-28T22:02:17.156Z")}]}' ) ;
347364 } ) ;
365+
366+ it ( 'should reject invalid Date ISO string to prevent code injection' , function ( ) {
367+ var fakeDate = Object . create ( Date . prototype ) ;
368+ fakeDate . toISOString = function ( ) { return '"+(global.__INJECTED_DATE="pwned")+"' ; } ;
369+ fakeDate . toJSON = function ( ) { return '2024-01-01' ; } ;
370+ throws ( function ( ) {
371+ serialize ( { d : fakeDate } ) ;
372+ } , TypeError ) ;
373+ strictEqual ( global . __INJECTED_DATE , undefined ) ;
374+ } ) ;
348375 } ) ;
349376
350377 describe ( 'maps' , function ( ) {
0 commit comments