From 61e061d554426a117fee40e59a3e2b150f22adae Mon Sep 17 00:00:00 2001 From: Will Hunt Date: Mon, 23 Dec 2024 17:13:29 +0000 Subject: [PATCH] Add an option at the config and state level to disable hook bodies. --- src/Connections/GenericHook.ts | 12 +++++++++--- src/config/sections/generichooks.ts | 3 +++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Connections/GenericHook.ts b/src/Connections/GenericHook.ts index 2f0e831f..c12ede6f 100644 --- a/src/Connections/GenericHook.ts +++ b/src/Connections/GenericHook.ts @@ -36,6 +36,8 @@ export interface GenericHookConnectionState extends IConnectionState { * (in UTC) time. */ expirationDate?: string; + + includeHookBody?: boolean; } export interface GenericHookSecrets { @@ -152,7 +154,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection } static validateState(state: Partial>): GenericHookConnectionState { - const {name, transformationFunction, waitForComplete, expirationDate: expirationDateStr} = state; + const {name, transformationFunction, waitForComplete, expirationDate: expirationDateStr, includeHookBody} = state; if (!name) { throw new ApiError('Missing name', ErrCode.BadValue); } @@ -162,6 +164,9 @@ export class GenericHookConnection extends BaseConnection implements IConnection if (waitForComplete !== undefined && typeof waitForComplete !== "boolean") { throw new ApiError("'waitForComplete' must be a boolean", ErrCode.BadValue); } + if (includeHookBody !== undefined && typeof includeHookBody !== "boolean") { + throw new ApiError("'includeHookBody' must be a boolean", ErrCode.BadValue); + } // Use !=, not !==, to check for both undefined and null if (transformationFunction != undefined) { if (!this.quickModule) { @@ -186,6 +191,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection name, transformationFunction: transformationFunction || undefined, waitForComplete, + includeHookBody: includeHookBody ?? true, expirationDate, }; } @@ -249,7 +255,6 @@ export class GenericHookConnection extends BaseConnection implements IConnection throw new ApiError('Expiration date must be set', ErrCode.BadValue); } - await GenericHookConnection.ensureRoomAccountData(roomId, intent, hookId, validState.name); await intent.underlyingClient.sendStateEvent(roomId, this.CanonicalEventType, validState.name, validState); const connection = new GenericHookConnection(roomId, validState, hookId, validState.name, messageClient, config.generic, as, intent, storage); @@ -580,7 +585,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection await ensureUserIsInRoom(senderIntent, this.intent.underlyingClient, this.roomId); // Matrix cannot handle float data, so make sure we parse out any floats. - const safeData = GenericHookConnection.sanitiseObjectForMatrixJSON(data); + const safeData = (this.config.includeHookBody && this.state.includeHookBody) ? GenericHookConnection.sanitiseObjectForMatrixJSON(data) : undefined; await this.messageClient.sendMatrixMessage(this.roomId, { msgtype: content.msgtype || "m.notice", @@ -617,6 +622,7 @@ export class GenericHookConnection extends BaseConnection implements IConnection waitForComplete: this.waitForComplete, name: this.state.name, expirationDate: this.state.expirationDate, + includeHookBody: this.config.includeHookBody && this.state.includeHookBody, }, ...(showSecrets ? { secrets: { url: new URL(this.hookId, this.config.parsedUrlPrefix), diff --git a/src/config/sections/generichooks.ts b/src/config/sections/generichooks.ts index f78d62ce..a18bfd12 100644 --- a/src/config/sections/generichooks.ts +++ b/src/config/sections/generichooks.ts @@ -19,6 +19,7 @@ export interface BridgeGenericWebhooksConfigYAML { maxExpiryTime?: string; sendExpiryNotice?: boolean; requireExpiryTime?: boolean; + includeHookBody?: boolean; } export class BridgeConfigGenericWebhooks { @@ -33,6 +34,7 @@ export class BridgeConfigGenericWebhooks { public readonly allowJsTransformationFunctions?: boolean; public readonly waitForComplete?: boolean; public readonly enableHttpGet: boolean; + public readonly includeHookBody: boolean; @hideKey() public readonly maxExpiryTimeMs?: number; @@ -47,6 +49,7 @@ export class BridgeConfigGenericWebhooks { this.enableHttpGet = yaml.enableHttpGet || false; this.sendExpiryNotice = yaml.sendExpiryNotice || false; this.requireExpiryTime = yaml.requireExpiryTime || false; + this.includeHookBody = yaml.includeHookBody || false; try { this.parsedUrlPrefix = makePrefixedUrl(yaml.urlPrefix); this.urlPrefix = () => { return this.parsedUrlPrefix.href; }