egg icon indicating copy to clipboard operation
egg copied to clipboard

关于 form post的问题

Open nxycdl opened this issue 9 years ago • 12 comments

我看了文档可以使用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
            },
        }

nxycdl avatar Oct 19 '17 02:10 nxycdl

<input type="text" name="_csrf"value="{{ ctx.csrf }}">

value 前面缺少空格

popomore avatar Oct 19 '17 03:10 popomore

手误,但是和空格没有关系,我增加了一个空格 也是invalid csrf token,这个配置了config ,还有别的什么配置吗?

<input type="text" name="_csrf" value="{{ ctx.csrf }}">

nxycdl avatar Oct 19 '17 03:10 nxycdl

那提供一个 demo 吧,看不出来

popomore avatar Oct 19 '17 03:10 popomore

https://github.com/nxycdl/eggtest.git 直接访问home路由;谢谢了。 http://localhost:7001/home

nxycdl avatar Oct 19 '17 04:10 nxycdl

我也碰到类似问题。

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>

image

2:传输的form-data image

3:断点调试 image

image

image

xiaoshuimao avatar Oct 19 '17 08:10 xiaoshuimao

<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>

fengmk2 avatar Oct 19 '17 09:10 fengmk2

谢谢!已经看到#1551

nxycdl avatar Oct 20 '17 02:10 nxycdl

@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的值都是空对象

images

也就是最终得到的token值是个空字符串,也就无法满足源码中assertCsrf() 中的条件从而抛出错误

SunShinewyf avatar Oct 29 '17 13:10 SunShinewyf

放到 url ,请看文档

popomore avatar Oct 29 '17 14:10 popomore

@popomore 我知道放在url里面是可以的,只是对fengmk2说将input放在form的最前面也是可以的比较疑惑而已

SunShinewyf avatar Oct 29 '17 14:10 SunShinewyf

Multipart 不行

popomore avatar Oct 29 '17 16:10 popomore

formidable 参考实现

ghlailq avatar Jan 16 '18 01:01 ghlailq