Skip to content

Commit

Permalink
Merge pull request #876 from snyk/chore/add-request-id-to-webhook-cal…
Browse files Browse the repository at this point in the history
…l-via-srvr

chore: add server side broker header
  • Loading branch information
aarlaud authored Nov 11, 2024
2 parents 694a622 + cb0a030 commit b4daa67
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/common/relay/forwardWebsocketRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ export const forwardWebSocketRequest = (
);
payload.headers[contentLengthHeader] = computeContentLength(payload);
}
if (options.config.brokerType !== 'client') {
preparedRequest.req.headers['x-snyk-broker'] = `${maskToken(
connectionIdentifier,
)}`;
}

incrementHttpRequestsTotal(false, 'outbound-request');
payload.streamingID
Expand Down
13 changes: 13 additions & 0 deletions test/functional/client-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
waitForBrokerClientConnections,
} from '../setup/broker-server';
import { TestWebServer, createTestWebServer } from '../setup/test-web-server';
import { maskToken } from '../../lib/common/utils/token';

const fixtures = path.resolve(__dirname, '..', 'fixtures');
const serverAccept = path.join(fixtures, 'server', 'filters.json');
Expand Down Expand Up @@ -74,6 +75,18 @@ describe('proxy requests originating from behind the broker client', () => {
});
});

it('successfully broker POST with x-broker-server header', async () => {
const response = await axiosClient.post(
`http://localhost:${bc.port}/echo-headers`,
{ some: { example: 'json' } },
);

expect(response.status).toEqual(200);
expect(response.data['x-snyk-broker']).toStrictEqual(
maskToken(brokerToken),
);
});

it('successfully broker exact bytes of POST body', async () => {
// stringify the JSON unusually to ensure an unusual exact body
const body = Buffer.from(
Expand Down
14 changes: 14 additions & 0 deletions test/functional/webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../setup/broker-server';
import { TestWebServer, createTestWebServer } from '../setup/test-web-server';
import { DEFAULT_TEST_WEB_SERVER_PORT } from '../setup/constants';
import { maskToken } from '../../lib/common/utils/token';

const fixtures = path.resolve(__dirname, '..', 'fixtures');
const serverAccept = path.join(fixtures, 'server', 'filters-webhook.json');
Expand Down Expand Up @@ -80,4 +81,17 @@ describe('proxy requests originating from behind the broker client', () => {
expect(response.status).toEqual(200);
expect(response.data).toStrictEqual('Received webhook via API');
});
it('successfully broker injects x-snyk-broker header to Webhook calls', async () => {
await closeBrokerServer(bs);

const response = await axiosClient.post(
`http://localhost:${bc.port}/webhook/github/return-req-headers`,
{ some: { example: 'json' } },
);

expect(response.status).toEqual(200);
expect(response.data['x-snyk-broker']).toStrictEqual(
maskToken('broker-token-12345'),
);
});
});
8 changes: 8 additions & 0 deletions test/setup/test-web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ const applyEchoRoutes = (app: Express) => {
},
);

echoRouter.post(
'/webhook/github/return-req-headers',
(req: express.Request, resp: express.Response) => {
resp.status(200);
resp.send(req.headers);
},
);

echoRouter.get('/test', (_: express.Request, resp: express.Response) => {
resp.status(200);
resp.send('All good');
Expand Down

0 comments on commit b4daa67

Please sign in to comment.