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

chore: moving off from url-parse #1017

Merged
merged 2 commits into from
Apr 5, 2024
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
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=";
manisha1997 marked this conversation as resolved.
Show resolved Hide resolved
const signature = "TfZzewPq8wqrGlMfyAud8+/IvJ0=";
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