-
Notifications
You must be signed in to change notification settings - Fork 2
feat(ai): extend the prompt injection defense to streamed suggestions #668
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { isLeaked, SUGGESTION_FALLBACK_MESSAGE } from './leakDetector'; | ||
|
|
||
| /** | ||
| * What the guard allows the transport to send downstream | ||
| */ | ||
| export interface GuardVerdict { | ||
| /** | ||
| * Text safe to forward now, which is what was fed in minus the holdback | ||
| */ | ||
| emit: string; | ||
|
|
||
| /** | ||
| * Whether a leak was detected. Once true it stays true and no further model | ||
| * text is forwarded. | ||
| */ | ||
| leaked: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Port implemented by the domain and consumed by the transport, so the | ||
| * provider adapter never needs to know what a leak is | ||
| */ | ||
| export interface StreamGuard { | ||
| /** | ||
| * Inspect the next piece of model output | ||
| * | ||
| * @param chunk - text delta produced by the model | ||
| * @returns {GuardVerdict} text safe to forward now | ||
| */ | ||
| push(chunk: string): GuardVerdict; | ||
|
|
||
| /** | ||
| * Release whatever is still withheld, at end of stream | ||
| * | ||
| * @returns {GuardVerdict} remaining text safe to forward | ||
| */ | ||
| flush(): GuardVerdict; | ||
| } | ||
|
|
||
| /** | ||
| * Streaming counterpart of {@link isLeaked}. | ||
| * | ||
| * The nonce can arrive split across two deltas, so scanning each delta alone | ||
| * would never see it whole. The guard therefore keeps a *holdback*: the last | ||
| * `nonce.length - 1` characters fed in so far, kept unsent. Every new delta is | ||
| * scanned together with the holdback, and only the part that can no longer | ||
| * begin the nonce is released. | ||
| * | ||
| * That length is the exact minimum. An occurrence of the nonce spans | ||
| * `nonce.length` characters, so holding one less guarantees it falls inside a | ||
| * single scanned window and never reaches the client. | ||
| * | ||
| * On detection the rest of the answer is replaced by | ||
| * {@link SUGGESTION_FALLBACK_MESSAGE}, emitted once. The prefix already sent | ||
| * cannot be retracted, which is acceptable: the nonce is what triggered | ||
| * detection and is still held back when it fires. | ||
| * | ||
| * @param nonce - per-request nonce used in the prompt markers | ||
| * @returns {StreamGuard} guard for a single stream, not reusable | ||
| */ | ||
| export function createLeakGuard(nonce: string): StreamGuard { | ||
| const holdback = Math.max(nonce.length - 1, 0); | ||
|
|
||
| let withheld = ''; | ||
| let leaked = false; | ||
|
|
||
| /** | ||
| * Mark the stream as leaked and produce the one verdict that still carries | ||
| * text: the fallback message | ||
| * | ||
| * @returns {GuardVerdict} verdict replacing the rest of the answer | ||
| */ | ||
| const reject = (): GuardVerdict => { | ||
| leaked = true; | ||
| withheld = ''; | ||
|
|
||
| return { | ||
| emit: SUGGESTION_FALLBACK_MESSAGE, | ||
| leaked: true, | ||
| }; | ||
| }; | ||
|
|
||
| return { | ||
| push(chunk: string): GuardVerdict { | ||
| if (leaked) { | ||
| return { | ||
| emit: '', | ||
| leaked: true, | ||
| }; | ||
| } | ||
|
|
||
| const window = withheld + chunk; | ||
|
|
||
| if (isLeaked(window, nonce)) { | ||
| return reject(); | ||
| } | ||
|
|
||
| const sendable = Math.max(window.length - holdback, 0); | ||
|
|
||
| withheld = window.slice(sendable); | ||
|
|
||
| return { | ||
| emit: window.slice(0, sendable), | ||
| leaked: false, | ||
| }; | ||
| }, | ||
|
|
||
| flush(): GuardVerdict { | ||
| if (leaked) { | ||
| return { | ||
| emit: '', | ||
| leaked: true, | ||
| }; | ||
| } | ||
|
|
||
| const pending = withheld; | ||
|
|
||
| withheld = ''; | ||
|
|
||
| if (isLeaked(pending, nonce)) { | ||
| return reject(); | ||
| } | ||
|
|
||
| return { | ||
| emit: pending, | ||
| leaked: false, | ||
| }; | ||
| }, | ||
| }; | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.