@@ -495,6 +495,80 @@ describe('serialize( obj )', function () {
495495 strictEqual ( serialize ( new URL ( 'x:</script>' ) ) , 'new URL("x:\\u003C\\u002Fscript\\u003E")' ) ;
496496 strictEqual ( eval ( serialize ( new URL ( 'x:</script>' ) ) ) . href , 'x:</script>' ) ;
497497 } ) ;
498+
499+ it ( 'should encode unsafe HTML chars in function bodies' , function ( ) {
500+ function fn ( ) { return '</script>' ; }
501+ var serialized = serialize ( fn ) ;
502+ strictEqual ( serialized . includes ( '\\u003C\\u002Fscript\\u003E' ) , true ) ;
503+ strictEqual ( serialized . includes ( '</script>' ) , false ) ;
504+ // Verify the function still works after deserialization
505+ var deserialized ; eval ( 'deserialized = ' + serialized ) ;
506+ strictEqual ( typeof deserialized , 'function' ) ;
507+ strictEqual ( deserialized ( ) , '</script>' ) ;
508+ } ) ;
509+
510+ it ( 'should encode unsafe HTML chars in arrow function bodies' , function ( ) {
511+ var fn = ( ) => { return '</script>' ; } ;
512+ var serialized = serialize ( fn ) ;
513+ strictEqual ( serialized . includes ( '\\u003C\\u002Fscript\\u003E' ) , true ) ;
514+ strictEqual ( serialized . includes ( '</script>' ) , false ) ;
515+ // Verify the function still works after deserialization
516+ var deserialized ; eval ( 'deserialized = ' + serialized ) ;
517+ strictEqual ( typeof deserialized , 'function' ) ;
518+ strictEqual ( deserialized ( ) , '</script>' ) ;
519+ } ) ;
520+
521+ it ( 'should encode unsafe HTML chars in enhanced literal object methods' , function ( ) {
522+ var obj = {
523+ fn ( ) { return '</script>' ; }
524+ } ;
525+ var serialized = serialize ( obj ) ;
526+ strictEqual ( serialized . includes ( '\\u003C\\u002Fscript\\u003E' ) , true ) ;
527+ strictEqual ( serialized . includes ( '</script>' ) , false ) ;
528+ // Verify the function still works after deserialization
529+ var deserialized ; eval ( 'deserialized = ' + serialized ) ;
530+ strictEqual ( deserialized . fn ( ) , '</script>' ) ;
531+ } ) ;
532+
533+ it ( 'should not escape function bodies when unsafe option is true' , function ( ) {
534+ function fn ( ) { return '</script>' ; }
535+ var serialized = serialize ( fn , { unsafe : true } ) ;
536+ strictEqual ( serialized . includes ( '</script>' ) , true ) ;
537+ strictEqual ( serialized . includes ( '\\u003C\\u002Fscript\\u003E' ) , false ) ;
538+ } ) ;
539+
540+ it ( 'should encode </script > with space before >' , function ( ) {
541+ function fn ( ) { return '</script >' ; }
542+ var serialized = serialize ( fn ) ;
543+ strictEqual ( serialized . includes ( '\\u003C\\u002Fscript' ) , true ) ;
544+ strictEqual ( serialized . includes ( '</script ' ) , false ) ;
545+ // Verify the function still works after deserialization
546+ var deserialized ; eval ( 'deserialized = ' + serialized ) ;
547+ strictEqual ( typeof deserialized , 'function' ) ;
548+ strictEqual ( deserialized ( ) , '</script >' ) ;
549+ } ) ;
550+
551+ it ( 'should encode </script foo> with attributes' , function ( ) {
552+ function fn ( ) { return '</script foo>' ; }
553+ var serialized = serialize ( fn ) ;
554+ strictEqual ( serialized . includes ( '\\u003C\\u002Fscript' ) , true ) ;
555+ strictEqual ( serialized . includes ( '</script ' ) , false ) ;
556+ // Verify the function still works after deserialization
557+ var deserialized ; eval ( 'deserialized = ' + serialized ) ;
558+ strictEqual ( typeof deserialized , 'function' ) ;
559+ strictEqual ( deserialized ( ) , '</script foo>' ) ;
560+ } ) ;
561+
562+ it ( 'should encode </script with tab before >' , function ( ) {
563+ function fn ( ) { return '</script\t>' ; }
564+ var serialized = serialize ( fn ) ;
565+ strictEqual ( serialized . includes ( '\\u003C\\u002Fscript' ) , true ) ;
566+ strictEqual ( serialized . includes ( '</script' ) , false ) ;
567+ // Verify the function still works after deserialization
568+ var deserialized ; eval ( 'deserialized = ' + serialized ) ;
569+ strictEqual ( typeof deserialized , 'function' ) ;
570+ strictEqual ( deserialized ( ) , '</script\t>' ) ;
571+ } ) ;
498572 } ) ;
499573
500574 describe ( 'options' , function ( ) {
0 commit comments