Skip to content

Commit a6cdbf4

Browse files
authored
fix(filesystem): reject Windows paths on POSIX (#4704)
Co-authored-by: Yohanes <CryoThrust@users.noreply.github.com>
1 parent ebaccfd commit a6cdbf4

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ describe('Lib Functions', () => {
173173

174174
describe('Security & Validation Functions', () => {
175175
describe('validatePath', () => {
176+
it('rejects Windows drive paths on POSIX hosts', async () => {
177+
if (process.platform === 'win32') return;
178+
179+
await expect(validatePath('C:\\Users\\me\\notes\\file.md'))
180+
.rejects.toThrow('Windows-style path received on a POSIX host');
181+
});
182+
176183
// Use Windows-compatible paths for testing
177184
const allowedDirs = process.platform === 'win32' ? ['C:\\Users\\test', 'C:\\temp'] : ['/home/user', '/tmp'];
178185

‎src/filesystem/lib.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ async function resolveUnicodeEquivalentPath(absolutePath: string): Promise<strin
139139

140140
export async function validatePath(requestedPath: string): Promise<string> {
141141
const expandedPath = expandHome(requestedPath);
142+
// Do not silently reinterpret a Windows drive path as a relative POSIX path.
143+
// This would create a literal filename such as `C:\\Users\\...` inside the
144+
// allowed root and report success for the wrong location.
145+
if (process.platform !== 'win32' && /^(?:[A-Za-z]:)(?:[\\/]|$)/.test(expandedPath)) {
146+
throw new Error(`Access denied - Windows-style path received on a POSIX host: ${requestedPath}`);
147+
}
142148
const absolute = path.isAbsolute(expandedPath)
143149
? path.resolve(expandedPath)
144150
: resolveRelativePathAgainstAllowedDirectories(expandedPath);

0 commit comments

Comments
 (0)