Skip to content

Commit

Permalink
chore: move to built-in URL class (#1017)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiwarishubham635 committed Apr 5, 2024
1 parent ca6aaf0 commit 18c6d6f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
2 changes: 0 additions & 2 deletions package.json
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
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=";
const isValid = validateRequest(
token,
signature,
Expand Down
17 changes: 8 additions & 9 deletions 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 {
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

0 comments on commit 18c6d6f

Please sign in to comment.