diff --git a/config/prompt_templates/generate_session_title.yaml b/config/prompt_templates/generate_session_title.yaml index de2d37c6..1b147b8f 100644 --- a/config/prompt_templates/generate_session_title.yaml +++ b/config/prompt_templates/generate_session_title.yaml @@ -9,8 +9,6 @@ templates: Requirements: - 3-8 words - - Extract the core topic + - Only extract the intent of the user's question, don't answer about it - Output only the title, no explanation needed - - IMPORTANT: Use {{language}} for the title - - User question: + - IMPORTANT: Use {{language}} for the title \ No newline at end of file diff --git a/internal/application/service/session.go b/internal/application/service/session.go index 941cddce..6d50d095 100644 --- a/internal/application/service/session.go +++ b/internal/application/service/session.go @@ -454,6 +454,7 @@ func (s *sessionService) GenerateTitleAsync( // sessionRepo.Update uses session.TenantID in WHERE, so the session row is updated correctly regardless of ctx. tenantID := ctx.Value(types.TenantIDContextKey) requestID := ctx.Value(types.RequestIDContextKey) + language := ctx.Value(types.LanguageContextKey) go func() { bgCtx := context.Background() if tenantID != nil { @@ -462,6 +463,9 @@ func (s *sessionService) GenerateTitleAsync( if requestID != nil { bgCtx = context.WithValue(bgCtx, types.RequestIDContextKey, requestID) } + if language != nil { + bgCtx = context.WithValue(bgCtx, types.LanguageContextKey, language) + } // Skip if title already exists if session.Title != "" { diff --git a/internal/middleware/language.go b/internal/middleware/language.go index 1ea4f5c8..65fa2f1b 100644 --- a/internal/middleware/language.go +++ b/internal/middleware/language.go @@ -42,6 +42,7 @@ func Language() gin.HandlerFunc { } // Inject into context + c.Set(types.LanguageContextKey.String(), lang) ctx := context.WithValue(c.Request.Context(), types.LanguageContextKey, lang) c.Request = c.Request.WithContext(ctx) diff --git a/internal/types/context_helpers.go b/internal/types/context_helpers.go index 637878f6..b4786a79 100644 --- a/internal/types/context_helpers.go +++ b/internal/types/context_helpers.go @@ -47,7 +47,7 @@ func SessionTenantIDFromContext(ctx context.Context) (uint64, bool) { } // LanguageFromContext extracts the language locale string from ctx (e.g. "zh-CN", "en-US"). -// Returns ("en-US", false) when the key is absent. +// Returns ("zh-CN", false) when the key is absent. func LanguageFromContext(ctx context.Context) (string, bool) { v, ok := ctx.Value(LanguageContextKey).(string) return v, ok && v != "" @@ -58,7 +58,7 @@ func LanguageFromContext(ctx context.Context) (string, bool) { func LanguageNameFromContext(ctx context.Context) string { lang, ok := LanguageFromContext(ctx) if !ok { - lang = "en-US" + lang = "zh-CN" } return LanguageLocaleName(lang) } diff --git a/internal/utils/security_test.go b/internal/utils/security_test.go new file mode 100644 index 00000000..5d00df59 --- /dev/null +++ b/internal/utils/security_test.go @@ -0,0 +1,106 @@ +package utils + +import ( + "strings" + "testing" +) + +func TestIsSSRFSafeURL(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + rawURL string + wantOK bool + wantReasonSub string + }{ + { + name: "empty URL", + rawURL: "", + wantOK: false, + wantReasonSub: "URL is empty", + }, + { + name: "invalid scheme", + rawURL: "ftp://example.com/file.txt", + wantOK: false, + wantReasonSub: "invalid scheme", + }, + { + name: "missing hostname", + rawURL: "https:///api/v1/ping", + wantOK: false, + wantReasonSub: "URL has no hostname", + }, + { + name: "restricted hostname", + rawURL: "https://localhost/health", + wantOK: false, + wantReasonSub: "is restricted", + }, + { + name: "restricted hostname suffix", + rawURL: "https://service.internal/status", + wantOK: false, + wantReasonSub: "hostname suffix .internal is restricted", + }, + { + name: "direct IPv4 blocked", + rawURL: "https://8.8.8.8/dns-query", + wantOK: false, + wantReasonSub: "direct IP address access is not allowed", + }, + { + name: "direct IPv6 blocked", + rawURL: "https://[2001:4860:4860::8888]/dns-query", + wantOK: false, + wantReasonSub: "direct IP address access is not allowed", + }, + { + name: "IP-like decimal hostname blocked", + rawURL: "https://2130706433/", + wantOK: false, + wantReasonSub: "IP-like hostname format is not allowed", + }, + { + name: "IP-like octal hostname blocked", + rawURL: "https://0177.0.0.1/", + wantOK: false, + wantReasonSub: "IP-like hostname format is not allowed", + }, + { + name: "blocked internal service port", + rawURL: "https://example.com:3306/db", + wantOK: false, + wantReasonSub: "port 3306 is blocked for security reasons", + }, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + ok, reason := IsSSRFSafeURL(tt.rawURL) + if ok != tt.wantOK { + t.Fatalf("IsSSRFSafeURL(%q) ok = %v, want %v, reason = %q", tt.rawURL, ok, tt.wantOK, reason) + } + if tt.wantReasonSub != "" && !strings.Contains(reason, tt.wantReasonSub) { + t.Fatalf("IsSSRFSafeURL(%q) reason = %q, want contains %q", tt.rawURL, reason, tt.wantReasonSub) + } + }) + } +} + +func TestIsSSRFSafeURL_AllowPublicDomain(t *testing.T) { + t.Parallel() + + ok, reason := IsSSRFSafeURL("https://example.com/path") + if !ok { + // This path depends on runtime DNS/network. If DNS is unavailable, skip to keep CI stable. + if strings.Contains(reason, "DNS resolution failed") { + t.Skipf("skip due to DNS unavailable in test environment: %s", reason) + } + t.Fatalf("expected public domain to be allowed, got ok=%v reason=%q", ok, reason) + } +}