1

I want to pass session from passport.js to puppeteer.

I used var cookies = req.cookies; in order to get the cookies value.

I console.logged it and it is something like this:

{ 'connect.sid': 's:qX4ZrttrjydtrjkgsdghsdghrewynZj4Ew2OUh.tTSILkcvgsegsegsegsr99gmW5
0XLcJefM' }

I thought that it would be as easy as to pass this value to puppeteer.

But puppeteer has a page.setCookie() function that its format is this:

page.setCookie(...cookies)
...cookies <...Object>
name <string> required
value <string> required
url <string>
domain <string>
path <string>
expires <number> Unix time in seconds.
httpOnly <boolean>
secure <boolean>
sameSite <string> "Strict" or "Lax".
returns: <Promise>

So how do i pass puppeteer my cookie? Is there a way to convert it from its raw value to the one that puppeteer accepts?

Maybe is that large string on my raw cookie, the value to pass to the object that puppeteer accepts?

user1584421
  • 3,499
  • 11
  • 46
  • 86
  • Possible duplicate of [How does Connect's session middleware's signed cookies work?](https://stackoverflow.com/questions/14843960/how-does-connects-session-middlewares-signed-cookies-work) – laggingreflex May 18 '18 at 00:15
  • I don't think it's a duplicate of that question. The objective and the means to get there are completely different. – user1584421 May 18 '18 at 10:58
  • Is it possible to convert my raw cookie to all these fields that puppeteer requires? Given i know the cookie and the secret.. – user1584421 May 18 '18 at 18:50

1 Answers1

1

If you are on MacOS (unfortunately I cannot currently comment to ask), I was searching for an answer to this problem for quite a while too.. and the answer is the cookie value is encrypted with your MacOS user credentials into an SQL DB table, which you need to first decrypt with something that can access your keychain (with your permission of course).

Once that is decrypted, part 2) is converting that into something Puppeteer will accept.

There's an NPM package that does the former, but not the latter (https://github.com/bertrandom/chrome-cookies-secure), but I've worked on it so that it does now generate Puppeteer ready cookies for any given Chrome profile. There is a PR request in for it (https://github.com/bertrandom/chrome-cookies-secure/pull/14) - not yet accepted - but I have had it running on a live app for a month.

Add the NPM Package for chrome-cookies-secure to your application and you'll be on your way.

The nuts and bolts of creating the Puppeteer object are as follows (you should be able to see the changes in index.js to the open PR, plus example in the README.MD):

// 'cookie' is an array of Objects with keys as written in decrypted SQL DB

let puppeteerCookies = [];

cookies.forEach(function(cookie, index) {
    const puppeteerCookieObject = {
        name: cookie.name,
        value: cookie.value,
        expires: cookie.expires_utc,
        domain: cookie.host_key,
        path: cookie.path
    }
    if (cookie.is_secure) {
        puppeteerCookieObject['Secure'] = true
    }
    if (cookie.is_httponly) {
        puppeteerCookieObject['HttpOnly'] = true
    }
    puppeteerCookies.push(puppeteerCookieObject)
})

return puppeteerCookies;

Hope this helps.

Reece Daniels
  • 1,147
  • 12
  • 16
  • The PR for this how now been accepted. Install the NPM package and use the 'Puppeteer' variable to retrieve cookies in a Puppeteer ready state. – Reece Daniels May 17 '19 at 13:33