Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions .ado/scripts/deepmerge.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import assert from "node:assert/strict";
import { createRequire } from "node:module";
import path from "node:path";
import process from "node:process";
import { test } from "node:test";

// Each project's test script runs this file from its own directory, so this
// checks the deepmerge copy installed in that project.
const require = createRequire(path.join(process.cwd(), "package.json"));
const maliciousObject = () =>
JSON.parse('{"__proto__":{"__deepmergeProbe":true},"kept":true}');
const attacks = [
["null-prototype target", () => [Object.create(null), maliciousObject()]],
["null placeholder", () => [{ value: null }, { value: maliciousObject() }]],
[
"undefined placeholder",
() => [{ value: undefined }, { value: maliciousObject() }],
],
[
"primitive placeholder",
() => [{ value: false }, { value: maliciousObject() }],
],
["own target __proto__", () => [maliciousObject(), { extra: true }]],
[
"own target and source __proto__",
() => [maliciousObject(), maliciousObject()],
],
["array element", () => [[], [maliciousObject()]]],
];

for (const entry of ["deepmerge", "deepmerge/dist/umd.js"]) {
const merge = require(entry);

for (const [name, inputs] of attacks) {
test(`${entry}: prevents prototype poisoning with ${name}`, () => {
const [target, source] = inputs();
const merged = merge(target, source);
const result = Array.isArray(merged)
? merged[0]
: (merged.value ?? merged);
assert.equal(result.__deepmergeProbe, undefined);
assert.equal(Object.hasOwn(result, "__proto__"), false);
assert.equal(Object.getPrototypeOf(result), Object.prototype);
assert.equal(result.kept, true);
assert.equal(Object.prototype.__deepmergeProbe, undefined);
});
}

test(`${entry}: preserves normal object and array merges and merge.all`, () => {
const target = { nested: { left: true }, list: [{ left: true }] };
const source = { nested: { right: true }, list: [{ right: true }] };
const expected = {
nested: { left: true, right: true },
list: [{ left: true }, { right: true }],
};
assert.deepEqual(merge(target, source), expected);
assert.deepEqual(merge.all([target, source, { last: true }]), {
...expected,
last: true,
});
assert.deepEqual(target, {
nested: { left: true },
list: [{ left: true }],
});
assert.deepEqual(source, {
nested: { right: true },
list: [{ right: true }],
});
});

test(`${entry}: preserves clone and arrayMerge options`, () => {
const value = { kept: true };
assert.equal(merge({}, { value }, { clone: false }).value, value);
assert.deepEqual(
merge([1], [2], { arrayMerge: (_target, source) => source }),
[2],
);
});

test(`${entry}: preserves legitimate own constructor and prototype keys`, () => {
const target = { constructor: { left: true }, prototype: { left: true } };
const source = { constructor: { right: true }, prototype: { right: true } };
assert.deepEqual(merge(target, source), {
constructor: { left: true, right: true },
prototype: { left: true, right: true },
});
});
}
55 changes: 55 additions & 0 deletions .ado/scripts/npm-source-lint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ function validYarnFixture(): Record<string, string> {
};
}

function validPatchedYarnFixture(): Record<string, string> {
const files = validYarnFixture();
const source = "patch:example@npm%3A1.0.0#../.yarn/patches/example.patch";
const manifest = JSON.parse(files["project/package.json"]);
manifest.resolutions = { example: source };
files["project/package.json"] = JSON.stringify(manifest);
files[".yarn/patches/example.patch"] = "";
files["project/yarn.lock"] += `
"example@${source}":
version: 1.0.0
resolution: "example@${source}::version=1.0.0&hash=abc123&locator=yarn-fixture%40workspace%3A."
checksum: 10c0/patched-example
languageName: node
linkType: hard
`;
return files;
}

function createFixture(files: Record<string, string>): string {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "npm-source-lint-"));
for (const [file, content] of Object.entries(files)) {
Expand Down Expand Up @@ -404,6 +422,43 @@ test("rejects a Yarn archive URL recorded only in a descriptor", () => {
assertViolation(files, /__archiveUrl\) are forbidden/);
});

