question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

cookieParser.signedCookie seems to not work properly

See original GitHub issue

Hello everyone. I’m trying to “decode” a signed cookie I receive in a websocket session. First, here is how I setup my express session with cookie-parser:

static.ts file

import * as cookieParser from 'cookie-parser';
import * as express from 'express';
import * as session from 'express-session';

const app = express();

app.use(cookieParser('foobarbaz1234567foobarbaz1234567'));
app.use(session({
  cookie: {
    httpOnly: true,
    maxAge: 600000, // 3600000 for 1 hour & 600000 for 10 minutes
    secure: process.env.VSCodeDebug && process.env.VSCodeDebug === 'true' ? false : true,
  },
  genid: () => genuuid(),
  resave: false,
  saveUninitialized: false,
  secret: 'foobarbaz1234567foobarbaz1234567',
  store: mystore,
}));

Somewhere in my code, when I receive a websocket connection, I’m able to get the signed cookie. But I want to decode it in order to have the session ID stored inside.

This is what I’m trying to do:

image

As you can see, i’m using the same secret password with “signedCookie” than in the cookieParser initialization. The result of signedCookie returns everytime the same string as the signed cookie (temp3 value in my current example). And according to your documentation: image it kinda says that the signature is invalid or something like that. Is it possible that I’m missing something in my approch ? In my database where the cookie is stored, I can see the decoded cookie (i.e the session ID). So I guess that something looks wrong with “signedCookie” function (or maybe with my approch).

You can see bellow what I have in my request parameter. Also, bellow, you can see what I observe when I’m trying to user signedCookie function. The output of this function is the same as the input (signedSession). And the secret is the same as in the cookie-parser configuration.

Here is what I have in my webSocketSingleton.ts file import * as cookieParser from 'cookie-parser'; image

Here is what I observe when using the signedCookie function

image

Here is the content of the “req” parameter

image

image

Can you help me please to resolve this issue ? Thank you in advance for your answer

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
CaioStoduto-zzcommented, Oct 2, 2020

Nowadays you need to decode the cookie value using decodeURIComponent() before using any of the following functions:

  1. cookieParser.JSONCookie
  2. cookieParser.JSONCookies (decodeURIComponent for each cookie)
  3. cookieParser.signedCookie
  4. cookieParser.signedCookies (decodeURIComponent for each cookie)

Because it doesn’t decode itself inside the function, now they just check if it starts with ‘j:’ or ‘s:’ (before, when this issue was created, it required an encodedURI and it would check if it starts with ‘j%3A’, for JSONCookie, or ‘s%3A’, for signedCookie, and then both of these functions would decode the input to continue their codes)

these prints are from the actual expressjs/cookie-parser source code image image

Conclusion: You need to use decodeURIComponent() before using JSONCookie(), JSONCookies(), signedCookie() or signedCookies() otherwise it will return the input value.

Example:

var cookieParser = require('cookie-parser')
const signedCookieValue = 's%3Accc.4qKyaFIB4mq9fpZViqe1L1hiHbbGfRTZDZHhFtTvI10' // FROM res.cookie('bbbbb', 'ccc', {signed: true})

const decodedSignedCookieValue = decodeURIComponent(signedCookieValue) // RESULT  s:ccc.4qKyaFIB4mq9fpZViqe1L1hiHbbGfRTZDZHhFtTvI10

// CORRECT WAY
cookieParser.signedCookie(decodedSignedCookieValue, 'SECRET') //RESULT ccc

// INCORRECT WAY
cookieParser.signedCookie(signedCookieValue, 'SECRET') //RESULT s%3Accc.4qKyaFIB4mq9fpZViqe1L1hiHbbGfRTZDZHhFtTvI10
1reaction
dougwilsoncommented, Feb 1, 2019

Taking the cookie from the last screenshot (please if you could send as text, as it look a really long time to type it out correctly 😃 ), here is the flow of unsigning the cookie, as an example:

$ node -pe 'var cookie = "connect.sid=s%3A1bdf23c0-9c30-93df-5147-930ece4f2f2b.Mx1bO5zIKawNmWtEZshwHG7BY%2BVCikhaUvrqWsY3TRU"; var sid = require("cookie").parse(cookie)["connect.sid"]; require("cookie-parser").signedCookie(sid, "foobarbaz1234567foobarbaz1234567")'
1bdf23c0-9c30-93df-5147-930ece4f2f2b

Basically:

(1) Parse the cookie header and get connect.sid value (2) Pass to signedCookie with signature

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connect signed cookie parsing falsy - Stack Overflow
I looked into source for parse function and found out that it calls unsign method which gets a substring of encoded value and...
Read more >
Express Cookie-Parser - Signed and Unsigned Cookies
– This method parses the cookie as a signed cookie. If the cookie is a signed cookie and signature can be validated, then...
Read more >
Cookies | NestJS - A progressive Node.js framework
Cookies. An HTTP cookie is a small piece of data stored by the user's browser. Cookies were designed to be a reliable mechanism...
Read more >
Express cookie-parser middleware
Parse a cookie value as a signed cookie. This will return the parsed unsigned value if it was a signed cookie and the...
Read more >
Working with Cookies in Node + Express using cookie-parser
However, as it comes to cookies, it does not contain built-in functionality for accessing them within the headers of a request or response....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found