expressjs / body-parser

Node.js body parsing middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Server hangs when body is undefined

measwel opened this issue · comments

commented

It seems that my node express server will hang when I post a request with body === undefined at it. The problem might be in body-parser.

Hi @measwel thanks for the report! Can you help us reproduce the issue? You can always provide a pull request with a fix, or all the following information:

  1. Version of node.js this module and any other you are using.
  2. Complete code that I can copy and paste and run without any modification to see the issue.
  3. Complete instructions on how to run the above code and exactly how to replicate the issue.

Thank you.

commented

Hi Doug,

I wrote a module called apithug which records requests made to the backend automatically. It then mutates those requests and sends them to the API to test if something breaks. One of the mutations assigns undefined to the body of a post request. You can replicate this by simply making such a request yourself.

node: v10.16.0
"body-parser": "^1.19.0"
"express": "^4.17.1"

PS I would be interested in getting help to make apithug into a better module available on github.

You can replicate this by simply making such a request yourself.

Please provide the instructions how to do this.

commented

I am using axios to fire the mutated request:

  axios({
    method: request.method,
    url: apithug.apiurl + request.path,
    data: undefined, <-- SET TO UNDEFINED
    headers: request.headers
  }).then((res) => {
    results.request = request;
    results.result = res;
    callback();
  }).catch((error) => {
    console.error(error.message);
    if (error.response && error.response.data.msg) {
      console.error(error.response.data.msg);
    }
    results.request = request;
    results.result = error;
    callback();
  });

The result is that express hangs and only comes back with a connection reset response after a long time. It also happens when the body is set to null.

I do not know if the hangup is due to body-parser. But body-parser is certainly affected by such a call I would say. It should check if the body is undefined or null.

So I had to modify that enough so it would actually run due to all the missing variables, but running with data: undefined on the latest axios against the example server at https://github.com/expressjs/body-parser#expressconnect-top-level-generic it does not hang.

commented

Strange. It hags here with either null or undefined. Let me test what happens if I remove body parser.

Right, I'm not doubting your report, but just trying to reproduce it so I can diagnose. There are a lot of differences between our environments, as I'm not running the same server code and even the client code you provided I had to heavily modify just so it would run without throwing exceptions about undefined variables.

commented

Hmm. The problem might not be with body-parser. It seems that no route handler whatsoever is invoked for such a request. I might just ignore it and hope that other requests will continue being processed if such a condition arises.

I'm going to close this for now, but if there is more information I can always reopen the issue.