test("accepts a shared Yarn patch with resolution parameters", () => {
assertValid(validPatchedYarnFixture());
});

test("rejects a missing shared Yarn patch", () => {
const files = validPatchedYarnFixture();
delete files[".yarn/patches/example.patch"];
assertViolation(
files,
/an existing local path contained within the repository/,
);
});

test("rejects a shared Yarn patch outside the repository", () => {
const files = validPatchedYarnFixture();
files["project/yarn.lock"] = files["project/yarn.lock"].replaceAll(
"../.yarn/patches/example.patch",
"../../outside.patch",
);
assertViolation(files, /local path contained within the repository/);
});

test("rejects encoded download URLs in Yarn patch resolution parameters", () => {
for (const source of [
"https://packages.example.com/example.tgz",
"git+ssh://git@example.test/repository.git",
]) {
const files = validPatchedYarnFixture();
const url = encodeURIComponent(encodeURIComponent(source));
files["project/yarn.lock"] = files["project/yarn.lock"].replace(
"&hash=abc123",
`&hash=abc123&download=${url}`,
);
assertViolation(files, /a semver, npm alias, workspace:/);
}
});

test("rejects a Yarn archive URL encoded inside a patch locator", () => {
const files = validYarnFixture();
const locator = `example@npm:1.0.0::__archiveUrl=${encodeURIComponent(`${PACKAGE_FEED_PROXY}example/-/example-1.0.0.tgz`)}`;
Expand Down
8 changes: 6 additions & 2 deletions .ado/scripts/npm-source-lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ function getDependencySourceError(
if (scheme === "file" || scheme === "link" || scheme === "portal") {
return getLocalPathError(root, file, reference);
}
if (scheme === "patch" && !hasRemoteReference(reference)) {
if (
scheme === "patch" &&
!hasRemoteReference(reference) &&
!/[a-z][a-z0-9+.-]*:\/\//i.test(reference)
) {
const [locator, patchPath] = reference.split("#", 2);
const locatorError = getDependencySourceError(root, file, locator);
if (locatorError) {
Expand All @@ -375,7 +379,7 @@ function getDependencySourceError(
? getDependencySourceError(
root,
file,
patchPath.replace(/^optional!/, ""),
patchPath.split("::", 1)[0].replace(/^optional!/, ""),
)
: undefined;
}
Expand Down
93 changes: 93 additions & 0 deletions .yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
diff --git a/dist/cjs.js b/dist/cjs.js
index 7c36cbdd06cc22c92d590be3f87a08a91ca06b40..b95c3a74970c3d61d025c7e7042af1b3ffe1caeb 100644
--- a/dist/cjs.js
+++ b/dist/cjs.js
@@ -69,6 +69,10 @@ function propertyIsOnObject(object, property) {
}
}

+function propertyIsForbidden(key) {
+ return key === '__proto__'
+}
+
// Protects from prototype poisoning and unexpected merging up the prototype chain.
function propertyIsUnsafe(target, key) {
return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet,
@@ -80,11 +84,14 @@ function mergeObject(target, source, options) {
var destination = {};
if (options.isMergeableObject(target)) {
getKeys(target).forEach(function(key) {
+ if (propertyIsForbidden(key)) {
+ return
+ }
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
});
}
getKeys(source).forEach(function(key) {
- if (propertyIsUnsafe(target, key)) {
+ if (propertyIsForbidden(key) || propertyIsUnsafe(target, key)) {
return
}

diff --git a/dist/umd.js b/dist/umd.js
index 4071e7c56ec8e5273fe166f6e723ac460fab8b72..98c48e59d7be96f1c3f0d48dfb966e973565e5de 100644
--- a/dist/umd.js
+++ b/dist/umd.js
@@ -73,6 +73,10 @@
}
}

+ function propertyIsForbidden(key) {
+ return key === '__proto__'
+ }
+
// Protects from prototype poisoning and unexpected merging up the prototype chain.
function propertyIsUnsafe(target, key) {
return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet,
@@ -84,11 +88,14 @@
var destination = {};
if (options.isMergeableObject(target)) {
getKeys(target).forEach(function(key) {
+ if (propertyIsForbidden(key)) {
+ return
+ }
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
});
}
getKeys(source).forEach(function(key) {
- if (propertyIsUnsafe(target, key)) {
+ if (propertyIsForbidden(key) || propertyIsUnsafe(target, key)) {
return
}

diff --git a/index.js b/index.js
index 77968aed5d456cccc294f60912ef381fa1e52a7b..8ba8453c50ea5242257a877646f4b4ae18283faf 100644
--- a/index.js
+++ b/index.js
@@ -44,6 +44,10 @@ function propertyIsOnObject(object, property) {
}
}

+function propertyIsForbidden(key) {
+ return key === '__proto__'
+}
+
// Protects from prototype poisoning and unexpected merging up the prototype chain.
function propertyIsUnsafe(target, key) {
return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet,
@@ -55,11 +59,14 @@ function mergeObject(target, source, options) {
var destination = {}
if (options.isMergeableObject(target)) {
getKeys(target).forEach(function(key) {
+ if (propertyIsForbidden(key)) {
+ return
+ }
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options)
})
}
getKeys(source).forEach(function(key) {
- if (propertyIsUnsafe(target, key)) {
+ if (propertyIsForbidden(key) || propertyIsUnsafe(target, key)) {
return
}

5 changes: 4 additions & 1 deletion API/hermes/cdp/tools/hermes-inspector-msggen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"flow": "flow",
"build": "babel src --out-dir bin --source-maps",
"watch": "babel src --out-dir bin --source-maps --watch",
"test": "jest"
"test": "node --test ../../../../../.ado/scripts/deepmerge.test.js && jest --passWithNoTests"
},
"dependencies": {
"devtools-protocol": "0.0.1383960",
Expand All @@ -37,5 +37,8 @@
"transform": {
".*": "<rootDir>/node_modules/babel-jest"
}
},
"resolutions": {
"deepmerge@npm:^4.2.2": "patch:deepmerge@npm%3A4.3.1#../../../../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch"
}
}
9 changes: 8 additions & 1 deletion API/hermes/cdp/tools/hermes-inspector-msggen/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2289,13 +2289,20 @@ __metadata:
languageName: node
linkType: hard

"deepmerge@npm:^4.2.2":
"deepmerge@npm:4.3.1":
version: 4.3.1
resolution: "deepmerge@npm:4.3.1"
checksum: 10c0/e53481aaf1aa2c4082b5342be6b6d8ad9dfe387bc92ce197a66dea08bd4265904a087e75e464f14d1347cf2ac8afe1e4c16b266e0561cc5df29382d3c5f80044
languageName: node
linkType: hard

"deepmerge@patch:deepmerge@npm%3A4.3.1#../../../../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch::locator=%40react-native%2Fhermes-inspector-msggen%40workspace%3A.":
version: 4.3.1
resolution: "deepmerge@patch:deepmerge@npm%3A4.3.1#../../../../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch::version=4.3.1&hash=6ed3c9&locator=%40react-native%2Fhermes-inspector-msggen%40workspace%3A."
checksum: 10c0/14b984011323bdb40620dffe74062403d56e34818b5c3dc73a7cb184ccfd479d779e258f331ffb07a7b4e5b39ad3d247c85e0bc396021522381ec824d85e0329
languageName: node
linkType: hard

"detect-newline@npm:^3.0.0":
version: 3.1.0
resolution: "detect-newline@npm:3.1.0"
Expand Down
3 changes: 2 additions & 1 deletion tools/hermes-parser/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"flow": "flow",
"format": "./node_modules/prettier/bin/prettier.cjs --config ./.prettierrc.json --write \"**/*.js\"",
"postinstall": "patch-package",
"test": "NODE_OPTIONS=\"--experimental-vm-modules\" jest"
"test": "node --test ../../../.ado/scripts/deepmerge.test.js && NODE_OPTIONS=\"--experimental-vm-modules\" jest"
},
"workspaces": [
"hermes-eslint",
Expand All @@ -47,6 +47,7 @@
"babel-plugin-syntax-hermes-parser"
],
"resolutions": {
"deepmerge@npm:^4.3.1": "patch:deepmerge@npm%3A4.3.1#../../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch",
"glob@npm:^10.4.1": "^14.0.3",
"glob@npm:^13.0.6": "^14.0.3",
"lodash": "^4.18.1",
Expand Down
9 changes: 8 additions & 1 deletion tools/hermes-parser/js/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3583,13 +3583,20 @@ __metadata:
languageName: node
linkType: hard

"deepmerge@npm:^4.3.1":
"deepmerge@npm:4.3.1":
version: 4.3.1
resolution: "deepmerge@npm:4.3.1"
checksum: 10c0/e53481aaf1aa2c4082b5342be6b6d8ad9dfe387bc92ce197a66dea08bd4265904a087e75e464f14d1347cf2ac8afe1e4c16b266e0561cc5df29382d3c5f80044
languageName: node
linkType: hard

"deepmerge@patch:deepmerge@npm%3A4.3.1#../../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch::locator=hermes-parser-packages%40workspace%3A.":
version: 4.3.1
resolution: "deepmerge@patch:deepmerge@npm%3A4.3.1#../../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch::version=4.3.1&hash=6ed3c9&locator=hermes-parser-packages%40workspace%3A."
checksum: 10c0/14b984011323bdb40620dffe74062403d56e34818b5c3dc73a7cb184ccfd479d779e258f331ffb07a7b4e5b39ad3d247c85e0bc396021522381ec824d85e0329
languageName: node
linkType: hard

"define-data-property@npm:^1.0.1, define-data-property@npm:^1.1.4":
version: 1.1.4
resolution: "define-data-property@npm:1.1.4"
Expand Down
6 changes: 5 additions & 1 deletion utils/promise/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
"promise": "8.3.0"
},
"scripts": {
"start": "rollup --config rollup.config.js index.js"
"start": "rollup --config rollup.config.js index.js",
"test": "node --test ../../.ado/scripts/deepmerge.test.js"
},
"devDependencies": {
"rollup": "~2.80.0"
},
"resolutions": {
"deepmerge@npm:^4.2.2": "patch:deepmerge@npm%3A4.3.1#../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch"
}
}
9 changes: 8 additions & 1 deletion utils/promise/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,20 @@ __metadata:
languageName: node
linkType: hard

"deepmerge@npm:^4.2.2":
"deepmerge@npm:4.3.1":
version: 4.3.1
resolution: "deepmerge@npm:4.3.1"
checksum: 10c0/e53481aaf1aa2c4082b5342be6b6d8ad9dfe387bc92ce197a66dea08bd4265904a087e75e464f14d1347cf2ac8afe1e4c16b266e0561cc5df29382d3c5f80044
languageName: node
linkType: hard

"deepmerge@patch:deepmerge@npm%3A4.3.1#../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch::locator=root-workspace-0b6124%40workspace%3A.":
version: 4.3.1
resolution: "deepmerge@patch:deepmerge@npm%3A4.3.1#../../.yarn/patches/deepmerge-npm-4.3.1-prototype-poisoning.patch::version=4.3.1&hash=6ed3c9&locator=root-workspace-0b6124%40workspace%3A."
checksum: 10c0/14b984011323bdb40620dffe74062403d56e34818b5c3dc73a7cb184ccfd479d779e258f331ffb07a7b4e5b39ad3d247c85e0bc396021522381ec824d85e0329
languageName: node
linkType: hard

"env-paths@npm:^2.2.0":
version: 2.2.1
resolution: "env-paths@npm:2.2.1"
Expand Down
Loading