当前位置:首页 > 文章列表 > Golang > Go问答 > chromedp 收到无效的 CSRF 令牌错误; Puppeteer 和浏览器都OK

chromedp 收到无效的 CSRF 令牌错误; Puppeteer 和浏览器都OK

来源:stackoverflow 2024-04-21 16:09:33 0浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《chromedp 收到无效的 CSRF 令牌错误; Puppeteer 和浏览器都OK》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我正在使用 chromedp 来测试我的基于 go 的网站。虽然我已经成功地使用它进行了基本的登录测试,但当我尝试注销刚刚登录的帐户时,我收到了 csrf 错误。

这是获取 csrf 错误的测试函数及其主要帮助程序。 httpserverurl 是正在运行的实时网络服务器或 httptest.server.url 的基本 url(无论哪种方式,我都会得到相同的 csrf 错误):

func testsignupduplicate(t *testing.t) {
    ctx, cancel := context.withtimeout(context.background(), 3*time.second)
    defer cancel()
    ctx, cancel = chromedp.newcontext(ctx) //   chromedp.withdebugf(log.printf),

    defer cancel()

    email := "[email protected]"
    password := "asdfasdf"

    signupwithcontext(ctx, t, email, password)

    defer func() {
        if err := usermanager.deletebyemail(email); err != nil {
            t.fatal(err)
        }
    }()

    var postsignoutclicklocationgot string
    postsignoutclicklocationexpected := httpserverurl + "/"
    if err := chromedp.run(ctx,
        chromedp.click("//button[@class='sign-out-form__button']"),
        chromedp.sleep(800*time.millisecond),
        chromedp.location(&postsignoutclicklocationgot),
    ); err != nil {
        t.fatal(err)
    }

    if postsignoutclicklocationgot != postsignoutclicklocationexpected {
        t.logf("expected to be redirected to <%s> after signing out, but was here instead: <%s>",
            postsignoutclicklocationexpected,
            postsignoutclicklocationgot,
        )
    }

    var location string
    var html string
    if err := chromedp.run(ctx,
        //chromedp.waitready("//footer"),
        chromedp.location(&location),
        chromedp.innerhtml("/html", &html),
    ); err != nil {
        t.fatalf("had trouble getting debug information: %s", err)
    }

    log.println(location)
    log.println(html)

    signupwithcontext(ctx, t, email, password)

    expectedalertheading := "e-mail address already in use"
    var gotalertheading string

    if err := chromedp.run(ctx,
        chromedp.text("//*[@class='alert__heading']", &gotalertheading),
    ); err != nil {
        t.fatalf("couldn’t get alert heading: %s", err)
    }

    if expectedalertheading != gotalertheading {
        t.fatalf("unexpected alert heading. want: «%s». got: «%s»", expectedalertheading, gotalertheading)
    }
}

func signupwithcontext(ctx context.context, t *testing.t, email, password string) {
    t.helper()

    if err := chromedp.run(ctx,
        chromedp.navigate(httpserverurl+"/signup/"),
        chromedp.waitvisible("#email", chromedp.byid),
        chromedp.sendkeys("#email", email, chromedp.byid),
        chromedp.sendkeys("#password", password, chromedp.byid),
        chromedp.submit("//button[@type='submit']"),
    ); err != nil {
        t.fatal(err)
    }
}

这是它的输出:

running tool: /usr/local/go/bin/go test -timeout 30s example.com/webdictions -run ^(testsignupduplicate)$

2019/07/05 15:26:02 http://127.0.0.1:53464/signout/
2019/07/05 15:26:02 
forbidden - csrf token invalid
--- fail: testsignupduplicate (3.01s) /users/comatoast/projects/predictionsweb/main_test.go:150: expected to be redirected to after signing out, but was here instead: /users/comatoast/projects/predictionsweb/main_test.go:177: couldn’t get alert heading: context deadline exceeded fail fail example.com/webdictions 3.073s

奇怪的是,puppeteer 程序不会出现这样的错误。最后,无论用户在开始测试之前是否已经拥有帐户,我都没有得到任何 csrf 错误的屏幕截图:

const puppeteer = require('puppeteer');

