fix(filesystem): reject Windows paths on POSIX (#4704)
Co-authored-by: Yohanes <CryoThrust@users.noreply.github.com>
This commit is contained in:
parent
ebaccfd623
commit
a6cdbf4dea
2 changed files with 13 additions and 0 deletions
|
|
@ -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'];
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue