cli/new: reject absolute template paths

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-02-11 14:06:34 +01:00
parent 40208a4bd6
commit c0d11068d5
2 changed files with 27 additions and 1 deletions
@@ -243,6 +243,28 @@ describe('loadPortableTemplateConfig', () => {
);
});
it('should throw an error if template point is absolute', async () => {
mockDir.setContent({
'package.json': JSON.stringify({
backstage: {
cli: {
new: {
templates: ['/invalid'],
},
},
},
}),
});
await expect(
loadPortableTemplateConfig({
packagePath: mockDir.resolve('package.json'),
}),
).rejects.toThrow(
"Failed to load template definition '/invalid'; caused by Error: Template target may not be an absolute path",
);
});
it('should handle missing backstage.new configuration', async () => {
mockDir.setContent({
'package.json': JSON.stringify({}),
@@ -15,7 +15,7 @@
*/
import fs from 'fs-extra';
import { resolve as resolvePath, dirname } from 'node:path';
import { resolve as resolvePath, dirname, isAbsolute } from 'node:path';
import { paths } from '../../paths';
import { defaultTemplates } from '../defaultTemplates';
import {
@@ -126,6 +126,10 @@ export async function loadPortableTemplateConfig(
}
function resolveLocalTemplatePath(pointer: string, basePath: string): string {
if (isAbsolute(pointer)) {
throw new Error(`Template target may not be an absolute path`);
}
if (pointer.startsWith('.')) {
return resolvePath(basePath, pointer, TEMPLATE_FILE_NAME);
}