Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何关闭安全威胁csrf防范 #509

Closed
778477 opened this issue Mar 4, 2017 · 14 comments
Closed

如何关闭安全威胁csrf防范 #509

778477 opened this issue Mar 4, 2017 · 14 comments

Comments

@778477
Copy link

778477 commented Mar 4, 2017

本地通过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.

@atian25
Copy link
Member

atian25 commented Mar 4, 2017

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

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

@atian25 atian25 closed this as completed Mar 4, 2017
@dlyt
Copy link

dlyt commented Jul 28, 2017

看了上面的,我还以为是把下面的代码加到{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;
};

@wuyunjiang
Copy link

wuyunjiang commented Apr 20, 2018

可是加进去后直接访问不到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

'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;
};

@laoliang330
Copy link

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

@atian25
Copy link
Member

atian25 commented May 10, 2018

I guess you forgot to config the post router

@reuwi
Copy link

reuwi commented May 22, 2018

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

@ghost
Copy link

ghost commented Jul 2, 2018

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

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

@atian25
Copy link
Member

atian25 commented Jul 2, 2018

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

@Gala-1024
Copy link

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

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

@atian25
Copy link
Member

atian25 commented Sep 21, 2018

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

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

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

@kkkkkcnm
Copy link

@atian25

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

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

@atian25
Copy link
Member

atian25 commented Sep 27, 2018

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

@nightmareT
Copy link

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

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

@DearVikki
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants