当前位置:首页 > 文章列表 > 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 <head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">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推荐
  • 讯飞AI大学堂免费AI认证证书:大模型工程师认证,提升您的职场竞争力
    免费AI认证证书
    科大讯飞AI大学堂推出免费大模型工程师认证,助力您掌握AI技能,提升职场竞争力。体系化学习,实战项目,权威认证,助您成为企业级大模型应用人才。
    12次使用
  • 茅茅虫AIGC检测:精准识别AI生成内容,保障学术诚信
    茅茅虫AIGC检测
    茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
    157次使用
  • 赛林匹克平台:科技赛事聚合,赋能AI、算力、量子计算创新
    赛林匹克平台(Challympics)
    探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
    187次使用
  • SEO  笔格AIPPT:AI智能PPT制作,免费生成,高效演示
    笔格AIPPT
    SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
    174次使用
  • 稿定PPT:在线AI演示设计,高效PPT制作工具
    稿定PPT
    告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
    161次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码