(async () => {
    const opts = {
        width: 800,
        height: 600,
        deviceScaleFactor: 2,
    }
    const browser = await puppeteer.launch({defaultViewport: opts});
    const page = await browser.newPage();
    await page.goto('http://www.localhost:3000/');
    await page.click("a[href='/signup/']");
    await page.type('#email', "[email protected]");
    await page.type('#password', 'asdfasdf');
    await page.click('[type="submit"]');
    await page.screenshot({path: '1. should be the dashboard after signup.png'});

    await page.click('.sign-out-form__button');
    await page.screenshot({path: '2. should be slash.png'});

    await page.click('a[href="/signup/"]')
    await page.screenshot({path: '3. signup again.png'});

    await page.type('#email', "[email protected]");
    await page.type('#password', 'asdfasdf');
    await page.click('[type="submit"]');

    await page.screenshot({path: '4. after second identical signup attempt.png'});

//    await page.screenshot({path: 'screenshot.png'});
    await browser.close();
})();

同样,当我尝试在 safari 或 chrome 中两次注册同一帐户时,我会收到正常的“此电子邮件地址已在使用中”错误,而不是 csrf 错误。如果有的话,我通过 chromedp 做错了什么?


解决方案


事实证明,在第二次访问“注册”页面时,我有一个一键“注销”表单, chromedp.Submit("//button[@type='submit']") 是点击。将 signUpWithContext 中的路径更改为明确的 chromedp.Submit("//form[@action='/signup/']//button[@type='submit']") 修复了单击提交按钮的问题格式错误。

理论要掌握,实操不能落!以上关于《chromedp 收到无效的 CSRF 令牌错误; Puppeteer 和浏览器都OK》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
使用 go 图像库从标准输出解码 bmp 图像使用 go 图像库从标准输出解码 bmp 图像
上一篇
使用 go 图像库从标准输出解码 bmp 图像
springboot2+es7怎么使用RestHighLevelClient
下一篇
springboot2+es7怎么使用RestHighLevelClient
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • SEO标题协启动:AI驱动的智能对话与内容生成平台 - 提升创作效率
    协启动
    SEO摘要协启动(XieQiDong Chatbot)是由深圳协启动传媒有限公司运营的AI智能服务平台,提供多模型支持的对话服务、文档处理和图像生成工具,旨在提升用户内容创作与信息处理效率。平台支持订阅制付费,适合个人及企业用户,满足日常聊天、文案生成、学习辅助等需求。
    2次使用
  • Brev AI:零注册门槛的全功能免费AI音乐创作平台
    Brev AI
    探索Brev AI,一个无需注册即可免费使用的AI音乐创作平台,提供多功能工具如音乐生成、去人声、歌词创作等,适用于内容创作、商业配乐和个人创作,满足您的音乐需求。
    2次使用
  • AI音乐实验室:一站式AI音乐创作平台,助力音乐创作
    AI音乐实验室
    AI音乐实验室(https://www.aimusiclab.cn/)是一款专注于AI音乐创作的平台,提供从作曲到分轨的全流程工具,降低音乐创作门槛。免费与付费结合,适用于音乐爱好者、独立音乐人及内容创作者,助力提升创作效率。
    2次使用
  • SEO标题PixPro:AI驱动网页端图像处理平台,提升效率的终极解决方案
    PixPro
    SEO摘要PixPro是一款专注于网页端AI图像处理的平台,提供高效、多功能的图像处理解决方案。通过AI擦除、扩图、抠图、裁切和压缩等功能,PixPro帮助开发者和企业实现“上传即处理”的智能化升级,适用于电商、社交媒体等高频图像处理场景。了解更多PixPro的核心功能和应用案例,提升您的图像处理效率。
    2次使用
  • EasyMusic.ai:零门槛AI音乐生成平台,专业级输出助力全场景创作
    EasyMusic
    EasyMusic.ai是一款面向全场景音乐创作需求的AI音乐生成平台,提供“零门槛创作 专业级输出”的服务。无论你是内容创作者、音乐人、游戏开发者还是教育工作者,都能通过EasyMusic.ai快速生成高品质音乐,满足短视频、游戏、广告、教育等多元需求。平台支持一键生成与深度定制,积累了超10万创作者,生成超100万首音乐作品,用户满意度达99%。
    3次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码