当前位置: > > > > chromedp 收到无效的 CSRF 令牌错误; Puppeteer 和浏览器都OK
来源:stackoverflow
2024-04-21 16:09:33
0浏览
收藏
在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 </pre></body> --- fail: testsignupduplicate (3.01s) /users/comatoast/projects/predictionsweb/main_test.go:150: expected to be redirected to <http://127.0.0.1:53464/> after signing out, but was here instead: <http://127.0.0.1:53464/signout/> /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》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注公众号吧!