fix: prevent temp directory leak in node-modules-utils#788
Open
parker-snyk wants to merge 2 commits intomainfrom
Open
fix: prevent temp directory leak in node-modules-utils#788parker-snyk wants to merge 2 commits intomainfrom
parker-snyk wants to merge 2 commits intomainfrom
Conversation
The persistNodeModules function had a scoping bug that could leak
temporary directories on disk.
The outer variables tmpDir and tempProjectRoot were initialized as
empty strings (lines 79-80). Inside the try block, createTempProjectDir
created a real temp directory and destructured the result into NEW
block-scoped variables with the same names (line 92: const { tmpDir,
tempProjectRoot } = ...). If saveOnDisk or any subsequent step threw,
the catch block returned the OUTER empty strings — not the temp
directory that was actually created. The caller, seeing empty strings,
would skip cleanup (line 116: if (!tempDir) continue), leaving the
temp directory orphaned.
Fix: removed the shadowing const destructure and instead assign to
the outer let variables, so the catch block always returns the actual
temp directory path for cleanup.
Also added type annotations to createFile() parameters (filePath,
fileContent were implicit any).
parker-snyk
added a commit
that referenced
this pull request
Apr 9, 2026
Adds unit tests for the persistNodeModules function to ensure that temporary directories are not leaked when an error occurs during disk write operations, verifying the fix implemented for PR #788.
parker-snyk
added a commit
that referenced
this pull request
Apr 9, 2026
Adds unit tests for the persistNodeModules function to ensure that temporary directories are not leaked when an error occurs during disk write operations. Verifying the fix implemented for PR #788.
838fe07 to
f5d9543
Compare
Adds unit tests for the persistNodeModules function to ensure that temporary directories are not leaked when an error occurs during disk write operations.
f5d9543 to
ffe2547
Compare
PR Reviewer Guide 🔍
|
craigarmstrong
approved these changes
Apr 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
createFilefunctionProblem
persistNodeModules()creates a temporary directory to write extracted node_modules files for dependency resolution. The function had a scoping bug:If
createTempProjectDirsucceeds (creating a real directory on disk) butsaveOnDiskor any later step throws, the catch block returns the outer empty strings instead of the actual temp directory path. The caller checks:This skips cleanup, leaving the temp directory orphaned on disk. Over many scans (especially in CI), these leaked directories accumulate.
Fix
Changed from
constdestructuring (which creates new block-scoped variables) to assigning toletvariables declared in the function scope:Also
Added
stringtype annotations tocreateFile(filePath, fileContent)parameters (were implicitany).Risk
Low. The fix changes variable scoping to prevent shadowing. The happy path behavior is identical. The error path now correctly returns the temp directory path so the caller can clean it up.
How should this be manually tested?
Any background context you want to provide?
What are the relevant tickets?
https://snyksec.atlassian.net/browse/CN-1040
Additional questions