Egg: docs: security中ignoreJSON的描述问题

Created on 2 Aug 2017  ·  8Comments  ·  Source: eggjs/egg

  • Node Version:
    v6.11.1
  • Egg Version:
    1.6.1
  • Plugin Name:
  • Plugin Version:
  • Platform:
  • Mini Showcase Repository:

文档中对 security 的 ignoreJSON 描述

// config/config.default.js
module.exports = {
  security: {
    csrf: {
      ignoreJSON: true, // 默认为 false,当设置为 true 时,将会放过所有 content-type 为 `application/json` 的请求
    },
  },
};

是不准确的。
从代码上看,最终判断请求是否是“JSON请求”使用的是type-is库,而这个库在判断的时候会优先判断请求体是否为空
https://github.com/jshttp/type-is#each-type-can-be

function typeofrequest (req, types_) {
  var types = types_

  // no body
  if (!hasbody(req)) {
    return null
  }
  ...
}

按照这个逻辑,如果服务端接收到一个空请求体的delete请求,就会报csrf 错误
image

至于 type-is 为何要这样判断不太清楚。
感觉这块的 json 判断逻辑可以修改,只按照content-type来判断。
或者先把文档修改清晰了,避免踩坑。

egg-security bug

Most helpful comment

我觉得可以换判断方式,在这个场景下不需要判断 body 是否存在

All 8 comments

是不是用 fetch 发的 delete 请求,我记得有 bug,可能下面两个请求头都没有

function hasbody (req) {
  return req.headers['transfer-encoding'] !== undefined ||
    !isNaN(req.headers['content-length'])
}

@popomore 用vue的vue-resource发的

@Yuliang-Lee 可以截一下这个时候发送的请求头么?

@dead-horse 请求头里是没有这两个字段
image

我觉得可以换判断方式,在这个场景下不需要判断 body 是否存在

cc @jaredleechn

+1 撒花

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zhaofinger picture zhaofinger  ·  3Comments

Leungkingman picture Leungkingman  ·  3Comments

dizhifeng picture dizhifeng  ·  3Comments

bupafengyu picture bupafengyu  ·  3Comments

whlsxl picture whlsxl  ·  3Comments