From af40361c9934f3fcfd3ef2cc2a47aa047a2eb34d Mon Sep 17 00:00:00 2001 From: Ron Buckton Date: Thu, 14 Dec 2023 13:47:04 -0500 Subject: [PATCH] Fix stack overflow crash in isProviderFor --- Reflect.ts | 13 +++++++------ ReflectLite.ts | 13 +++++++------ ReflectNoConflict.ts | 13 +++++++------ test/reflect-other.ts | 12 ++++++++++++ 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/Reflect.ts b/Reflect.ts index 6c3cd29..5419b0a 100644 --- a/Reflect.ts +++ b/Reflect.ts @@ -1745,6 +1745,7 @@ namespace Reflect { } function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { + const { defineMetadata, hasOwnMetadata, getOwnMetadata, getOwnMetadataKeys, deleteMetadata } = reflect; const metadataOwner = new _WeakMap>(); const provider: MetadataProvider = { isProviderFor(O, P) { @@ -1752,7 +1753,7 @@ namespace Reflect { if (!IsUndefined(metadataPropertySet)) { return metadataPropertySet.has(P); } - if (reflect.getOwnMetadataKeys(O, P!).length) { + if (getOwnMetadataKeys(O, P!).length) { if (IsUndefined(metadataPropertySet)) { metadataPropertySet = new _Set(); metadataOwner.set(O, metadataPropertySet); @@ -1762,11 +1763,11 @@ namespace Reflect { } return false; }, - OrdinaryDefineOwnMetadata: reflect.defineMetadata, - OrdinaryHasOwnMetadata: reflect.hasOwnMetadata, - OrdinaryGetOwnMetadata: reflect.getOwnMetadata, - OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys, - OrdinaryDeleteMetadata: reflect.deleteMetadata, + OrdinaryDefineOwnMetadata: defineMetadata, + OrdinaryHasOwnMetadata: hasOwnMetadata, + OrdinaryGetOwnMetadata: getOwnMetadata, + OrdinaryOwnMetadataKeys: getOwnMetadataKeys, + OrdinaryDeleteMetadata: deleteMetadata, }; return provider; } diff --git a/ReflectLite.ts b/ReflectLite.ts index 9e6de71..c62121a 100644 --- a/ReflectLite.ts +++ b/ReflectLite.ts @@ -1705,6 +1705,7 @@ namespace Reflect { } function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { + const { defineMetadata, hasOwnMetadata, getOwnMetadata, getOwnMetadataKeys, deleteMetadata } = reflect; const metadataOwner = new _WeakMap>(); const provider: MetadataProvider = { isProviderFor(O, P) { @@ -1712,7 +1713,7 @@ namespace Reflect { if (!IsUndefined(metadataPropertySet)) { return metadataPropertySet.has(P); } - if (reflect.getOwnMetadataKeys(O, P!).length) { + if (getOwnMetadataKeys(O, P!).length) { if (IsUndefined(metadataPropertySet)) { metadataPropertySet = new _Set(); metadataOwner.set(O, metadataPropertySet); @@ -1722,11 +1723,11 @@ namespace Reflect { } return false; }, - OrdinaryDefineOwnMetadata: reflect.defineMetadata, - OrdinaryHasOwnMetadata: reflect.hasOwnMetadata, - OrdinaryGetOwnMetadata: reflect.getOwnMetadata, - OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys, - OrdinaryDeleteMetadata: reflect.deleteMetadata, + OrdinaryDefineOwnMetadata: defineMetadata, + OrdinaryHasOwnMetadata: hasOwnMetadata, + OrdinaryGetOwnMetadata: getOwnMetadata, + OrdinaryOwnMetadataKeys: getOwnMetadataKeys, + OrdinaryDeleteMetadata: deleteMetadata, }; return provider; } diff --git a/ReflectNoConflict.ts b/ReflectNoConflict.ts index 7185505..34be15a 100644 --- a/ReflectNoConflict.ts +++ b/ReflectNoConflict.ts @@ -1582,6 +1582,7 @@ function CreateMetadataProvider(registry: MetadataRegistry): MetadataProvider { } function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { + const { defineMetadata, hasOwnMetadata, getOwnMetadata, getOwnMetadataKeys, deleteMetadata } = reflect; const metadataOwner = new _WeakMap>(); const provider: MetadataProvider = { isProviderFor(O, P) { @@ -1589,7 +1590,7 @@ function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { if (!IsUndefined(metadataPropertySet)) { return metadataPropertySet.has(P); } - if (reflect.getOwnMetadataKeys(O, P!).length) { + if (getOwnMetadataKeys(O, P!).length) { if (IsUndefined(metadataPropertySet)) { metadataPropertySet = new _Set(); metadataOwner.set(O, metadataPropertySet); @@ -1599,11 +1600,11 @@ function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { } return false; }, - OrdinaryDefineOwnMetadata: reflect.defineMetadata, - OrdinaryHasOwnMetadata: reflect.hasOwnMetadata, - OrdinaryGetOwnMetadata: reflect.getOwnMetadata, - OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys, - OrdinaryDeleteMetadata: reflect.deleteMetadata, + OrdinaryDefineOwnMetadata: defineMetadata, + OrdinaryHasOwnMetadata: hasOwnMetadata, + OrdinaryGetOwnMetadata: getOwnMetadata, + OrdinaryOwnMetadataKeys: getOwnMetadataKeys, + OrdinaryDeleteMetadata: deleteMetadata, }; return provider; } diff --git a/test/reflect-other.ts b/test/reflect-other.ts index 83007b1..5cc01be 100644 --- a/test/reflect-other.ts +++ b/test/reflect-other.ts @@ -25,5 +25,17 @@ for (const { name, header, context } of suites.filter(s => s.global)) { assert.strictEqual(Reflect.getOwnMetadata("key", obj), "value"); }); }); + + it("isProviderFor crash", () => { + const { Reflect } = script(context)` + Reflect.defineMetadata = function() {}; + Reflect.getOwnMetadataKeys = function() { return [] }; + Reflect.getMetadataKeys = function() { return []; } + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.getMetadataKeys(obj); + }); }); } \ No newline at end of file