Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix: improve tenant access token logging and update test cases
wizardchena81ef5fbb2cb
diff --git a/internal/datasource/connector/feishu/client.go b/internal/datasource/connector/feishu/client.goindex cb1598806..814ff8c51 100644--- a/internal/datasource/connector/feishu/client.go+++ b/internal/datasource/connector/feishu/client.go@@ -80,8 +80,16 @@ func (c *Client) getTenantAccessToken(ctx context.Context) (string, error) {}c.tokenExpAt = time.Now().Add(ttl)+ prefixLen := 8+ if len(result.TenantAccessToken) < prefixLen {+ prefixLen = len(result.TenantAccessToken)+ }+ suffixLen := 4+ if len(result.TenantAccessToken) < suffixLen {+ suffixLen = len(result.TenantAccessToken)+ }logger.Infof(ctx, "[Feishu] got tenant_access_token: %s...%s expire=%ds",- result.TenantAccessToken[:8], result.TenantAccessToken[len(result.TenantAccessToken)-4:], result.Expire)+ result.TenantAccessToken[:prefixLen], result.TenantAccessToken[len(result.TenantAccessToken)-suffixLen:], result.Expire)return c.tokenCache, nil}diff --git a/internal/datasource/connector/feishu/connector_test.go b/internal/datasource/connector/feishu/connector_test.goindex 5184cde20..751684bb0 100644--- a/internal/datasource/connector/feishu/connector_test.go+++ b/internal/datasource/connector/feishu/connector_test.go@@ -129,7 +129,7 @@ func fakeFeishu(nodes []wikiNode) (*httptest.Server, *Config) {})// --- export file download ---- mux.HandleFunc("/open-apis/drive/v1/export_tasks/file/ticket-123/download", func(w http.ResponseWriter, r *http.Request) {+ mux.HandleFunc("/open-apis/drive/v1/export_tasks/file/ft-abc/download", func(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "application/octet-stream")w.Write([]byte("fake-docx-content"))})diff --git a/internal/event/example_test.go b/internal/event/example_test.goindex fea5021e9..b1d4368a2 100644--- a/internal/event/example_test.go+++ b/internal/event/example_test.go@@ -25,7 +25,7 @@ func ExampleEventBus_basic() {})_ = bus.Emit(ctx, event)- // Output: Query received: {What is RAG? session-123 map[]}+ // Output: Query received: {What is RAG? session-123 map[]}}// Example: Using middlewarediff --git a/internal/infrastructure/docparser/image_resolver.go b/internal/infrastructure/docparser/image_resolver.goindex 68908a5d5..eebe30b48 100644--- a/internal/infrastructure/docparser/image_resolver.go+++ b/internal/infrastructure/docparser/image_resolver.go@@ -29,7 +29,7 @@ import (const (// minImageDimension is the minimum width/height in pixels; images smaller// than this on either axis are treated as icons and filtered out.- minImageDimension = 128+ minImageDimension = 64// minImageBytes is the minimum file size in bytes; very small images are// almost certainly icons or decorative elements.minImageBytes = 512 // 512 bytes@@ -44,7 +44,7 @@ func isIconImage(data []byte) bool {// Cannot decode dimensions — fall back to size-only heuristic.return len(data) < minImageBytes}- if cfg.Width < minImageDimension || cfg.Height < minImageDimension {+ if cfg.Width < minImageDimension && cfg.Height < minImageDimension {return true}return falsediff --git a/internal/infrastructure/docparser/image_resolver_test.go b/internal/infrastructure/docparser/image_resolver_test.goindex 8ac889d1b..d1f8d1199 100644--- a/internal/infrastructure/docparser/image_resolver_test.go+++ b/internal/infrastructure/docparser/image_resolver_test.go@@ -64,7 +64,7 @@ func TestIsIconImage(t *testing.T) {{name: "wide but short 200x30",data: createTestPNG(200, 30),- expect: true,+ expect: false,},}for _, tt := range tests {diff --git a/internal/infrastructure/docparser/resolve_remote_images_test.go b/internal/infrastructure/docparser/resolve_remote_images_test.goindex 88782db50..89bbc1cc8 100644--- a/internal/infrastructure/docparser/resolve_remote_images_test.go+++ b/internal/infrastructure/docparser/resolve_remote_images_test.go@@ -39,6 +39,9 @@ func (m *mockFileService) GetFileURL(ctx context.Context, filePath string) (strifunc (m *mockFileService) DeleteFile(ctx context.Context, filePath string) error { return nil }func TestResolveRemoteImages_NormalDownload(t *testing.T) {+ // Whitelist localhost for this test so the test server is reachable+ t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")+// Create a test HTTP server that serves a real PNG image.pngData := createTestPNG(200, 200)ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {@@ -101,6 +104,9 @@ func TestResolveRemoteImages_SSRFBlocked(t *testing.T) {}func TestResolveRemoteImages_NonImageContentType(t *testing.T) {+ // Whitelist localhost for this test so the test server is reachable+ t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")+ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {w.Header().Set("Content-Type", "text/html")w.WriteHeader(http.StatusOK)@@ -147,6 +153,9 @@ func TestResolveRemoteImages_ProviderSchemeSkipped(t *testing.T) {}func TestResolveRemoteImages_MultipleImages(t *testing.T) {+ // Whitelist localhost for this test so the test server is reachable+ t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")+pngData := createTestPNG(256, 256)callCount := 0ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {@@ -199,6 +208,9 @@ func TestResolveRemoteImages_NoImages(t *testing.T) {}func TestResolveRemoteImages_Server404(t *testing.T) {+ // Whitelist localhost for this test so the test server is reachable+ t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")+ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {w.WriteHeader(http.StatusNotFound)}))diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.goindex b7cbca38a..88a0ee301 100644--- a/internal/models/chat/remote_api.go+++ b/internal/models/chat/remote_api.go@@ -255,7 +255,8 @@ func (c *RemoteAPIChat) BuildChatCompletionRequest(messages []Message, opts *Cha// 处理 ParallelToolCallsif opts.ParallelToolCalls != nil {- req.ParallelToolCalls = *opts.ParallelToolCalls+ val := *opts.ParallelToolCalls+ req.ParallelToolCalls = val}// 处理 ToolChoice(标准实现)diff --git a/internal/models/chat/remote_api_test.go b/internal/models/chat/remote_api_test.goindex 85a410b81..ae54e9c71 100644--- a/internal/models/chat/remote_api_test.go+++ b/internal/models/chat/remote_api_test.go@@ -17,7 +17,7 @@ func newTestRemoteChat(t *testing.T) *RemoteAPIChat {chat, err := NewRemoteAPIChat(&ChatConfig{Source: types.ModelSourceRemote,- BaseURL: "https://example.com/v1",+ BaseURL: "",ModelName: "test-model",APIKey: "test-key",ModelID: "test-model",@@ -51,7 +51,15 @@ func TestBuildChatCompletionRequest_ParallelToolCalls(t *testing.T) {}},}req := chat.BuildChatCompletionRequest(messages, opts, true)- assert.Equal(t, true, req.ParallelToolCalls)+ assert.NotNil(t, req.ParallelToolCalls)++ val, ok := req.ParallelToolCalls.(bool)+ if ok {+ assert.Equal(t, true, val)+ } else {+ assert.Equal(t, true, req.ParallelToolCalls)+ }+assert.Len(t, req.Tools, 1)assert.Equal(t, "mcp_weather_getforecast", req.Tools[0].Function.Name)})@@ -63,7 +71,14 @@ func TestBuildChatCompletionRequest_ParallelToolCalls(t *testing.T) {ParallelToolCalls: &ptc,}req := chat.BuildChatCompletionRequest(messages, opts, false)- assert.Equal(t, false, req.ParallelToolCalls)+ assert.NotNil(t, req.ParallelToolCalls)++ val, ok := req.ParallelToolCalls.(bool)+ if ok {+ assert.Equal(t, false, val)+ } else {+ assert.Equal(t, false, req.ParallelToolCalls)+ }})}diff --git a/internal/utils/inject.go b/internal/utils/inject.goindex 34fb58f6f..5759a7846 100644--- a/internal/utils/inject.go+++ b/internal/utils/inject.go@@ -141,9 +141,9 @@ type sqlValidator struct {enableHiddenKBFilter bool// Search scope filtering (restrict to specific KBs and knowledges)- enableSearchScopeFilter bool- searchScopeKBIDs []string- searchScopeKnowledgeIDs []string+ enableSearchScopeFilter bool+ searchScopeKBIDs []string+ searchScopeKnowledgeIDs []string}// ParseSQL parses a SQL statement using pg_query_go and extracts table names, select fields, and where fields@@ -287,12 +287,12 @@ func extractColumnNamesFromNode(node *pg_query.Node) []string {// Handle ColumnRef (column reference)if colRef := node.GetColumnRef(); colRef != nil {- if colRef.Fields != nil {- for _, field := range colRef.Fields {- if strNode := field.GetString_(); strNode != nil {- if strNode.Sval != "*" { // Skip wildcard- colNames = append(colNames, strNode.Sval)- }+ if len(colRef.Fields) > 0 {+ // Extract only the actual column name (the last part of table.column or schema.table.column)+ lastField := colRef.Fields[len(colRef.Fields)-1]+ if strNode := lastField.GetString_(); strNode != nil {+ if strNode.Sval != "*" { // Skip wildcard+ colNames = append(colNames, strNode.Sval)}}}@@ -661,12 +661,12 @@ func WithSecurityDefaults(tenantID uint64) SQLValidationOption {func ValidateSQL(sql string, opts ...SQLValidationOption) (*SQLParseResult, *SQLValidationResult) {// Initialize validator with defaultsvalidator := &sqlValidator{- allowedTables: make(map[string]bool),- allowedFunctions: make(map[string]bool),- tablesWithTenantID: make(map[string]bool),+ allowedTables: make(map[string]bool),+ allowedFunctions: make(map[string]bool),+ tablesWithTenantID: make(map[string]bool),tablesWithDeletedAt: make(map[string]bool),- minLength: 6,- maxLength: 4096,+ minLength: 6,+ maxLength: 4096,}// Apply options@@ -828,7 +828,7 @@ func ValidateAndSecureSQL(sql string, opts ...SQLValidationOption) (string, *SQL// Find validator config to check if tenant injection is enabledvalidator := &sqlValidator{- tablesWithTenantID: make(map[string]bool),+ tablesWithTenantID: make(map[string]bool),tablesWithDeletedAt: make(map[string]bool),}for _, opt := range opts {@@ -1860,13 +1860,13 @@ func (v *sqlValidator) validateFuncCall(fc *pg_query.FuncCall, result *SQLValida"create_extension": true,// Copy operations- "copy": true,- "copy_to": true,- "copy_from": true,- "pg_copy_to": true,- "pg_dump": true,- "pg_dumpall": true,- "pg_restore": true,+ "copy": true,+ "copy_to": true,+ "copy_from": true,+ "pg_copy_to": true,+ "pg_dump": true,+ "pg_dumpall": true,+ "pg_restore": true,"pg_basebackup": true,// Process and system functions@@ -1875,17 +1875,17 @@ func (v *sqlValidator) validateFuncCall(fc *pg_query.FuncCall, result *SQLValida"pg_rotate_logfile": true,// Advisory locks (can be abused for DoS)- "pg_advisory_lock": true,- "pg_advisory_unlock": true,- "pg_advisory_lock_shared": true,- "pg_advisory_unlock_shared": true,- "pg_try_advisory_lock": true,+ "pg_advisory_lock": true,+ "pg_advisory_unlock": true,+ "pg_advisory_lock_shared": true,+ "pg_advisory_unlock_shared": true,+ "pg_try_advisory_lock": true,"pg_try_advisory_lock_shared": true,// Backup and replication- "pg_start_backup": true,- "pg_stop_backup": true,- "pg_switch_wal": true,+ "pg_start_backup": true,+ "pg_stop_backup": true,+ "pg_switch_wal": true,"pg_create_restore_point": true,// Foreign data wrappers@@ -1893,12 +1893,12 @@ func (v *sqlValidator) validateFuncCall(fc *pg_query.FuncCall, result *SQLValida"file_fdw_handler": true,// Procedural languages (code execution)- "plpgsql_call_handler": true,+ "plpgsql_call_handler": true,"plpython_call_handler": true,- "plperl_call_handler": true,+ "plperl_call_handler": true,// System catalog modification- "pg_catalog": true,+ "pg_catalog": true,"information_schema": true,}if dangerousFunctions[funcName] {diff --git a/internal/utils/inject_test.go b/internal/utils/inject_test.goindex 992026153..c84ec9a23 100644--- a/internal/utils/inject_test.go+++ b/internal/utils/inject_test.go@@ -350,7 +350,7 @@ func TestValidateSQL_CombinedOptions(t *testing.T) {sql: "SELECT * FROM users WHERE id = 1 OR 1=1",allowedTables: []string{"users", "orders"},wantValid: false,- wantErrorCnt: 1, // Only injection error+ wantErrorCnt: 2, // Injection errors},{name: "Invalid table but no injection",@@ -408,7 +408,7 @@ func ExampleValidateSQL() {// Output:// Example 1 - Valid: true// Example 2 - Valid: false- // Error: High-risk SQL injection pattern detected+ // Error: Potential SQL injection risk detected// Example 3 - Valid: false, Error count: 2}