-
Notifications
You must be signed in to change notification settings - Fork 127
fix(dictation): redact API keys from streaming transcriber errors #852
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
28
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
28 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 2814b35
fix(dictation): initialize non-zero session ID and filter stale sessi…
euxaristia fdbf8ba
fix(dictation): redact API keys from streaming transcriber errors
euxaristia c47f0a4
fixup(dictation): address review: redact session/write errors, move r…
euxaristia f241312
fixup(dictation): sync Deepgram redaction test on CloseStream before …
euxaristia d039c73
fix(dictation): preserve context.Canceled through streaming error red…
euxaristia 7453dab
fix(dictation): close remaining cancellation and redaction gaps
euxaristia 89ad2b3
test(dictation): cover dial and startup cancel identity
euxaristia b4754d6
fix(dictation): prefer cancel over a racing OpenAI realtime error
euxaristia 676438f
fix(dictation): prevent auto-submit on cancelled realtime race
euxaristia 136d596
test(dictation): exercise writeErrCh cancellation branch in realtime …
euxaristia 0853700
fix(dictation): add custom key header redaction and single-pass redac…
euxaristia 9500b57
fix(dictation): track dictation sessionID to drop stale completions a…
euxaristia fee2e2e
fix(dictation): initialize non-zero session ID and filter stale sessi…
euxaristia c2d3b66
Merge branch 'fix/streaming-dictation-key-redaction' of https://githu…
euxaristia c19ceef
fix(dictation): drop providerio widen; pin session and redaction tests
euxaristia 03fa588
fix(dictation): ignore stale batch startup by session ID
euxaristia f34ad75
fix(dictation): exit writers on cancel and pin dial/write redaction p…
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,278 @@ | ||
| package dictation | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/coder/websocket" | ||
| ) | ||
|
|
||
| // Non-cancellation dial failure: handshake returns 101 with an Upgrade header | ||
| // equal to the API key so the dial error embeds the key and redaction must strip it. | ||
| func TestDeepgramDialFailureRedactsKey(t *testing.T) { | ||
| const key = "sk-test-dial-key-abcdef" | ||
| baseURL := dialFailServerWithUpgrade(t, key) | ||
|
|
||
| tr, err := NewDeepgramTranscriber(DeepgramConfig{APIKey: key, BaseURL: baseURL}) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| chunks := make(chan []byte) | ||
| close(chunks) | ||
| _, ferr := tr.StreamTranscribe(context.Background(), chunks, nil) | ||
| if ferr == nil { | ||
| t.Fatal("expected dial failure") | ||
| } | ||
| if errors.Is(ferr, context.Canceled) { | ||
| t.Fatalf("dial failure should not be context.Canceled: %v", ferr) | ||
| } | ||
| if strings.Contains(ferr.Error(), key) { | ||
| t.Errorf("API key leaked from dial failure: %v", ferr) | ||
| } | ||
| if !strings.Contains(ferr.Error(), "connecting to Deepgram") { | ||
| t.Errorf("expected dial-path error prefix, got: %v", ferr) | ||
| } | ||
| } | ||
|
|
||
| // dialFailServerWithUpgrade returns a ws URL whose handshake responds 101 with | ||
| // Upgrade set to upgradeValue (not "websocket"), so websocket.Dial fails with | ||
| // that value in the error string. Hijacks and closes immediately to avoid the | ||
| // library's 3s body-drain timeout on failed dials. | ||
| func dialFailServerWithUpgrade(t *testing.T, upgradeValue string) string { | ||
| t.Helper() | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| hj, ok := w.(http.Hijacker) | ||
| if !ok { | ||
| http.Error(w, "hijack not supported", http.StatusInternalServerError) | ||
| return | ||
| } | ||
| conn, bufrw, err := hj.Hijack() | ||
| if err != nil { | ||
| return | ||
| } | ||
| defer conn.Close() | ||
| _, _ = bufrw.WriteString("HTTP/1.1 101 Switching Protocols\r\n") | ||
| _, _ = bufrw.WriteString("Connection: Upgrade\r\n") | ||
| _, _ = bufrw.WriteString("Upgrade: " + upgradeValue + "\r\n\r\n") | ||
| _ = bufrw.Flush() | ||
| })) | ||
| t.Cleanup(srv.Close) | ||
| return "ws" + strings.TrimPrefix(srv.URL, "http") | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| 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) | ||
| } | ||
| } | ||
|
|
||
| // TestDeepgramStreamErrorRedactsHeaderShapedKey exercises the same stream-error | ||
| // redaction path as TestDeepgramStreamTranscribeErrorRedaction, with a close | ||
| // reason that embeds the key next to an X-Api-Key-shaped header label. | ||
| func TestDeepgramStreamErrorRedactsHeaderShapedKey(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) | ||
| } | ||
| } | ||
|
|
||
| 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") | ||
| } | ||
| got := ferr.Error() | ||
| if strings.Contains(got, "sk-key1234567890abcdef1234") { | ||
| t.Fatalf("API key leaked into stream error: %q", got) | ||
| } | ||
| // One key occurrence must produce exactly one redaction marker (no double wrap). | ||
| if strings.Count(got, "[REDACTED]") != 1 { | ||
| t.Fatalf("redaction markers in %q: want exactly 1 [REDACTED]", got) | ||
| } | ||
| } | ||
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.