From 3e26b590588dc7e514062f350a012fade2d5fb71 Mon Sep 17 00:00:00 2001 From: Zack Sheppard Date: Fri, 27 Oct 2023 20:26:15 +0100 Subject: [PATCH 1/2] fix(@nestjs/event-emitter): Circumvent crash in reflect-metadata This fixes #1008 by circumventing the situation where reflect-metadata would be given a value that was neither a function nor an object because the event-emitter was somehow tricked into traversing it as if it were. E.G. by being given a Proxy wrapping and removing some keys of a class. --- lib/events-metadata.accessor.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/events-metadata.accessor.ts b/lib/events-metadata.accessor.ts index 8275e59b..66269bb3 100644 --- a/lib/events-metadata.accessor.ts +++ b/lib/events-metadata.accessor.ts @@ -10,6 +10,15 @@ export class EventsMetadataAccessor { getEventHandlerMetadata( target: Type, ): OnEventMetadata[] | undefined { + // Circumvent a crash that comes from reflect-metadata if it is + // given a non-object non-function target to reflect upon. + if ( + target === undefined || + (typeof target !== 'function' && typeof target !== 'object') + ) { + return undefined; + } + const metadata = this.reflector.get(EVENT_LISTENER_METADATA, target); if (!metadata) { return undefined; From 36d68f1663dcf43243dd28e0f865302b1bca69b1 Mon Sep 17 00:00:00 2001 From: Zack Sheppard Date: Fri, 27 Oct 2023 22:35:36 +0100 Subject: [PATCH 2/2] fix(@nestjs/event-emitter): Also capture the case where target is null --- lib/events-metadata.accessor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/events-metadata.accessor.ts b/lib/events-metadata.accessor.ts index 66269bb3..3d1b3f4b 100644 --- a/lib/events-metadata.accessor.ts +++ b/lib/events-metadata.accessor.ts @@ -13,7 +13,7 @@ export class EventsMetadataAccessor { // Circumvent a crash that comes from reflect-metadata if it is // given a non-object non-function target to reflect upon. if ( - target === undefined || + !target || (typeof target !== 'function' && typeof target !== 'object') ) { return undefined;