Skip to content

Commit 8a3a07f

Browse files
committed
fix(cache): encode provided-exports value dependencies unambiguously
The provided-exports cache key joined "key=value" pairs with "|", so a DefinePlugin version spelling out those separators could forge another key's entry and reuse a prior configuration's inlined literals. Serialize the still-lazy requests and the value versions as JSON instead. Adds two PersistentCaching cases: a DefinePlugin-provided inlined export changing between cached builds, and two version sets that collided under the old encoding.
1 parent 0573cad commit 8a3a07f

2 files changed

Lines changed: 105 additions & 52 deletions

File tree

‎lib/FlagDependencyExportsPlugin.js‎

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,69 +30,49 @@ const PLUGIN_LOGGER_NAME = `webpack.${PLUGIN_NAME}`;
3030
/** @typedef {{ etag: string, data: InstanceType<RestoreProvidedData> }} MemCacheEntry */
3131

3232
/**
33+
* Cache key fragment for what changes provided exports without changing the
34+
* module's source: still-lazy barrel requests and DefinePlugin value versions.
3335
* @param {Module} module the module
3436
* @param {LazyBarrelController} lazyBarrelController the compilation's lazy barrel controller
3537
* @param {HashFunction} hashFunction hash function for long keys
36-
* @returns {string} the hash
38+
* @returns {string} etag for provided-exports mem/persistent cache, "" when neither applies
3739
*/
38-
const getLazyRequestsHash = (module, lazyBarrelController, hashFunction) => {
39-
const lazyKeys = lazyBarrelController.getLazyRequests(module);
40-
if (!lazyKeys) return "";
41-
const joined = [...lazyKeys].join("|");
42-
if (joined.length < 100) return joined;
43-
const hash = createHash(hashFunction);
44-
hash.update(joined);
45-
return /** @type {string} */ (hash.digest("hex"));
46-
};
47-
48-
/**
49-
* DefinePlugin (and similar) can change provided exports without changing the
50-
* source hash; fold valueDependencies into the provided-exports cache key.
51-
* @param {Module} module the module
52-
* @param {HashFunction} hashFunction hash function for long keys
53-
* @returns {string} key fragment, or "" when absent
54-
*/
55-
const getValueDependenciesHash = (module, hashFunction) => {
56-
const valueDependencies =
57-
/** @type {BuildInfo | undefined} */
58-
(module.buildInfo) &&
59-
/** @type {BuildInfo} */
60-
(module.buildInfo).valueDependencies;
61-
if (!valueDependencies || valueDependencies.size === 0) return "";
62-
const parts = [];
63-
for (const key of [...valueDependencies.keys()].sort()) {
64-
const value = valueDependencies.get(key);
65-
parts.push(
66-
`${key}=${
67-
typeof value === "string"
68-
? value
69-
: value === undefined
70-
? ""
71-
: `[${[...value].sort().join(",")}]`
72-
}`
73-
);
40+
const getProvidedExportsEtag = (module, lazyBarrelController, hashFunction) => {
41+
const lazyRequests = lazyBarrelController.getLazyRequests(module);
42+
const buildInfo = /** @type {BuildInfo | undefined} */ (module.buildInfo);
43+
const valueDependencies = buildInfo && buildInfo.valueDependencies;
44+
const hasValueDependencies =
45+
valueDependencies !== undefined && valueDependencies.size > 0;
46+
if (!lazyRequests && !hasValueDependencies) return "";
47+
/** @type {(string | string[] | null)[][]} */
48+
const valueEntries = [];
49+
if (hasValueDependencies) {
50+
const versions =
51+
/** @type {NonNullable<BuildInfo["valueDependencies"]>} */
52+
(valueDependencies);
53+
for (const key of [...versions.keys()].sort()) {
54+
const value = versions.get(key);
55+
valueEntries.push([
56+
key,
57+
value === undefined
58+
? null
59+
: typeof value === "string"
60+
? value
61+
: [...value].sort()
62+
]);
63+
}
7464
}
75-
const joined = parts.join("|");
65+
// JSON, so a separator inside a request or a version cannot forge another entry
66+
const joined = JSON.stringify([
67+
lazyRequests ? [...lazyRequests] : [],
68+
valueEntries
69+
]);
7670
if (joined.length < 100) return joined;
7771
const hash = createHash(hashFunction);
7872
hash.update(joined);
7973
return /** @type {string} */ (hash.digest("hex"));
8074
};
8175

82-
/**
83-
* @param {Module} module the module
84-
* @param {LazyBarrelController} lazyBarrelController the compilation's lazy barrel controller
85-
* @param {HashFunction} hashFunction hash function for long keys
86-
* @returns {string} etag for provided-exports mem/persistent cache
87-
*/
88-
const getProvidedExportsEtag = (module, lazyBarrelController, hashFunction) => {
89-
const lazy = getLazyRequestsHash(module, lazyBarrelController, hashFunction);
90-
const valueDeps = getValueDependenciesHash(module, hashFunction);
91-
if (!lazy) return valueDeps;
92-
if (!valueDeps) return lazy;
93-
return `${lazy}|${valueDeps}`;
94-
};
95-
9676
class FlagDependencyExportsPlugin {
9777
/**
9878
* Applies the plugin by registering its hooks on the compiler.

‎test/PersistentCaching.test.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,77 @@ export const FLAG = "off";
512512
expect(second).toContain("inlined export .NUM */6");
513513
expect(second).toContain('inlined export .FLAG */"off"');
514514
}, 100000);
515+
516+
// A DefinePlugin value changes the inlined literal without touching the
517+
// module source, so its build hash alone cannot key the provided exports.
518+
it("should invalidate consumer codegen when a plugin-provided inlined export changes", async () => {
519+
const { DefinePlugin } = require("../");
520+
521+
const configFor = (/** @type {string} */ flag) => ({
522+
mode: "production",
523+
optimization: { minimize: false },
524+
output: {
525+
...config.output,
526+
pathinfo: true
527+
},
528+
plugins: [new DefinePlugin({ "process.env.FLAG": JSON.stringify(flag) })]
529+
});
530+
await updateSrc({
531+
"index.js": `import { FLAG } from "./env.js";
532+
export default FLAG;
533+
`,
534+
"env.js": "export const FLAG = process.env.FLAG;\n"
535+
});
536+
await compile(configFor("on"));
537+
expect(execute()).toBe("on");
538+
const first = await readFile(path.resolve(outputPath, "main.js"), "utf8");
539+
expect(first).toContain('inlined export .FLAG */"on"');
540+
541+
await compile(configFor("off"));
542+
expect(execute()).toBe("off");
543+
const second = await readFile(path.resolve(outputPath, "main.js"), "utf8");
544+
expect(second).toContain('inlined export .FLAG */"off"');
545+
}, 100000);
546+
547+
// A value version is user-supplied text; spelling out the encoding's own
548+
// separators must not forge another key's entry in the cache key.
549+
it("should key value dependencies whose versions contain the separators", async () => {
550+
const { DefinePlugin } = require("../");
551+
552+
const keyB = `${DefinePlugin.VALUE_DEP_PREFIX}process.env.B`;
553+
const configFor = (
554+
/** @type {string} */ suffix,
555+
/** @type {string} */ versionA,
556+
/** @type {string} */ versionB
557+
) => ({
558+
mode: "production",
559+
optimization: { minimize: false },
560+
plugins: [
561+
new DefinePlugin({
562+
"process.env.A": DefinePlugin.runtimeValue(
563+
() => JSON.stringify(`a${suffix}`),
564+
{ version: versionA }
565+
),
566+
"process.env.B": DefinePlugin.runtimeValue(
567+
() => JSON.stringify(`b${suffix}`),
568+
{ version: versionB }
569+
)
570+
})
571+
]
572+
});
573+
await updateSrc({
574+
"index.js": `import { A, B } from "./env.js";
575+
export default [A, B];
576+
`,
577+
"env.js": `export const A = process.env.A;
578+
export const B = process.env.B;
579+
`
580+
});
581+
await compile(configFor("1", `1|${keyB}=2`, "3"));
582+
expect(execute()).toEqual(["a1", "b1"]);
583+
584+
// The two version sets concatenate to the same "key=value|…" text
585+
await compile(configFor("2", "1", `2|${keyB}=3`));
586+
expect(execute()).toEqual(["a2", "b2"]);
587+
}, 100000);
515588
});

0 commit comments

Comments
 (0)