diff --git a/backend/internal/service/openai_gateway_failed_event_sanitize_test.go b/backend/internal/service/openai_gateway_failed_event_sanitize_test.go new file mode 100644 index 0000000000..31f8fb55a1 --- /dev/null +++ b/backend/internal/service/openai_gateway_failed_event_sanitize_test.go @@ -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)) +} diff --git a/backend/internal/service/openai_gateway_response_handling.go b/backend/internal/service/openai_gateway_response_handling.go index 72a0d4df69..3555dd2887 100644 --- a/backend/internal/service/openai_gateway_response_handling.go +++ b/backend/internal/service/openai_gateway_response_handling.go @@ -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) }