Skip to content

Commit a0307d0

Browse files
box-sdk-buildbox-sdk-build
andauthored
fix: fix slash escaping when calculating webhook signature (box/box-codegen#736) (#624)
Co-authored-by: box-sdk-build <box-sdk-build@box.com>
1 parent be3af44 commit a0307d0

File tree

5 files changed

+75
-33
lines changed

5 files changed

+75
-33
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "20cb559", "specHash": "630fc85", "version": "1.15.1" }
1+
{ "engineHash": "70390f5", "specHash": "630fc85", "version": "1.15.1" }

package-lock.json

Lines changed: 30 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/internal/utilsBrowser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ export async function computeWebhookSignature(
291291
signatureKey: string,
292292
): Promise<string | null> {
293293
const escapedBody = jsonStringifyWithEscapedUnicode(body).replace(
294-
/\//g,
294+
/(?<!\\)\//g,
295295
'\\/',
296296
);
297297
if (headers['box-signature-version'] !== '1') {

src/internal/utilsNode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export async function computeWebhookSignature(
246246
signatureKey: string,
247247
): Promise<string | null> {
248248
const escapedBody = jsonStringifyWithEscapedUnicode(body).replace(
249-
/\//g,
249+
/(?<!\\)\//g,
250250
'\\/',
251251
);
252252
if (headers['box-signature-version'] !== '1') {

src/test/webhooks.generated.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ test('testWebhookValidation', async function testWebhookValidation(): Promise<an
123123
'{"webhook":{"id":"1234567890"},"trigger":"FILE.UPLOADED","source":{"id":"1234567890","type":"file","name":"\uD83D\uDE00 2020-08-05.txt"}}';
124124
const bodyWithCarriageReturn: string =
125125
'{"webhook":{"id":"1234567890"},"trigger":"FILE.UPLOADED","source":{"id":"1234567890","type":"file","name":"test \\r"}}';
126+
const bodyWithForwardSlash: string =
127+
'{"webhook":{"id":"1234567890"},"trigger":"FILE.UPLOADED","source":{"id":"1234567890","type":"file","name":"\\/"}}';
128+
const bodyWithBackSlash: string =
129+
'{"webhook":{"id":"1234567890"},"trigger":"FILE.UPLOADED","source":{"id":"1234567890","type":"file","name":"\\\\"}}';
126130
const headers: {
127131
readonly [key: string]: string;
128132
} = {
@@ -157,6 +161,22 @@ test('testWebhookValidation', async function testWebhookValidation(): Promise<an
157161
['box-signature-primary']: 'SVkbKgy3dEEf2PbbzpNu2lDZS7zZ/aboU7HOZgBGrJk=',
158162
},
159163
};
164+
const headersWithForwardSlash: {
165+
readonly [key: string]: any;
166+
} = {
167+
...headers,
168+
...{
169+
['box-signature-primary']: 't41PWT5ZB6OcysnD6SDy9Ud+p9hdXxIdXqcdweyZv/Q=',
170+
},
171+
};
172+
const headersWithBackSlash: {
173+
readonly [key: string]: any;
174+
} = {
175+
...headers,
176+
...{
177+
['box-signature-primary']: 'ERpMZwUQsGDTfj82ehdX6VvDZfvOhK5ULNfVmwVAGe0=',
178+
},
179+
};
160180
const currentDatetime: string = dateTimeToString(
161181
epochSecondsToDateTime(getEpochTimeInSeconds()),
162182
);
@@ -307,6 +327,28 @@ test('testWebhookValidation', async function testWebhookValidation(): Promise<an
307327
) {
308328
throw new Error('Assertion failed');
309329
}
330+
if (
331+
!(
332+
(await computeWebhookSignature(
333+
bodyWithForwardSlash,
334+
headersWithForwardSlash,
335+
primaryKey,
336+
)) == headersWithForwardSlash['box-signature-primary']
337+
)
338+
) {
339+
throw new Error('Assertion failed');
340+
}
341+
if (
342+
!(
343+
(await computeWebhookSignature(
344+
bodyWithBackSlash,
345+
headersWithBackSlash,
346+
primaryKey,
347+
)) == headersWithBackSlash['box-signature-primary']
348+
)
349+
) {
350+
throw new Error('Assertion failed');
351+
}
310352
if (
311353
!(await WebhooksManager.validateMessage(
312354
body,

0 commit comments

Comments
 (0)