mirror of
https://github.com/wassname/pi-telegram.git
synced 2026-06-27 18:05:53 +08:00
15fa661b7a
The first-DM auto-pair behavior combined with ! shell passthrough meant
the first account to DM the bot gained arbitrary shell access. This
removes that footgun entirely.
- allowedUserId must be set before polling starts; missing config blocks
polling with a TUI warning rather than silently accepting any sender
- TELEGRAM_ALLOWED_USER_ID env var is read on session start and
overwrites the saved config value, so rotating the allowed user is a
restart away
- /telegram-setup now prompts for a numeric user ID after the bot token
if one is not already configured
- Denied senders receive an auth error reply; their numeric ID is also
logged to the pi TUI as a warning so operators can identify themselves
on a fresh install without needing @userinfobot
- Dropped the {kind: "pair"} authorization state entirely; undefined
allowedUserId now produces deny, not pair
- Removed pairTelegramUserIfNeeded, shouldPair, shouldNotifyPaired
Existing installs with allowedUserId already in telegram.json are
unaffected. Fresh installs require explicit configuration.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
/**
|
|
* Regression tests for Telegram setup prompt defaults
|
|
* Covers token-prefill priority across stored config, environment variables, and placeholder fallback
|
|
*/
|
|
|
|
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { __telegramTestUtils } from "../index.ts";
|
|
|
|
test("Bot token input prefers stored config over env vars", () => {
|
|
const value = __telegramTestUtils.getTelegramBotTokenInputDefault(
|
|
{
|
|
TELEGRAM_KEY: "key-last",
|
|
TELEGRAM_TOKEN: "token-third",
|
|
TELEGRAM_BOT_KEY: "key-second",
|
|
TELEGRAM_BOT_TOKEN: "token-first",
|
|
},
|
|
"stored-token",
|
|
);
|
|
assert.equal(value, "stored-token");
|
|
});
|
|
|
|
test("Bot token input prefers the first configured Telegram env var when no config exists", () => {
|
|
const value = __telegramTestUtils.getTelegramBotTokenInputDefault({
|
|
TELEGRAM_KEY: "key-last",
|
|
TELEGRAM_TOKEN: "token-third",
|
|
TELEGRAM_BOT_KEY: "key-second",
|
|
TELEGRAM_BOT_TOKEN: "token-first",
|
|
});
|
|
assert.equal(value, "token-first");
|
|
});
|
|
|
|
test("Bot token prompt uses the editor when a real prefill exists", () => {
|
|
const prompt = __telegramTestUtils.getTelegramBotTokenPromptSpec({
|
|
TELEGRAM_BOT_TOKEN: "token-first",
|
|
});
|
|
assert.deepEqual(prompt, {
|
|
method: "editor",
|
|
value: "token-first",
|
|
});
|
|
});
|
|
|
|
test("Bot token prompt shows stored config before env values", () => {
|
|
const prompt = __telegramTestUtils.getTelegramBotTokenPromptSpec(
|
|
{
|
|
TELEGRAM_BOT_TOKEN: "token-first",
|
|
},
|
|
"stored-token",
|
|
);
|
|
assert.deepEqual(prompt, {
|
|
method: "editor",
|
|
value: "stored-token",
|
|
});
|
|
});
|
|
|
|
test("Bot token input skips blank env vars and falls back to config", () => {
|
|
const value = __telegramTestUtils.getTelegramBotTokenInputDefault(
|
|
{
|
|
TELEGRAM_BOT_TOKEN: " ",
|
|
TELEGRAM_BOT_KEY: "",
|
|
TELEGRAM_TOKEN: " ",
|
|
},
|
|
"stored-token",
|
|
);
|
|
assert.equal(value, "stored-token");
|
|
});
|
|
|
|
test("Bot token input falls back to placeholder when no value exists", () => {
|
|
const value = __telegramTestUtils.getTelegramBotTokenInputDefault({});
|
|
assert.equal(value, "123456:ABCDEF...");
|
|
});
|
|
|
|
test("Bot token prompt uses placeholder input when no prefill exists", () => {
|
|
const prompt = __telegramTestUtils.getTelegramBotTokenPromptSpec({});
|
|
assert.deepEqual(prompt, {
|
|
method: "input",
|
|
value: "123456:ABCDEF...",
|
|
});
|
|
});
|
|
|
|
test("readAllowedUserIdFromEnv returns undefined when env var is not set", () => {
|
|
assert.equal(__telegramTestUtils.readAllowedUserIdFromEnv({}), undefined);
|
|
assert.equal(__telegramTestUtils.readAllowedUserIdFromEnv({ TELEGRAM_ALLOWED_USER_ID: " " }), undefined);
|
|
});
|
|
|
|
test("readAllowedUserIdFromEnv parses a valid positive integer", () => {
|
|
assert.equal(__telegramTestUtils.readAllowedUserIdFromEnv({ TELEGRAM_ALLOWED_USER_ID: "123456789" }), 123456789);
|
|
assert.equal(__telegramTestUtils.readAllowedUserIdFromEnv({ TELEGRAM_ALLOWED_USER_ID: " 42 " }), 42);
|
|
});
|
|
|
|
test("readAllowedUserIdFromEnv throws on non-integer or non-positive value", () => {
|
|
assert.throws(
|
|
() => __telegramTestUtils.readAllowedUserIdFromEnv({ TELEGRAM_ALLOWED_USER_ID: "notanumber" }),
|
|
/not a valid Telegram user ID/,
|
|
);
|
|
assert.throws(
|
|
() => __telegramTestUtils.readAllowedUserIdFromEnv({ TELEGRAM_ALLOWED_USER_ID: "0" }),
|
|
/not a valid Telegram user ID/,
|
|
);
|
|
assert.throws(
|
|
() => __telegramTestUtils.readAllowedUserIdFromEnv({ TELEGRAM_ALLOWED_USER_ID: "-5" }),
|
|
/not a valid Telegram user ID/,
|
|
);
|
|
});
|