Skip to content

fix(security): address input validation gaps in message handling#168

Merged
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-161-cbee42e4c27b
Apr 8, 2026
Merged

fix(security): address input validation gaps in message handling#168
xlabtg merged 3 commits intoxlabtg:mainfrom
konard:issue-161-cbee42e4c27b

Conversation

@konard
Copy link
Copy Markdown

@konard konard commented Apr 7, 2026

Summary

Fixes #161 — addresses four input validation gaps reported in the high-severity security issue.

Changes

1. analyzeMessage chatId allowlist bypass (handlers.ts)

parseInt("-100123abc") returns -100123, which could bypass group_allow_from checks by injecting trailing characters into a valid chatId. Fixed by using Number() + Number.isInteger() for strict validation — any non-integer chatId is denied.

2. sanitizeForContext missing length cap (sanitize.ts)

sanitizeForContext had no size limit, allowing large context payloads to be injected into system prompts. Added a 32 KB (32 768 character) cap, consistent with the spirit of the existing 128-char cap on sanitizeForPrompt.

3. peerCache eviction only removed one entry (bridge.ts)

When the cache hit 5 000 entries it only evicted the single oldest entry per insertion, allowing near-unbounded growth under high load. Replaced with a batch eviction strategy: when the limit is exceeded, the oldest half is deleted (down to 2 500 entries). Added a private evictPeerCacheIfNeeded() helper to deduplicate the two identical eviction blocks.

Note: Issue point #2 (isOwner fallback logic in runtime.ts) was reviewed and the existing behavior is intentional and correctly documented in a code comment: owner_id takes strict precedence; admin_ids is the explicit fallback only when owner_id is absent. No change needed.

Test plan

  • Added 2 tests in handlers.test.ts verifying that non-integer chatIds (e.g., "-100123abc", "notanumber") are rejected by the allowlist check even when a numeric prefix matches
  • Updated sanitize.test.ts to verify the 32 KB cap is enforced and existing passing tests updated to match new behaviour
  • All 196 tests in the affected test files pass (src/utils/__tests__/sanitize.test.ts, src/telegram/__tests__/handlers.test.ts)
  • TypeScript compiles with 0 errors

🤖 Generated with Claude Code

konard and others added 2 commits April 7, 2026 10:33
Adding .gitkeep for PR creation (default mode).
This file will be removed when the task is complete.

Issue: xlabtg#161
- Validate chatId is a strict integer before allowlist check in
  handlers.ts to prevent parseInt partial-match bypass
- Add 32 KB length cap to sanitizeForContext to prevent large
  payload injection via RAG context
- Replace single-entry peerCache eviction with batch eviction
  (halves cache to 2500 entries) to prevent unbounded memory growth

Fixes xlabtg#161

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@konard konard changed the title [WIP] 🔴 HIGH: Input validation gaps in message handling and sanitization fix(security): address input validation gaps in message handling Apr 7, 2026
@konard konard marked this pull request as ready for review April 7, 2026 10:38
@konard
Copy link
Copy Markdown
Author

konard commented Apr 7, 2026

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $1.314564
  • Calculated by Anthropic: $1.314564 USD
  • Difference: $-0.000000 (-0.00%)

📊 Context and tokens usage:

  • Context window: 75.8K / 1M input tokens (8%), 12.4K / 64K output tokens (19%)

Total: 69.3K + 2.9M cached input tokens, 12.4K output tokens, $1.314564 cost

🤖 Models used:

  • Tool: Anthropic Claude Code
  • Requested: sonnet
  • Model: Claude Sonnet 4.6 (claude-sonnet-4-6)

📎 Log file uploaded as Gist (1119KB)


Now working session is ended, feel free to review and add any feedback on the solution draft.

@konard
Copy link
Copy Markdown
Author

konard commented Apr 7, 2026

✅ Ready to merge

This pull request is now ready to be merged:

  • CI workflows exist but were not triggered for this commit
  • No merge conflicts
  • No pending changes

Monitored by hive-mind with --auto-restart-until-mergeable flag

@xlabtg xlabtg merged commit a1eca3a into xlabtg:main Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🔴 HIGH: Input validation gaps in message handling and sanitization

2 participants