Skip to content

fix(gateway): isolate quoted context from reference expansion - #4

Draft
Bonchaloo wants to merge 2 commits into
mainfrom
fix/gateway-reply-quote-framing
Draft

Bonchaloo wants to merge 2 commits into
mainfrom
fix/gateway-reply-quote-framing

Conversation

@Bonchaloo

@Bonchaloo Bonchaloo commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Issue

Telegram reply quotes and Discord channel-history backfill could contain text written by another sender. The gateway placed both strings in the current user's message before processing @file/@url references. A third-party @file marker therefore reached the reference expander under the authorized sender's turn. Reply text was also interpolated raw into [Replying to: "..."], so punctuation could break the visible quote frame.

Change

  • Process context references only from the sender's original event.text. Quoted replies, channel history, sender names, attachment names, image analysis, and speech transcripts retain their text without activating @file/@url. The sender's own references still work alongside media context.
  • JSON encode reply quotes and channel history onto single lines, with explicit prior-message labels. Reply quotes remain bounded to 500 characters. This closes Unicode line-separator and forged [New message] line bypasses found in independent review.

Verification

  • An offline harness AST-executed the actual _prepare_inbound_message_text method with a mocked context expander. On base 929dd9c0d776186d1e8bf268cccfdb31e0398365, synthetic quote, channel-history, image-analysis, and transcript markers each invoked the expander and surfaced a mock canary. On this branch, none did; the sender's own marker still invoked it. No file or URL was read.
  • The prior reply formatter emitted a separate operator-looking line for U+2028, U+2029, and U+0085; the patched formatter keeps each inside one JSON string. Raw channel history emitted two [New message] lines with a synthetic spoof; the patched formatter emits one.
  • scripts/run_tests.sh tests/gateway/test_reply_context_security.py -q: 9 passed.
  • pytest tests/gateway/test_reply_to_injection.py tests/gateway/test_session.py -q: 106 passed using the isolated Python 3.13 test interpreter with read-only Python 3.12 site-packages for httpx. This mixed environment is a local check, not a clean dependency install.
  • Ruff, Python compile, and git diff --check: passed. Initial CI failures outside these paths are documented in the local assessment; new CI is pending.

No live gateway message, model compliance, real file read, network fetch, or terminal command was tested. Draft for security review; please do not merge until the integration tests run.

@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

🔎 Lint report: fix/gateway-reply-quote-framing vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 12440 on HEAD, 12432 on base (🆕 +8)

🆕 New issues (8):

Rule Count
unsupported-operator 3
invalid-argument-type 2
invalid-assignment 1
unresolved-import 1
unresolved-attribute 1
First entries
tests/gateway/test_reply_to_injection.py:290: [invalid-assignment] invalid-assignment: Object of type `() -> Literal["text"]` is not assignable to attribute `_decide_image_input_mode` of type `def _decide_image_input_mode(self) -> str`
tests/gateway/test_reply_context_security.py:5: [unresolved-import] unresolved-import: Cannot resolve imported module `pytest`
gateway/run.py:9535: [invalid-argument-type] invalid-argument-type: Argument to function `format_channel_context` is incorrect: Expected `str`, found `str | None`
tests/gateway/test_reply_to_injection.py:269: [unsupported-operator] unsupported-operator: Operator `in` is not supported between objects of type `Literal["@file:canary.txt"]` and `str | None`
gateway/run.py:9543: [invalid-argument-type] invalid-argument-type: Argument to function `format_reply_context` is incorrect: Expected `str`, found `str | None`
tests/gateway/test_reply_to_injection.py:308: [unresolved-attribute] unresolved-attribute: Attribute `args` is not defined on `None` in union `_Call | None`
tests/gateway/test_reply_to_injection.py:309: [unsupported-operator] unsupported-operator: Operator `in` is not supported between objects of type `Literal["@file:external.txt"]` and `str | None`
tests/gateway/test_reply_to_injection.py:310: [unsupported-operator] unsupported-operator: Operator `in` is not supported between objects of type `Literal["MOCK_FILE_CANARY"]` and `str | None`

✅ Fixed issues: none

Unchanged: 6744 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

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.

1 participant