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

Assign default value to twilioHeader during validation #446

Merged
merged 5 commits into from
May 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions lib/webhooks/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function getExpectedBodyHash(body) {
@returns {boolean} - valid
*/
function validateRequest(authToken, twilioHeader, url, params) {
twilioHeader = twilioHeader || '';
var expectedSignature = getExpectedTwilioSignature(authToken, url, params);
return scmp(Buffer.from(twilioHeader), Buffer.from(expectedSignature));
}
Expand Down
26 changes: 26 additions & 0 deletions spec/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ describe('Request validation', () => {

expect(isValid).toBeFalsy();
});

it('should fail when signature undefined', () => {
const isValid = validateRequest(token, undefined, requestUrl, defaultParams);

expect(isValid).toBeFalsy();
});
});

describe('Request validation middleware', () => {
Expand Down Expand Up @@ -116,6 +122,26 @@ describe('Request validation middleware', () => {
expect(response.statusCode).toEqual(403);
});

it('should send 403 when no twilio signature header', () => {
const newUrl = fullUrl.pathname + fullUrl.search + '&somethingUnexpected=true';

// Make a copy of the default request so the header object can be modified without
// affecting other tests.
const noSignatureRequest = Object.assign({}, defaultRequest);
noSignatureRequest.headers = Object.assign({}, defaultRequest.headers);
delete noSignatureRequest.headers['X-Twilio-Signature'];

const request = httpMocks.createRequest(Object.assign({}, defaultRequest, {
mniehe marked this conversation as resolved.
Show resolved Hide resolved
originalUrl: newUrl,
}));

middleware(request, response, error => {
expect(true).toBeFalsy();
});

expect(response.statusCode).toEqual(403);
});

it('should bypass validation if given {validate:false}', done => {
const request = httpMocks.createRequest(defaultRequest);

Expand Down