From ee4e1f5429b18df509a26c423d5294a97011f153 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 3 Nov 2023 11:07:39 +0000 Subject: [PATCH] Trim project name and validate whitespace (#14976) * add whitespace check to fixProjectname * add whitespace check to validateProjectName * Log a warning if the project name starts or ends with whitespace --------- Co-authored-by: Alf Eaton GitOrigin-RevId: ba81e0f795e52dbba92c9fbb48cebfd8e547af08 --- .../Features/Project/ProjectDetailsHandler.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/services/web/app/src/Features/Project/ProjectDetailsHandler.js b/services/web/app/src/Features/Project/ProjectDetailsHandler.js index 1c90ec9e0b..ad89efe7d5 100644 --- a/services/web/app/src/Features/Project/ProjectDetailsHandler.js +++ b/services/web/app/src/Features/Project/ProjectDetailsHandler.js @@ -137,6 +137,19 @@ async function validateProjectName(name) { 'Project name cannot contain \\ characters' ) } + if (name !== name.trim()) { + const message = 'Project name cannot start or end with whitespace' + + const error = new Errors.InvalidNameError({ + message, + info: { name }, + }) + + logger.warn({ error }, message) + + // TODO: throw the error, after checking that it won't cause problems + // throw error + } } // FIXME: we should put a lock around this to make it completely safe, but we would need to do that at @@ -163,7 +176,10 @@ async function generateUniqueName(userId, name, suffixes = []) { } function fixProjectName(name) { - if (name === '' || !name) { + // Remove any leading or trailing whitespace + name = typeof name === 'string' ? name.trim() : '' + // Apply a default name if the name is empty + if (name === '') { name = 'Untitled' } if (name.indexOf('/') > -1) {