关于 form post的问题
我看了文档可以使用2种方式提交 一种是隐藏域 另外一种是 url query
目前我只能使用 url query 能正确提交 使用隐藏域就提示 invalid csrf token
下面是我的form 如果去掉?_csrf={{ ctx.csrf | safe }} 就提示 invalid csrf token , 加上就没有问题?
请问下是不是我写法或是配置有什么问题?谢谢。
<form method="POST" action="/upload?_csrf={{ ctx.csrf | safe }}" enctype="multipart/form-data">
title: <input name="title"/>
file: <input name="file" type="file"/>
<p id="myDiv"></p>
<input type="text" name="_csrf"value="{{ ctx.csrf }}">
<button type="submit">upload</button>
<button type="button" id="b01">POST1 -->JSON</button>
<button type="button" id="b02">POST2 ajax 通过cookie获取csrf</button>
</form>
// config.defaults.js
security: {
xframe: {enable: true},
csrf: {
enable: true,
useSession: false, // if useSession set to true, the secret will keep in session instead of cookie
ignoreJSON: true, // skip check JSON requests if ignoreJSON set to true
cookieName: 'csrfToken', // csrf token's cookie name
sessionName: 'csrfToken', // csrf token's session name
headerName: 'x-csrf-token', // request csrf token's name in header
bodyName: '_csrf', // request csrf token's name in body
queryName: '_csrf', // request csrf token's name in query
},
}
<input type="text" name="_csrf"value="{{ ctx.csrf }}">
value 前面缺少空格
手误,但是和空格没有关系,我增加了一个空格 也是invalid csrf token,这个配置了config ,还有别的什么配置吗?
<input type="text" name="_csrf" value="{{ ctx.csrf }}">
那提供一个 demo 吧,看不出来
https://github.com/nxycdl/eggtest.git 直接访问home路由;谢谢了。 http://localhost:7001/home
我也碰到类似问题。
1:同步渲染的form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form method="POST" action="/upload" enctype="multipart/form-data">
title: <input name="title" />
file: <input name="file" type="file" />
<input type="hidden" name="_csrf" value="{{ ctx.csrf }}">
<button type="submit">upload</button>
</form>
</body>
</html>

2:传输的form-data

3:断点调试



<input type="text" name="_csrf"value="{{ ctx.csrf }}"> 放到 form 的最前面
<form method="POST" action="/upload" enctype="multipart/form-data">
<input type="text" name="_csrf"value="{{ ctx.csrf }}">
title: <input name="title"/>
file: <input name="file" type="file"/>
<p id="myDiv"></p>
<button type="submit">upload</button>
<button type="button" id="b01">POST1 -->JSON</button>
<button type="button" id="b02">POST2 ajax 通过cookie获取csrf</button>
</form>
谢谢!已经看到#1551
@fengmk2 试了一下将<input type="text" name="_csrf"value="{{ ctx.csrf }}">放在form最前面的情况,也是会抛出invalid csrf token的错误提示,直接用的是上面给的例子,不知道是有什么地方配置出错了?还请指出。
通过源码追踪,发现凡是将<input type="text" name="_csrf"value="{{ ctx.csrf }}">放在form表单里面的任何地方,在源码中下面的函数中:
get [INPUT_TOKEN]() {
const { headerName, bodyName, queryName } = this.app.config.security.csrf;
const token = findToken(this.query, queryName) || findToken(this.request.body, bodyName) ||
(headerName && this.get(headerName));
debug('get token %s, secret', token, this[CSRF_SECRET]);
return token;
},
对于url query的情况,this.query自然就是ctx.csrf的值,但是对于input type=file的情况(无论是否放在form最前面),this.request.body的值都是空对象

也就是最终得到的token值是个空字符串,也就无法满足源码中assertCsrf() 中的条件从而抛出错误
放到 url ,请看文档
@popomore 我知道放在url里面是可以的,只是对fengmk2说将input放在form的最前面也是可以的比较疑惑而已
Multipart 不行
formidable
参考实现