Repository navigation
fix(express): forward query strings without deprecated url.parse #10142
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
modules/express/test/unit/clientRoutes/proxiedQueryStrings.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import 'should'; | ||
| import nock from 'nock'; | ||
| import { common } from '@bitgo/sdk-core'; | ||
| import { setupAgent } from '../../lib/testutil'; | ||
|
|
||
| const BITGO_TEST_URI = common.Environments.test.uri; | ||
|
|
||
| /** | ||
| * createAPIPath() (via the REST proxy handlers) is responsible for forwarding the client's | ||
| * query string bytes verbatim to the BitGo API. These tests run real requests through the | ||
| * express app and assert the exact upstream request path the BitGo SDK receives. | ||
| */ | ||
| describe('proxied request query strings', function () { | ||
| const upstreamPaths: string[] = []; | ||
|
|
||
| before(function () { | ||
| if (!nock.isActive()) { | ||
| nock.activate(); | ||
| } | ||
| // Block external network but allow supertest's local connections | ||
| nock.disableNetConnect(); | ||
| nock.enableNetConnect('127.0.0.1'); | ||
| }); | ||
|
|
||
| beforeEach(function () { | ||
| nock.cleanAll(); | ||
| upstreamPaths.length = 0; | ||
| // Single interceptor capturing every upstream GET path; the SDK prefetches | ||
| // /api/v1/client/constants on every unauthenticated BitGo instance, so filter it out. | ||
| nock(BITGO_TEST_URI) | ||
| .persist(true) | ||
| .get(/.*/) | ||
| .reply(function (this: { req: { path: string } }) { | ||
| const path = this.req.path; | ||
| if (!path.startsWith('/api/v1/client/constants')) { | ||
| upstreamPaths.push(path); | ||
| } | ||
| return [200, { ok: true }] as const; | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(function () { | ||
| nock.cleanAll(); | ||
| }); | ||
|
|
||
| after(function () { | ||
| if (nock.isActive()) { | ||
| nock.restore(); | ||
| } | ||
| }); | ||
|
|
||
| it('should forward a v1 path without query string', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v1/wallets'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v1/wallets']); | ||
| }); | ||
|
|
||
| it('should forward a simple v1 query string verbatim', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v1/wallets?allTokens=true&limit=5'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v1/wallets?allTokens=true&limit=5']); | ||
| }); | ||
|
|
||
| it('should forward percent-encoded v1 query values verbatim', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v1/wallets?space=%20&apostrophe=%27&plus=%2B'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v1/wallets?space=%20&apostrophe=%27&plus=%2B']); | ||
| }); | ||
|
|
||
| it('should forward a plus sign in a v1 query verbatim', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v1/wallets?label=a+b'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v1/wallets?label=a+b']); | ||
| }); | ||
|
|
||
| it('should forward repeated v1 query keys verbatim', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v1/wallets?coin=eth&coin=btc'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v1/wallets?coin=eth&coin=btc']); | ||
| }); | ||
|
|
||
| it('should forward a v1 query parameter with an empty value verbatim', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v1/wallets?empty='); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v1/wallets?empty=']); | ||
| }); | ||
|
|
||
| it('should not forward a bare question mark with no query params', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v1/wallets?'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v1/wallets']); | ||
| }); | ||
|
|
||
| it('should forward a v2 user path with query string verbatim', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v2/user/me?client=web'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v2/user/me?client=web']); | ||
| }); | ||
|
|
||
| it('should forward a v2 coin path with query string verbatim', async function () { | ||
| const agent = setupAgent(); | ||
| const res = await agent.get('/api/v2/tbtc/wallet?allTokens=true'); | ||
| res.status.should.equal(200); | ||
| upstreamPaths.should.deepEqual(['/api/v2/tbtc/wallet?allTokens=true']); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.