Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion forge/db/controllers/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ module.exports = {
throw new ControllerError('invalid_project_name', 'name not allowed', 409)
}

if (/^[a-zA-Z][a-zA-Z0-9-]*$/.test(safeName) === false) {
if (app.db.models.Project.NAME_PATTERN.test(safeName) === false) {
throw new ControllerError('invalid_project_name', 'name not allowed', 409)
}

Expand Down
3 changes: 3 additions & 0 deletions forge/db/models/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const BANNED_NAME_LIST = [
'registry'
]

const NAME_PATTERN = /^[a-zA-Z][a-zA-Z0-9-]*$/

/** @type {FFModel} */
module.exports = {
name: 'Project',
Expand Down Expand Up @@ -429,6 +431,7 @@ module.exports = {
},
static: {
BANNED_NAME_LIST,
NAME_PATTERN,
isNameUsed: async (name) => {
const safeName = name?.toLowerCase()
const count = await this.count({
Expand Down
6 changes: 5 additions & 1 deletion forge/routes/api/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,10 @@ module.exports = async function (app) {
reply.status(409).type('application/json').send({ code: 'invalid_project_name', error: 'name not allowed' })
return
}
if (app.db.models.Project.NAME_PATTERN.test(reqSafeName) === false) {
reply.status(409).type('application/json').send({ code: 'invalid_project_name', error: 'name not allowed' })
return
}
if (await app.db.models.Project.isNameUsed(reqSafeName)) {
reply.status(409).type('application/json').send({ code: 'invalid_project_name', error: 'name in use' })
return
Expand Down Expand Up @@ -1384,7 +1388,7 @@ module.exports = async function (app) {
return
}

if (/^[a-zA-Z][a-zA-Z0-9-]*$/.test(safeName) === false) {
if (app.db.models.Project.NAME_PATTERN.test(safeName) === false) {
reply.code(409).send({ code: 'invalid_project_name', error: 'name not allowed' })
return
}
Expand Down
27 changes: 22 additions & 5 deletions test/unit/forge/routes/api/project_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ describe('Project API', function () {
method: 'PUT',
url: `/api/v1/projects/${TestObjects.project1.id}`,
payload: {
name: 'new project name'
name: 'new-project-name'
},
cookies: { sid: TestObjects.tokens.alice }
})
Expand All @@ -1721,7 +1721,7 @@ describe('Project API', function () {
url: `/api/v1/projects/${TestObjects.project1.id}`,
cookies: { sid: TestObjects.tokens.alice }
})
JSON.parse(newResponse.payload).should.have.property('name', 'new project name')
JSON.parse(newResponse.payload).should.have.property('name', 'new-project-name')
})

it('Updates the name fail for running instance', async function () {
Expand All @@ -1746,12 +1746,12 @@ describe('Project API', function () {
method: 'PUT',
url: `/api/v1/projects/${TestObjects.project1.id}`,
payload: {
name: 'new project name'
name: 'new-project-name'
},
cookies: { sid: TestObjects.tokens.alice }
})
response.statusCode.should.equal(200)
JSON.parse(response.payload).should.have.property('name', 'new project name')
JSON.parse(response.payload).should.have.property('name', 'new-project-name')
})

it('Non-owner cannot change project name', async function () {
Expand All @@ -1760,12 +1760,29 @@ describe('Project API', function () {
method: 'PUT',
url: `/api/v1/projects/${TestObjects.project1.id}`,
payload: {
name: 'new project name'
name: 'new-project-name'
},
cookies: { sid: TestObjects.tokens.bob }
})
response.statusCode.should.equal(403)
})

it('Rejects a name that fails the create-time pattern', async function () {
await TestObjects.project1.reload()
const originalName = TestObjects.project1.name
for (const name of ['bad.name:1/x', 'Bad/Name!!<x>', '1starts-with-digit', 'has space']) {
const response = await app.inject({
method: 'PUT',
url: `/api/v1/projects/${TestObjects.project1.id}`,
payload: { name },
cookies: { sid: TestObjects.tokens.alice }
})
response.statusCode.should.equal(409)
response.json().should.have.property('code', 'invalid_project_name')
}
await TestObjects.project1.reload()
TestObjects.project1.name.should.equal(originalName)
})
})

it('Change 1 project setting', async function () {
Expand Down
Loading