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

feat: move to built-in URL class #998

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"jsonwebtoken": "^9.0.0",
"qs": "^6.9.4",
"scmp": "^2.1.0",
"url-parse": "^1.5.9",
"xmlbuilder": "^13.0.2"
},
"devDependencies": {
Expand All @@ -36,7 +35,6 @@
"@types/jsonwebtoken": "^9.0.0",
"@types/node": "^18.11.18",
"@types/qs": "^6.9.7",
"@types/url-parse": "^1.4.8",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"eslint": "^8.31.0",
"express": "^4.17.1",
Expand Down
2 changes: 1 addition & 1 deletion spec/validation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe("Request validation", () => {

it("should validate urls with special characters", () => {
const specialRequestUrl = requestUrl + "&Body=It's+amazing";
const signature = "dsq4Ehbj6cs+KdTkpF5sSSplOWw=";
const signature = "TfZzewPq8wqrGlMfyAud8+/IvJ0=";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this change has been made a test case for checking special characters only, right?

Copy link
Author

@43081j 43081j Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this changed because URL encodes single quotes, whereas url-parse doesn't

that means the signature is different, but should be ok since url-parse was wrong/off-spec basically

probably is worth you double checking i got the right signature though

the test is still useful i think

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh got it, I checked. The encoding differs in URL than url-parse. This is correct I think, url-parse wasn't encoding quotes and hashtags. The signature is correct, it passes the tests.

const isValid = validateRequest(
token,
signature,
Expand Down
17 changes: 8 additions & 9 deletions src/webhooks/webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const scmp = require("scmp");
import crypto from "crypto";
import urllib from "url";
import Url from "url-parse";
import { IncomingHttpHeaders } from "http2";

export interface Request {
Expand Down Expand Up @@ -65,7 +64,7 @@ export interface WebhookOptions {
* @param parsedUrl - The parsed url object that Twilio requested on your server
* @returns URL with standard port number included
*/
function buildUrlWithStandardPort(parsedUrl: Url<string>): string {
function buildUrlWithStandardPort(parsedUrl: URL): string {
let url = "";
const port = parsedUrl.protocol === "https:" ? ":443" : ":80";

Expand All @@ -74,7 +73,7 @@ function buildUrlWithStandardPort(parsedUrl: Url<string>): string {
url += parsedUrl.password ? ":" + parsedUrl.password : "";
url += parsedUrl.username || parsedUrl.password ? "@" : "";
url += parsedUrl.host ? parsedUrl.host + port : "";
url += parsedUrl.pathname + parsedUrl.query + parsedUrl.hash;
url += parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;

return url;
}
Expand All @@ -85,7 +84,7 @@ function buildUrlWithStandardPort(parsedUrl: Url<string>): string {
@param parsedUrl - The parsed url object that Twilio requested on your server
@returns URL with port
*/
function addPort(parsedUrl: Url<string>): string {
function addPort(parsedUrl: URL): string {
if (!parsedUrl.port) {
return buildUrlWithStandardPort(parsedUrl);
}
Expand All @@ -98,8 +97,8 @@ function addPort(parsedUrl: Url<string>): string {
@param parsedUrl - The parsed url object that Twilio requested on your server
@returns URL without port
*/
function removePort(parsedUrl: Url<string>): string {
parsedUrl.set("port", "");
function removePort(parsedUrl: URL): string {
parsedUrl.port = '';
return parsedUrl.toString();
}

Expand Down Expand Up @@ -179,7 +178,7 @@ export function validateRequest(
params: Record<string, any>
): boolean {
twilioHeader = twilioHeader || "";
const urlObject = new Url(url);
const urlObject = new URL(url);
const urlWithPort = addPort(urlObject);
const urlWithoutPort = removePort(urlObject);

Expand Down Expand Up @@ -233,10 +232,10 @@ export function validateRequestWithBody(
url: string,
body: string
): boolean {
const urlObject = new Url(url, true);
const urlObject = new URL(url);
return (
validateRequest(authToken, twilioHeader, url, {}) &&
validateBody(body, urlObject.query.bodySHA256 || "")
validateBody(body, urlObject.searchParams.get('bodySHA256') || "")
);
}

Expand Down
Loading