-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve the actual paths for base and target
Signed-off-by: blam <ben@blam.sh>
- Loading branch information
1 parent
1ab0d3e
commit 78f892b
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2024 The Backstage Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { createMockDirectory } from '@backstage/backend-test-utils'; | ||
import { resolveSafeChildPath } from './paths'; | ||
|
||
describe('paths', () => { | ||
describe('resolveSafeChildPath', () => { | ||
const mockDir = createMockDirectory(); | ||
const secondDirectory = createMockDirectory(); | ||
|
||
const workspacePath = mockDir.resolve('workspace'); | ||
|
||
beforeEach(() => { | ||
mockDir.setContent({ | ||
[`${workspacePath}/README.md`]: '### README.md', | ||
}); | ||
secondDirectory.setContent({ | ||
[`index.md`]: '### index.md', | ||
}); | ||
}); | ||
|
||
it('should throw an error if the path is outside of the base path', () => { | ||
expect(() => | ||
resolveSafeChildPath(workspacePath, secondDirectory.path), | ||
).toThrow( | ||
'Relative path is not allowed to refer to a directory outside its parent', | ||
); | ||
}); | ||
|
||
it('should resolve to the full path if the target is inside the directory', () => { | ||
expect(resolveSafeChildPath(workspacePath, './README.md')).toEqual( | ||
`${workspacePath}/README.md`, | ||
); | ||
}); | ||
|
||
it('should throw an error if the path is a symlink to a directory outside of the base path', () => { | ||
mockDir.addContent({ | ||
[`${workspacePath}/symlink`]: ({ symlink }) => | ||
symlink(secondDirectory.path), | ||
}); | ||
|
||
expect(() => | ||
resolveSafeChildPath(workspacePath, './symlink/index.md'), | ||
).toThrow( | ||
'Relative path is not allowed to refer to a directory outside its parent', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters