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

fix: Stop using path.join to create URLs. #1455

Merged
merged 4 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 14 additions & 12 deletions lib/StripeResource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const path = require('path');

const utils = require('./utils');
const {
StripeConnectionError,
Expand Down Expand Up @@ -71,22 +69,26 @@ StripeResource.prototype = {
validateRequest: null,

createFullPath(commandPath, urlData) {
return path
.join(
this.basePath(urlData),
this.path(urlData),
typeof commandPath == 'function' ? commandPath(urlData) : commandPath
)
.replace(/\\/g, '/'); // ugly workaround for Windows
return this._joinUrlParts([
this.basePath(urlData),
this.path(urlData),
typeof commandPath == 'function' ? commandPath(urlData) : commandPath,
]);
},

// Creates a relative resource path with symbols left in (unlike
// createFullPath which takes some data to replace them with). For example it
// might produce: /invoices/{id}
createResourcePathWithSymbols(pathWithSymbols) {
return `/${path
.join(this.resourcePath, pathWithSymbols || '')
.replace(/\\/g, '/')}`; // ugly workaround for Windows
return `/${this._joinUrlParts([this.resourcePath, pathWithSymbols || ''])}`;
},

_joinUrlParts(parts) {
dcr-stripe marked this conversation as resolved.
Show resolved Hide resolved
// Replace any accidentally doubled up parenthesis. This previously used
// path.join, which would do this as well. Unfortuantely we need to do this
dcr-stripe marked this conversation as resolved.
Show resolved Hide resolved
// as the functions for creating paths are technically part of the public
// interface and so we need to preserve backwards compatibility.
return parts.join('/').replace(/\/\//g, '/');
dcr-stripe marked this conversation as resolved.
Show resolved Hide resolved
},

// DEPRECATED: Here for backcompat in case users relied on this.
Expand Down
1 change: 0 additions & 1 deletion lib/makeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ function getRequestOpts(self, requestArgs, spec, overrideData) {
const commandPath = utils.makeURLInterpolator(
isUsingFullPath ? spec.fullPath : spec.path || ''
);

// When using fullPath, we ignore the resource path as it should already be
// fully qualified.
const path = isUsingFullPath
Expand Down
17 changes: 17 additions & 0 deletions test/StripeResource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('StripeResource', () => {
const path = stripe.invoices.createResourcePathWithSymbols('{id}');
expect(path).to.equal('/invoices/{id}');
});

it('Handles accidental double slashes', () => {
stripe.invoices.create({});
const path = stripe.invoices.createResourcePathWithSymbols('/{id}');
expect(path).to.equal('/invoices/{id}');
});
});

describe('_makeHeaders', () => {
Expand Down Expand Up @@ -94,6 +100,17 @@ describe('StripeResource', () => {
});
});

it('handles . as a query param', (done) => {
const scope = nock(`https://${stripe.getConstant('DEFAULT_HOST')}`)
.get('/v1/customers/.', '')
.reply(200, '{}');

realStripe.customers.retrieve('.', (err, response) => {
done(err);
scope.done();
});
});

it('works correctly with undefined optional arguments', (done) => {
const scope = nock(`https://${stripe.getConstant('DEFAULT_HOST')}`)
.get('/v1/accounts/acct_123')
Expand Down