Skip to content

Commit

Permalink
fix: make sure to construct the target from the resolved base path too
Browse files Browse the repository at this point in the history
Signed-off-by: blam <ben@blam.sh>
  • Loading branch information
benjdlambert committed Feb 22, 2024
1 parent 8cc5dd9 commit edf65d7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/backend-common/src/paths.test.ts
Expand Up @@ -16,6 +16,7 @@

import { createMockDirectory } from '@backstage/backend-test-utils';
import { resolveSafeChildPath } from './paths';
import fs from 'fs/promises';

describe('paths', () => {
describe('resolveSafeChildPath', () => {
Expand All @@ -41,9 +42,9 @@ describe('paths', () => {
);
});

it('should resolve to the full path if the target is inside the directory', () => {
it('should resolve to the full path if the target is inside the directory', async () => {
expect(resolveSafeChildPath(workspacePath, './README.md')).toEqual(
`${workspacePath}/README.md`,
`${await fs.realpath(workspacePath)}/README.md`,
);
});

Expand Down
5 changes: 3 additions & 2 deletions packages/backend-common/src/paths.ts
Expand Up @@ -63,9 +63,10 @@ export function resolvePackagePath(name: string, ...paths: string[]) {
* @returns A path that is guaranteed to point to or within the base path.
*/
export function resolveSafeChildPath(base: string, path: string): string {
const targetPath = resolvePath(base, path);
const resolvedBasePath = resolveRealPath(base);
const targetPath = resolvePath(resolvedBasePath, path);

if (!isChildPath(resolveRealPath(base), resolveRealPath(targetPath))) {
if (!isChildPath(resolvedBasePath, resolveRealPath(targetPath))) {
throw new NotAllowedError(
'Relative path is not allowed to refer to a directory outside its parent',
);
Expand Down

0 comments on commit edf65d7

Please sign in to comment.