egg icon indicating copy to clipboard operation
egg copied to clipboard

egg body 解析bug

Open 1119302165 opened this issue 4 years ago • 5 comments

问题机器egg环境:

"egg": {
    "declarations": true
  },
  "dependencies": {
    "autocannon": "^7.9.0",
    "egg": "^2",
    "egg-redis": "^2.4.0",
    "egg-scripts": "^2",
    "tsc": "^2.0.4",
    "undici": "^5.7.0"
  },
  "devDependencies": {
    "autod": "^3",
    "autod-egg": "^1",
    "egg-bin": "^4",
    "egg-ci": "^2",
    "egg-mock": "^4",
    "eslint": "^8",
    "eslint-config-egg": "^12"
  },
  "engines": {
    "node": ">=16.0.0"
  },

问题复现: post body 的字符串内容为json {"path":"\n"} 传输过去的内容为 "{\"path\":\"\n\"}" 在解析这个内容出现json解析错误,在springboot环境可解析正常,问题应该出现在egg http body 解析这块逻辑,报错如下。

`2022-07-19 16:20:19,344 WARN 39558 [-/127.0.0.1/-/0ms POST /new] nodejs.SyntaxError: Unexpected token 
 in JSON at position 9, check bodyParser config
 in JSON at position 9, check bodyParser config
    at JSON.parse (<anonymous>)
    at parse (src/nodejs/release/node_modules/[email protected]@co-body/lib/json.js:57:17)
    at AsyncFunction.module.exports [as json] (src/nodejs/release/node_modules/[email protected]@co-body/lib/json.js:41:20)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async parseBody (src/nodejs/release/node_modules/[email protected]@koa-bodyparser/index.js:100:14)
    at async bodyParser (src/nodejs/release/node_modules/[email protected]@koa-bodyparser/index.js:85:19)
    at async src/nodejs/release/node_modules/[email protected]@koa-static-cache/index.js:39:63
    at async notfound (src/nodejs/release/node_modules/[email protected]@egg/app/middleware/notfound.js:5:5)
    at async meta (src/nodejs/release/node_modules/[email protected]@egg/app/middleware/meta.js:18:5)
status: 400
body: "{\"path\":\"\n\"}"
pid: 39558`

1119302165 avatar Jul 19 '22 08:07 1119302165

header 不对吧。堆栈信息都有了,自己 debug 看看啥原因呗。

从报错只看得出是解析失败,除非你给出可复现的仓库,不然我们也没法查

atian25 avatar Jul 19 '22 08:07 atian25

背景是: 原来的客户端都访问的是springboot后端,以前是正常,springboot是获取到content内容后,通过com.alibaba.fastjson 成功转为json对象。切换为egg实现好像有点问题。

springboot 接口处理逻辑:

    @RequestMapping(value = "/new")
    public Object addPath(@RequestBody String path){
        System.out.println("content:"+path);
        JSONObject pathObject = (JSONObject) JSON.parse(path);
        System.out.println("value:"+pathObject.get("path").toString()); 
        ....
}

springboot 处理打印日志结果:

2022-07-19 17:28:20.621  INFO 46143 --- [  XNIO-1 task-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
content:{"path":"
"}
value:

复现代码也比较简单,egg起了7001端口,路由增加了new路径。

client.js 完整代码如下:

const undici = require('undici');
const httpClientPool = new undici.Pool('http://127.0.0.1:7001', {
    connections: 50
});

function post(path, params) {
    return httpClientPool.request({
        path,
        headers: { "content-type": "application/json" },
        method: 'POST',
        body: params
    });
}

async function test() {
    let path = "\n";
    await post("/new", `{"path":"${path}"}`)   
}

test();

server controller 代码如下:


"use strict";

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

class HomeController extends Controller {

  async new() {
    const { ctx } = this;
    const {
      request: { body },
    } = this.ctx;
    var path = body.path;
    ctx.body = {
      result: true,
    };
  }
}

module.exports = HomeController;

1119302165 avatar Jul 19 '22 09:07 1119302165

- await post("/new", \`{"path":"${path}"}\`);
+ await post('/new', JSON.stringify({ path: '\n' }));

atian25 avatar Jul 19 '22 09:07 atian25

const path = "\n";  // <-- should be "\\n"
const a = `{"path":"${path}"}`;
JSON.parse(a);
{"path":"

SyntaxError: Unexpected token 
 in JSON at position 9
    at JSON.parse (<anonymous>)

atian25 avatar Jul 19 '22 10:07 atian25

。

1119302165 avatar Jul 19 '22 11:07 1119302165