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:
@@ -9,8 +9,6 @@ templates:
|
|||||||
|
|
||||||
Requirements:
|
Requirements:
|
||||||
- 3-8 words
|
- 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
|
- Output only the title, no explanation needed
|
||||||
- IMPORTANT: Use {{language}} for the title
|
- IMPORTANT: Use {{language}} for the title
|
||||||
|
|
||||||
User question:
|
|
||||||
@@ -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.
|
// sessionRepo.Update uses session.TenantID in WHERE, so the session row is updated correctly regardless of ctx.
|
||||||
tenantID := ctx.Value(types.TenantIDContextKey)
|
tenantID := ctx.Value(types.TenantIDContextKey)
|
||||||
requestID := ctx.Value(types.RequestIDContextKey)
|
requestID := ctx.Value(types.RequestIDContextKey)
|
||||||
|
language := ctx.Value(types.LanguageContextKey)
|
||||||
go func() {
|
go func() {
|
||||||
bgCtx := context.Background()
|
bgCtx := context.Background()
|
||||||
if tenantID != nil {
|
if tenantID != nil {
|
||||||
@@ -462,6 +463,9 @@ func (s *sessionService) GenerateTitleAsync(
|
|||||||
if requestID != nil {
|
if requestID != nil {
|
||||||
bgCtx = context.WithValue(bgCtx, types.RequestIDContextKey, requestID)
|
bgCtx = context.WithValue(bgCtx, types.RequestIDContextKey, requestID)
|
||||||
}
|
}
|
||||||
|
if language != nil {
|
||||||
|
bgCtx = context.WithValue(bgCtx, types.LanguageContextKey, language)
|
||||||
|
}
|
||||||
|
|
||||||
// Skip if title already exists
|
// Skip if title already exists
|
||||||
if session.Title != "" {
|
if session.Title != "" {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ func Language() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Inject into context
|
// Inject into context
|
||||||
|
c.Set(types.LanguageContextKey.String(), lang)
|
||||||
ctx := context.WithValue(c.Request.Context(), types.LanguageContextKey, lang)
|
ctx := context.WithValue(c.Request.Context(), types.LanguageContextKey, lang)
|
||||||
c.Request = c.Request.WithContext(ctx)
|
c.Request = c.Request.WithContext(ctx)
|
||||||
|
|
||||||
|
|||||||
@@ -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").
|
// 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) {
|
func LanguageFromContext(ctx context.Context) (string, bool) {
|
||||||
v, ok := ctx.Value(LanguageContextKey).(string)
|
v, ok := ctx.Value(LanguageContextKey).(string)
|
||||||
return v, ok && v != ""
|
return v, ok && v != ""
|
||||||
@@ -58,7 +58,7 @@ func LanguageFromContext(ctx context.Context) (string, bool) {
|
|||||||
func LanguageNameFromContext(ctx context.Context) string {
|
func LanguageNameFromContext(ctx context.Context) string {
|
||||||
lang, ok := LanguageFromContext(ctx)
|
lang, ok := LanguageFromContext(ctx)
|
||||||
if !ok {
|
if !ok {
|
||||||
lang = "en-US"
|
lang = "zh-CN"
|
||||||
}
|
}
|
||||||
return LanguageLocaleName(lang)
|
return LanguageLocaleName(lang)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user