feat(ai): extend the prompt injection defense to streamed suggestions - #668
Open
Reversean wants to merge 1 commit into
Open
feat(ai): extend the prompt injection defense to streamed suggestions#668Reversean wants to merge 1 commit into
Reversean wants to merge 1 commit into
Conversation
Reversean
force-pushed
the
fix/ai-stream-prompt-injection
branch
2 times, most recently
from
July 29, 2026 16:21
a08550b to
18f7aa1
Compare
Reversean
force-pushed
the
fix/ai-stream-prompt-injection
branch
from
July 29, 2026 17:22
18f7aa1 to
571263c
Compare
Reversean
force-pushed
the
fix/ai-stream-prompt-injection
branch
from
July 29, 2026 17:50
571263c to
e64c1a7
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Extends the existing prompt-injection / marker-leak defense from one-shot completions to streamed AI suggestions by introducing nonce-based spotlighting, deterministic leak detection, and a streaming “holdback” guard that prevents nonce substrings from crossing chunk boundaries.
Changes:
- Add spotlighting markers + per-request random nonce in the prompt, and reject outputs that reproduce the nonce.
- Add streaming leak defense via a holdback guard wired into
streamText(...).experimental_transform. - Add an Express
/integration/ai/streamroute that proxies the UI message stream response; expand tests and shared test helpers accordingly.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| test/services/askAi.test.ts | Expands AIService tests to cover spotlighting, rejection reporting, and stream guard wiring. |
| test/services/askAi-spotlighting.test.ts | Adds unit coverage for prompt marker wrapping and nonce generation/collision handling. |
| test/services/askAi-leak-detector.test.ts | Adds unit tests for deterministic nonce leak detection behavior. |
| test/services/askAi-holdback.test.ts | Adds unit tests for the streaming holdback guard behavior and edge cases. |
| test/integrations/vercel-ai.test.ts | Adds tests for streamText usage and the guarded transform wiring/semantics. |
| test/integrations/github-routes.test.ts | Refactors to reuse the new Express request helper. |
| test/integrations/ai-routes.test.ts | Adds route-level tests for the new SSE proxy endpoint and auth/validation paths. |
| test/helpers/expressRequest.ts | Introduces a reusable Express request/response harness that supports pipe() streaming. |
| src/services/types.ts | Exports the Event type for reuse (e.g., AI service internals). |
| src/services/askAi/security/spotlighting.ts | Implements nonce-based marker wrapping (buildEventPrompt) + system spotlighting instruction. |
| src/services/askAi/security/leakDetector.ts | Adds isLeaked + fallback message constant for suggestion rejection. |
| src/services/askAi/security/holdback.ts | Implements streaming holdback guard (createLeakGuard) to detect nonce across deltas. |
| src/services/askAi/inputs/eventSolving.ts | Documents that this serialization is unwrapped/untrusted and must be wrapped via spotlighting. |
| src/services/ai.ts | Updates AIService to use spotlighting + leak detection, and adds a streaming suggestion method. |
| src/integrations/vercel-ai/routes.ts | Adds /integration/ai/stream proxy route for the UI message stream response. |
| src/integrations/vercel-ai/index.ts | Adds vercelAIApi.stream and the guarded transform that applies the stream guard to deltas. |
| src/index.ts | Registers the new AI assistant routes on the Express app. |
| src/directives/requireUserInWorkspace.ts | Exports checkUserInWorkspaceByProjectId for reuse in the new route. |
| package.json | Bumps package version. |
| .eslintrc.js | Declares Fetch/Streams globals used in tests and streaming code. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Reversean
added a commit
that referenced
this pull request
Aug 5, 2026
The stream route mapped any error from streamSuggestion to a 404 "Event not found", including failures unrelated to the event lookup (e.g. a stream construction error). Only the exact "Event not found" error is now reported as 404; anything else is forwarded to Express's error handling. Flagged by Copilot while reviewing #668, against code this PR added.
The one-shot path scans the finished answer before returning it. A streamed answer leaves the server while it is still being written, so the same scan cannot be applied: a nonce split across two deltas passes a per-delta check untouched and the marker reaches the client. The stream now runs through a guard holding back the last nonce.length - 1 characters and scanning them together with each new delta, releasing only text that can no longer begin the nonce. That length is the exact minimum for an occurrence to always fall inside a single scanned window. The guard is applied to the model's typed stream parts rather than the encoded SSE bytes, where JSON envelopes and escaping would split the nonce beyond the reach of a substring scan. On detection the rest of the answer is replaced by the fallback message and the event ids are logged. The stream is not aborted: aborting obliges the caller to synthesize finish chunks whose shape follows the SDK version, and suppressing text keeps the stream well formed instead. What counts as a leak stays in the domain layer behind a port, so the provider adapter still knows nothing about it.
Reversean
force-pushed
the
fix/ai-stream-prompt-injection
branch
from
August 5, 2026 14:12
e64c1a7 to
983fded
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The one-shot path scans the finished answer before returning it. A stream cannot be checked that way: it leaves the server while still being written, and a nonce split across two deltas passes a per-chunk scan untouched.
Add a holdback guard. It withholds the last
nonce.length − 1characters and scanswithheld + new chunkon every delta, releasing only text that cannot be the start of the nonce. The bound is exact — for the nonce to straddle the send boundary, more thannonce.length − 1characters after its first one must already be sent — so every occurrence falls inside one scanned window and the nonce never reaches the client. Detection itself is unchanged and shared with the one-shot path.The guard runs in
streamText'sexperimental_transform, on typedTextStreamPartchunks: in the encoded SSE bytes the nonce is split by JSON envelopes and escaping, where no substring search can find it. The withheld tail is released ontext-endrather than in the TransformStream'sflush, so deltas stay inside the block they belong to.stopStreamis not used: it requires synthesisingfinish-stepandfinishchunks whose shape is tied to the SDK version. Suppressing text instead keeps the stream well-formed, at the cost of tokens the model goes on producing unheard — paid only when the tripwire fires.On detection the guard emits the fallback message once and stays silent, logging
eventIdandoriginalEventId. The prefix already sent cannot be retracted, which is acceptable: the nonce is what triggered detection and is still withheld when it fires.The transport takes a
StreamGuardport declared in the domain layer and does not know what a leak is.