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
This commit is contained in:
Brian Gough 2023-11-03 11:07:39 +00:00 committed by Copybot
parent 6b8dfaa782
commit ee4e1f5429

View file

@ -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) {