fix(filesystem): reject Windows paths on POSIX (#4704)

Co-authored-by: Yohanes <CryoThrust@users.noreply.github.com>
This commit is contained in:
Yohanes 2026-08-31 05:43:38 +08:00 • committed by GitHub
commit a6cdbf4dea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View file

@ -173,6 +173,13 @@ describe('Lib Functions', () => {
describe('Security & Validation Functions', () => {
describe('validatePath', () => {
it('rejects Windows drive paths on POSIX hosts', async () => {
if (process.platform === 'win32') return;
await expect(validatePath('C:\\Users\\me\\notes\\file.md'))
.rejects.toThrow('Windows-style path received on a POSIX host');
});
// Use Windows-compatible paths for testing
const allowedDirs = process.platform === 'win32' ? ['C:\\Users\\test', 'C:\\temp'] : ['/home/user', '/tmp'];

View file

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