我有一个Express 4 应用程序,它使csurf用户在 API 路由上进行 CSRF 保护。该应用程序运行良好,CSRF 保护确实在没有csrf-token
标头的请求会给出适当的错误的情况下工作。
我使用Ava进行测试,使用supertest测试路线。启用 CSRF 检查时以下测试失败,但如果我删除中间件则通过:
test('booking api no auth', async t => {
t.plan(4)
const server = await request(makeServer(t.context.config, t.context.connection))
const csrf = await server
.get('/')
.then(res => new JSDOM(res.text))
.then(dom => dom.window.document.querySelector('meta[name="csrf_token"]'))
.then(csrfMeta => csrfMeta.getAttribute('content'))
const GET = await server
.get('/v2/Booking')
.set('csrf-token', csrf)
const POST = await server
.post('/v2/Booking')
.set('csrf-token', csrf)
.send({
name: 'Test',
description: 'Test',
category: 'diving',
minimumPax: 1,
maximumPax: 2,
priceAdult: 1,
priceChild: 1
})
const res = { GET, POST }
t.is(res.GET.status, 403)
t.deepEqual(res.GET.body, text['403'])
t.is(res.POST.status, 201)
t.truthy(res.POST.body._id)
})
我已经验证了请求中确实设置了标头。任何对可行的替代库的想法或建议表示赞赏。