restify / restify/node-restify

bodyReader does not work with both gunzip and maxBodySize

Open
#1,785 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
10.7k
Forks
975
Avg merge
1d 6h
Merged PRs (30d)
5

Description

Bug Report

bodyReader plugin setup with maxBodySize when called with Content-Encoding: gzip with body size over the body limit causes zlib buffer error and eventually stops process.

Restify Version

8.3.2

Node.js Version

10.14.2

Expected behaviour

Return "413 Body Too Long". Same behaviour as for not gzipped content.

Actual behaviour

server is down with error:

events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: unexpected end of file
    at Zlib.zlibOnError [as onerror] (zlib.js:154:17)
Emitted 'error' event at:
    at Zlib.zlibOnError [as onerror] (zlib.js:157:8)

Repro case

'use strict';

const restify = require('restify');
const got = require('got');
const zlib = require('zlib');

const payload = { apple: 'red' };
const body = zlib.gzipSync(JSON.stringify(payload));

// set maxBodySize smaller than gzipped body
const maxBodySize = body.byteLength - 1;

const server = restify.createServer();
server.listen(3000, '127.0.0.1');
server.use(restify.plugins.bodyParser({ maxBodySize }));
server.post('/foo', (req, res, next) => {
    res.send();
    next();
});

got({
    url: 'http://127.0.0.1:3000/foo',
    body,
    json: false,
    throwHttpErrors: false,
    headers: {
        'Content-Encoding': 'gzip',
        'Content-Type': 'application/json'
    }
})
    .then(response => console.log(response.statusCode))
    .catch(err => console.log(err));

Cause

bodyReader done handler function is not triggered when gunzip transformer emits error due to incomplete buffer when it is incomplete because maxBodySize limit was reached.

Are you willing and able to fix this?

Yes. PR is on the way.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the bodyParser/bodyReader path, especially the done handler and gunzip transformer error handling described in the issue. Reproduce the supplied maxBodySize and Content-Encoding: gzip case; done means the request returns 413 Body Too Long without the process stopping.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.