Skip to content

Commit 1125377

Browse files
authored
fix(fmt): reject unsafe cache locations (#234)
1 parent 7dcb51d commit 1125377

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎packages/rstack/src/fmt/cli.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ const runFmtCLI = async (args: string[]): Promise<void> => {
288288
}
289289

290290
const cacheDirPath = cacheLocation ? path.resolve(cwd, cacheLocation) : undefined;
291+
if (cacheDirPath) {
292+
const cacheDirPrefix = cacheDirPath.endsWith(path.sep)
293+
? cacheDirPath
294+
: `${cacheDirPath}${path.sep}`;
295+
if (cwd === cacheDirPath || cwd.startsWith(cacheDirPrefix)) {
296+
throw new Error(
297+
'The --cache-location directory cannot be the current working directory or an ancestor.',
298+
);
299+
}
300+
}
301+
291302
const config = await loadFmtConfig(cwd);
292303
const files = await discoverFmtFiles({
293304
cwd,

‎packages/rstack/tests/cli/fmt/index.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ test.each(['relative', 'absolute'] as const)('uses a %s custom cache location',
230230
expect(existsSync(path.join(projectPath, '.rstack'))).toBe(false);
231231
});
232232

233+
test.each(['.', '..'])('rejects a custom cache location at %s', (cacheLocation) => {
234+
const result = runFmt(['--cache-location', cacheLocation, '.']);
235+
236+
expect(result.status).toBe(2);
237+
expect(result.stdout).toBe('');
238+
expect(result.stderr).toContain(
239+
'The --cache-location directory cannot be the current working directory or an ancestor.',
240+
);
241+
});
242+
233243
test('excludes the custom cache directory from formatting', () => {
234244
const cacheLocation = 'custom-cache';
235245
writeProjectFile('index.ts', 'const value = 1;\n');

0 commit comments

Comments
 (0)