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

fix: validate non-string path params #835

Merged
merged 1 commit into from
Dec 6, 2022
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
15 changes: 9 additions & 6 deletions lib/base/utility.ts
Original file line number Diff line number Diff line change
@@ -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)
);
}
2 changes: 2 additions & 0 deletions spec/unit/base/utility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ describe("isValidPathParam", () => {

expect(isValidPathParam("AC123")).toBeTruthy();
expect(isValidPathParam("space in name")).toBeTruthy();
expect(isValidPathParam(123)).toBeTruthy();
expect(isValidPathParam({})).toBeTruthy();
});
});