Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): stop wildcard export walking the prototype chain

joshistoast7ba5475b3e6f
diff --git a/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.test.ts b/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.test.ts
index 9e6ed914d2..cc1ada0e3a 100644
--- a/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.test.ts
+++ b/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.test.ts
@@ -126,6 +126,29 @@ describe('wildcardsToNestedRecord', () => {
wildcardsFromNestedRecord(wildcardsToNestedRecord(wildcards)).sort((a, b) => a.name.localeCompare(b.name))
).toEqual(wildcards.sort((a, b) => a.name.localeCompare(b.name)));
});
+
+ // Regression: `node[segment] ??= {}` declines to assign when the inherited
+ // value is truthy, so the walk stepped onto `Object.prototype` and wrote the
+ // leaf there. `constructor`, `toString` and `valueOf` are all valid wildcard
+ // names on both sides of the wire.
+ it('nests a name that collides with something on Object.prototype', () => {
+ const inheritedNames = ['constructor', 'toString', 'valueOf', 'hasOwnProperty'];
+ const wildcards = inheritedNames.map((name) => ({ name: `${name}/keys`, values: ['red', 'blue'] }));
+ const nested = wildcardsToNestedRecord(wildcards);
+
+ expect(typeof Object.keys).toBe('function');
+ expect(Object.keys(nested)).toEqual(inheritedNames);
+ expect(JSON.parse(JSON.stringify(nested))).toEqual(
+ Object.fromEntries(inheritedNames.map((name) => [name, { keys: ['red', 'blue'] }]))
+ );
+ expect(wildcardsFromNestedRecord(nested)).toEqual(wildcards);
+ });
+
+ it('keeps such a name as a leaf too', () => {
+ const nested = wildcardsToNestedRecord([{ name: 'constructor', values: ['red'] }]);
+
+ expect(JSON.stringify(nested)).toBe('{"constructor":["red"]}');
+ });
});
describe('planWildcardImport', () => {
diff --git a/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.ts b/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.ts
index b1c8e094ca..5c6217e9cb 100644
--- a/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.ts
+++ b/invokeai/frontend/webv2/src/features/generation/core/wildcardTransfer.ts
@@ -124,9 +124,17 @@ const getAncestorNames = (name: string): string[] => {
* reads back identically. Deciding this from the name set rather than from
* whatever the loop has written so far keeps the output independent of the order
* the wildcards arrive in.
+ *
+ * Every level is a null-prototype object, and levels are created through an own
+ * key check rather than `??=`. A name segment is user text, and `constructor`,
+ * `toString` and `valueOf` are all valid wildcard names: on an ordinary `{}` the
+ * inherited value is truthy, so `??=` would decline to assign and the walk would
+ * step onto `Object.prototype` and write the leaf there. `Object.keys` becoming
+ * an array of colours takes the whole tab down, and the wildcard vanishes from
+ * the export either way.
*/
export const wildcardsToNestedRecord = (wildcards: readonly ParsedWildcard[]): Record<string, unknown> => {
- const root: Record<string, unknown> = {};
+ const root: Record<string, unknown> = Object.create(null) as Record<string, unknown>;
const names = new Set(wildcards.map((wildcard) => wildcard.name));
for (const wildcard of wildcards) {
@@ -145,7 +153,10 @@ export const wildcardsToNestedRecord = (wildcards: readonly ParsedWildcard[]): R
let node = root;
for (const segment of segments) {
- node[segment] ??= {};
+ if (!Object.hasOwn(node, segment)) {
+ node[segment] = Object.create(null) as Record<string, unknown>;
+ }
+
node = node[segment] as Record<string, unknown>;
}