mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
fix(openai): strip safety review details from client-visible failed events
This commit is contained in:
1 parent
f53641e53c
commit
a622eca4f9
2 files changed
+53
No files matched your search
@@ -0,0 +1,40 @@
|
||||
//go:build unit
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func TestSanitizeOpenAIResponseFailedEventStripsSafetyReviewDetails(t *testing.T) {
|
||||
payload := []byte(`{"type":"response.failed","sequence_number":3,"response":{"id":"resp_1","status":"failed","error":{"type":"invalid_request_error","code":"misalignment_policy_violation","message":"This request was blocked by our safety systems.","misalignment":{"detailed_explanation":"another user's private project summary","steer":{"message":"another user's private instruction"}}}}}`)
|
||||
|
||||
out, changed := sanitizeOpenAIResponseFailedEventForClient(payload, "response.failed", false)
|
||||
require.True(t, changed)
|
||||
require.False(t, gjson.GetBytes(out, "response.error.misalignment").Exists())
|
||||
require.Equal(t, "misalignment_policy_violation", gjson.GetBytes(out, "response.error.code").String())
|
||||
require.Equal(t, "This request was blocked by our safety systems.", gjson.GetBytes(out, "response.error.message").String())
|
||||
require.NotContains(t, string(out), "another user's private")
|
||||
}
|
||||
|
||||
func TestSanitizeOpenAIErrorEventStripsSafetyReviewDetails(t *testing.T) {
|
||||
payload := []byte(`{"type":"error","sequence_number":3,"error":{"type":"invalid_request_error","code":"misalignment_policy_violation","message":"This request was blocked by our safety systems.","misalignment":{"detailed_explanation":"another user's private project summary"}}}`)
|
||||
|
||||
out, changed := sanitizeOpenAIResponseFailedEventForClient(payload, "error", false)
|
||||
require.True(t, changed)
|
||||
require.False(t, gjson.GetBytes(out, "error.misalignment").Exists())
|
||||
require.Equal(t, "misalignment_policy_violation", gjson.GetBytes(out, "error.code").String())
|
||||
require.Equal(t, "This request was blocked by our safety systems.", gjson.GetBytes(out, "error.message").String())
|
||||
require.NotContains(t, string(out), "another user's private")
|
||||
}
|
||||
|
||||
func TestSanitizeOpenAIFailedEventWithoutReviewDetailsIsUnchanged(t *testing.T) {
|
||||
payload := []byte(`{"type":"response.failed","response":{"status":"failed","error":{"type":"server_error","code":"upstream_error","message":"Upstream response failed"}}}`)
|
||||
|
||||
out, changed := sanitizeOpenAIResponseFailedEventForClient(payload, "response.failed", false)
|
||||
require.False(t, changed)
|
||||
require.JSONEq(t, string(payload), string(out))
|
||||
}
|
||||
@@ -2022,6 +2022,19 @@ func sanitizeOpenAIResponseFailedEventForClient(payload []byte, eventType string
|
||||
if rewritten, changed := sanitizeOpenAICapacityShedErrorCodeForClient(updated); changed {
|
||||
updated = rewritten
|
||||
}
|
||||
// A shared OAuth account can return a safety-review explanation that
|
||||
// belongs to another downstream user's session. Keep the stable status,
|
||||
// code and message, but never forward the review body.
|
||||
for _, path := range []string{"response.error.misalignment", "error.misalignment"} {
|
||||
if !gjson.GetBytes(updated, path).Exists() {
|
||||
continue
|
||||
}
|
||||
next, err := sjson.DeleteBytes(updated, path)
|
||||
if err != nil {
|
||||
return payload, false
|
||||
}
|
||||
updated = next
|
||||
}
|
||||
if !isFailedEvent {
|
||||
return updated, !bytes.Equal(updated, payload)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user