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

Document but discourage protocol config option #940

Merged
merged 1 commit into from
Jul 1, 2020
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ const stripe = Stripe('sk_test_...', {
| `timeout` | 80000 | [Maximum time each request can take in ms.](#configuring-timeout) |
| `host` | `'api.stripe.com'` | Host that requests are made to. |
| `port` | 443 | Port that requests are made to. |
| `protocol` | `'https'` | `'https'` or `'http'`. `http` is never appropriate for sending requests to Stripe servers, and we strongly discourage `http`, even in local testing scenarios, as this can result in your credentials being transmitted over an insecure channel.
| `telemetry` | `true` | Allow Stripe to send latency [telemetry](#request-latency-telemetry). |

Note: Both `maxNetworkRetries` and `timeout` can be overridden on a per-request basis.
Expand Down
10 changes: 10 additions & 0 deletions lib/stripe.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ function Stripe(key, config = {}) {
this.once = this._emitter.once.bind(this._emitter);
this.off = this._emitter.removeListener.bind(this._emitter);

if (
props.protocol &&
props.protocol !== 'https' &&
(!props.host || /\.stripe\.com$/.test(props.host))
) {
throw new Error(
'The `https` protocol must be used when sending requests to `*.stripe.com`'
);
}

this._api = {
auth: null,
host: props.host || DEFAULT_HOST,
Expand Down
41 changes: 41 additions & 0 deletions test/stripe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,47 @@ describe('Stripe Module', function() {
});
}).to.not.throw();
});
it('should forbid sending http to *.stripe.com', () => {
expect(() => {
Stripe(testUtils.getUserStripeKey(), {
host: 'foo.stripe.com',
protocol: 'http',
});
}).to.throw(/The `https` protocol must be used/);

expect(() => {
Stripe(testUtils.getUserStripeKey(), {
protocol: 'http',
});
}).to.throw(/The `https` protocol must be used/);

expect(() => {
Stripe(testUtils.getUserStripeKey(), {
protocol: 'http',
host: 'api.stripe.com',
});
}).to.throw(/The `https` protocol must be used/);

expect(() => {
Stripe(testUtils.getUserStripeKey(), {
protocol: 'https',
host: 'api.stripe.com',
});
}).not.to.throw();

expect(() => {
Stripe(testUtils.getUserStripeKey(), {
host: 'api.stripe.com',
});
}).not.to.throw();

expect(() => {
Stripe(testUtils.getUserStripeKey(), {
protocol: 'http',
host: 'localhost',
});
}).not.to.throw();
});

it('should perform a no-op if null, undefined or empty values are passed', () => {
const cases = [null, undefined, '', {}];
Expand Down