From 03c1128a726b6b05a4411899d1c7d9c133ab2882 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Wed, 29 Jan 2025 17:07:48 +0100 Subject: [PATCH] fix(isEmptyOrWhitespace): avoid exception throwing when passed value is undefined the req.body value from "routes/api/branches" actually seems to never get parsed into a JS object, but arrives as text string, so req.body.prefix could be undefined, which of course would cause an error to be thrown, when trying to call "match" on undefined. --- src/routes/api/branches.ts | 1 + src/services/utils.ts | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/routes/api/branches.ts b/src/routes/api/branches.ts index 746ebb92a..b9c5f751d 100644 --- a/src/routes/api/branches.ts +++ b/src/routes/api/branches.ts @@ -216,6 +216,7 @@ function deleteBranch(req: Request) { function setPrefix(req: Request) { const branchId = req.params.branchId; + //TriliumNextTODO: req.body arrives as string, so req.body.prefix will be undefined – did the code below ever even work? const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix; const branch = becca.getBranchOrThrow(branchId); diff --git a/src/services/utils.ts b/src/services/utils.ts index b00c1e488..f101f6064 100644 --- a/src/services/utils.ts +++ b/src/services/utils.ts @@ -71,8 +71,9 @@ export function hash(text: string) { return crypto.createHash("sha1").update(text).digest("base64"); } -export function isEmptyOrWhitespace(str: string) { - return str === null || str.match(/^ *$/) !== null; +export function isEmptyOrWhitespace(str: string | null | undefined) { + if (!str) return true; + return str.match(/^ *$/) !== null; } export function sanitizeSqlIdentifier(str: string) {