Prevent chunk files from escaping output.path when chunk names contain path separators #20783
Replies: 1 comment 1 reply
-
|
Changing output path using
No, you can't use numeric id because it contains something in comments, it will break long term cache
Will break a lot of patterns
This is the only one place where we can improve it, but again - please show me your real problem to understand why it happens |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Have you used AI?
None
Feature Proposal
Please harden chunk name handling so path separator components in webpackChunkName cannot cause emitted chunk files to resolve outside output.path.
A value like
"../../outside/chunk"should either emit a warning and fall back toa numeric chunk ID, or have the path components stripped silently. As currently implemented, webpackChunkName values appear to flow into emitted chunk paths without containment enforcement.
Three locations could each independently implement this:
lib/dependencies/ImportParserPlugin.js(~line 337): validatewebpackChunkNamebefore storing it on the chunk object. Reject valuescontaining
/,\, or..and emit a warning, falling back to a numeric ID.This is the earliest and most visible fix point.
lib/TemplatedPathPlugin.js(lines 262 and 285): applypath.basename()to the
[name]replacement value before substitution, stripping any directorycomponents regardless of how the chunk name was set.
lib/Compiler.jsemitAssetsfunction: after computingtargetPath = join(outputFileSystem, outputPath, targetFile), assert thattargetPathstarts withoutputPathbefore proceeding tomkdirpandwriteFile. This defense-in-depth check would cover all asset emission paths,including the
[path]/[pathname]cases in [path] in assetModuleFilename may unexpectedly traverse parent directories #11937 and asset/resource builds outside of dist when [pathname] contains references to hoisted node_modules #14392.Option 1 gives the clearest developer-facing feedback. Option 3 is the broadest
fix and closes the same gap for asset module filenames at the same time.
Thank you! Keep up the great work
Feature Use Case
Feature Use Case
When a
webpackChunkNamemagic comment contains../sequences, webpack writesthe generated chunk file to a path outside
output.pathwith no warning or error.This is surprising because
output.pathis documented as the target outputdirectory, and developers expect all emitted files to be contained within it.
Reproduction
src/app.js:After running npx webpack:
$ find dist -name "*.js" | sort
dist/a/b/main.js# expected -- inside output.pathdist/outside/chunk.js# unexpected -- escaped output.path via ../../dist/outside/chunk.jsis written two levels above the configured output.pathof dist/a/b. The directory is created automatically by webpack if it does not
exist.
Why this is surprising:
path.posix.join('/project/dist/a/b', '../../outside/chunk.js')resolves to
/project/dist/outside/chunk.js. webpack performs this join atCompiler.js:742and proceeds to create parent directories and write the fileat the resolved path with no check that it remains within
output.path.Expected behavior: I think webpack should treat output.path as a strict containment boundary for emitted chunk assets.
This behavior also appears in #11937 ([path] in assetModuleFilename) and
#14392 ([pathname] with hoisted node_modules). A fix at the emitAssets
level would resolve all three cases in one place.
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions