package utils import ( "strconv" "strings" ) const ( defaultMaxLogChars = 12000 dataURLRedaction = "[image-data-redacted]" ) // CompactImageDataURLForLog removes every data URI before log or dump output. // It deliberately handles non-canonical forms because debug paths may receive // JSON-escaped or percent-encoded requests in addition to ordinary data: URLs. func CompactImageDataURLForLog(raw string) string { masked := RedactDataURLsForLog(raw) if len(masked) <= defaultMaxLogChars { return masked } return masked[:defaultMaxLogChars] + "... (truncated, total " + strconv.Itoa(len(masked)) + " chars)" } // RedactDataURLsForLog removes data URIs without truncating surrounding text. func RedactDataURLsForLog(raw string) string { return redactDataURLs(raw) } func redactDataURLs(raw string) string { var out strings.Builder out.Grow(len(raw)) for i := 0; i < len(raw); { prefixLen := dataURLPrefixLen(raw[i:]) if prefixLen == 0 { out.WriteByte(raw[i]) i++ continue } out.WriteString(dataURLRedaction) i += prefixLen for i < len(raw) && !isDataURLTerminator(raw, i) { i++ } } return out.String() } // dataURLPrefixLen recognizes "data:" when each ASCII character is either // literal, percent encoded, or represented as one-or-more JSON escape layers. func dataURLPrefixLen(value string) int { pos := 0 for i := 0; i < len("data:"); i++ { consumed := encodedASCIILen(value[pos:], "data:"[i]) if consumed == 0 { return 0 } pos += consumed } return pos } func encodedASCIILen(value string, want byte) int { if len(value) > 0 && strings.EqualFold(value[:1], string(want)) { return 1 } if len(value) >= 3 && value[0] == '%' { pos := 0 for len(value) >= pos+3 && strings.EqualFold(value[pos:pos+3], "%25") { pos += 3 } if pos == 0 { if decoded, ok := decodeHexByte(value[1], value[2]); ok && decoded == want { return 3 } } else if len(value) >= pos+2 { if decoded, ok := decodeHexByte(value[pos], value[pos+1]); ok && decoded == want { return pos + 2 } } } backslashes := 0 for backslashes < len(value) && value[backslashes] == '\\' { backslashes++ } if backslashes > 0 && len(value) >= backslashes+5 && (value[backslashes] == 'u' || value[backslashes] == 'U') { hex := value[backslashes+1 : backslashes+5] if strings.EqualFold(hex[:2], "00") { if decoded, ok := decodeHexByte(hex[2], hex[3]); ok && decoded == want { return backslashes + 5 } } } return 0 } func decodeHexByte(high, low byte) (byte, bool) { hex := func(ch byte) (byte, bool) { switch { case ch >= '0' && ch <= '9': return ch - '0', true case ch >= 'a' && ch <= 'f': return ch - 'a' + 10, true case ch >= 'A' && ch <= 'F': return ch - 'A' + 10, true default: return 0, false } } hi, ok := hex(high) if !ok { return 0, false } lo, ok := hex(low) if !ok { return 0, false } return hi<<4 | lo, true } // Data characters may legally include punctuation such as parentheses and // backslashes. Stop only at whitespace or an unescaped serialized-string quote. func isDataURLTerminator(raw string, pos int) bool { switch raw[pos] { case ' ', '\t', '\r', '\n': return true case '"': backslashes := 0 for i := pos - 1; i >= 0 && raw[i] == '\\'; i-- { backslashes++ } return backslashes%2 == 0 default: return false } }