Skip to content

Restify 5.0 Response.js - Maximum call stack size exceeded - Node 8.1.4 #1407

@ianvonholt

Description

@ianvonholt

Bug Report

Restify Version

restify@5.0.0

Node.js Version

8.1.4

Expected behaviour

Calling res.getHeaders() would return the appropriate header object.

Actual behaviour

./node_modules/restify/lib/response.js:119
Response.prototype.getHeaders = function getHeaders() {
                                                   ^

RangeError: Maximum call stack size exceeded
    at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:119:52)
    at ServerResponse.get (_http_outgoing.js:130:17)
    at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
    at ServerResponse.get (_http_outgoing.js:130:17)
    at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
    at ServerResponse.get (_http_outgoing.js:130:17)
    at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
    at ServerResponse.get (_http_outgoing.js:130:17)
    at ServerResponse.getHeaders (./node_modules/restify/lib/response.js:120:17)
    at ServerResponse.get (_http_outgoing.js:130:17)

Repro case

const restify = require('restify');
const server = restify.createServer();
server.get('/', function (req, res, next) {
  res.send(200, res.getHeaders());
  next();
});
server.listen(8080, function () {});

Cause

The use of _headers is deprecated and it appears that it is currently a wrapper for getHeaders() Public API in Node 8.1.4.

https://nodejs.org/api/deprecations.html#deprecations_dep0066_outgoingmessage_headers_outgoingmessage_headernames

https://github.com/nodejs/node/blob/v8.1.4/lib/_http_outgoing.js#L128-L144

The problem is the inheritance that Restify 5.0 displays with http.ServerResponse. At https://github.com/restify/node-restify/blob/5.x/lib/response.js#L22 Response is set as the appropriate object.

https://github.com/restify/node-restify/blob/5.x/lib/response.js#L119-L121 Then goes to override the Public API function and causes an infinite loop.

Are you willing and able to fix this?

Simply commenting out https://github.com/restify/node-restify/blob/5.x/lib/response.js#L119-L121 would fix the issue in Node 8 environments as the method already exists.

Node 4.x and Node 6.x do not have a response.getHeaders function and this function seems legitimate in Restify.

A simple replacement would be to check the Response object prior to addition:

if (typeof Response.prototype.getHeaders !== 'function') {
  Response.prototype.getHeaders = function getHeaders() {
      return (this._headers || {});
  };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions