-
Notifications
You must be signed in to change notification settings - Fork 51.5k
fix(core): Only resolve the filepath once #22767
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
base: master
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 issue found across 11 files
Prompt for AI agents (all 1 issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="packages/core/src/execution-engine/node-execution-context/utils/file-system-helper-functions.ts">
<violation number="1" location="packages/core/src/execution-engine/node-execution-context/utils/file-system-helper-functions.ts:94">
P1: The try/catch block will not catch `ELOOP` errors because `createReadStream` opens files asynchronously. The error is emitted via the stream's `'error'` event, not thrown synchronously. You need to attach an error handler to the stream or use `fs.open()` with `O_NOFOLLOW` first, then create the stream from the fd.</violation>
</file>
Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR
packages/core/src/execution-engine/node-execution-context/utils/file-system-helper-functions.ts
Outdated
Show resolved
Hide resolved
|
E2E Tests: n8n tests passed after 10m 9s Run Details
Groups
This message was posted automatically by
currents.dev | Integration Settings
|
…s/file-system-helper-functions.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
15a88a9 to
5dc9144
Compare
|
|
||
| try { | ||
| await fsAccess(filePath); | ||
| await fsAccess(resolvedFilePath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note: the NodeJS docs say: Using fsPromises.access() to check for the accessibility of a file before calling fsPromises.open() is not recommended.. Rather you should just try to open the file, and handle any potential failure. We should think about fixing this, potentially in this PR; but it's a bit orthogonal to the issue at hand.
| export async function isFilePathBlocked(filePath: string): Promise<boolean> { | ||
| const allowedPaths = getAllowedPaths(); | ||
| let resolvedFilePath = ''; | ||
| const resolvePath = async (path: PathLike): Promise<ResolvedFilePath> => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Rest of the functions in this file have been defined with function. Maybe we could keep it consistent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
packages/workflow/src/interfaces.ts
Outdated
| returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExecutionData[]; | ||
| } | ||
|
|
||
| const __brand = Symbol('brand'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or:
| const __brand = Symbol('brand'); | |
| const __brand = Symbol('resolvedFilePath'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| new NodeOperationError( | ||
| node, | ||
| `The file "${String(resolvedFilePath)}" could not be opened.`, | ||
| { level: 'warning' }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we attach the original error as cause?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. (I think this is what you meant?)
| resolvePath(path: PathLike): Promise<ResolvedFilePath>; | ||
| isFilePathBlocked(filePath: ResolvedFilePath): boolean; | ||
| createReadStream(filePath: ResolvedFilePath): Promise<Readable>; | ||
| getStoragePath(): string; | ||
| writeContentToFile( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add docs to all these functions with mention that "Use resolvePath to resolve the path"?
| ); | ||
| } | ||
| return await fsWriteFile(filePath, content, { encoding: 'binary', flag }); | ||
| return await fsWriteFile(resolvedFilePath, content, { encoding: 'binary', flag }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this still have the issue? Can we make this so that fsWriteFile doesn't follow symlinks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! I think I'm handling the flags correctly here.
Cadiac
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a smart way to solve this 👍
| const resolvePath = async (path: PathLike): Promise<ResolvedFilePath> => { | ||
| try { | ||
| resolvedFilePath = await fsRealpath(filePath); | ||
| return (await fsRealpath(path)) as ResolvedFilePath; // apply brand, since we know it's resolved now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Branded types seem like a smart way to enforce this 👍
Summary
Make sure we only resolve filepaths once, so we know that when we read them they haven't been changed to link somewhere else.
Related Linear tickets, Github issues, and Community forum posts
Review / Merge checklist
release/backport(if the PR is an urgent fix that needs to be backported)