Skip to content
Open
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
14 changes: 8 additions & 6 deletions modules/express/src/clientRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import debugLib from 'debug';
import express from 'express';
import type { ParamsDictionary } from 'express-serve-static-core';
import * as _ from 'lodash';
import * as url from 'url';
import * as superagent from 'superagent';

// RequestTracer should be extracted into a separate npm package (along with
Expand Down Expand Up @@ -333,11 +332,14 @@ function handleCalculateMinerFeeInfo(req: ExpressApiRouteRequest<'express.calcul
function createAPIPath(req: express.Request) {
let apiPath = '/' + req.params[0];
if (!_.isEmpty(req.query)) {
// req.params does not contain the querystring, so we manually add them here
const urlDetails = url.parse(req.url);
if (urlDetails.search) {
// "search" is the properly URL encoded query params, prefixed with "?"
apiPath += urlDetails.search;
// req.params does not contain the querystring, so we take it from the raw req.url.
// Slice instead of URL parsing to preserve the query bytes exactly as the client sent
// them (e.g. WHATWG URL would re-encode a raw "'"). The fragment part cannot be
// forwarded, matching legacy url.parse().search behavior.
const queryStart = req.url.indexOf('?');
if (queryStart !== -1) {
const fragmentStart = req.url.indexOf('#', queryStart);
apiPath += fragmentStart === -1 ? req.url.slice(queryStart) : req.url.slice(queryStart, fragmentStart);
Comment on lines +339 to +342

Copy link
Copy Markdown

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.parse in setRequestQueryString (modules/sdk-api/src/api.ts:212) and calculateHMACSubject (modules/sdk-hmac/src/hmac.ts:45). Fix: remove url.parse along 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, through handleV2CoinSpecificREST -> createAPIPath -> redirectRequest. In bitgoAPI.ts:599 _sendRequestWithHmac calls setRequestQueryString(req), which runs urlLib.parse(req.url) at api.ts:212. It then calls calculateRequestHeaders, which reaches urlLib.parse(urlPath) at sdk-hmac/src/hmac.ts:45, and verifyResponse parses 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. _sendRequestWithHmac calls setRequestQueryString(req) at bitgoAPI.ts:599, which always runs urlLib.parse(req.url) (sdk-api/src/api.ts:212). In the Docker image, sdk-api and sdk-hmac live in /var/modules, not node_modules, so these calls count as application code and the warning is still printed.

}
}
return apiPath;
Expand Down
114 changes: 114 additions & 0 deletions modules/express/test/unit/clientRoutes/proxiedQueryStrings.ts
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']);
});
});
Loading