Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get method does not check options's Object prototype #12092

Closed
mihai1voicescu opened this issue Mar 28, 2017 · 6 comments
Closed

Get method does not check options's Object prototype #12092

mihai1voicescu opened this issue Mar 28, 2017 · 6 comments
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. http Issues or PRs related to the http subsystem. question Issues that look for answers.

Comments

@mihai1voicescu
Copy link

  • Version: v6.10.0
  • Platform: 3.10.0-514.2.2.el7.x86_64 deps: update openssl to 1.0.1j #1 SMP Tue Dec 6 23:06:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux AND Windows 8.1 64

The get method of http and https modules does not check the prototype of the given object. I guess this replicates to all other request/response methods.
The following code is ran on a machine that does not have anything running on port 8989:

'use strict';

const http = require("http");

let options = {
        port: 8989
};
let optionsChild = Object.create(options);

http.get(options, function () {
        console.log("Success options");
}).on('error', console.error);
http.get(optionsChild, function () {
        console.log("Success optionsChild");
}).on('error', console.error);

And we receive the following output:

{ Error: connect ECONNREFUSED 127.0.0.1:8989
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1087:14)
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 8989 }
Success optionsChild

This means the port takes the default value and successfully connects instead of taking the 8989 port from the prototype.

  1. Is this intended?

The need for this code arose from the necessity to reuse some default options in multiple requests. So I would like to ask a followup question:

  1. Can reusing(altering) the options Object passed to a http/https request cause unexpected behavior(requests being made to the last value of the object instead of the value at the moment of the function call or performance losses)?
@cjihrig
Copy link
Contributor

cjihrig commented Mar 28, 2017

The issue is that, under the hood, http.get() copies the options provided by the user using util._extend(). util._extend() copies properties using Object.keys(). Object.keys() doesn't include the prototype.

@vsemozhetbyt vsemozhetbyt added http Issues or PRs related to the http subsystem. question Issues that look for answers. labels Mar 28, 2017
@seishun
Copy link
Contributor

seishun commented Mar 28, 2017

Last time this was discussed it led to nowhere: nodejs/node-v0.x-archive#7587 (FWIW, my opinion on it hasn't changed)

But I agree it should at least be documented.

@raphaelokon
Copy link
Contributor

@seishun I agree on the documentation bit.

@raphaelokon
Copy link
Contributor

I think this issue is just a mod of this one here: nodejs/node-v0.x-archive#7587 (comment)

@mihai1voicescu
Copy link
Author

Thank you for the quick response!
Indeed the documentation should at least specify that.

Regarding the second question. If _extend is used then a shallow copy is made. Any alteration to the properties not on the first level will impact the first request if the object is used after a asynchronous call. Is this the case?

@joyeecheung joyeecheung added doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. labels Mar 29, 2017
@joyeecheung
Copy link
Member

Added doc and good first contribution labels. Feel free to send in a PR mentioning this behavior in doc/http.md :)

@jasnell jasnell closed this as completed in e1161a3 Apr 4, 2017
italoacasas pushed a commit to italoacasas/node that referenced this issue Apr 10, 2017
Extra notes that options doesn't include the prototype when copied

Fixes: nodejs#12092
PR-URL: nodejs#12124
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors. http Issues or PRs related to the http subsystem. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

6 participants