Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Telegram Trigger Node): Verify Webhook requests #8383

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/nodes-base/nodes/Telegram/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,9 @@ export function getImageBySize(photos: IDataObject[], size: string): IDataObject
export function getPropertyName(operation: string) {
return operation.replace('send', '').toLowerCase();
}

export function getSecretToken(this: IHookFunctions | IWebhookFunctions) {
// Only characters A-Z, a-z, 0-9, _ and - are allowed.
const secret_token = `${this.getWorkflow().id}_${this.getNode().id}`;
return secret_token.replace(/[^a-zA-Z0-9\_\-]+/g, '');
}
28 changes: 26 additions & 2 deletions packages/nodes-base/nodes/Telegram/TelegramTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
IWebhookResponseData,
} from 'n8n-workflow';

import { apiRequest, getImageBySize } from './GenericFunctions';
import { apiRequest, getImageBySize, getSecretToken } from './GenericFunctions';

import type { IEvent } from './IEvent';

Expand All @@ -17,7 +17,8 @@ export class TelegramTrigger implements INodeType {
name: 'telegramTrigger',
icon: 'file:telegram.svg',
group: ['trigger'],
version: 1,
version: [1, 1.1],
defaultVersion: 1.1,
subtitle: '=Updates: {{$parameter["updates"].join(", ")}}',
description: 'Starts the workflow on a Telegram update',
defaults: {
Expand All @@ -40,6 +41,13 @@ export class TelegramTrigger implements INodeType {
},
],
properties: [
{
displayName:
'Due to Telegram API limitations, you can use just one Telegram trigger for each bot at a time',
name: 'telegramTriggerNotice',
type: 'notice',
default: '',
},
{
displayName: 'Trigger On',
name: 'updates',
Expand Down Expand Up @@ -188,9 +196,12 @@ export class TelegramTrigger implements INodeType {

const endpoint = 'setWebhook';

const secret_token = getSecretToken.call(this);

const body = {
url: webhookUrl,
allowed_updates: allowedUpdates,
secret_token,
};

await apiRequest.call(this, 'POST', endpoint, body);
Expand All @@ -216,6 +227,19 @@ export class TelegramTrigger implements INodeType {
const credentials = await this.getCredentials('telegramApi');

const bodyData = this.getBodyData() as IEvent;
const headerData = this.getHeaderData();

const nodeVersion = this.getNode().typeVersion;
if (nodeVersion > 1) {
const secret = getSecretToken.call(this);
if (secret !== headerData['x-telegram-bot-api-secret-token']) {
const res = this.getResponseObject();
res.status(403).json({ message: 'Provided secret is not valid' });
return {
noWebhookResponse: true,
};
}
}

const additionalFields = this.getNodeParameter('additionalFields') as IDataObject;

Expand Down
Loading