From afb5c4381948a752770446181ad14987fa0546c1 Mon Sep 17 00:00:00 2001 From: Sam Harrison Date: Mon, 5 Dec 2022 13:48:27 -0600 Subject: [PATCH] fix: validate non-string path params --- lib/base/utility.ts | 15 +++++++++------ spec/unit/base/utility.spec.ts | 2 ++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/base/utility.ts b/lib/base/utility.ts index cc259ef7d3..4b5dba12e9 100644 --- a/lib/base/utility.ts +++ b/lib/base/utility.ts @@ -1,11 +1,14 @@ +const INVALID_PATH_PARAM_CHARS = ["/", "?"]; + export const trim = (str, c = "\\s") => str.replace(new RegExp(`^([${c}]*)(.*?)([${c}]*)$`), "$2"); -export function isValidPathParam(param: string): boolean { - return ( - param !== null && - param !== undefined && - !param.includes("/") && - !param.includes("?") +export function isValidPathParam(param: any): boolean { + if (param === null || param === undefined) return false; + + const paramString = param.toString(); + + return INVALID_PATH_PARAM_CHARS.every( + (invalidChar) => !paramString.includes(invalidChar) ); } diff --git a/spec/unit/base/utility.spec.ts b/spec/unit/base/utility.spec.ts index a88d337b8b..538e471f1b 100644 --- a/spec/unit/base/utility.spec.ts +++ b/spec/unit/base/utility.spec.ts @@ -9,5 +9,7 @@ describe("isValidPathParam", () => { expect(isValidPathParam("AC123")).toBeTruthy(); expect(isValidPathParam("space in name")).toBeTruthy(); + expect(isValidPathParam(123)).toBeTruthy(); + expect(isValidPathParam({})).toBeTruthy(); }); });