Egg: 如何关闭安全威胁csrf防范

Created on 4 Mar 2017  ·  14Comments  ·  Source: eggjs/egg

本地通过curl模拟POST请求

提示

2017-03-04 20:15:12,969 WARN 11913 [-/::1/-/36ms POST /watchFiles] missing csrf token. See https://eggjs.org/zh-cn/core/security.html#安全威胁csrf的防范
2017-03-04 20:15:12,974 WARN 11913 [-/::1/-/40ms POST /watchFiles] nodejs.ForbiddenError: missing csrf token
    at Object.throw (/Users/guomiaoyou/work/egg/dumplings/node_modules/koa/lib/context.js:91:23)
    at Object.assertCsrf (/Users/guomiaoyou/work/egg/dumplings/node_modules/egg-security/app/extend/context.js:104:17)
    at Object.csrf (/Users/guomiaoyou/work/egg/dumplings/node_modules/egg-security/lib/middlewares/csrf.js:30:10)
    at next (native)
    at Object.<anonymous> (/Users/guomiaoyou/work/egg/dumplings/node_modules/koa-compose/index.js:28:19)
    at next (native)
    at onFulfilled (/Users/guomiaoyou/work/egg/dumplings/node_modules/co/index.js:65:19)
    at /Users/guomiaoyou/work/egg/dumplings/node_modules/co/index.js:54:5
    at Object.co (/Users/guomiaoyou/work/egg/dumplings/node_modules/co/index.js:50:10)
    at Object.toPromise (/Users/guomiaoyou/work/egg/dumplings/node_modules/co/index.js:118:63)
message: 'missing csrf token'
pid: 11913
hostname: guomiaoyoudeMacBook-Pro.local

如何关闭安全威胁csrf防范?

配置{app_root}/config/plugin.js 如下:

exports.nunjucks = {
    enable: true,
    package: 'egg-view-nunjucks'
};

exports.csrf = {
  enable:false
};

not work.

Most helpful comment

https://eggjs.org/zh-cn/faq.html#为什么会有-csrf-报错

// config/config.default.js
module.exports = {
  security: {
    csrf: {
      enable: false,
    },
  },
};

All 14 comments

https://eggjs.org/zh-cn/faq.html#为什么会有-csrf-报错

// config/config.default.js
module.exports = {
  security: {
    csrf: {
      enable: false,
    },
  },
};

看了上面的,我还以为是把下面的代码加到{app_root}/config/plugin.js

module.exports = {
  security: {
    csrf: {
      enable: false,
    },
  },
};

结果并不是,看了#562才明白在哪改。
config.default.js中修改

module.exports = appInfo => {
  const config = {};

  // should change to your own
  config.keys = appInfo.name + '';

  // add your config here
  config.security = {
    csrf: {
      enable: false,
    },
  };
  return config;
};

可是加进去后直接访问不到post地址了,直接返回{"message":"Not Found"}
router.js

'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
  router.post('/postTest', controller.post.index);
};
````
/controller/post.js
```javascript
'use strict';

const Controller = require('egg').Controller;

class TestController extends Controller {
  async index(data) {
     console.log(data)
     this.ctx.body = 'tets';
  }
}

module.exports = TestController;

/config.default.js

'use strict';

module.exports = appInfo => {
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1524018239453_7529';

  // add your config here
  config.middleware = [];

  config.security = {
    csrf: {
      enable: false,
    },
  };

  return config;
};

我测试的时候怎么报404呢?同一个路由,接收改成get就ok,改成post就报404!

I guess you forgot to config the post router

@atian25 请问首次请求是post请求,客户端肯定是没有csrf token的,所以肯定会报错,这个问题应该怎么解决呢?

@gaoshijun1993 正常用户访问肯定会先发起一个 GET 请求获取页面。直接发 POST 请求,可以认为是恶意请求了。

如果真有这种需求,做个类似 GET /init-csrf 的路由,用来拿 CSRF Token 就行了。

如果能通过 ajax 获取的话,那还不如直接关掉。。。

我测试的时候怎么报404呢?同一个路由,接收改成get就ok,改成post就报404!

你没返回内容吧,在控制器里面加上试试this.ctx.body = {
a: 11
}

我测试的时候怎么报404呢?同一个路由,接收改成get就ok,改成post就报404!

你没返回内容吧,在控制器里面加上试试this.ctx.body = {
a: 11
}

他很大可能是没有配置路由

@atian25

    router.post('/form', app.controller.post.index);

这样不就是已经定义路由了吗?请问你说的配置是什么意思

提供最小可复现仓库再讨论

我测试的时候怎么报404呢?同一个路由,接收改成get就ok,改成post就报404!

我也遇到类似情况,暂时发现post请求不带参数时可以正常返回,加了参数就会404 ,断点显示请求到了后端但是只到中间件没有跑到路由里面

一般这种情况是代码里有地方报错了…或者之前的配置没有热更新成功。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

weijiatan456 picture weijiatan456  ·  3Comments

Azard picture Azard  ·  3Comments

lvgg3271 picture lvgg3271  ·  3Comments

Leungkingman picture Leungkingman  ·  3Comments

bupafengyu picture bupafengyu  ·  3Comments