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
Open
danielpeng1
wants to merge
1
commit into
master
Choose a base branch
from
WCN-4315-bitgo-express-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−6
Open
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 (optional) Operators running Express from source or the Docker image will most likely still see the DEP0169 warning on authenticated proxied requests, so the PR's goal is not met. The same request still reaches legacy
url.parseinsetRequestQueryString(modules/sdk-api/src/api.ts:212) andcalculateHMACSubject(modules/sdk-hmac/src/hmac.ts:45). Fix: removeurl.parsealong the whole proxied-request path, which means changing sdk-api and sdk-hmac. That is larger than this change, so either do it or narrow the PR's claim. Node prints DEP0169 only once per process, so moving the first call elsewhere does not remove the warning.Why this was flagged
Trigger: an authenticated client (access token set) sends a proxied GET with a query, e.g.
/api/v2/tbtc/wallet?allTokens=true, throughhandleV2CoinSpecificREST->createAPIPath->redirectRequest. In bitgoAPI.ts:599_sendRequestWithHmaccallssetRequestQueryString(req), which runsurlLib.parse(req.url)at api.ts:212. It then callscalculateRequestHeaders, which reachesurlLib.parse(urlPath)at sdk-hmac/src/hmac.ts:45, andverifyResponseparses the same way. Node only raises DEP0169 when the caller is not inside node_modules. The Dockerfile copies sdk-api and sdk-hmac to /var/modules/... (lines 59, 61), and the monorepo runs them from modules/..., which are the same setups where clientRoutes.ts emitted the warning on base. The warning is deduplicated per process, so operators still get exactly one DEP0169, just from a different file. The new unit test only checks upstream paths and never checks that the warning is gone.Verification: Severity: nit. The warning was already there on the base.
_sendRequestWithHmaccallssetRequestQueryString(req)at bitgoAPI.ts:599, which always runsurlLib.parse(req.url)(sdk-api/src/api.ts:212). In the Docker image, sdk-api and sdk-hmac live in/var/modules, notnode_modules, so these calls count as application code and the warning is still printed.