Преглед изворни кода

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 <alf.eaton@overleaf.com>
GitOrigin-RevId: ba81e0f795e52dbba92c9fbb48cebfd8e547af08
Brian Gough пре 2 година
родитељ
комит
ee4e1f5429
1 измењених фајлова са 17 додато и 1 уклоњено
  1. 17 1
      services/web/app/src/Features/Project/ProjectDetailsHandler.js

+ 17 - 1
services/web/app/src/Features/Project/ProjectDetailsHandler.js

@@ -137,6 +137,19 @@ async function validateProjectName(name) {
       'Project name cannot contain \\ characters'
       '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
 // 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) {
 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'
     name = 'Untitled'
   }
   }
   if (name.indexOf('/') > -1) {
   if (name.indexOf('/') > -1) {