From fff93f392aa059b4374fc2e242c35bb755f2fbea Mon Sep 17 00:00:00 2001 From: Dan Gowans Date: Tue, 24 Jan 2023 10:39:16 -0500 Subject: [PATCH] simplify regular expressions --- helpers/functions.authentication.js | 9 ++++----- helpers/functions.authentication.ts | 10 +++++----- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/helpers/functions.authentication.js b/helpers/functions.authentication.js index 72b3d300..c355a57f 100644 --- a/helpers/functions.authentication.js +++ b/helpers/functions.authentication.js @@ -43,6 +43,8 @@ const safeRedirects = new Set([ '/workorders/outlook', '/reports' ]); +const recordUrl = /^(\/(maps|lots|lotoccupancies|workorders)\/)\d+(\/edit)?$/; +const printUrl = /^\/print\/(pdf|screen)\/[\d/=?A-Za-z-]+$/; export function getSafeRedirectURL(possibleRedirectURL = '') { const urlPrefix = configFunctions.getProperty('reverseProxy.urlPrefix'); if (typeof possibleRedirectURL === 'string') { @@ -51,11 +53,8 @@ export function getSafeRedirectURL(possibleRedirectURL = '') { : possibleRedirectURL; const urlToCheckLowerCase = urlToCheck.toLowerCase(); if (safeRedirects.has(urlToCheckLowerCase) || - /^(\/maps\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^(\/lots\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^(\/lotoccupancies\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^(\/workorders\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^\/print\/(pdf|screen)\/[\d/=?A-Za-z-]+$/.test(urlToCheck)) { + recordUrl.test(urlToCheckLowerCase) || + printUrl.test(urlToCheck)) { return urlPrefix + urlToCheck; } } diff --git a/helpers/functions.authentication.ts b/helpers/functions.authentication.ts index 8a1a6a45..5613662d 100644 --- a/helpers/functions.authentication.ts +++ b/helpers/functions.authentication.ts @@ -59,6 +59,9 @@ const safeRedirects = new Set([ '/reports' ]) +const recordUrl = /^(\/(maps|lots|lotoccupancies|workorders)\/)\d+(\/edit)?$/ +const printUrl = /^\/print\/(pdf|screen)\/[\d/=?A-Za-z-]+$/ + export function getSafeRedirectURL(possibleRedirectURL = ''): string { const urlPrefix = configFunctions.getProperty('reverseProxy.urlPrefix') @@ -71,11 +74,8 @@ export function getSafeRedirectURL(possibleRedirectURL = ''): string { if ( safeRedirects.has(urlToCheckLowerCase) || - /^(\/maps\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^(\/lots\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^(\/lotoccupancies\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^(\/workorders\/)\d+(\/edit)?$/.test(urlToCheckLowerCase) || - /^\/print\/(pdf|screen)\/[\d/=?A-Za-z-]+$/.test(urlToCheck) + recordUrl.test(urlToCheckLowerCase) || + printUrl.test(urlToCheck) ) { return urlPrefix + urlToCheck }