Skip to content

Commit fcdf06e

Browse files
fix(config): ignore inherited keys when restoring env refs (#109463)
* fix(config): use Object.hasOwn in restoreEnvVarRefs to avoid prototype pollution When key comes from Object.entries(incoming) on user config data, 'key in parsed' traverses the prototype chain. If a config key collides with an Object.prototype property (e.g. 'toString'), the check incorrectly returns true and calls restoreEnvVarRefs with a prototype function instead of a config value. Replace with Object.hasOwn(parsed, key) to check only own properties. * test(config): prove inherited env refs are ignored --------- Co-authored-by: Peter Steinberger <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent 7752e33 commit fcdf06e

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

‎src/config/env-preserve.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,4 +833,16 @@ describe("restoreEnvVarRefs", () => {
833833
resolved: "${MY_TOKEN}", // should restore ref
834834
});
835835
});
836+
837+
it("does not restore env refs from inherited parsed properties", () => {
838+
// isPlainObject accepts custom prototypes, but only own properties represent authored config.
839+
const parsed = Object.create({
840+
toString: "${TEST_VALUE}",
841+
}) as Record<string, unknown>;
842+
const testEnv = { TEST_VALUE: "resolved-value" } as unknown as NodeJS.ProcessEnv;
843+
844+
expect(restoreEnvVarRefs({ toString: "resolved-value" }, parsed, testEnv)).toEqual({
845+
toString: "resolved-value",
846+
});
847+
});
836848
});

‎src/config/env-preserve.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ export function restoreEnvVarRefs(
820820
if (isPlainObject(incoming) && isPlainObject(parsed)) {
821821
const result: Record<string, unknown> = {};
822822
for (const [key, value] of Object.entries(incoming)) {
823-
if (key in parsed) {
823+
if (Object.hasOwn(parsed, key)) {
824824
result[key] = restoreEnvVarRefs(value, parsed[key], env);
825825
} else {
826826
// New key added by caller — keep as-is

0 commit comments

Comments
 (0)