Skip to content
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
13 changes: 13 additions & 0 deletions modules/express/src/clientRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ const debug = debugLib('bitgo:express');

const BITGOEXPRESS_USER_AGENT = `BitGoExpress/${pjson.version} BitGoJS/${version}`;

/**
* Headers from the incoming request that should be forwarded to the BitGo API.
* Header names must be lowercase (Express normalizes incoming headers to lowercase).
*/
const FORWARDED_HEADERS = ['x-bitgo-otp'];

function handlePing(
req: ExpressApiRouteRequest<'express.ping', 'get'>,
res: express.Response,
Expand Down Expand Up @@ -1340,6 +1346,13 @@ export function redirectRequest(
request.set('enterprise-id', req.params.enterpriseId);
}

for (const header of FORWARDED_HEADERS) {
const value = req.headers[header];
if (value) {
request.set(header, Array.isArray(value) ? value[0] : value);
}
}

return request.result().then((result) => {
const status = request.res?.statusCode || 200;
return { status, body: result };
Expand Down
31 changes: 31 additions & 0 deletions modules/express/test/unit/clientRoutes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('common methods', () => {
req = {
body: {},
params: {},
headers: {},
bitgo,
} as express.Request;
next = () => undefined;
Expand Down Expand Up @@ -52,6 +53,36 @@ describe('common methods', () => {
result.body.should.deepEqual({ success: true });
});

it('should forward X-BitGo-OTP header when present', async () => {
const url = 'https://example.com/api';
const setStub = sandbox.stub();
const response = { res: { statusCode: 200 }, result: async () => ({ success: true }), set: setStub };
sandbox
.stub(bitgo, 'get')
.withArgs(url)
.returns(response as any);

req.headers = { 'x-bitgo-otp': '123456' } as any;
const result = await redirectRequest(bitgo, 'GET', url, req, next);
result.status.should.equal(200);
setStub.calledWith('x-bitgo-otp', '123456').should.be.true();
});

it('should not forward X-BitGo-OTP header when not present', async () => {
const url = 'https://example.com/api';
const setStub = sandbox.stub();
const response = { res: { statusCode: 200 }, result: async () => ({ success: true }), set: setStub };
sandbox
.stub(bitgo, 'get')
.withArgs(url)
.returns(response as any);

req.headers = {} as any;
const result = await redirectRequest(bitgo, 'GET', url, req, next);
result.status.should.equal(200);
setStub.calledWith('x-bitgo-otp').should.be.false();
});

it('should handle error response and return status and body', async () => {
const url = 'https://example.com/api';
const response = {
Expand Down
Loading