Skip to content

Commit 00f2c18

Browse files
fix(copy): exclude a file symlink resolving into output.path too
The symlink guard only reached directories. A followed link to a FILE is reported by `stat` as a plain file and returned before anything canonicalizes it, so the lexical check saw the link's own name and copied the build's own output under it. A pattern naming such a link directly took the same path. Reproduced before fixing: with output.path at static/build and a link static/link.txt pointing into it, the copy emitted link.txt carrying the stale output, and `from: static/link.txt` emitted named.txt/link.txt. Resolve a followed file the same way a followed directory already is, in the walk and for a directly named one. Reverting lib/CopyPlugin.js fails both new assertions.
1 parent 8183731 commit 00f2c18

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎lib/CopyPlugin.js‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ const realpath = (fs, directory) =>
246246
});
247247
});
248248

249+
/**
250+
* A followed symlink is read as what it points at, so where its own name sits
251+
* says nothing about where its bytes come from.
252+
* @param {InputFileSystem} fs input file system
253+
* @param {string} filePath absolute path of a file reached through a symlink
254+
* @param {InOutputPathMatcher} isInOutputPath what is treated as output
255+
* @returns {Promise<boolean>} true when the file resolves into the output directory
256+
*/
257+
const resolvesIntoOutputPath = async (fs, filePath, isInOutputPath) => {
258+
const realPath = await realpath(fs, filePath);
259+
return isInOutputPath(normalizePathSeparatorsForPath(realPath));
260+
};
261+
249262
/**
250263
* @param {InputFileSystem} fs input file system
251264
* @param {string} filePath path of the symbolic link
@@ -289,7 +302,12 @@ const walkDirectory = async (options, directory, visited, maxDepth) => {
289302
const stats = isSymlink ? await stat(fs, filePath) : entry;
290303
// the target is gone, or is neither a file nor a directory
291304
if (stats === undefined) return [];
292-
if (stats.isFile()) return [filePath];
305+
if (stats.isFile()) {
306+
return isSymlink &&
307+
(await resolvesIntoOutputPath(fs, filePath, isInOutputPath))
308+
? []
309+
: [filePath];
310+
}
293311
if (!stats.isDirectory() || maxDepth === 0) return [];
294312
// an ignored directory is not listed at all, which is what makes an
295313
// `ignore` naming one cheaper than dropping its files afterwards
@@ -701,7 +719,11 @@ class CopyPlugin {
701719
const normalizedBase = normalizePathSeparatorsForPath(base);
702720
const entries =
703721
typeOfFrom === "file"
704-
? ignore.file(normalizePathSeparatorsForPath(absoluteFrom))
722+
? ignore.file(normalizePathSeparatorsForPath(absoluteFrom)) ||
723+
// `stat` followed the link to call this a file, so the name the
724+
// pattern used says nothing about what it reaches
725+
(followSymlinks &&
726+
(await resolvesIntoOutputPath(fs, absoluteFrom, isInOutputPath)))
705727
? []
706728
: [absoluteFrom]
707729
: (

‎test/unitCases/CopyPlugin.unittest.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const staticPath = path.join(tempFolderPath, "static");
1818
const outputPath = path.join(staticPath, "build");
1919
const stalePath = path.join(outputPath, "stale.txt");
2020
const aliasPath = path.join(staticPath, "alias");
21+
const fileAliasPath = path.join(staticPath, "stale-link.txt");
2122

2223
// codes a machine that cannot make a directory symlink reports
2324
const SYMLINK_UNSUPPORTED = new Set(["EPERM", "EACCES", "ENOSYS", "UNKNOWN"]);
@@ -39,6 +40,9 @@ const createFiles = () => {
3940
fs.writeFileSync(stalePath, "stale");
4041

4142
try {
43+
// a file link is what `stat` reports as a plain file, so it reaches the
44+
// output directory without ever being recognized as a link
45+
fs.symlinkSync(stalePath, fileAliasPath, "file");
4246
fs.symlinkSync(outputPath, aliasPath, "junction");
4347
} catch (err) {
4448
const { code } = /** @type {NodeJS.ErrnoException} */ (err);
@@ -161,6 +165,28 @@ describe("CopyPlugin", () => {
161165
);
162166
});
163167

168+
it("should not follow a file symlink into the output path", () => {
169+
if (!aliased) return;
170+
// `stat` follows the link and reports a plain file, so nothing about the
171+
// walk says the bytes come from the output directory
172+
expect(compilation.getAsset("stale-link.txt")).toBeUndefined();
173+
});
174+
175+
it("should copy nothing when the pattern names such a file symlink", async () => {
176+
if (!aliased) return;
177+
const fromFileAlias = await compile({
178+
from: "static/stale-link.txt",
179+
to: "named"
180+
});
181+
// `to` names a directory, so the copy would land as `named/stale-link.txt`
182+
// rather than under the name `to` gives
183+
expect(
184+
[...fromFileAlias.getAssets()].filter((asset) =>
185+
asset.name.startsWith("named")
186+
)
187+
).toHaveLength(0);
188+
});
189+
164190
it("should not make a file inside the output path a dependency", () => {
165191
expect([...compilation.fileDependencies]).not.toContain(stalePath);
166192
});

0 commit comments

Comments
 (0)