package securityaudit import ( "crypto/sha256" "encoding/hex" "encoding/json" "errors" "regexp" "sort" "strings" "unicode/utf8" ) var ( ErrNoPromptText = errors.New("prompt audit request contains no user text") bearerPattern = regexp.MustCompile(`(?i)\bBearer\s+[A-Za-z0-9._~+\-/]+=*`) apiKeyPattern = regexp.MustCompile(`(?i)\b(sk|rk|pk|api[_-]?key|token|secret|password)[-_:=\s]+[A-Za-z0-9._~+\-/]{8,}`) canaryPattern = regexp.MustCompile(`(?i)([A-Z]+_CANARY_)[A-Za-z0-9_-]+`) emailPattern = regexp.MustCompile(`(?i)\b[A-Z0-9._%+\-]+@[A-Z0-9.\-]+\.[A-Z]{2,}\b`) phonePattern = regexp.MustCompile(`(?:\+?\d[\d\s().-]{8,}\d)`) ) func ExtractPromptSnapshot(req Request) (PromptSnapshot, error) { var document any if err := json.Unmarshal(req.Body, &document); err != nil { return PromptSnapshot{}, errors.New("prompt audit request JSON is invalid") } segments := extractProtocolSegments(req.Protocol, document) segments = normalizeSegmentsLatestFirst(segments) if len(segments) == 0 { return PromptSnapshot{}, ErrNoPromptText } scanText := strings.Join(segments, "\n\n") digest := sha256.Sum256([]byte(scanText)) stage := strings.TrimSpace(req.Stage) if stage == "" { stage = "http" } return PromptSnapshot{ RequestID: req.RequestID, UserID: req.UserID, UsernameSnapshot: req.Username, UserEmailSnapshot: req.UserEmail, APIKeyID: req.APIKeyID, APIKeyNameSnapshot: req.APIKeyName, GroupID: cloneInt64Ptr(req.GroupID), GroupName: req.GroupName, Provider: req.Provider, Endpoint: req.Endpoint, Protocol: req.Protocol, Model: req.Model, PromptHash: hex.EncodeToString(digest[:]), RedactedPreview: BuildPromptPreview(scanText, DefaultPromptPreviewMaxRunes), PromptLength: utf8.RuneCountInString(scanText), MessageCount: len(segments), Stage: stage, ScanText: scanText, }, nil } // DefaultPromptPreviewMaxRunes caps how much sanitized prompt text may be // considered before BuildPromptPreview withholds the majority for storage/UI. const DefaultPromptPreviewMaxRunes = 96 func extractProtocolSegments(protocol string, document any) []string { root, _ := document.(map[string]any) protocol = strings.ToLower(strings.TrimSpace(protocol)) switch protocol { case "openai_chat_completions", "openai_chat", "chat_completions": return extractChatLikeSegments(root) case "anthropic_messages", "claude_messages", "messages": return append(extractAnthropicSystem(root["system"]), extractMessages(root["messages"], clientInstructionRoles...)...) case "gemini", "gemini_generate_content": return extractGeminiRoot(root) case "openai_responses", "responses", "responses_websocket": if frameType := stringValue(root["type"]); frameType != "" || protocol == "responses_websocket" { if frameType != "response.create" { return nil } if input, exists := root["input"]; exists && input != nil { return append(extractInstructions(root["instructions"]), extractResponses(input)...) } if response, ok := root["response"].(map[string]any); ok { return append(extractInstructions(response["instructions"]), extractResponses(response["input"])...) } return extractInstructions(root["instructions"]) } return append(extractInstructions(root["instructions"]), extractResponses(root["input"])...) case "openai_images", "grok_media", "media", "images": return extractMediaPrompts(root) default: if segments := extractChatLikeSegments(root); len(segments) > 0 { return segments } if responses := append(extractInstructions(root["instructions"]), extractResponses(root["input"])...); len(responses) > 0 { return responses } if gemini := extractGeminiRoot(root); len(gemini) > 0 { return gemini } return extractMediaPrompts(root) } } var clientInstructionRoles = []string{"user", "system", "developer"} func extractChatLikeSegments(root map[string]any) []string { if root == nil { return nil } return extractMessages(root["messages"], clientInstructionRoles...) } func extractMessages(value any, wantedRoles ...string) []string { items, ok := value.([]any) if !ok { return nil } wanted := make(map[string]struct{}, len(wantedRoles)) for _, role := range wantedRoles { wanted[strings.ToLower(strings.TrimSpace(role))] = struct{}{} } result := make([]string, 0, len(items)) for _, item := range items { message, ok := item.(map[string]any) if !ok { continue } role := strings.ToLower(stringValue(message["role"])) if _, match := wanted[role]; !match { continue } texts := contentTexts(message["content"]) if len(texts) > 0 { result = append(result, strings.Join(texts, "\n")) } } return result } func extractInstructions(value any) []string { switch typed := value.(type) { case string: if text := strings.TrimSpace(typed); text != "" { return []string{text} } case []any: return contentTexts(typed) case map[string]any: return contentTexts(typed) } return nil } func extractAnthropicSystem(value any) []string { switch typed := value.(type) { case string: if text := strings.TrimSpace(typed); text != "" { return []string{text} } case []any: return contentTexts(typed) case map[string]any: return contentTexts(typed) } return nil } func extractResponses(value any) []string { switch typed := value.(type) { case string: return []string{typed} case []any: result := make([]string, 0, len(typed)) for _, item := range typed { switch entry := item.(type) { case string: result = append(result, entry) case map[string]any: role := strings.ToLower(stringValue(entry["role"])) if role != "" && role != "user" && role != "system" && role != "developer" { continue } if content, exists := entry["content"]; exists { if texts := contentTexts(content); len(texts) > 0 { result = append(result, strings.Join(texts, "\n")) } } else if text := stringValue(entry["text"]); text != "" { result = append(result, text) } } } return result case map[string]any: role := strings.ToLower(stringValue(typed["role"])) if role != "" && role != "user" && role != "system" && role != "developer" { return nil } return contentTexts(typed["content"]) default: return nil } } func extractGemini(value any) []string { var contents []any switch typed := value.(type) { case []any: contents = typed case map[string]any: contents = []any{typed} default: return nil } result := make([]string, 0, len(contents)) for _, item := range contents { content, ok := item.(map[string]any) if !ok { continue } role := strings.ToLower(stringValue(content["role"])) if role != "" && role != "user" { continue } parts, _ := content["parts"].([]any) for _, part := range parts { if object, ok := part.(map[string]any); ok { if text := stringValue(object["text"]); text != "" { result = append(result, text) } } } } return result } func extractGeminiRoot(root map[string]any) []string { if root == nil { return nil } result := extractGeminiSystemInstruction(root["systemInstruction"]) result = append(result, extractGeminiSystemInstruction(root["system_instruction"])...) result = append(result, extractGemini(root["contents"])...) result = append(result, extractGemini(root["content"])...) result = append(result, extractGeminiInstances(root["instances"])...) if requests, ok := root["requests"].([]any); ok { for _, item := range requests { request, ok := item.(map[string]any) if !ok { continue } result = append(result, extractGeminiSystemInstruction(request["systemInstruction"])...) result = append(result, extractGeminiSystemInstruction(request["system_instruction"])...) result = append(result, extractGemini(request["contents"])...) result = append(result, extractGemini(request["content"])...) result = append(result, extractGeminiInstances(request["instances"])...) } } return result } func extractGeminiSystemInstruction(value any) []string { switch typed := value.(type) { case string: if text := strings.TrimSpace(typed); text != "" { return []string{text} } case map[string]any: if parts, ok := typed["parts"].([]any); ok { result := make([]string, 0, len(parts)) for _, part := range parts { if object, ok := part.(map[string]any); ok { if text := stringValue(object["text"]); text != "" { result = append(result, text) } } } return result } return contentTexts(typed) case []any: return extractGemini(typed) } return nil } func extractGeminiInstances(value any) []string { instances, ok := value.([]any) if !ok { return nil } result := make([]string, 0, len(instances)) for _, item := range instances { if instance, ok := item.(map[string]any); ok { if prompt := stringValue(instance["prompt"]); prompt != "" { result = append(result, prompt) } } } return result } func extractMediaPrompts(root map[string]any) []string { if root == nil { return nil } result := make([]string, 0, 4) seen := map[string]struct{}{} var walk func(any, string) walk = func(value any, key string) { switch typed := value.(type) { case map[string]any: keys := make([]string, 0, len(typed)) for childKey := range typed { keys = append(keys, childKey) } sort.Strings(keys) for _, childKey := range keys { walk(typed[childKey], childKey) } case []any: for _, item := range typed { walk(item, key) } case string: if !isMediaPromptKey(key) || looksLikeMediaPayload(typed) { return } text := strings.TrimSpace(typed) if text == "" { return } if _, duplicate := seen[text]; duplicate { return } seen[text] = struct{}{} result = append(result, text) } } walk(root, "") return result } func isMediaPromptKey(key string) bool { normalized := strings.NewReplacer("_", "", "-", "").Replace(strings.ToLower(strings.TrimSpace(key))) switch normalized { case "prompt", "inputprompt", "textprompt", "description", "query", "lyrics", "negativeprompt", "positiveprompt", "gptdescriptionprompt", "prompten", "finalprompt", "finalzhprompt", "origprompt", "actualprompt", "imageprompt", "input": return true default: return false } } func looksLikeMediaPayload(value string) bool { trimmed := strings.TrimSpace(value) lower := strings.ToLower(trimmed) if strings.HasPrefix(lower, "data:image/") || strings.HasPrefix(lower, "data:video/") || strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://") { return true } if len(trimmed) >= 256 { for _, r := range trimmed { alphaNumeric := (r >= 'A' && r <= 'Z') || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') if !alphaNumeric && r != '+' && r != '/' && r != '=' { return false } } return true } return false } func contentTexts(value any) []string { switch typed := value.(type) { case string: return []string{typed} case []any: result := make([]string, 0, len(typed)) for _, part := range typed { object, ok := part.(map[string]any) if !ok { continue } typeName := strings.ToLower(stringValue(object["type"])) if typeName != "" && typeName != "text" && typeName != "input_text" { continue } if text := stringValue(object["text"]); text != "" { result = append(result, text) } } return result case map[string]any: if text := stringValue(typed["text"]); text != "" { return []string{text} } } return nil } func normalizeSegmentsLatestFirst(values []string) []string { normalized := make([]string, 0, len(values)) for _, value := range values { value = strings.TrimSpace(value) if value != "" { normalized = append(normalized, value) } } if len(normalized) <= 1 { return normalized } latest := normalized[len(normalized)-1] result := make([]string, 0, len(normalized)) result = append(result, latest) result = append(result, normalized[:len(normalized)-1]...) return result } func RedactPreview(value string, maxRunes int) string { value = bearerPattern.ReplaceAllString(value, "Bearer ***") value = apiKeyPattern.ReplaceAllStringFunc(value, func(match string) string { if index := strings.IndexAny(match, ":= \t"); index >= 0 { return match[:index+1] + "***" } return "***" }) value = canaryPattern.ReplaceAllString(value, "${1}***") value = emailPattern.ReplaceAllString(value, "***@***") value = phonePattern.ReplaceAllString(value, "***PHONE***") return TrimRunes(value, maxRunes) } // BuildPromptPreview stores only a short, non-recoverable head of sanitized // input. Ordinary confidential prompts must not land nearly intact in PostgreSQL // or the admin UI merely because no secret regex matched. func BuildPromptPreview(value string, maxRunes int) string { if maxRunes <= 0 { maxRunes = DefaultPromptPreviewMaxRunes } redacted := strings.TrimSpace(RedactPreview(value, maxRunes)) if redacted == "" { return "" } runes := []rune(redacted) hadTruncation := strings.HasSuffix(redacted, "…") if hadTruncation && len(runes) > 0 { runes = runes[:len(runes)-1] } if len(runes) == 0 { return "***…" } // Short unlabelled secrets would otherwise leak a recoverable prefix (e.g. // 20 runes → 5 visible). Fully withhold anything below the keep threshold. const minLengthForPartialPreview = 32 if len(runes) < minLengthForPartialPreview { if hadTruncation { return "***…" } return "***" } // Keep at most a quarter of the already-truncated text, and never more than // 24 runes, so the majority of prompt content is withheld by default. keep := len(runes) / 4 if keep > 24 { keep = 24 } preview := string(runes[:keep]) + "***" if hadTruncation || keep < len(runes) { preview += "…" } return preview } func TrimRunes(value string, limit int) string { if limit <= 0 { return "" } runes := []rune(value) if len(runes) <= limit { return value } return string(runes[:limit]) + "…" } func stringValue(value any) string { text, _ := value.(string) return strings.TrimSpace(text) } func cloneInt64Ptr(value *int64) *int64 { if value == nil { return nil } cloned := *value return &cloned }