From 78e605b1a18af351773347dd4673203a99cb450a Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Fri, 26 Mar 2021 06:48:09 +0900 Subject: [PATCH] Fix #757 Add event type name validation & channel_type filter middleware (#857) --- src/App.spec.ts | 7 +++++++ src/App.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/App.spec.ts b/src/App.spec.ts index 0632b0e58..3e4d66da0 100644 --- a/src/App.spec.ts +++ b/src/App.spec.ts @@ -560,6 +560,13 @@ describe('App', () => { assert(error.code === ErrorCode.MultipleListenerError); assert.sameMembers(error.originals, errorsToThrow); }); + + it('should detect invalid event names', async () => { + app.event('app_mention', async () => {}); + app.event('message', async () => {}); + assert.throws(() => app.event('message.channels', async () => {}), 'Although the document mentions'); + assert.throws(() => app.event(/message\..+/, async () => {}), 'Although the document mentions'); + }); }); describe('WorkflowStep middleware', () => { diff --git a/src/App.ts b/src/App.ts index a17a9c949..f873a7cc8 100644 --- a/src/App.ts +++ b/src/App.ts @@ -436,6 +436,21 @@ export default class App { eventNameOrPattern: EventType, ...listeners: Middleware>[] ): void { + let invalidEventName = false; + if (typeof eventNameOrPattern === 'string') { + const name = eventNameOrPattern as string; + invalidEventName = name.startsWith('message.'); + } else if (eventNameOrPattern instanceof RegExp) { + const name = (eventNameOrPattern as RegExp).source; + invalidEventName = name.startsWith('message\\.'); + } + if (invalidEventName) { + throw new AppInitializationError( + `Although the document mentions "${eventNameOrPattern}",` + + 'it is not a valid event type. Use "message" instead. ' + + 'If you want to filter message events, you can use event.channel_type for it.', + ); + } this.listeners.push([ onlyEvents, matchEventType(eventNameOrPattern),