feat(dingtalk): enable secure multimodal image QA
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
)
|
||||
|
||||
var llmDebug struct {
|
||||
@@ -36,10 +38,14 @@ func configureLLMDebugLog() {
|
||||
dir = "llm_debug"
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "llm_debug: failed to create dir %s: %v\n", dir, err)
|
||||
return
|
||||
}
|
||||
if err := os.Chmod(dir, 0o700); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "llm_debug: failed to secure dir %s: %v\n", dir, err)
|
||||
return
|
||||
}
|
||||
|
||||
llmDebug.dir = dir
|
||||
llmDebug.enabled = true
|
||||
@@ -101,12 +107,16 @@ func LLMDebugLog(ctx context.Context, record *LLMCallRecord) {
|
||||
defer llmDebug.mu.Unlock()
|
||||
|
||||
path := filepath.Join(llmDebug.dir, filename)
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "llm_debug: open %s: %v\n", path, err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
if err := f.Chmod(0o600); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "llm_debug: secure %s: %v\n", path, err)
|
||||
return
|
||||
}
|
||||
_, _ = f.WriteString(text)
|
||||
}
|
||||
|
||||
@@ -132,16 +142,17 @@ func formatRecord(r *LLMCallRecord) string {
|
||||
}
|
||||
|
||||
for _, s := range r.Sections {
|
||||
content := secutils.RedactDataURLsForLog(s.Content)
|
||||
b.WriteString(fmt.Sprintf("\n---------- %s ----------\n", s.Title))
|
||||
b.WriteString(s.Content)
|
||||
if !strings.HasSuffix(s.Content, "\n") {
|
||||
b.WriteString(content)
|
||||
if !strings.HasSuffix(content, "\n") {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
}
|
||||
|
||||
if r.Error != "" {
|
||||
b.WriteString("\n---------- Error ----------\n")
|
||||
b.WriteString(r.Error)
|
||||
b.WriteString(secutils.RedactDataURLsForLog(r.Error))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
|
||||
@@ -163,7 +174,7 @@ func extractRequestID(ctx context.Context) string {
|
||||
|
||||
// LLMCallRecord holds all information for one model API call.
|
||||
type LLMCallRecord struct {
|
||||
CallType string // "Chat", "Chat Stream", "Embedding", "Rerank", "VLM"
|
||||
CallType string // "Chat", "Chat Stream", "Embedding", "Rerank", "VLM"
|
||||
Model string
|
||||
Duration time.Duration
|
||||
Sections []RecordSection
|
||||
@@ -213,11 +224,7 @@ func FormatMessages(messages []LLMMessage) string {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
for _, img := range m.Images {
|
||||
if len([]rune(img)) > 80 {
|
||||
b.WriteString(fmt.Sprintf("[image: %s (%d bytes)]\n", TruncateRunes(img, 80), len(img)))
|
||||
} else {
|
||||
b.WriteString(fmt.Sprintf("[image: %s]\n", img))
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("[image: redacted (%d bytes)]\n", len(img)))
|
||||
}
|
||||
for _, tc := range m.ToolCalls {
|
||||
b.WriteString(fmt.Sprintf(" -> tool_call: id=%s, func=%s, args=%s\n", tc.ID, tc.FuncName, tc.Arguments))
|
||||
|
||||
@@ -17,6 +17,32 @@ func newEntry(level logrus.Level, msg string, data logrus.Fields) *logrus.Entry
|
||||
return e
|
||||
}
|
||||
|
||||
func TestLLMCallRecordFormatRedactsDataURIInSectionsAndErrors(t *testing.T) {
|
||||
secret := "TOPSECRETBASE64"
|
||||
record := &LLMCallRecord{
|
||||
Sections: []RecordSection{{Title: "Response", Content: "data:image/png;base64," + secret}},
|
||||
Error: "provider echoed data:text/plain," + secret,
|
||||
}
|
||||
got := formatRecord(record)
|
||||
if strings.Contains(got, secret) || strings.Contains(strings.ToLower(got), "data:") {
|
||||
t.Fatalf("record leaked data URI: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatMessagesRedactsImageValues(t *testing.T) {
|
||||
secret := "TOPSECRETBASE64"
|
||||
got := FormatMessages([]LLMMessage{{
|
||||
Role: "user",
|
||||
Images: []string{"data:image/png;base64," + secret},
|
||||
}})
|
||||
if strings.Contains(got, secret) || strings.Contains(strings.ToLower(got), "data:image") {
|
||||
t.Fatalf("FormatMessages leaked image value: %q", got)
|
||||
}
|
||||
if !strings.Contains(got, "redacted") {
|
||||
t.Fatalf("FormatMessages missing redaction marker: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormat_DefaultModeUnchanged(t *testing.T) {
|
||||
f := &CustomFormatter{} // no template, no color
|
||||
entry := newEntry(logrus.InfoLevel, "hello", logrus.Fields{
|
||||
|
||||
Reference in New Issue
Block a user