mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
330 lines
9.4 KiB
Go
330 lines
9.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/pkg/googleapi"
|
|
"github.com/Wei-Shaw/sub2api/internal/securityaudit"
|
|
"github.com/Wei-Shaw/sub2api/internal/service"
|
|
coderws "github.com/coder/websocket"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func securityAuditStreamCommitted(c *gin.Context) bool {
|
|
if service.StopOpenAICompactSSEKeepaliveCommitted(c) {
|
|
return true
|
|
}
|
|
return c != nil && c.Writer != nil && c.Writer.Written()
|
|
}
|
|
|
|
func writeGenericSecurityAuditSSEError(c *gin.Context, status int, errType, code, message string) {
|
|
if c == nil || c.Writer == nil {
|
|
return
|
|
}
|
|
service.MarkOpsStreamError(c, errType, message, status)
|
|
if inboundIsResponses(c) {
|
|
if writeResponsesFailedSSE(c, errType, code, message) {
|
|
return
|
|
}
|
|
}
|
|
flusher, ok := c.Writer.(http.Flusher)
|
|
if !ok {
|
|
return
|
|
}
|
|
errObj := gin.H{"type": errType, "message": message}
|
|
if code != "" {
|
|
errObj["code"] = code
|
|
}
|
|
payload, err := json.Marshal(gin.H{"error": errObj})
|
|
if err != nil {
|
|
payload = []byte(`{"error":{"type":"content_policy_violation","message":"Request blocked by content policy"}}`)
|
|
}
|
|
_, _ = fmt.Fprintf(c.Writer, "event: error\ndata: %s\n\n", payload)
|
|
flusher.Flush()
|
|
}
|
|
|
|
func (h *OpenAIGatewayHandler) openAISecurityAuditError(c *gin.Context, decision *securityaudit.Decision) {
|
|
if decision == nil {
|
|
return
|
|
}
|
|
status := securityAuditStatus(decision)
|
|
code := securityAuditErrorCode(decision)
|
|
msg := securityAuditMessage(decision)
|
|
if securityAuditStreamCommitted(c) {
|
|
errType := code
|
|
if decision.Legacy == nil || !decision.Legacy.Blocked {
|
|
errType = "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
}
|
|
if h != nil {
|
|
h.handleStreamingAwareErrorWithCode(c, status, errType, code, msg, true, false)
|
|
return
|
|
}
|
|
writeGenericSecurityAuditSSEError(c, status, errType, code, msg)
|
|
return
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
h.errorResponse(c, status, code, msg)
|
|
return
|
|
}
|
|
errType := "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
c.JSON(status, gin.H{"error": gin.H{
|
|
"type": errType, "code": code, "message": msg,
|
|
}})
|
|
}
|
|
|
|
func (h *GatewayHandler) openAISecurityAuditError(c *gin.Context, decision *securityaudit.Decision) {
|
|
if decision == nil {
|
|
return
|
|
}
|
|
status := securityAuditStatus(decision)
|
|
code := securityAuditErrorCode(decision)
|
|
msg := securityAuditMessage(decision)
|
|
if securityAuditStreamCommitted(c) {
|
|
errType := code
|
|
if decision.Legacy == nil || !decision.Legacy.Blocked {
|
|
errType = "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
}
|
|
if h != nil {
|
|
h.handleStreamingAwareErrorWithCode(c, status, errType, code, msg, true)
|
|
return
|
|
}
|
|
writeGenericSecurityAuditSSEError(c, status, errType, code, msg)
|
|
return
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
h.chatCompletionsErrorResponse(c, status, code, msg)
|
|
return
|
|
}
|
|
errType := "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
c.JSON(status, gin.H{"error": gin.H{
|
|
"type": errType, "code": code, "message": msg,
|
|
}})
|
|
}
|
|
|
|
func (h *GatewayHandler) responsesSecurityAuditError(c *gin.Context, decision *securityaudit.Decision) {
|
|
if decision == nil {
|
|
return
|
|
}
|
|
status := securityAuditStatus(decision)
|
|
code := securityAuditErrorCode(decision)
|
|
msg := securityAuditMessage(decision)
|
|
if securityAuditStreamCommitted(c) {
|
|
errType := code
|
|
if decision.Legacy == nil || !decision.Legacy.Blocked {
|
|
errType = "api_error"
|
|
}
|
|
service.MarkOpsStreamError(c, errType, msg, status)
|
|
if !writeResponsesFailedSSE(c, errType, code, msg) {
|
|
writeGenericSecurityAuditSSEError(c, status, errType, code, msg)
|
|
}
|
|
return
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
h.responsesErrorResponse(c, status, code, msg)
|
|
return
|
|
}
|
|
c.JSON(status, gin.H{"error": gin.H{
|
|
"type": "api_error", "code": code, "message": msg,
|
|
}})
|
|
}
|
|
|
|
func (h *GatewayHandler) anthropicSecurityAuditError(c *gin.Context, decision *securityaudit.Decision) {
|
|
if decision == nil {
|
|
return
|
|
}
|
|
status := securityAuditStatus(decision)
|
|
code := securityAuditErrorCode(decision)
|
|
msg := securityAuditMessage(decision)
|
|
if securityAuditStreamCommitted(c) {
|
|
errType := code
|
|
if decision.Legacy == nil || !decision.Legacy.Blocked {
|
|
errType = "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
}
|
|
if h != nil {
|
|
h.handleStreamingAwareErrorWithCode(c, status, errType, code, msg, true)
|
|
return
|
|
}
|
|
writeGenericSecurityAuditSSEError(c, status, errType, code, msg)
|
|
return
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
h.errorResponse(c, status, code, msg)
|
|
return
|
|
}
|
|
errType := "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
c.JSON(status, gin.H{"type": "error", "error": gin.H{
|
|
"type": errType, "code": code, "message": msg,
|
|
}})
|
|
}
|
|
|
|
func (h *OpenAIGatewayHandler) anthropicSecurityAuditError(c *gin.Context, decision *securityaudit.Decision) {
|
|
if decision == nil {
|
|
return
|
|
}
|
|
status := securityAuditStatus(decision)
|
|
code := securityAuditErrorCode(decision)
|
|
msg := securityAuditMessage(decision)
|
|
if securityAuditStreamCommitted(c) {
|
|
errType := code
|
|
if decision.Legacy == nil || !decision.Legacy.Blocked {
|
|
errType = "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
}
|
|
h.anthropicStreamingAwareError(c, status, errType, msg, true)
|
|
return
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
h.anthropicErrorResponse(c, status, code, msg)
|
|
return
|
|
}
|
|
errType := "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
c.JSON(status, gin.H{"type": "error", "error": gin.H{
|
|
"type": errType, "code": code, "message": msg,
|
|
}})
|
|
}
|
|
|
|
func googleSecurityAuditError(c *gin.Context, decision *securityaudit.Decision) {
|
|
if decision == nil {
|
|
return
|
|
}
|
|
status := securityAuditStatus(decision)
|
|
code := securityAuditErrorCode(decision)
|
|
msg := securityAuditMessage(decision)
|
|
if securityAuditStreamCommitted(c) {
|
|
service.MarkOpsStreamError(c, code, msg, status)
|
|
if flusher, ok := c.Writer.(http.Flusher); ok {
|
|
googleStatus := googleapi.HTTPStatusToGoogleStatus(status)
|
|
payload, err := json.Marshal(gin.H{
|
|
"error": gin.H{
|
|
"code": status,
|
|
"message": msg,
|
|
"status": googleStatus,
|
|
},
|
|
})
|
|
if err == nil {
|
|
_, _ = fmt.Fprintf(c.Writer, "data: %s\n\n", payload)
|
|
flusher.Flush()
|
|
}
|
|
}
|
|
return
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
googleErrorWithType(c, status, code, "", msg)
|
|
return
|
|
}
|
|
errType := "api_error"
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
errType = "permission_error"
|
|
}
|
|
service.SetOpsLocalErrorType(c, errType, code)
|
|
googleStatus := googleapi.HTTPStatusToGoogleStatus(status)
|
|
if status == http.StatusServiceUnavailable {
|
|
googleStatus = "UNAVAILABLE"
|
|
}
|
|
requestID := ""
|
|
if c != nil && c.Request != nil {
|
|
requestID = contentModerationRequestID(c.Request.Context())
|
|
}
|
|
c.JSON(status, gin.H{"error": gin.H{
|
|
"code": status, "message": msg, "status": googleStatus,
|
|
"details": []gin.H{{
|
|
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
|
|
"reason": code,
|
|
"domain": "sub2api.securityaudit",
|
|
"metadata": gin.H{"request_id": requestID},
|
|
}},
|
|
}})
|
|
}
|
|
|
|
func writeSecurityAuditWSError(ctx context.Context, conn *coderws.Conn, decision *securityaudit.Decision) {
|
|
if conn == nil || decision == nil {
|
|
return
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
legacy := decision.Legacy
|
|
writeContentModerationWSError(ctx, conn, (legacyContentModerationDecision{legacy}).toService())
|
|
return
|
|
}
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
payload, err := json.Marshal(gin.H{
|
|
"event_id": "evt_prompt_guard_rejected", "type": "error",
|
|
"error": gin.H{"type": "invalid_request_error", "code": securityAuditErrorCode(decision), "message": securityAuditMessage(decision)},
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
writeCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
_ = conn.Write(writeCtx, coderws.MessageText, payload)
|
|
}
|
|
|
|
type legacyContentModerationDecision struct{ value *securityaudit.LegacyDecision }
|
|
|
|
func (d legacyContentModerationDecision) toService() *service.ContentModerationDecision {
|
|
if d.value == nil {
|
|
return nil
|
|
}
|
|
return &service.ContentModerationDecision{Allowed: d.value.Allowed, Blocked: d.value.Blocked, Flagged: d.value.Flagged, Message: d.value.Message, StatusCode: d.value.StatusCode, Action: d.value.Action}
|
|
}
|
|
|
|
func securityAuditWSCloseStatus(decision *securityaudit.Decision) coderws.StatusCode {
|
|
if decision == nil {
|
|
return coderws.StatusInternalError
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
return coderws.StatusPolicyViolation
|
|
}
|
|
if decision.Kind == securityaudit.DecisionBlock {
|
|
return coderws.StatusCode(4403)
|
|
}
|
|
return coderws.StatusTryAgainLater
|
|
}
|
|
|
|
func securityAuditWSCloseReason(decision *securityaudit.Decision) string {
|
|
if decision == nil {
|
|
return securityaudit.ErrorCodeUnavailable
|
|
}
|
|
if decision.Legacy != nil && decision.Legacy.Blocked {
|
|
message := strings.TrimSpace(decision.Legacy.Message)
|
|
if message != "" {
|
|
return message
|
|
}
|
|
return "content_policy_violation"
|
|
}
|
|
code := securityAuditErrorCode(decision)
|
|
if code == "" {
|
|
return securityaudit.ErrorCodeUnavailable
|
|
}
|
|
return code
|
|
}
|