pillarjs / cookies

Signed and unsigned cookies based on Keygrip
MIT License
1.29k stars 152 forks source link

Throwing error "argument value is invalid" for Indic languages #124

Closed AjayPoshak closed 4 years ago

AjayPoshak commented 4 years ago

We support Indic languages as as value in our cookies. But it is breaking during setting cookies via this package. Value of cookie we're passing is

{ 
  regionName: 'കൊച്ചി',
  regionCode: 'MUMBAI',
  subName: 'കൊച്ചി',
  subCode: 'MWEST',
}

Here is the error log

TypeError: argument value is invalid
    at new Cookie (/Users/ajayposhak/Sites/M6/site/node_modules/cookies/lib/cookies.js:117:11)
    at Cookies.set (/Users/ajayposhak/Sites/M6/site/node_modules/cookies/lib/cookies.js:80:16)
    at Object._default (/Users/ajayposhak/Sites/M6/site/src/server/middlewares/ExtractWebViewParams.js:71:54)
    at _default.next (<anonymous>)
    at onFulfilled (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:65:19)
    at /Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:54:5
    at new Promise (<anonymous>)
    at Object.co (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:50:10)
    at Object.toPromise (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:118:63)
    at next (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:99:29)
    at onFulfilled (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:69:7)
    at /Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:54:5
    at new Promise (<anonymous>)
    at Object.co (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:50:10)
    at Object.toPromise (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:118:63)
    at next (/Users/ajayposhak/Sites/M6/site/node_modules/co/index.js:99:29)
AjayPoshak commented 4 years ago

I think this regex is causing the issue: https://github.com/pillarjs/cookies/blob/fac05de8dfa77d722dac5237f0d8c54b6e0222ac/index.js#L23

dougwilson commented 4 years ago

Yes, this is a limitation of HTTP headers; Node.js would just throw later if this package did not. You need to encode your value in some of your own choosing. Perhaps using encodeURI or base64.