@niwa-labs/telegram 0.0.0-stage → 0.0.0
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/LICENSE +21 -0
- package/README.md +2 -2
- package/package.json +18 -4
- package/src/bot.ts +212 -0
- package/src/index.ts +964 -0
- package/test/bot.test.ts +101 -0
- package/test/telegram.test.ts +1371 -0
- package/tsconfig.json +6 -0
package/test/bot.test.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// The client of the Bot API never lets its token out in an error, whatever Telegram, or what stands
|
|
2
|
+
// in its place, says back.
|
|
3
|
+
|
|
4
|
+
import { expect, test } from "bun:test";
|
|
5
|
+
|
|
6
|
+
import { NotSent } from "@niwa-labs/core";
|
|
7
|
+
import { createBot } from "../src/bot.ts";
|
|
8
|
+
import { chatId } from "../src/index.ts";
|
|
9
|
+
|
|
10
|
+
const TOKEN = "123456:secret-token/with+chars";
|
|
11
|
+
|
|
12
|
+
/** A server that answers every call with the given response, quoting the path it was called on. */
|
|
13
|
+
function echoing(respond: (path: string) => Response): { base: string; stop(): void } {
|
|
14
|
+
const server = Bun.serve({ port: 0, fetch: (request) => respond(new URL(request.url).pathname) });
|
|
15
|
+
return { base: server.url.toString().replace(/\/$/, ""), stop: () => void server.stop(true) };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function failure(call: () => Promise<unknown>): Promise<string> {
|
|
19
|
+
try {
|
|
20
|
+
await call();
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return error instanceof Error ? error.message : String(error);
|
|
23
|
+
}
|
|
24
|
+
throw new Error("the call should have failed");
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
test("bot: a body that is not JSON is not quoted, so an echoed path cannot leak the token", async () => {
|
|
28
|
+
const server = echoing((path) => new Response(`<html>no such path ${path}</html>`, { status: 502 }));
|
|
29
|
+
try {
|
|
30
|
+
const bot = createBot({ token: TOKEN, apiBase: server.base });
|
|
31
|
+
const message = await failure(() => bot.sendMessage(chatId(1), "hi"));
|
|
32
|
+
expect(message).not.toContain(TOKEN);
|
|
33
|
+
expect(message).not.toContain(encodeURIComponent(TOKEN));
|
|
34
|
+
expect(message).toContain("sendMessage");
|
|
35
|
+
expect(message).toContain("502");
|
|
36
|
+
} finally {
|
|
37
|
+
server.stop();
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test("bot: the description of a refusal is redacted", async () => {
|
|
42
|
+
const server = echoing((path) => Response.json({ ok: false, error_code: 404, description: `Not Found: ${path}` }, { status: 404 }));
|
|
43
|
+
try {
|
|
44
|
+
const bot = createBot({ token: TOKEN, apiBase: server.base });
|
|
45
|
+
const message = await failure(() => bot.getUpdates(0, 0));
|
|
46
|
+
expect(message).not.toContain(TOKEN);
|
|
47
|
+
expect(message).not.toContain(encodeURIComponent(TOKEN));
|
|
48
|
+
expect(message).toContain("Not Found");
|
|
49
|
+
} finally {
|
|
50
|
+
server.stop();
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("bot: a body that breaks off while it is read does not leak the token", async () => {
|
|
55
|
+
const real = globalThis.fetch;
|
|
56
|
+
// A body that fails the way a dropped connection does, quoting the URL it was read from.
|
|
57
|
+
globalThis.fetch = (async (input: Parameters<typeof fetch>[0]) => {
|
|
58
|
+
const body = new ReadableStream({
|
|
59
|
+
start(controller) {
|
|
60
|
+
controller.error(new Error(`connection lost on ${String(input)}`));
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
return new Response(body, { status: 200 });
|
|
64
|
+
}) as typeof fetch;
|
|
65
|
+
try {
|
|
66
|
+
const bot = createBot({ token: TOKEN, apiBase: "http://telegram.invalid" });
|
|
67
|
+
const message = await failure(() => bot.sendMessage(chatId(1), "hi"));
|
|
68
|
+
expect(message).not.toContain(TOKEN);
|
|
69
|
+
expect(message).not.toContain(encodeURIComponent(TOKEN));
|
|
70
|
+
expect(message).toContain("sendMessage");
|
|
71
|
+
expect(message).toContain("connection lost");
|
|
72
|
+
} finally {
|
|
73
|
+
globalThis.fetch = real;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("a rejected send is unsent and a server failure remains uncertain", async () => {
|
|
78
|
+
for (const status of [403, 429, 502]) {
|
|
79
|
+
const server = echoing(() => Response.json({ ok: false, error_code: status, description: "refused" }, { status }));
|
|
80
|
+
try {
|
|
81
|
+
const error = await createBot({ token: TOKEN, apiBase: server.base }).sendMessage(chatId(1), "reply").catch((error: unknown) => error);
|
|
82
|
+
expect(error).toBeInstanceOf(Error);
|
|
83
|
+
expect(error instanceof NotSent).toBe(status < 500);
|
|
84
|
+
} finally {
|
|
85
|
+
server.stop();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("connection refusal is unsent and a connection reset can follow delivery", async () => {
|
|
91
|
+
const real = globalThis.fetch;
|
|
92
|
+
try {
|
|
93
|
+
for (const code of ["ECONNREFUSED", "ECONNRESET"]) {
|
|
94
|
+
globalThis.fetch = (async () => { throw Object.assign(new Error("network failure"), { code }); }) as typeof fetch;
|
|
95
|
+
const failure = await createBot({ token: TOKEN }).sendMessage(chatId(1), "reply").catch((error: unknown) => error);
|
|
96
|
+
expect(failure instanceof NotSent).toBe(code === "ECONNREFUSED");
|
|
97
|
+
}
|
|
98
|
+
} finally {
|
|
99
|
+
globalThis.fetch = real;
|
|
100
|
+
}
|
|
101
|
+
});
|