You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How can I differentiate a tampered signed cookie from a false JSON cookie? #168
Forewarning: I am extremely new to Node.js and NPM.
From reading the documentation of cookieParser, it appears that cookies that are signed but fail validation are assigned false in the req.signedCookies object.
Signed cookies that fail signature validation will have the value false instead of the tampered value.
Also, cookies prefixed with j: are automatically parsed with JSON.parse.
In addition, this module supports special "JSON cookies". These are cookie where the value is prefixed with j:. When these values are encountered, the value will be exposed as the result of JSON.parse.
However, the string 'false' is valid JSON, so it should get parsed to false. I also found no option to disable the built-in JSON parser. How can I differentiate a valid signed j:'false' cookie from an invalid signed cookie?
If this is not possible, I request that this be relabeled to a bug report or feature request.
It is not a policy. JSONCookies uses if (val) after JSON.parse, so anything falsy is treated as "not JSON" and the raw j:... string is kept.
JSONCookie already returns undefined on a missing j: prefix or a parse error. The truthy check was a cheap way to skip those two cases. It also skips false, null, 0, and "".
So boolean false on req.signedCookies is still only a failed signature. A real JSON false stays 'j:false'. Same for j:null / j:0 / j:"".
Activity
official-burak commentedon Aug 26, 2026
Treat boolean
falseas a bad signature. A valid signed JSONfalsenever becomes booleanfalse.JSONCookiesonly overwrites whenJSON.parsereturns a truthy value (if (val)inindex.js). Soj:falsestays the string'j:false'onreq.signedCookies.Same skip happens for
j:null,j:0, andj:"". There is no flag to turn JSON cookies off.the-can-of-soup commentedon Aug 27, 2026
That seems quite arbitrary. Why are falsy JSON values ignored?
official-burak commentedon Aug 31, 2026
It is not a policy.
JSONCookiesusesif (val)afterJSON.parse, so anything falsy is treated as "not JSON" and the rawj:...string is kept.JSONCookiealready returnsundefinedon a missingj:prefix or a parse error. The truthy check was a cheap way to skip those two cases. It also skipsfalse,null,0, and"".So boolean
falseonreq.signedCookiesis still only a failed signature. A real JSONfalsestays'j:false'. Same forj:null/j:0/j:"".the-can-of-soup commentedon Aug 31, 2026
Sorry, but I understand what you meant, I just don't understand the reasoning.
official-burak commentedon Aug 31, 2026
There isn't one.
if (val)is just a sloppy truthy check. Parse errors stay strings, and false/null/0/""get the same treatment by accident.fix: keep boolean false reserved for the signature-failure marker