Files
agent-zero/tests/test_telegram_webhook_security.py
Jan Tomášek 188cff2073 Move optional runtimes into persistent gVisor plugin sandboxes
Move tunnel, browser, document, speech and integration runtimes into plugin-owned
sandboxes with lazy dependencies and instance-scoped lifecycle management.
Reuse Tuxee display assets and the sandbox image's Python/Node runtimes, enable
automatic Desktop login, and show elapsed time for each browser launch.

Slim the outer image, use offline ONNX embeddings, and calculate sandbox storage
only on demand. Include regression coverage and updated runtime documentation.
Exclude local handoff notes and temporary verification artifacts from publication.
2026-11-08 21:43:22 +01:00

90 lines
4.2 KiB
Python

import asyncio
import threading
import unittest
from unittest.mock import AsyncMock, patch
from flask import Flask
from helpers import api, cache
from plugins._telegram_integration.helpers import bot_manager as bm
class TelegramWebhookSecurityTests(unittest.TestCase):
def test_http_authentication_before_real_dispatch(self):
received = []
async def receive(message):
received.append((message.from_user.id, message.text))
with patch.object(bm, "_bots", {}), patch.object(bm, 'cache_bot_info', new_callable=AsyncMock), \
patch.object(bm, 'rpc', new_callable=AsyncMock) as rpc:
instance = bm.create_bot("security_test", "123456:test", receive, receive)
app = Flask(__name__)
api.register_api_route(app, threading.Lock())
client = app.test_client()
payload = {"update_id": 1, "message": {
"message_id": 1, "date": 1700000000,
"from": {"id": 424242, "is_bot": False, "first_name": "Test"},
"chat": {"id": 424242, "type": "private"},
"text": "POC_CONFIRMED",
}}
rpc.return_value = [{'kind': 'message', 'event': payload['message']}]
try:
for active, secret, header, expected in [
(False, "", "", 403),
(False, "x" * 32, "x" * 32, 403),
(True, "", "", 403),
(True, "x" * 32, "", 403),
(True, "x" * 32, "wrong", 403),
(True, "x" * 32, "é", 403),
(True, "x" * 32, "x" * 32, 200),
]:
with self.subTest(active=active, header=header, expected=expected):
instance.webhook_active = active
instance.webhook_secret = secret
received.clear()
response = client.post(
"/api/plugins/_telegram_integration/webhook?bot=security_test",
json=payload,
headers={"X-Telegram-Bot-Api-Secret-Token": header},
)
self.assertEqual(response.status_code, expected)
self.assertEqual(received, [(424242, "POC_CONFIRMED")] if expected == 200 else [])
finally:
cache.clear(api.CACHE_AREA)
asyncio.run(instance.bot.session.close())
def test_webhook_setup_and_polling_revoke_authentication(self):
async def receive(message):
pass
async def run():
with patch.object(bm, "_bots", {}):
instance = bm.create_bot("security_test", "123456:test", receive, receive)
try:
with patch.object(instance.bot, "set_webhook", new_callable=AsyncMock) as register:
for secret in ("", "short", "é" * 32, "x" * 257):
with self.subTest(secret=secret):
with self.assertRaises(ValueError):
await bm.setup_webhook(instance, "https://example.invalid", secret)
self.assertFalse(instance.webhook_active)
register.assert_not_awaited()
await bm.setup_webhook(instance, "https://example.invalid", "x" * 32)
self.assertEqual(register.await_args.kwargs["secret_token"], "x" * 32)
self.assertTrue(instance.webhook_active)
with patch.object(instance.bot, "delete_webhook", new_callable=AsyncMock), patch.object(
bm, "cache_bot_info", new_callable=AsyncMock
), patch.object(bm, "rpc", new_callable=AsyncMock, return_value=[]):
task = await bm.start_polling(instance)
self.assertFalse(instance.webhook_active)
self.assertEqual(instance.webhook_secret, "")
await bm.stop_polling(instance)
self.assertTrue(task.cancelled())
finally:
await instance.bot.session.close()
asyncio.run(run())
if __name__ == "__main__":
unittest.main()