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: switch to a URL parsing lib that does not add percent-encoding #519

Merged
merged 1 commit into from
Dec 31, 2019
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
17 changes: 9 additions & 8 deletions lib/webhooks/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ var crypto = require('crypto');
var _ = require('lodash');
var scmp = require('scmp');
var urllib = require('url');
var Url = require('url-parse');

/**
* Utility function to construct the URL string, since Node.js url library won't include standard port numbers
*
* @param {URL} parsedUrl - The parsed url object that Twilio requested on your server
* @param {Url} parsedUrl - The parsed url object that Twilio requested on your server
* @returns {string} - URL with standard port number included
*/
function buildUrlWithStandardPort(parsedUrl) {
Expand All @@ -20,15 +21,15 @@ function buildUrlWithStandardPort(parsedUrl) {
url += parsedUrl.password ? ':' + parsedUrl.password : '';
url += (parsedUrl.username || parsedUrl.password) ? '@' : '';
url += parsedUrl.host ? parsedUrl.host + port : '';
url += parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
url += parsedUrl.pathname + parsedUrl.query + parsedUrl.hash;

return url;
}

/**
Utility function to add a port number to a URL

@param {URL} parsedUrl - The parsed url object that Twilio requested on your server
@param {Url} parsedUrl - The parsed url object that Twilio requested on your server
@returns {string} - URL with port
*/
function addPort(parsedUrl) {
Expand All @@ -41,11 +42,11 @@ function addPort(parsedUrl) {
/**
Utility function to remove a port number from a URL

@param {URL} parsedUrl - The parsed url object that Twilio requested on your server
@param {Url} parsedUrl - The parsed url object that Twilio requested on your server
@returns {string} - URL without port
*/
function removePort(parsedUrl) {
parsedUrl.port = '';
parsedUrl.set('port', '');
return parsedUrl.toString();
}

Expand Down Expand Up @@ -95,7 +96,7 @@ function getExpectedBodyHash(body) {
*/
function validateRequest(authToken, twilioHeader, url, params) {
twilioHeader = twilioHeader || '';
const urlObject = new urllib.URL(url);
const urlObject = new Url(url);
const urlWithPort = addPort(urlObject);
const urlWithoutPort = removePort(urlObject);

Expand Down Expand Up @@ -127,9 +128,9 @@ function validateBody(body, bodyHash) {
@returns {boolean} - valid
*/
function validateRequestWithBody(authToken, twilioHeader, url, body) {
const urlObject = new urllib.URL(url);
const urlObject = new Url(url, true);
return validateRequest(authToken, twilioHeader, url, {}) &&
validateBody(body, urlObject.searchParams.get('bodySHA256'));
validateBody(body, urlObject.query.bodySHA256);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"request": "^2.88.0",
"rootpath": "^0.1.2",
"scmp": "^2.0.0",
"url-parse": "^1.4.7",
"xmlbuilder": "^13.0.2"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions spec/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ describe('Request validation', () => {

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

it('should validate urls with special characters', () => {
const specialRequestUrl = requestUrl + '&Body=It\'s+amazing';
const signature = 'dsq4Ehbj6cs+KdTkpF5sSSplOWw=';
const isValid = validateRequest(token, signature, specialRequestUrl, defaultParams);

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

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