Skip to content
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

fix: webhook validation with an array parameter #687

Merged
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
18 changes: 17 additions & 1 deletion lib/webhooks/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ function removePort(parsedUrl) {
return parsedUrl.toString();
}

/**
Utility function to convert request parameter to a string format

@param {string} paramName - The request parameter name
@param {string|array<string>} paramValue - The request parameter value
@returns {string} - Formatted parameter string
*/
function toFormUrlEncodedParam(paramName, paramValue) {
if (paramValue instanceof Array) {
return paramValue
.map(val => toFormUrlEncodedParam(paramName, val))
.reduce((acc, val) => acc + val, '');
}
return paramName + paramValue;
}

/**
Utility function to get the expected signature for a given request

Expand All @@ -65,7 +81,7 @@ function getExpectedTwilioSignature(authToken, url, params) {

var data = Object.keys(params)
.sort()
.reduce((acc, key) => acc + key + params[key], url);
.reduce((acc, key) => acc + toFormUrlEncodedParam(key, params[key]), url);

return crypto
.createHmac('sha1', authToken)
Expand Down
8 changes: 8 additions & 0 deletions spec/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ describe('Request validation', () => {

expect(isValid).toBeTruthy();
});

it('should validate request body with an array parameter', () => {
const paramsWithArray = { 'MessagingBinding.Address': ['+1325xxxxxxx', '+1415xxxxxxx'] };
const signature = '83O6e2vORAoJHUNzJjDWN1jz+BA=';
const isValid = validateRequest(token, signature, requestUrl, paramsWithArray);

expect(isValid).toBeTruthy();
});
});

describe('Request validation middleware', () => {
Expand Down