@zhin.js/adapter-telegram 5.0.3 → 6.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +63 -0
- package/README.md +4 -3
- package/adapters/telegram.ts +12 -0
- package/commands/endpoint/add/[name:string].ts +3 -0
- package/commands/endpoint/list.ts +3 -0
- package/commands/endpoint/remove/[name:string].ts +3 -0
- package/lib/endpoint.d.ts +8 -4
- package/lib/endpoint.js +67 -4
- package/lib/index.d.ts +1 -1
- package/lib/index.js +1 -1
- package/lib/polling.js +2 -2
- package/lib/protocol.d.ts +39 -0
- package/lib/protocol.js +137 -14
- package/lib/telegram-endpoint-commands.d.ts +1 -0
- package/lib/telegram-endpoint-commands.js +16 -0
- package/lib/telegram-runtime-state.d.ts +1 -0
- package/lib/telegram-runtime-state.js +6 -0
- package/lib/webhook.d.ts +2 -3
- package/lib/webhook.js +11 -1
- package/package.json +19 -13
- package/plugin.ts +5 -1
- package/schema.json +48 -13
- package/src/endpoint.ts +82 -5
- package/src/index.ts +3 -0
- package/src/polling.ts +2 -1
- package/src/protocol.ts +164 -11
- package/src/telegram-endpoint-commands.ts +17 -0
- package/src/telegram-runtime-state.ts +7 -0
- package/src/webhook.ts +9 -1
package/src/webhook.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Telegram webhook HTTP: secret token → parse → handle update.
|
|
3
3
|
*/
|
|
4
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
4
5
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
5
6
|
import type { HttpHost, HttpRouteRegistration } from '@zhin.js/host-http';
|
|
6
7
|
import { getLogger } from '@zhin.js/logger';
|
|
@@ -8,6 +9,13 @@ import { readTextBody, type ResolvedTelegramConfig, type TelegramUpdate } from '
|
|
|
8
9
|
|
|
9
10
|
const logger = getLogger('telegram');
|
|
10
11
|
|
|
12
|
+
/** 等长时才 timingSafeEqual,避免长度差异直接抛异常。 */
|
|
13
|
+
export function safeTokenEqual(a: string, b: string): boolean {
|
|
14
|
+
const bufA = Buffer.from(a, 'utf8');
|
|
15
|
+
const bufB = Buffer.from(b, 'utf8');
|
|
16
|
+
return bufA.length === bufB.length && timingSafeEqual(bufA, bufB);
|
|
17
|
+
}
|
|
18
|
+
|
|
11
19
|
export interface TelegramWebhookHandler {
|
|
12
20
|
readonly config: ResolvedTelegramConfig;
|
|
13
21
|
readonly isOpen: boolean;
|
|
@@ -36,7 +44,7 @@ export async function handleTelegramWebhookRequest(
|
|
|
36
44
|
if (secret) {
|
|
37
45
|
const header = request.headers['x-telegram-bot-api-secret-token'];
|
|
38
46
|
const token = Array.isArray(header) ? header[0] : header;
|
|
39
|
-
if (token
|
|
47
|
+
if (!token || !safeTokenEqual(token, secret)) {
|
|
40
48
|
response.writeHead(403, { 'Content-Type': 'application/json' });
|
|
41
49
|
response.end(JSON.stringify({ ok: false, description: 'Invalid secret token' }));
|
|
42
50
|
return;
|