-
Notifications
You must be signed in to change notification settings - Fork 117
fix(dictation): redact API keys from streaming transcriber errors #710
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
euxaristia
wants to merge
11
commits into
Gitlawb:main
Choose a base branch
from
euxaristia:fix/streaming-dictation-key-redaction
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
076622a
fix(dictation): redact API keys from streaming transcriber errors
euxaristia c0dceca
fixup(dictation): address review: redact session/write errors, move r…
euxaristia 12248b6
fixup(dictation): sync Deepgram redaction test on CloseStream before …
euxaristia 04418ab
fix(dictation): preserve context.Canceled through streaming error red…
euxaristia 7ae37e7
fix(dictation): close remaining cancellation and redaction gaps
euxaristia 3eda0ad
test(dictation): cover dial and startup cancel identity
euxaristia 9826b8f
fix(dictation): prefer cancel over a racing OpenAI realtime error
euxaristia 56c6a73
fix(dictation): prevent auto-submit on cancelled realtime race
euxaristia 84f2111
test(dictation): exercise writeErrCh cancellation branch in realtime …
euxaristia cffc46d
fix(dictation): add custom key header redaction and single-pass redac…
euxaristia cb9ba5a
fix(dictation): track dictation sessionID to drop stale completions a…
euxaristia 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| package dictation | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/coder/websocket" | ||
| ) | ||
|
|
||
| func TestDeepgramStreamTranscribeErrorRedaction(t *testing.T) { | ||
| url := wsTestServer(t, func(ctx context.Context, c *websocket.Conn) { | ||
| // Drain the client's audio frames, then wait for CloseStream so the | ||
| // client has flushed its writes before we reject the connection. Closing | ||
| // immediately (before CloseStream) risks the client failing on a write | ||
| // error and never observing this API-key-bearing close reason. | ||
| for { | ||
| typ, data, err := c.Read(ctx) | ||
| if err != nil { | ||
| return | ||
| } | ||
| if typ == websocket.MessageText && strings.Contains(string(data), "CloseStream") { | ||
| break | ||
| } | ||
| } | ||
| c.Close(websocket.StatusPolicyViolation, "invalid key sk-test-key") | ||
| }) | ||
|
|
||
| tr, err := NewDeepgramTranscriber(DeepgramConfig{APIKey: "sk-test-key", BaseURL: url}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| chunks := make(chan []byte, 1) | ||
| chunks <- make([]byte, 320) | ||
| close(chunks) | ||
| _, ferr := tr.StreamTranscribe(context.Background(), chunks, func(string, bool) {}) | ||
| if ferr == nil { | ||
| t.Fatal("expected error") | ||
| } | ||
| if strings.Contains(ferr.Error(), "sk-test-key") { | ||
| t.Errorf("API key leaked: %v", ferr) | ||
| } | ||
| } | ||
|
|
||
| func TestDeepgramStreamTranscribeCancelKeepsSentinel(t *testing.T) { | ||
| firstFrame := make(chan struct{}) | ||
| url := wsTestServer(t, func(ctx context.Context, c *websocket.Conn) { | ||
| // Hold the connection open, never answering, so the client blocks in | ||
| // Read until its context is cancelled (the Esc-abort path). | ||
| var once sync.Once | ||
| for { | ||
| if _, _, err := c.Read(ctx); err != nil { | ||
| return | ||
| } | ||
| once.Do(func() { close(firstFrame) }) | ||
| } | ||
| }) | ||
|
|
||
| tr, err := NewDeepgramTranscriber(DeepgramConfig{APIKey: "sk-test-key", BaseURL: url}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| chunks := make(chan []byte, 1) | ||
| defer close(chunks) | ||
| chunks <- make([]byte, 320) | ||
| // The channel stays open: the session is live when the user aborts. | ||
| errCh := make(chan error, 1) | ||
| go func() { | ||
| _, ferr := tr.StreamTranscribe(ctx, chunks, nil) | ||
| errCh <- ferr | ||
| }() | ||
|
|
||
| select { | ||
| case <-firstFrame: | ||
| cancel() | ||
| ferr := <-errCh | ||
| if !errors.Is(ferr, context.Canceled) { | ||
| t.Fatalf("cancelled stream error lost the context.Canceled sentinel: %v", ferr) | ||
| } | ||
| case ferr := <-errCh: | ||
| t.Fatalf("StreamTranscribe failed early instead of blocking: %v", ferr) | ||
| } | ||
| } | ||
|
|
||
| // Esc can cancel while the WebSocket dial is still pending. The dial error is | ||
| // redacted with %s, so we must return ctx.Err() rather than a flat string. | ||
| func TestDeepgramStreamTranscribeDialCancelKeepsSentinel(t *testing.T) { | ||
| tr, err := NewDeepgramTranscriber(DeepgramConfig{ | ||
| APIKey: "sk-test-key", | ||
| BaseURL: "ws://127.0.0.1:1", // never reached; ctx is already cancelled | ||
| }) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
| chunks := make(chan []byte) | ||
| defer close(chunks) | ||
|
|
||
| _, ferr := tr.StreamTranscribe(ctx, chunks, nil) | ||
| if !errors.Is(ferr, context.Canceled) { | ||
| t.Fatalf("dial-cancel lost the context.Canceled sentinel: %v", ferr) | ||
| } | ||
| } | ||
|
|
||
| // Esc right after accept can race the first write or the first Read; both | ||
| // setup/write redaction sites must still yield context.Canceled. | ||
| func TestDeepgramStreamTranscribeStartupCancelKeepsSentinel(t *testing.T) { | ||
| accepted := make(chan struct{}) | ||
| url := wsTestServer(t, func(ctx context.Context, c *websocket.Conn) { | ||
| close(accepted) | ||
| for { | ||
| if _, _, err := c.Read(ctx); err != nil { | ||
| return | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| tr, err := NewDeepgramTranscriber(DeepgramConfig{APIKey: "sk-test-key", BaseURL: url}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
| chunks := make(chan []byte, 1) | ||
| defer close(chunks) | ||
| // No frames yet: the writer blocks on chunks or the reader blocks on Read. | ||
| errCh := make(chan error, 1) | ||
| go func() { | ||
| _, ferr := tr.StreamTranscribe(ctx, chunks, nil) | ||
| errCh <- ferr | ||
| }() | ||
|
|
||
| select { | ||
| case <-accepted: | ||
| cancel() | ||
| ferr := <-errCh | ||
| if !errors.Is(ferr, context.Canceled) { | ||
| t.Fatalf("startup-cancel lost the context.Canceled sentinel: %v", ferr) | ||
| } | ||
| case ferr := <-errCh: | ||
| t.Fatalf("StreamTranscribe failed before accept: %v", ferr) | ||
| } | ||
| } | ||
|
|
||
| func TestDeepgramCustomHeaderErrorRedaction(t *testing.T) { | ||
| url := wsTestServer(t, func(ctx context.Context, c *websocket.Conn) { | ||
| for { | ||
| typ, data, err := c.Read(ctx) | ||
| if err != nil { | ||
| return | ||
| } | ||
| if typ == websocket.MessageText && strings.Contains(string(data), "CloseStream") { | ||
| break | ||
| } | ||
| } | ||
| c.Close(websocket.StatusPolicyViolation, "X-Api-Key: sk-custom-secret-key-1234567890 failed") | ||
| }) | ||
|
|
||
| tr, err := NewDeepgramTranscriber(DeepgramConfig{APIKey: "sk-custom-secret-key-1234567890", BaseURL: url}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| chunks := make(chan []byte, 1) | ||
| chunks <- make([]byte, 320) | ||
| close(chunks) | ||
| _, ferr := tr.StreamTranscribe(context.Background(), chunks, func(string, bool) {}) | ||
| if ferr == nil { | ||
| t.Fatal("expected error") | ||
| } | ||
| if strings.Contains(ferr.Error(), "sk-custom-secret-key-1234567890") { | ||
| t.Errorf("Custom header API key leaked: %v", ferr) | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func TestDeepgramSinglePassRedaction(t *testing.T) { | ||
| url := wsTestServer(t, func(ctx context.Context, c *websocket.Conn) { | ||
| for { | ||
| typ, data, err := c.Read(ctx) | ||
| if err != nil { | ||
| return | ||
| } | ||
| if typ == websocket.MessageText && strings.Contains(string(data), "CloseStream") { | ||
| break | ||
| } | ||
| } | ||
| c.Close(websocket.StatusPolicyViolation, "error with sk-key1234567890abcdef1234") | ||
| }) | ||
|
|
||
| tr, err := NewDeepgramTranscriber(DeepgramConfig{APIKey: "sk-key1234567890abcdef1234", BaseURL: url}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| chunks := make(chan []byte, 1) | ||
| chunks <- make([]byte, 320) | ||
| close(chunks) | ||
| _, ferr := tr.StreamTranscribe(context.Background(), chunks, func(string, bool) {}) | ||
| if ferr == nil { | ||
| t.Fatal("expected error") | ||
| } | ||
| if strings.Contains(ferr.Error(), "[REDACTED:[REDACTED") { | ||
| t.Errorf("nested redaction marker created: %v", ferr) | ||
| } | ||
| } | ||
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.