File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -294,6 +294,18 @@ describe('Set cookie', () => {
294294 } ) . toThrowError ( 'path must not contain ";", "\\r", or "\\n"' )
295295 } )
296296
297+ it ( 'Should throw Error for invalid cookie name' , ( ) => {
298+ expect ( ( ) => {
299+ serialize ( 'legit\r\nX-Injected: evil' , 'value' )
300+ } ) . toThrowError ( 'Invalid cookie name' )
301+ expect ( ( ) => {
302+ serialize ( 'bad;name' , 'value' )
303+ } ) . toThrowError ( 'Invalid cookie name' )
304+ expect ( ( ) => {
305+ serialize ( 'bad=name' , 'value' )
306+ } ) . toThrowError ( 'Invalid cookie name' )
307+ } )
308+
297309 it ( 'Should serialize cookie with lowercase priority values' , ( ) => {
298310 const lowSerialized = serialize ( 'test_cookie' , 'value' , {
299311 priority : 'low' ,
Original file line number Diff line number Diff line change @@ -139,6 +139,10 @@ export const parseSigned = async (
139139}
140140
141141const _serialize = ( name : string , value : string , opt : CookieOptions = { } ) : string => {
142+ if ( ! validCookieNameRegEx . test ( name ) ) {
143+ throw new Error ( 'Invalid cookie name' )
144+ }
145+
142146 let cookie = `${ name } =${ value } `
143147
144148 if ( name . startsWith ( '__Secure-' ) && ! opt . secure ) {
You can’t perform that action at this time.
0 commit comments