egg-security icon indicating copy to clipboard operation
egg-security copied to clipboard

egg-security中通过中间件设置一些安全头对egg-static不起作用,看起来是egg-static的中间件先执行直接响应了body, 导致 egg-security的中间件没机会执行,这个顺序有办法调整么?

Open WormGirl opened this issue 1 year ago • 10 comments

WormGirl avatar Jun 09 '23 07:06 WormGirl

对,static 中间件比 security 要早加载,可能是一个问题。

fengmk2 avatar Jun 09 '23 08:06 fengmk2

对,static 中间件比 security 要早加载,可能是一个问题。

这个考虑后续版本调整这个顺序么

WormGirl avatar Jun 09 '23 08:06 WormGirl

是需要对 html 还是 js 附加的安全头? 前者的话,不推荐用 egg-static 托管 html,应该用 egg-view,通过 controller 去渲染一个 html, 这样就会经过 security 了。

atian25 avatar Jun 09 '23 08:06 atian25

是需要对 html 还是 js 附加的安全头? 前者的话,不推荐用 egg-static 托管 html,应该用 egg-view,通过 controller 去渲染一个 html, 这样就会经过 security 了。

html用了egg-view没有问题 主要是js和css这些静态资源 内部有一些安全扫描会扫到js文件那些请求缺失一些安全策略的头,项目原因无法使用cdn

WormGirl avatar Jun 09 '23 08:06 WormGirl

可以 PR 改下 https://github.com/eggjs/egg-static/blob/master/app.js

可能得加个配置开关, cc @fengmk2 怎么看?

atian25 avatar Jun 20 '23 01:06 atian25

可以 PR 改下 https://github.com/eggjs/egg-static/blob/master/app.js

可能得加个配置开关, cc @fengmk2 怎么看?

可以加个开关,改默认顺序我担心有 breaking change

fengmk2 avatar Jun 20 '23 01:06 fengmk2

这个问题怎么说 现在egg-staic下的页面请求不过egg-security,这些请求都有csrf的风险

WormGirl avatar Jul 24 '23 03:07 WormGirl

大概什么时候能处理 现在安全问题抓的紧 有啥临时方案调整这两中间件顺序么

WormGirl avatar Jul 24 '23 03:07 WormGirl

这样写是可以调整顺序的

 configDidLoad() {
    const { app } = this;
    const index0 = app.config.coreMiddleware.indexOf('bodyParser');
    const index1 = app.config.coreMiddleware.indexOf('securities');
    const arr = app.config.coreMiddleware.slice(index0 + 1, index1 + 1);
    app.config.coreMiddleware.splice(index0 + 1, arr.length);
    const index2 = app.config.coreMiddleware.indexOf('static');
    app.config.coreMiddleware.splice(index2, 0, ...arr);
  }

WormGirl avatar Nov 17 '23 06:11 WormGirl

我也遇到了这个问题。 用egg-static渲染的页面,这时候cookie中还没有crsfToken,直接提交post请求时,就会提示‘nodejs.ForbiddenError: invalid csrf token’。 我是通过:页面加载完成后,先发送一个get请求。这样能解决这个问题

svenjia avatar Jan 24 '24 02:01 svenjia