将原始Cookie转换为木偶接受 [英] Converting a raw cookie to be accepted by puppeteer

查看:84
本文介绍了将原始Cookie转换为木偶接受的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将会话从passport.js传递到伪造者。



我使用了 var cookies = req.cookies; 以获取cookie值。



我在console.logging上,它是这样的:

  {'connect.sid':'s:qX4ZrttrjydtrjkgsdghsdghrewynZj4Ew2OUh.tTSILkcvgsegsegsegsrs99gmW5 
0XLcJefM'}
$ p b
$ b

我认为将这个值传递给伪造者很容易。



但是伪造者有一个page.setCookie()函数,其格式是这样:

  page.setCookie(... cookies)
... cookies< ...对象>
名称< string>必需的
值< string>必需的
url< string>
域< string>
path< string>
到期< number> Unix时间,以秒为单位。
httpOnly< boolean>
secure< boolean>
sameSite< string> 严格或宽松。
返回:< Promise>

那么我如何传递伪造我的cookie?



也许是我原始cookie上的那个大字符串,该值传递给对象伪造者接受吗?

解决方案

如果您使用的是MacOS(不幸的是,我目前无法发表评论),我正在寻找答案这个问题也解决了很长时间..答案是使用您的MacOS用户凭据将cookie值加密到SQL DB表中,您首先需要使用可以访问您的钥匙串的某种东西解密(当然需要您的许可)



解密后,第2部分)会将其转换为Puppeteer可以接受的东西。



NPM软件包可以执行前者,但不能执行后者( https://github.com/bertrandom/ chrome-cookies-secure ),但是我已经对其进行了处理,因此它现在确实会生成Puppeteer re适用于任何给定Chrome配置文件的ady cookie。有一个PR请求( https://github.com/bertrandom / chrome-cookies-secure / pull / 14 )-尚未接受-但我已经在一个实时应用程序上运行了一个月。



添加NPM软件包,用于 chrome-cookies-secure 到您的应用程序,您将一路顺风。



创建Puppeteer对象的基本操作如下(您应该能够看到index.js对打开的PR的更改以及README.MD中的示例):

  //'cookie'是对象的数组,其密钥是用解密的SQL DB中编写的

let puppeteerCookies = [];

cookies.forEach(function(cookie,index){
const puppeteerCookieObject = {
名称:cookie.name,
值:cookie.value,
到期:cookie.expires_utc,
域:cookie.host_key,
路径:cookie.path
}
if(cookie.is_secure){
puppeteerCookieObject ['Secure '] = true
}
if(cookie.is_httponly){
puppeteerCookieObject ['HttpOnly'] = true
}
puppeteerCookies.push(puppeteerCookieObject)
})

return puppeteerCookies;

希望这会有所帮助。


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?

解决方案

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.

这篇关于将原始Cookie转换为木偶接受的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