refactor: update session title generation and language context handling

- Modified the session title generation template to focus on extracting only the intent of the user's question.
- Enhanced the session service to include language context when generating titles.
- Updated middleware to inject language into the request context.
- Added a new test suite for validating SSRF safety of URLs.
This commit is contained in:
wizardchen
2026-03-17 17:07:08 +08:00
committed by lyingbug
parent f7750d65c1
commit 17da967f92
5 changed files with 115 additions and 6 deletions
@@ -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
+4
View File
@@ -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 != "" {
+1
View File
@@ -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)
+2 -2
View File
@@ -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)
}
+106
View File
@@ -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)
}
}