Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't allow files to be nested inside other files #1052

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@semantic-release/npm": "^9.0.1",
"@types/jest": "^29.0.0",
"@types/jest": "^29.5.12",
"@types/mime": "^3.0.0",
"@types/node": "^10.17.60",
"app-root-path": "^3.1.0",
Expand Down
20 changes: 18 additions & 2 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -911,8 +911,17 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
readFileSync(file: TFileId, options?: opts.IReadFileOptions | string): TDataOut {
const opts = getReadFileOptions(options);
const flagsNum = flagsToNumber(opts.flag);
return this.readFileBase(file, flagsNum, opts.encoding as BufferEncoding);

// Check if the path is a directory, and throw EISDIR error if so
if (typeof file === 'string') {
if (this.existsSync(file) && this.statSync(file).isDirectory()) {
throw createError('EISDIR', 'readFile', file);
}
}

return this.readFileBase(file, flagsNum, opts.encoding as BufferEncoding);
}


readFile(id: TFileId, callback: TCallback<TDataOut>);
readFile(id: TFileId, options: opts.IReadFileOptions | string, callback: TCallback<TDataOut>);
Expand Down Expand Up @@ -1064,8 +1073,15 @@ export class Volume implements FsCallbackApi, FsSynchronousApi {
const flagsNum = flagsToNumber(opts.flag);
const modeNum = modeToNumber(opts.mode);
const buf = dataToBuffer(data, opts.encoding);

// Check if the parent path is a file, and throw ENOTDIR error if so
const parentPath = pathModule.dirname(id as string);
if (this.existsSync(parentPath) && !this.statSync(parentPath).isDirectory()) {
throw createError('ENOTDIR', 'writeFile', id as string);
}

this.writeFileBase(id, buf, flagsNum, modeNum);
}
}

writeFile(id: TFileId, data: TData, callback: TCallback<void>);
writeFile(id: TFileId, data: TData, options: opts.IWriteFileOptions | string, callback: TCallback<void>);
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
"declaration": true,
"skipLibCheck": true,
"noEmitHelpers": true,
"importHelpers": true
"importHelpers": true,
"types": ["node", "jest"]
},
"include": ["src"],
"exclude": ["src/__tests__", "node_modules", "lib", "es6", "es2020", "esm", "docs", "README.md"],
"include": ["src", "src/__tests__"],
"exclude": ["node_modules", "lib", "es6", "es2020", "esm", "docs", "README.md"],
"typedocOptions": {
"entryPoints": [
"src/index.ts",
Expand Down
Loading
Loading