From 1ad2b1b61ebb430051f7d804b0cc7ebfe7922b6f Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 22 Feb 2024 22:16:25 +0100 Subject: [PATCH] chore: return the original input path, and add a test Signed-off-by: blam --- packages/backend-common/src/paths.test.ts | 11 ++++++++--- packages/backend-common/src/paths.ts | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/backend-common/src/paths.test.ts b/packages/backend-common/src/paths.test.ts index 94b96580e9..6073d43a24 100644 --- a/packages/backend-common/src/paths.test.ts +++ b/packages/backend-common/src/paths.test.ts @@ -16,7 +16,6 @@ import { createMockDirectory } from '@backstage/backend-test-utils'; import { resolveSafeChildPath } from './paths'; -import fs from 'fs/promises'; describe('paths', () => { describe('resolveSafeChildPath', () => { @@ -42,9 +41,9 @@ describe('paths', () => { ); }); - it('should resolve to the full path if the target is inside the directory', async () => { + it('should resolve to the full path if the target is inside the directory', () => { expect(resolveSafeChildPath(workspacePath, './README.md')).toEqual( - `${await fs.realpath(workspacePath)}/README.md`, + `${workspacePath}/README.md`, ); }); @@ -60,5 +59,11 @@ describe('paths', () => { 'Relative path is not allowed to refer to a directory outside its parent', ); }); + + it('should not throw an error when a folder is referenced that doesnt already exist', () => { + expect(resolveSafeChildPath(workspacePath, 'template')).toEqual( + `${workspacePath}/template`, + ); + }); }); }); diff --git a/packages/backend-common/src/paths.ts b/packages/backend-common/src/paths.ts index b5d4b331fd..c77f232633 100644 --- a/packages/backend-common/src/paths.ts +++ b/packages/backend-common/src/paths.ts @@ -72,7 +72,8 @@ export function resolveSafeChildPath(base: string, path: string): string { ); } - return targetPath; + // Don't return the resolved path as the original could be a symlink + return resolvePath(base, path); } function resolveRealPath(path: string): string {