From 18c6d6f184552cf85c11f1098633d8228d81bb87 Mon Sep 17 00:00:00 2001 From: Shubham Date: Fri, 5 Apr 2024 16:05:12 +0530 Subject: [PATCH] chore: move to built-in URL class (#1017) --- package.json | 2 -- spec/validation.spec.js | 2 +- src/webhooks/webhooks.ts | 17 ++++++++--------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 24ed143f90..b43d38da07 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/spec/validation.spec.js b/spec/validation.spec.js index c7566039af..7e1e67f6ac 100644 --- a/spec/validation.spec.js +++ b/spec/validation.spec.js @@ -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="; const isValid = validateRequest( token, signature, diff --git a/src/webhooks/webhooks.ts b/src/webhooks/webhooks.ts index b5f6c7e292..edc8791775 100644 --- a/src/webhooks/webhooks.ts +++ b/src/webhooks/webhooks.ts @@ -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 { @@ -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 { +function buildUrlWithStandardPort(parsedUrl: URL): string { let url = ""; const port = parsedUrl.protocol === "https:" ? ":443" : ":80"; @@ -74,7 +73,7 @@ function buildUrlWithStandardPort(parsedUrl: Url): 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; } @@ -85,7 +84,7 @@ function buildUrlWithStandardPort(parsedUrl: Url): string { @param parsedUrl - The parsed url object that Twilio requested on your server @returns URL with port */ -function addPort(parsedUrl: Url): string { +function addPort(parsedUrl: URL): string { if (!parsedUrl.port) { return buildUrlWithStandardPort(parsedUrl); } @@ -98,8 +97,8 @@ function addPort(parsedUrl: Url): string { @param parsedUrl - The parsed url object that Twilio requested on your server @returns URL without port */ -function removePort(parsedUrl: Url): string { - parsedUrl.set("port", ""); +function removePort(parsedUrl: URL): string { + parsedUrl.port = ""; return parsedUrl.toString(); } @@ -179,7 +178,7 @@ export function validateRequest( params: Record ): boolean { twilioHeader = twilioHeader || ""; - const urlObject = new Url(url); + const urlObject = new URL(url); const urlWithPort = addPort(urlObject); const urlWithoutPort = removePort(urlObject); @@ -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") || "") ); }