diff --git a/forge/db/controllers/ProjectTemplate.js b/forge/db/controllers/ProjectTemplate.js index ebf845ef78..6e06fca8e0 100644 --- a/forge/db/controllers/ProjectTemplate.js +++ b/forge/db/controllers/ProjectTemplate.js @@ -219,11 +219,35 @@ module.exports = { throw new Error('Invalid settings.apiMaxLength') } } - if (result.debugMaxLenth) { - if (!Number.isInteger(result.debugMaxLength) || result.debugMaxLength < 0) { + const isClearedDebugMaxLength = result.debugMaxLength === '' || result.debugMaxLength === null + if (result.debugMaxLength !== undefined && !isClearedDebugMaxLength) { + const isStringNumber = typeof result.debugMaxLength === 'string' && /^\d+$/.test(result.debugMaxLength) + const debugMaxLength = isStringNumber ? Number(result.debugMaxLength) : result.debugMaxLength + const isValid = Number.isInteger(debugMaxLength) && debugMaxLength >= 0 + if (isValid) { + result.debugMaxLength = debugMaxLength + } else if (importing) { + delete result.debugMaxLength + } else { throw new Error('Invalid settings.debugMaxLength') } } + const textSettings = ['codeEditor', 'theme', 'page_title', 'page_favicon', 'header_title', 'header_url'] + textSettings.forEach(name => { + const value = getTemplateValue(result, name) + const isSet = value !== undefined && value !== null + if (isSet && typeof value !== 'string') { + const [group, key] = name.split('_') + if (key) { + delete result[group][key] + } else { + delete result[group] + } + } + }) + if (result.disableEditor !== undefined && typeof result.disableEditor !== 'boolean') { + delete result.disableEditor + } if (result.httpNodeCORS) { if (result.httpNodeCORS?.origin) { if (result.httpNodeCORS.origin !== '*') { @@ -235,6 +259,7 @@ module.exports = { if (url.pathname !== '/') { throw new Error('Invalid httpNodeCORS.origin path') } + result.httpNodeCORS.origin = url.origin } catch (err) { throw new Error(`Invalid httpNodeCORS.origin - ${err.toString()}`) } diff --git a/test/unit/forge/db/controllers/ProjectTemplate_spec.js b/test/unit/forge/db/controllers/ProjectTemplate_spec.js index 574e559c5e..fe3b9d3924 100644 --- a/test/unit/forge/db/controllers/ProjectTemplate_spec.js +++ b/test/unit/forge/db/controllers/ProjectTemplate_spec.js @@ -106,6 +106,76 @@ describe('Project Template controller', function () { } }) + describe('debugMaxLength', function () { + const validate = (debugMaxLength, importing) => app.db.controllers.ProjectTemplate.validateSettings({ debugMaxLength }, undefined, importing) + + it('rejects values that are not a whole number', function () { + ;['1000 + 1', 'abc', -1, 1.5].forEach(value => { + should.throws(() => validate(value), /Invalid settings.debugMaxLength/) + }) + }) + it('stores a numeric string as a number', function () { + validate('456').should.have.property('debugMaxLength', 456) + }) + it('keeps an empty value so clearing the field is saved', function () { + validate('').should.have.property('debugMaxLength', '') + validate(null).should.have.property('debugMaxLength', null) + }) + it('drops an invalid value when importing', function () { + validate('abc', true).should.not.have.property('debugMaxLength') + }) + }) + + describe('editor setting types', function () { + const validate = (settings) => app.db.controllers.ProjectTemplate.validateSettings(settings) + const textSettings = [ + ['codeEditor'], + ['theme'], + ['page', 'title'], + ['page', 'favicon'], + ['header', 'title'], + ['header', 'url'] + ] + const build = (path, value) => path.length === 1 ? { [path[0]]: value } : { [path[0]]: { [path[1]]: value } } + const read = (result, path) => path.reduce((o, k) => o?.[k], result) + + it('keeps string and null text settings', function () { + textSettings.forEach(path => { + should(read(validate(build(path, 'value')), path)).equal('value') + should(read(validate(build(path, null)), path)).equal(null) + }) + }) + it('drops text settings that are not strings', function () { + textSettings.forEach(path => { + ;[{}, 1, ['a']].forEach(value => { + should(read(validate(build(path, value)), path)).be.undefined() + }) + }) + }) + it('keeps a boolean disableEditor', function () { + validate({ disableEditor: true }).should.have.property('disableEditor', true) + validate({ disableEditor: false }).should.have.property('disableEditor', false) + }) + it('drops a disableEditor that is not a boolean', function () { + validate({ disableEditor: 'true' }).should.not.have.property('disableEditor') + }) + }) + + describe('httpNodeCORS origin', function () { + const validate = (origin) => app.db.controllers.ProjectTemplate.validateSettings({ + httpNodeCORS: { enabled: true, origin, GET: true, POST: true, PUT: true, PATCH: true, HEAD: true, DELETE: true } + }) + + it('stores the normalised origin', function () { + validate('http://a.example/').httpNodeCORS.should.have.property('origin', 'http://a.example') + validate('https://a.example:8443/').httpNodeCORS.should.have.property('origin', 'https://a.example:8443') + validate('http://a.example/#" + x + "').httpNodeCORS.should.have.property('origin', 'http://a.example') + }) + it('keeps a wildcard origin', function () { + validate('*').httpNodeCORS.should.have.property('origin', '*') + }) + }) + it('only passes through settings a template policy allows', async function () { const templateProperties = { name: 'template',