Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(logger): keep LLM debug records inside their directory

wizardchenef938f9114cc
diff --git a/internal/logger/llm_logger.go b/internal/logger/llm_logger.go
index 4058a8421..c692e1748 100644
--- a/internal/logger/llm_logger.go
+++ b/internal/logger/llm_logger.go
@@ -36,7 +36,8 @@ func configureLLMDebugLog() {
dir = "llm_debug"
}
- if err := os.MkdirAll(dir, 0o755); err != nil {
+ // Records hold full prompts and model replies: keep them to the service user.
+ if err := os.MkdirAll(dir, 0o700); err != nil {
fmt.Fprintf(os.Stderr, "llm_debug: failed to create dir %s: %v\n", dir, err)
return
}
@@ -101,7 +102,7 @@ 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
@@ -110,9 +111,28 @@ func LLMDebugLog(ctx context.Context, record *LLMCallRecord) {
_, _ = f.WriteString(text)
}
+// maxDebugFilenameID bounds the request-id part of a debug file name, well
+// inside the 255-byte name limit of common filesystems.
+const maxDebugFilenameID = 128
+
+// buildFilename derives the debug file name from the request id. The id comes
+// from the client's X-Request-ID header, so everything outside a plain
+// identifier alphabet is replaced: a "../" in it must not pick the directory
+// the record is written to.
func buildFilename(reqID string) string {
- if reqID != "" {
- return reqID + ".log"
+ safe := strings.Map(func(r rune) rune {
+ switch {
+ case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '-', r == '_':
+ return r
+ default:
+ return '_'
+ }
+ }, reqID)
+ if len(safe) > maxDebugFilenameID {
+ safe = safe[:maxDebugFilenameID]
+ }
+ if strings.Trim(safe, "_") != "" {
+ return safe + ".log"
}
return time.Now().Format("20060102_150405.000") + ".log"
}
diff --git a/internal/logger/llm_logger_test.go b/internal/logger/llm_logger_test.go
new file mode 100644
index 000000000..89e02462d
--- /dev/null
+++ b/internal/logger/llm_logger_test.go
@@ -0,0 +1,53 @@
+package logger
+
+import (
+ "context"
+ "os"
+ "path/filepath"
+ "runtime"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestBuildFilenameKeepsRequestIDInsideDir(t *testing.T) {
+ require.Equal(t, "3f2a-9c_1.log", buildFilename("3f2a-9c_1"))
+ require.Equal(t, "______etc_cron_d_x.log", buildFilename("../../etc/cron.d/x"))
+ require.Len(t, buildFilename(string(make([]byte, 500))), len("20060102_150405.000.log"),
+ "an id with nothing usable falls back to a timestamp")
+ long := buildFilename(string(func() []byte {
+ b := make([]byte, 300)
+ for i := range b {
+ b[i] = 'a'
+ }
+ return b
+ }()))
+ require.Len(t, long, maxDebugFilenameID+len(".log"))
+}
+
+func TestLLMDebugLogWritesPrivateFileInsideDir(t *testing.T) {
+ root := t.TempDir()
+ dir := filepath.Join(root, "llm_debug")
+ t.Setenv("LLM_DEBUG_LOG", dir)
+ saved := llmDebug.dir
+ savedEnabled := llmDebug.enabled
+ t.Cleanup(func() {
+ llmDebug.dir = saved
+ llmDebug.enabled = savedEnabled
+ })
+ configureLLMDebugLog()
+
+ ctx := WithRequestID(context.Background(), "../escaped")
+ LLMDebugLog(ctx, &LLMCallRecord{CallType: "Chat", Model: "m"})
+
+ _, err := os.Stat(filepath.Join(root, "escaped.log"))
+ require.True(t, os.IsNotExist(err), "the request id must not choose the directory")
+ info, err := os.Stat(filepath.Join(dir, "___escaped.log"))
+ require.NoError(t, err)
+ if runtime.GOOS != "windows" {
+ require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
+ dirInfo, err := os.Stat(dir)
+ require.NoError(t, err)
+ require.Equal(t, os.FileMode(0o700), dirInfo.Mode().Perm())
+ }
+}