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

Feature disable tls certs #1511

Merged
merged 3 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ In Node.js SuperAgent supports methods to configure HTTPS requests:
- `.cert()`: Set the client certificate chain(s)
- `.key()`: Set the client private key(s)
- `.pfx()`: Set the client PFX or PKCS12 encoded private key and certificate chain
- `.disableTLSCerts()`: Does not reject expired or invalid TLS certs. Sets internally `rejectUnauthorized=true`. *Be warned, this method allows MITM attacks.*

For more information, see Node.js [https.request docs](https://nodejs.org/api/https.html#https_https_request_options_callback).

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"zuul": "^3.12.0"
},
"engines": {
"node": ">= 6.4.0"
"node": ">= 7.0.0"
},
"homepage": "https://github.com/visionmedia/superagent",
"husky": {
Expand Down
3 changes: 2 additions & 1 deletion src/agent-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function Agent() {
'ca',
'key',
'pfx',
'cert'
'cert',
'disableTLSCerts'
].forEach(fn => {
// Default setting for all requests from this agent
Agent.prototype[fn] = function(...args) {
Expand Down
4 changes: 4 additions & 0 deletions src/node/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ function Agent(options) {
if (options.cert) {
this.cert(options.cert);
}

if (options.rejectUnauthorized === false) {
this.disableTLSCerts();
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,19 @@ Request.prototype.cert = function(cert) {
return this;
};

/**
* Do not reject expired or invalid TLS certs.
* sets `rejectUnauthorized=true`. Be warned that this allows MITM attacks.
*
* @return {Request} for chaining
* @api public
*/

Request.prototype.disableTLSCerts = function() {
this._disableTLSCerts = true;
return this;
};

/**
* Return an http[s] request.
*
Expand Down Expand Up @@ -743,6 +756,7 @@ Request.prototype.request = function() {
options.cert = this._cert;
options.passphrase = this._passphrase;
options.agent = this._agent;
options.rejectUnauthorized = !this._disableTLSCerts;

// Allows request.get('https://1.2.3.4/').set('Host', 'example.com')
if (this._header.host) {
Expand Down
9 changes: 9 additions & 0 deletions test/node/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ describe('https', () => {
);
});

it('should not reject unauthorized response', () => {
return request
.get(testEndpoint)
.disableTLSCerts()
.then(({ status }) => {
assert.strictEqual(status, 200);
});
});

it('should trust localhost unauthorized response', () => {
return request.get(testEndpoint).trustLocalhost(true);
});
Expand Down