

















@@ -0,0 +1,175 @@
1+import { describe, expect, it } from "vitest";
2+import { stringifyTelegramRawUpdateForLog } from "./raw-update-log.js";
3+4+describe("stringifyTelegramRawUpdateForLog", () => {
5+it("redacts private Telegram raw update fields before verbose logging", () => {
6+const update = {
7+update_id: 98765,
8+message: {
9+message_id: 44,
10+from: {
11+id: 123456,
12+is_bot: false,
13+first_name: "Alice",
14+last_name: "Example",
15+username: "alice_private",
16+language_code: "en-US",
17+is_premium: true,
18+},
19+chat: {
20+id: -1001234567890,
21+type: "private",
22+title: "Private Chat",
23+username: "private_chat",
24+},
25+text: "please inspect https://private.example/secret",
26+entities: [{ type: "url", offset: 15, length: 30, url: "https://private.example/entity" }],
27+link_preview_options: { url: "https://private.example/preview" },
28+new_chat_members: [
29+{
30+id: 246810,
31+is_bot: false,
32+first_name: "New",
33+last_name: "Member",
34+username: "new_member_user",
35+language_code: "fr-CA",
36+added_to_attachment_menu: true,
37+},
38+],
39+},
40+callback_query: {
41+id: "callback-id",
42+from: { id: 7777, first_name: "Bob", username: "bob_private" },
43+data: "sensitive callback payload",
44+},
45+};
46+47+const rawLog = stringifyTelegramRawUpdateForLog(update);
48+49+expect(rawLog).toContain('"update_id":98765');
50+expect(rawLog).toContain('"message_id":44');
51+expect(rawLog).toContain('"text":"[redacted]"');
52+expect(rawLog).toContain('"url":"[redacted]"');
53+for (const privateValue of [
54+"123456",
55+"-1001234567890",
56+"Alice",
57+"Example",
58+"alice_private",
59+"en-US",
60+"Private Chat",
61+"private_chat",
62+"please inspect",
63+"https://private.example",
64+"246810",
65+"New",
66+"Member",
67+"new_member_user",
68+"fr-CA",
69+"added_to_attachment_menu",
70+"7777",
71+"Bob",
72+"bob_private",
73+"sensitive callback payload",
74+]) {
75+expect(rawLog).not.toContain(privateValue);
76+}
77+});
78+79+it("redacts identifiers from less common Telegram update shapes", () => {
80+const update = {
81+update_id: 45678,
82+business_connection: {
83+id: "business-connection-id",
84+user: {
85+id: 111222,
86+is_bot: false,
87+first_name: "Business",
88+username: "business_user",
89+},
90+user_chat_id: 333444,
91+date: 1712345678,
92+can_reply: true,
93+is_enabled: true,
94+},
95+chat_join_request: {
96+chat: {
97+id: -100555666,
98+type: "supergroup",
99+title: "Join Request Group",
100+username: "join_request_group",
101+},
102+from: {
103+id: 777888,
104+is_bot: false,
105+first_name: "Joiner",
106+username: "join_user",
107+},
108+user_chat_id: 999000,
109+date: 1712345679,
110+bio: "private bio",
111+invite_link: {
112+invite_link: "https://t.me/+private-invite",
113+creator: {
114+id: 222333,
115+is_bot: false,
116+first_name: "Creator",
117+username: "invite_creator",
118+},
119+},
120+},
121+message_reaction: {
122+chat: {
123+id: -100111222,
124+type: "supergroup",
125+title: "Reaction Group",
126+},
127+message_id: 99,
128+actor_chat: {
129+id: -100333444,
130+type: "channel",
131+title: "Actor Channel",
132+username: "actor_channel",
133+},
134+date: 1712345680,
135+old_reaction: [],
136+new_reaction: [],
137+},
138+};
139+140+const rawLog = stringifyTelegramRawUpdateForLog(update);
141+142+expect(rawLog).toContain('"update_id":45678');
143+expect(rawLog).toContain('"message_id":99');
144+expect(rawLog).toContain('"can_reply":true');
145+expect(rawLog).toContain('"is_enabled":true');
146+expect(rawLog).toContain('"user_chat_id":"[redacted]"');
147+expect(rawLog).toContain('"id":"[redacted]"');
148+for (const privateValue of [
149+"business-connection-id",
150+"111222",
151+"Business",
152+"business_user",
153+"333444",
154+"-100555666",
155+"Join Request Group",
156+"join_request_group",
157+"777888",
158+"Joiner",
159+"join_user",
160+"999000",
161+"private bio",
162+"https://t.me/+private-invite",
163+"222333",
164+"Creator",
165+"invite_creator",
166+"-100111222",
167+"Reaction Group",
168+"-100333444",
169+"Actor Channel",
170+"actor_channel",
171+]) {
172+expect(rawLog).not.toContain(privateValue);
173+}
174+});
175+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。