chore: 初始化仓库,导入项目基线代码

Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、
Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。
今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
runny committed 2026-11-16 18:12:23 +08:00
commit 7ec21f35b0
5767 files changed
+1436788

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
package utils
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"io"
"os"
"strings"
)
// EncPrefix marks a string as AES-256-GCM encrypted
const EncPrefix = "enc:v1:"
// GetAESKey reads the 32-byte AES key from SYSTEM_AES_KEY env.
// Returns nil if not set or not exactly 32 bytes.
func GetAESKey() []byte {
key := []byte(os.Getenv("SYSTEM_AES_KEY"))
if len(key) == 32 {
return key
}
return nil
}
// EncryptAESGCM encrypts plaintext with AES-256-GCM.
// Returns the original string if empty, already encrypted, or key is nil.
func EncryptAESGCM(plaintext string, key []byte) (string, error) {
if plaintext == "" || key == nil {
return plaintext, nil
}
if strings.HasPrefix(plaintext, EncPrefix) {
return plaintext, nil
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonce := make([]byte, aesgcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return "", err
}
ciphertext := aesgcm.Seal(nil, nonce, []byte(plaintext), nil)
combined := append(nonce, ciphertext...)
return EncPrefix + base64.RawURLEncoding.EncodeToString(combined), nil
}
// ErrEncryptedDataMissingKey is returned by DecryptStoredSecret when the value
// carries the enc:v1: prefix but no AES key is available to decrypt it. This
// signals an operator misconfiguration (typically a rotated or unset
// SYSTEM_AES_KEY) and must propagate so the system fails loudly instead of
// silently using ciphertext as a credential.
var ErrEncryptedDataMissingKey = errors.New("encrypted data found but SYSTEM_AES_KEY is not set or has wrong length")
// DecryptAESGCM decrypts an AES-256-GCM encrypted string.
// If the string lacks the enc:v1: prefix, it's treated as legacy plaintext and returned as-is.
func DecryptAESGCM(encrypted string, key []byte) (string, error) {
if encrypted == "" || key == nil {
return encrypted, nil
}
if !strings.HasPrefix(encrypted, EncPrefix) {
return encrypted, nil
}
data, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(encrypted, EncPrefix))
if err != nil {
return "", err
}
if len(data) < 12 {
return "", errors.New("invalid encrypted data: too short")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
nonce, ciphertext := data[:aesgcm.NonceSize()], data[aesgcm.NonceSize():]
plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
return "", err
}
return string(plaintext), nil
}
// DecryptStoredSecret decrypts a value loaded from the database with strict
// error propagation. Use this in hot-path code that is about to actually
// USE the credential (e.g. building an upstream HTTP request) — a rotated
// or missing key must surface as a loud error rather than silently
// degrading to "no credential", because the user expects the configured
// upstream call to either work or fail with a clear "auth misconfigured"
// message.
//
// Behaviour:
// - empty input -> empty output, no error.
// - no enc:v1: prefix -> returned as-is (legacy plaintext column), no error.
// - has enc:v1: prefix and SYSTEM_AES_KEY is missing or wrong length ->
// returns ErrEncryptedDataMissingKey.
// - has enc:v1: prefix and key is set -> decrypts, returns any decryption
// error verbatim (e.g. base64 decode failure, GCM auth tag mismatch from a
// rotated key).
//
// For GORM Scan paths that load a whole row (where erroring out hides the
// rest of the resource from the user) use DecryptStoredSecretLenient
// instead — it lets the row load with the secret blanked out so the UI
// can render "credential not configured" and the user can re-enter it,
// without crashing list endpoints when the operator rotates or removes
// SYSTEM_AES_KEY.
func DecryptStoredSecret(encrypted string) (string, error) {
if encrypted == "" {
return "", nil
}
if !strings.HasPrefix(encrypted, EncPrefix) {
return encrypted, nil
}
key := GetAESKey()
if key == nil {
return "", ErrEncryptedDataMissingKey
}
return DecryptAESGCM(encrypted, key)
}
// DecryptStoredSecretLenient is the load-path counterpart for use inside
// GORM Scan / AfterFind hooks. It prefers graceful degradation over
// failing the whole row load.
//
// Returns:
// - (plaintext, true) for empty input, legacy plaintext, or successful decrypt
// - ("", false) when the value has the enc:v1: prefix but cannot be
// decrypted (SYSTEM_AES_KEY missing, rotated, or ciphertext corrupted).
// The bool=false case is the operator's signal: log a warning and treat
// the field as unconfigured. The row still loads so the rest of the
// resource displays normally; the UI shows "credential not configured",
// and any upstream call attempted with this empty value fails with a
// clear "missing api key" rather than sending ciphertext as the key.
//
// Rationale: a single line of broken ciphertext used to crash entire list
// endpoints (e.g. ListModels returning "" because one row failed Scan),
// hiding all other models from the user. Lenient load + loud per-row log
// is strictly more recoverable than fail-fast Scan.
func DecryptStoredSecretLenient(encrypted string) (plaintext string, ok bool) {
plain, err := DecryptStoredSecret(encrypted)
if err != nil {
return "", false
}
return plain, true
}
+159
View File
@@ -0,0 +1,159 @@
package utils
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const testAESKey = "01234567890123456789012345678901" // 32 bytes
func TestEncryptAESGCM(t *testing.T) {
key := []byte(testAESKey)
t.Run("encrypts plaintext with enc:v1: prefix", func(t *testing.T) {
encrypted, err := EncryptAESGCM("sk-secret-key", key)
require.NoError(t, err)
assert.True(t, strings.HasPrefix(encrypted, EncPrefix))
assert.NotEqual(t, "sk-secret-key", encrypted)
})
t.Run("returns empty string as-is", func(t *testing.T) {
encrypted, err := EncryptAESGCM("", key)
require.NoError(t, err)
assert.Equal(t, "", encrypted)
})
t.Run("returns already encrypted string as-is (idempotent)", func(t *testing.T) {
first, err := EncryptAESGCM("sk-secret-key", key)
require.NoError(t, err)
second, err := EncryptAESGCM(first, key)
require.NoError(t, err)
assert.Equal(t, first, second, "re-encrypting should be a no-op")
})
t.Run("returns plaintext when key is nil", func(t *testing.T) {
encrypted, err := EncryptAESGCM("sk-secret-key", nil)
require.NoError(t, err)
assert.Equal(t, "sk-secret-key", encrypted)
})
}
func TestDecryptAESGCM(t *testing.T) {
key := []byte(testAESKey)
t.Run("round-trip encrypt then decrypt", func(t *testing.T) {
original := "sk-my-secret-api-key"
encrypted, err := EncryptAESGCM(original, key)
require.NoError(t, err)
decrypted, err := DecryptAESGCM(encrypted, key)
require.NoError(t, err)
assert.Equal(t, original, decrypted)
})
t.Run("returns legacy plaintext as-is (no enc:v1: prefix)", func(t *testing.T) {
decrypted, err := DecryptAESGCM("sk-legacy-plaintext", key)
require.NoError(t, err)
assert.Equal(t, "sk-legacy-plaintext", decrypted)
})
t.Run("returns empty string as-is", func(t *testing.T) {
decrypted, err := DecryptAESGCM("", key)
require.NoError(t, err)
assert.Equal(t, "", decrypted)
})
t.Run("returns as-is when key is nil", func(t *testing.T) {
decrypted, err := DecryptAESGCM("enc:v1:something", nil)
require.NoError(t, err)
assert.Equal(t, "enc:v1:something", decrypted)
})
}
func TestDecryptStoredSecret(t *testing.T) {
t.Run("returns empty string as-is", func(t *testing.T) {
t.Setenv("SYSTEM_AES_KEY", testAESKey)
out, err := DecryptStoredSecret("")
require.NoError(t, err)
assert.Equal(t, "", out)
})
t.Run("legacy plaintext (no enc:v1: prefix) returned as-is", func(t *testing.T) {
t.Setenv("SYSTEM_AES_KEY", testAESKey)
out, err := DecryptStoredSecret("sk-legacy-plaintext")
require.NoError(t, err)
assert.Equal(t, "sk-legacy-plaintext", out)
})
t.Run("legacy plaintext is returned even when key is unset", func(t *testing.T) {
t.Setenv("SYSTEM_AES_KEY", "")
out, err := DecryptStoredSecret("sk-legacy-plaintext")
require.NoError(t, err)
assert.Equal(t, "sk-legacy-plaintext", out)
})
t.Run("round-trip with valid key", func(t *testing.T) {
t.Setenv("SYSTEM_AES_KEY", testAESKey)
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
require.NoError(t, err)
out, err := DecryptStoredSecret(encrypted)
require.NoError(t, err)
assert.Equal(t, "sk-secret", out)
})
t.Run("encrypted value with missing key returns ErrEncryptedDataMissingKey", func(t *testing.T) {
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
require.NoError(t, err)
t.Setenv("SYSTEM_AES_KEY", "")
out, err := DecryptStoredSecret(encrypted)
require.ErrorIs(t, err, ErrEncryptedDataMissingKey)
assert.Equal(t, "", out, "ciphertext must NOT leak when decryption is impossible")
})
t.Run("encrypted value with wrong-length key returns ErrEncryptedDataMissingKey", func(t *testing.T) {
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
require.NoError(t, err)
t.Setenv("SYSTEM_AES_KEY", "too-short")
out, err := DecryptStoredSecret(encrypted)
require.ErrorIs(t, err, ErrEncryptedDataMissingKey)
assert.Equal(t, "", out)
})
t.Run("encrypted value with rotated key returns auth-tag error", func(t *testing.T) {
encrypted, err := EncryptAESGCM("sk-secret", []byte(testAESKey))
require.NoError(t, err)
t.Setenv("SYSTEM_AES_KEY", "abcdefghijklmnopqrstuvwxyz123456")
out, err := DecryptStoredSecret(encrypted)
require.Error(t, err)
assert.NotErrorIs(t, err, ErrEncryptedDataMissingKey, "should be a real auth failure, not the missing-key sentinel")
assert.Equal(t, "", out, "ciphertext must NOT leak when decryption fails")
})
}
func TestGetAESKey(t *testing.T) {
t.Run("returns key when SYSTEM_AES_KEY is 32 bytes", func(t *testing.T) {
t.Setenv("SYSTEM_AES_KEY", testAESKey)
key := GetAESKey()
assert.Equal(t, []byte(testAESKey), key)
})
t.Run("returns nil when SYSTEM_AES_KEY is not set", func(t *testing.T) {
t.Setenv("SYSTEM_AES_KEY", "")
key := GetAESKey()
assert.Nil(t, key)
})
t.Run("returns nil when SYSTEM_AES_KEY is wrong length", func(t *testing.T) {
t.Setenv("SYSTEM_AES_KEY", "too-short")
key := GetAESKey()
assert.Nil(t, key)
})
}
+90
View File
@@ -0,0 +1,90 @@
package utils
import (
"context"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
// CleanupStaleRunningTasks 清理可能残留的running task keys
// 这是一个调试和维护工具,可以用来清理因异常情况导致的残留running keys
func CleanupStaleRunningTasks(ctx context.Context, redisClient *redis.Client, keyPrefix string, maxAge time.Duration) (int, error) {
// 获取所有匹配的keys
keys, err := redisClient.Keys(ctx, keyPrefix+"*").Result()
if err != nil {
return 0, fmt.Errorf("failed to get keys: %w", err)
}
if len(keys) == 0 {
return 0, nil
}
// 检查每个key的TTL
var staleTasks []string
for _, key := range keys {
ttl, err := redisClient.TTL(ctx, key).Result()
if err != nil {
continue // 跳过错误的key
}
// 如果TTL小于0(永不过期)或者剩余时间太长(可能是残留的),标记为stale
if ttl < 0 || ttl > maxAge {
staleTasks = append(staleTasks, key)
}
}
if len(staleTasks) == 0 {
return 0, nil
}
// 删除stale keys
deleted, err := redisClient.Del(ctx, staleTasks...).Result()
if err != nil {
return 0, fmt.Errorf("failed to delete stale keys: %w", err)
}
return int(deleted), nil
}
// CheckRunningTaskStatus 检查指定running task的状态
func CheckRunningTaskStatus(ctx context.Context, redisClient *redis.Client, runningKey, progressKey string) (map[string]interface{}, error) {
result := make(map[string]interface{})
// 检查running key
runningTaskID, err := redisClient.Get(ctx, runningKey).Result()
if err != nil {
if err == redis.Nil {
result["running_task_exists"] = false
} else {
return nil, fmt.Errorf("failed to get running task: %w", err)
}
} else {
result["running_task_exists"] = true
result["running_task_id"] = runningTaskID
// 获取running key的TTL
ttl, _ := redisClient.TTL(ctx, runningKey).Result()
result["running_task_ttl"] = ttl.String()
}
// 检查progress key
progressData, err := redisClient.Get(ctx, progressKey).Result()
if err != nil {
if err == redis.Nil {
result["progress_exists"] = false
} else {
return nil, fmt.Errorf("failed to get progress: %w", err)
}
} else {
result["progress_exists"] = true
result["progress_data"] = progressData
// 获取progress key的TTL
ttl, _ := redisClient.TTL(ctx, progressKey).Result()
result["progress_ttl"] = ttl.String()
}
return result, nil
}
+46
View File
@@ -0,0 +1,46 @@
package utils
import (
"net/http"
"strings"
)
// reservedHeaderKeys 列出不允许被用户自定义头覆盖的关键请求头。
// 这些头由各 provider 的签名、鉴权或 SSE 流程控制,覆盖后可能直接导致调用失败。
var reservedHeaderKeys = map[string]struct{}{
"authorization": {},
"api-key": {},
"x-api-key": {},
"x-goog-api-key": {},
"content-type": {},
"content-length": {},
"accept-encoding": {},
"host": {},
"connection": {},
"transfer-encoding": {},
}
// IsReservedHeader 判断某个 header key 是否为保留 header,保留 header 不允许被自定义头覆盖。
func IsReservedHeader(key string) bool {
_, ok := reservedHeaderKeys[strings.ToLower(strings.TrimSpace(key))]
return ok
}
// ApplyCustomHeaders 将用户自定义的 header 写入 http.Request。
// 保留 header(Authorization、api-key、Content-Type 等)会被跳过以避免破坏鉴权/签名。
// 其它 header 会直接覆盖同名条目,允许用户替换默认值(例如 Accept)。
func ApplyCustomHeaders(req *http.Request, headers map[string]string) {
if req == nil || len(headers) == 0 {
return
}
for k, v := range headers {
name := strings.TrimSpace(k)
if name == "" {
continue
}
if IsReservedHeader(name) {
continue
}
req.Header.Set(name, v)
}
}
+47
View File
@@ -0,0 +1,47 @@
package utils
import (
"net/http"
"testing"
)
func TestApplyCustomHeaders_SkipReserved(t *testing.T) {
req, _ := http.NewRequest("GET", "https://example.com", nil)
req.Header.Set("Authorization", "Bearer original")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-goog-api-key", "google-original")
ApplyCustomHeaders(req, map[string]string{
"Authorization": "Bearer injected",
"Content-Type": "text/plain",
"x-goog-api-key": "google-injected",
"X-Trace-Id": "trace-123",
"X-Route": "edge",
"": "empty-key-should-be-skipped",
})
if got := req.Header.Get("Authorization"); got != "Bearer original" {
t.Fatalf("authorization overwritten: %q", got)
}
if got := req.Header.Get("Content-Type"); got != "application/json" {
t.Fatalf("content-type overwritten: %q", got)
}
if got := req.Header.Get("x-goog-api-key"); got != "google-original" {
t.Fatalf("x-goog-api-key overwritten: %q", got)
}
if got := req.Header.Get("X-Trace-Id"); got != "trace-123" {
t.Fatalf("X-Trace-Id not injected: %q", got)
}
if got := req.Header.Get("X-Route"); got != "edge" {
t.Fatalf("X-Route not injected: %q", got)
}
}
func TestApplyCustomHeaders_NilSafe(t *testing.T) {
ApplyCustomHeaders(nil, map[string]string{"x": "y"})
req, _ := http.NewRequest("GET", "https://example.com", nil)
ApplyCustomHeaders(req, nil)
if len(req.Header) != 0 {
t.Fatalf("unexpected headers added: %+v", req.Header)
}
}
+73
View File
@@ -0,0 +1,73 @@
package utils
import (
"os"
"strconv"
)
const (
defaultMaxFileSizeMB = 50
defaultMaxSkillBundleSizeMB = 256
// maxSkillBundleSizeMBCeiling matches the install-time uncompressed
// archive cap: a download larger than that cannot become a valid skill.
maxSkillBundleSizeMBCeiling = 512
)
// GetMaxFileSize returns the maximum file upload size in bytes.
// Default is 50MB, can be configured via MAX_FILE_SIZE_MB environment variable.
//
// MAX_FILE_SIZE_MB is intentionally a deploy-time-only knob (NOT a
// runtime system_setting). The effective upload limit is gated by
// three other layers that all read this env at startup and cache the
// value:
// - frontend nginx client_max_body_size (envsubst into nginx.conf)
// - docreader gRPC max_send/recv_message_length
// - frontend client-side check via window.__RUNTIME_CONFIG__
//
// Surfacing a SystemAdmin UI knob whose effect is silently capped by
// any of the above would mislead operators ("I raised it to 200MB but
// nginx still returns 413"). Until all four layers can be reconfigured
// in lockstep without container restarts, every call site must read
// the env directly via this helper.
func GetMaxFileSize() int64 {
return GetMaxFileSizeMB() * 1024 * 1024
}
// GetMaxFileSizeMB returns the maximum file upload size in MB. Same
// caveat as GetMaxFileSize — handlers should prefer SystemSettingService.GetInt.
func GetMaxFileSizeMB() int64 {
return envSizeMB("MAX_FILE_SIZE_MB", defaultMaxFileSizeMB)
}
// GetMaxSkillBundleSize is the compressed zip / source-download cap for
// skills. Knowledge uploads stay on GetMaxFileSize: packages such as
// ppt-master exceed 50MB, but raising the document limit would also
// inflate docreader gRPC messages. A GitHub zipball is the whole
// repository archive, not the SKILL.md subtree, so a huge monorepo can
// still be refused here even when the skill itself is small.
func GetMaxSkillBundleSize() int64 {
return GetMaxSkillBundleSizeMB() * 1024 * 1024
}
// GetMaxSkillBundleSizeMB returns the skill zip cap in MB.
// MAX_SKILL_BUNDLE_SIZE_MB defaults to 256, is never below MAX_FILE_SIZE_MB,
// and is capped at 512.
func GetMaxSkillBundleSizeMB() int64 {
skillMB := envSizeMB("MAX_SKILL_BUNDLE_SIZE_MB", defaultMaxSkillBundleSizeMB)
if fileMB := GetMaxFileSizeMB(); skillMB < fileMB {
skillMB = fileMB
}
if skillMB > maxSkillBundleSizeMBCeiling {
return maxSkillBundleSizeMBCeiling
}
return skillMB
}
func envSizeMB(key string, fallback int64) int64 {
if sizeStr := os.Getenv(key); sizeStr != "" {
if size, err := strconv.ParseInt(sizeStr, 10, 64); err == nil && size > 0 {
return size
}
}
return fallback
}
+40
View File
@@ -0,0 +1,40 @@
package utils
import "testing"
func TestGetMaxSkillBundleSizeMB(t *testing.T) {
t.Run("defaults above the knowledge upload cap", func(t *testing.T) {
t.Setenv("MAX_FILE_SIZE_MB", "")
t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "")
if got := GetMaxSkillBundleSizeMB(); got != defaultMaxSkillBundleSizeMB {
t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want %d", got, defaultMaxSkillBundleSizeMB)
}
if GetMaxFileSizeMB() != defaultMaxFileSizeMB {
t.Fatalf("knowledge cap must stay %d MB", defaultMaxFileSizeMB)
}
})
t.Run("honours an explicit skill cap above the knowledge cap", func(t *testing.T) {
t.Setenv("MAX_FILE_SIZE_MB", "50")
t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "300")
if got := GetMaxSkillBundleSizeMB(); got != 300 {
t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want 300", got)
}
})
t.Run("never below the knowledge upload cap", func(t *testing.T) {
t.Setenv("MAX_FILE_SIZE_MB", "200")
t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "64")
if got := GetMaxSkillBundleSizeMB(); got != 200 {
t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want 200", got)
}
})
t.Run("caps at the uncompressed archive ceiling", func(t *testing.T) {
t.Setenv("MAX_FILE_SIZE_MB", "50")
t.Setenv("MAX_SKILL_BUNDLE_SIZE_MB", "4096")
if got := GetMaxSkillBundleSizeMB(); got != maxSkillBundleSizeMBCeiling {
t.Fatalf("GetMaxSkillBundleSizeMB() = %d, want %d", got, maxSkillBundleSizeMBCeiling)
}
})
}
+84
View File
@@ -0,0 +1,84 @@
package utils
import "strings"
// IsActiveBrowserContentExt reports whether a file extension can execute script
// or otherwise participate as active same-origin browser content when served
// inline. User-uploaded files with these extensions must be downloaded instead
// of previewed inline.
func IsActiveBrowserContentExt(ext string) bool {
switch strings.ToLower(ext) {
case ".svg", ".svgz", ".html", ".htm", ".xhtml", ".xml", ".js", ".mjs", ".css":
return true
default:
return false
}
}
// GetContentTypeByExt returns the content type based on file extension
func GetContentTypeByExt(ext string) string {
if IsActiveBrowserContentExt(ext) {
return "application/octet-stream"
}
switch strings.ToLower(ext) {
case ".csv":
return "text/csv; charset=utf-8"
case ".json":
return "application/json"
case ".pdf":
return "application/pdf"
case ".doc":
return "application/msword"
case ".docx":
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
case ".ppt":
return "application/vnd.ms-powerpoint"
case ".pptx":
return "application/vnd.openxmlformats-officedocument.presentationml.presentation"
case ".xls":
return "application/vnd.ms-excel"
case ".xlsx":
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
case ".png":
return "image/png"
case ".jpg", ".jpeg":
return "image/jpeg"
case ".gif":
return "image/gif"
case ".webp":
return "image/webp"
case ".txt":
return "text/plain; charset=utf-8"
case ".mp4":
return "video/mp4"
case ".mp3":
return "audio/mpeg"
case ".wav":
return "audio/wav"
case ".zip":
return "application/zip"
case ".tar":
return "application/x-tar"
case ".gz":
return "application/gzip"
case ".md":
return "text/markdown; charset=utf-8"
default:
return "application/octet-stream"
}
}
// SafeContentTypeByFilename returns a browser-safe content type for serving
// user-controlled files and whether that file may be displayed inline.
func SafeContentTypeByFilename(filename string) (string, bool) {
ext := filename
if idx := strings.LastIndex(ext, "."); idx >= 0 {
ext = ext[idx:]
} else {
ext = ""
}
if IsActiveBrowserContentExt(ext) {
return "application/octet-stream", false
}
return GetContentTypeByExt(ext), true
}
+46
View File
@@ -0,0 +1,46 @@
package utils
import (
"fmt"
"io"
"net/http"
"strings"
"time"
)
var defaultHTTPClient = NewSSRFSafeHTTPClient(SSRFSafeHTTPClientConfig{
Timeout: 60 * time.Second,
MaxRedirects: 10,
})
// DownloadBytes fetches the content at the given HTTP(S) URL and returns the
// raw bytes. It reuses a package-level http.Client with a 60-second timeout.
// Responses exceeding the configured MAX_FILE_SIZE_MB limit are rejected.
func DownloadBytes(url string) ([]byte, error) {
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
return nil, fmt.Errorf("unsupported URL scheme: %s", url)
}
if err := ValidateURLForSSRF(url); err != nil {
return nil, fmt.Errorf("URL rejected by SSRF policy: %w", err)
}
resp, err := defaultHTTPClient.Get(url)
if err != nil {
return nil, fmt.Errorf("HTTP GET: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP %d for %s", resp.StatusCode, url)
}
maxBytes := GetMaxFileSize()
if resp.ContentLength > maxBytes {
return nil, fmt.Errorf("download size exceeds limit of %d bytes (MAX_FILE_SIZE_MB)", maxBytes)
}
data, err := io.ReadAll(io.LimitReader(resp.Body, maxBytes+1))
if err != nil {
return nil, fmt.Errorf("read body: %w", err)
}
if int64(len(data)) > maxBytes {
return nil, fmt.Errorf("download size exceeds limit of %d bytes (MAX_FILE_SIZE_MB)", maxBytes)
}
return data, nil
}
+121
View File
@@ -0,0 +1,121 @@
package utils
import (
"errors"
"io"
"net/http"
"strings"
"testing"
)
type downloadTestTransport func(*http.Request) (*http.Response, error)
func (f downloadTestTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
type downloadTestBody struct {
remaining int64
read int64
closed bool
err error
}
func (b *downloadTestBody) Read(p []byte) (int, error) {
if b.err != nil {
return 0, b.err
}
if b.remaining == 0 {
return 0, io.EOF
}
n := int64(len(p))
if n > b.remaining {
n = b.remaining
}
clear(p[:int(n)])
b.remaining -= n
b.read += n
return int(n), nil
}
func (b *downloadTestBody) Close() error {
b.closed = true
return nil
}
func TestDownloadBytesSizeLimit(t *testing.T) {
const limit = int64(1024 * 1024)
t.Setenv("MAX_FILE_SIZE_MB", "1")
t.Setenv("SSRF_WHITELIST", "download.example")
t.Setenv("SSRF_WHITELIST_EXTRA", "")
ResetSSRFWhitelistForTest()
t.Cleanup(ResetSSRFWhitelistForTest)
originalClient := defaultHTTPClient
t.Cleanup(func() { defaultHTTPClient = originalClient })
tests := []struct {
name string
contentLength int64
size int64
status int
readErr error
wantErr string
wantRead int64
}{
{name: "small body", contentLength: 4, size: 4, wantRead: 4},
{name: "empty body"},
{name: "exact limit", contentLength: limit, size: limit, wantRead: limit},
{name: "exact limit without length", contentLength: -1, size: limit, wantRead: limit},
{name: "oversized content length", contentLength: limit + 1, size: limit + 1, wantErr: "exceeds"},
{
name: "oversized stream without length", contentLength: -1, size: 2 * limit,
wantErr: "exceeds", wantRead: limit + 1,
},
{
name: "understated content length", contentLength: 1, size: 2 * limit,
wantErr: "exceeds", wantRead: limit + 1,
},
{name: "HTTP failure", status: http.StatusNotFound, contentLength: 4, size: 4, wantErr: "HTTP 404"},
{
name: "read failure", contentLength: -1, readErr: errors.New("broken stream"),
wantErr: "read body: broken stream",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body := &downloadTestBody{remaining: tt.size, err: tt.readErr}
status := tt.status
if status == 0 {
status = http.StatusOK
}
transport := downloadTestTransport(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: status,
ContentLength: tt.contentLength,
Body: body,
Header: make(http.Header),
Request: req,
}, nil
})
defaultHTTPClient = &http.Client{Transport: transport}
data, err := DownloadBytes("https://download.example/image.png")
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("error = %v, want %q", err, tt.wantErr)
}
if data != nil {
t.Fatal("failed download returned partial data")
}
} else if err != nil || int64(len(data)) != tt.size {
t.Fatalf("download length = %d, error = %v, want %d bytes", len(data), err, tt.size)
}
if body.read != tt.wantRead {
t.Errorf("read %d bytes, want %d", body.read, tt.wantRead)
}
if !body.closed {
t.Error("response body was not closed")
}
})
}
}
+52
View File
@@ -0,0 +1,52 @@
package utils
import (
"sort"
"strings"
)
// supportedImportFileExtensions is the single source of truth for extensions
// accepted by every knowledge import path: direct upload, file-URL download,
// the worker's post-download re-check and data source connectors. Keeping one
// set avoids the drift that let direct upload accept xlsx while URL import
// rejected it (#2447). Locally registered parser formats are checked against
// this set by docparser's contract test to prevent advertised-but-rejected
// formats (#4598).
var supportedImportFileExtensions = map[string]struct{}{
"pdf": {}, "txt": {}, "docx": {}, "doc": {}, "epub": {},
"docm": {}, "odt": {}, "rtf": {},
"html": {}, "htm": {}, "mhtml": {}, "md": {}, "markdown": {},
"xmind": {},
"png": {}, "jpg": {}, "jpeg": {}, "gif": {},
"bmp": {}, "tiff": {}, "webp": {},
"csv": {}, "xlsx": {}, "xls": {}, "pptx": {}, "ppt": {}, "json": {},
"xlsm": {}, "ods": {}, "pptm": {}, "odp": {},
"mp3": {}, "wav": {}, "m4a": {}, "flac": {}, "ogg": {},
}
// NormalizeImportExtension lowercases an extension and strips surrounding
// whitespace and one leading dot, so callers can pass "xlsx", ".XLSX" or a
// raw user-supplied file_type.
func NormalizeImportExtension(ext string) string {
return strings.ToLower(strings.TrimPrefix(strings.TrimSpace(ext), "."))
}
// IsSupportedImportExtension reports whether a bare extension can be imported.
func IsSupportedImportExtension(ext string) bool {
ext = NormalizeImportExtension(ext)
if ext == "" {
return false
}
_, ok := supportedImportFileExtensions[ext]
return ok
}
// SupportedImportExtensions returns a sorted copy of the supported extensions.
func SupportedImportExtensions() []string {
out := make([]string, 0, len(supportedImportFileExtensions))
for ext := range supportedImportFileExtensions {
out = append(out, ext)
}
sort.Strings(out)
return out
}
+69
View File
@@ -0,0 +1,69 @@
package utils
import (
"sort"
"testing"
)
func TestNormalizeImportExtension(t *testing.T) {
tests := []struct {
ext string
want string
}{
{"pdf", "pdf"},
{".PDF", "pdf"},
{" .XLSX ", "xlsx"},
{".", ""},
{"", ""},
}
for _, tt := range tests {
if got := NormalizeImportExtension(tt.ext); got != tt.want {
t.Errorf("NormalizeImportExtension(%q) = %q, want %q", tt.ext, got, tt.want)
}
}
}
func TestIsSupportedImportExtension(t *testing.T) {
tests := []struct {
ext string
want bool
}{
{"pdf", true},
{" .XLSX ", true},
{"XMIND", true},
{"xlsm", true},
{"docm", true},
{"pptm", true},
{"odt", true},
{"ods", true},
{"odp", true},
{"rtf", true},
{"bmp", true},
{"tiff", true},
{"webp", true},
{"exe", false},
{"mp4", false},
{"unknown", false},
{"", false},
}
for _, tt := range tests {
if got := IsSupportedImportExtension(tt.ext); got != tt.want {
t.Errorf("IsSupportedImportExtension(%q) = %v, want %v", tt.ext, got, tt.want)
}
}
}
func TestSupportedImportExtensions(t *testing.T) {
exts := SupportedImportExtensions()
if len(exts) != len(supportedImportFileExtensions) {
t.Fatalf("SupportedImportExtensions() has %d entries, want %d", len(exts), len(supportedImportFileExtensions))
}
if !sort.StringsAreSorted(exts) {
t.Errorf("SupportedImportExtensions() is not sorted: %v", exts)
}
for _, ext := range exts {
if !IsSupportedImportExtension(ext) {
t.Errorf("SupportedImportExtensions() contains unsupported extension %q", ext)
}
}
}
File diff suppressed because it is too large. Load diff
+205
View File
@@ -0,0 +1,205 @@
package utils
import (
"database/sql"
"reflect"
"strings"
"testing"
_ "github.com/mattn/go-sqlite3"
)
func sharedTenantFixture(t *testing.T) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
db.SetMaxOpenConns(1)
t.Cleanup(func() { _ = db.Close() })
_, err = db.Exec(`
CREATE TABLE knowledge_bases (id TEXT, tenant_id INTEGER, name TEXT);
CREATE TABLE knowledges (id TEXT, tenant_id INTEGER, knowledge_base_id TEXT, title TEXT);
CREATE TABLE chunks (id TEXT, tenant_id INTEGER, knowledge_base_id TEXT, knowledge_id TEXT);
CREATE TABLE knowledge_tag_relations (knowledge_id TEXT, tag_id TEXT);
CREATE TABLE sessions (id TEXT, tenant_id INTEGER);
CREATE TABLE tenants (id INTEGER, name TEXT);
INSERT INTO knowledge_bases VALUES
('own-kb', 1, 'own'), ('shared-kb', 2, 'shared'),
('private-kb', 2, 'private'), ('shared-kb', 1, 'wrong-owner'),
('own-kb', 2, 'swapped-owner');
INSERT INTO knowledges VALUES
('own-doc', 1, 'own-kb', 'own'), ('shared-doc', 2, 'shared-kb', 'shared'),
('private-doc', 2, 'private-kb', 'private'),
('shared-doc', 1, 'shared-kb', 'wrong-owner'),
('own-doc', 2, 'own-kb', 'swapped-owner'),
('other-doc', 2, 'shared-kb', 'outside-document'),
('untagged-doc', 2, 'shared-kb', 'outside-tag');
INSERT INTO chunks VALUES
('own', 1, 'own-kb', 'own-doc'), ('shared', 2, 'shared-kb', 'shared-doc'),
('private', 2, 'private-kb', 'private-doc'),
('wrong-owner', 1, 'shared-kb', 'shared-doc'),
('swapped-owner', 2, 'own-kb', 'own-doc'),
('outside-document', 2, 'shared-kb', 'other-doc'),
('outside-tag', 2, 'shared-kb', 'untagged-doc');
INSERT INTO knowledge_tag_relations VALUES
('own-doc', 'own-tag'), ('shared-doc', 'shared-tag'), ('other-doc', 'shared-tag');
INSERT INTO sessions VALUES ('own-session', 1), ('other-session', 2);
INSERT INTO tenants VALUES (1, 'own-tenant'), (2, 'other-tenant');`)
if err != nil {
t.Fatal(err)
}
return db
}
func assertSharedTenantQuery(t *testing.T, db *sql.DB, query string, want []string, opts ...SQLValidationOption) {
t.Helper()
secured, validation, err := ValidateAndSecureSQL(query, opts...)
if err != nil || !validation.Valid {
t.Fatalf("secure query %q: validation=%+v, err=%v", query, validation, err)
}
rows, err := db.Query(secured)
if err != nil {
t.Fatalf("execute secured SQL %s: %v", secured, err)
}
defer func() { _ = rows.Close() }()
var got []string
for rows.Next() {
var value string
if err := rows.Scan(&value); err != nil {
t.Fatal(err)
}
got = append(got, value)
}
if err := rows.Err(); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("results = %v, want %v; secured SQL: %s", got, want, secured)
}
}
func TestStructuredScopeTenantPairing(t *testing.T) {
db := sharedTenantFixture(t)
scopes := []SearchScope{
{TenantID: 1, KnowledgeBaseID: "own-kb", KnowledgeIDs: []string{"own-doc"}, TagIDs: []string{"own-tag"}},
{
TenantID: 2, KnowledgeBaseID: "shared-kb",
KnowledgeIDs: []string{"shared-doc", "untagged-doc"}, TagIDs: []string{"shared-tag"},
},
}
for _, query := range []string{
"SELECT name FROM knowledge_bases ORDER BY name",
"SELECT k.title FROM knowledges k ORDER BY k.title",
"SELECT c.id FROM chunks c ORDER BY c.id",
`SELECT "Shared Rows".id FROM chunks "Shared Rows" ORDER BY "Shared Rows".id`,
"SELECT c.id FROM chunks c JOIN knowledges k ON k.id = c.knowledge_id ORDER BY c.id",
"SELECT c.id FROM chunks c JOIN chunks d ON c.knowledge_id = d.knowledge_id ORDER BY c.id",
"SELECT c.id FROM chunks c JOIN knowledge_bases kb ON kb.id = c.knowledge_base_id ORDER BY c.id",
"SELECT c.id FROM chunks c WHERE c.id IN (SELECT c.id FROM chunks c) ORDER BY c.id",
} {
t.Run(query, func(t *testing.T) {
assertSharedTenantQuery(t, db, query, []string{"own", "shared"},
WithTenantIsolation(1), WithSearchScopes(scopes))
})
}
}
func TestStructuredScopeTenantTagOnly(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db, "SELECT c.id FROM chunks c ORDER BY c.id", []string{"outside-document", "shared"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{TenantID: 2, KnowledgeBaseID: "shared-kb", TagIDs: []string{"shared-tag"}},
}))
}
func TestStructuredScopeTenantZeroFallback(t *testing.T) {
db := sharedTenantFixture(t)
for _, query := range []string{
"SELECT name FROM knowledge_bases ORDER BY name",
"SELECT title FROM knowledges ORDER BY title",
"SELECT id FROM chunks ORDER BY id",
} {
t.Run(query, func(t *testing.T) {
assertSharedTenantQuery(t, db, query, []string{"own"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{KnowledgeBaseID: "own-kb"},
}))
})
}
}
func TestStructuredScopeInvalidTenantScopesFailClosed(t *testing.T) {
db := sharedTenantFixture(t)
for _, table := range []string{"knowledge_bases", "knowledges", "chunks"} {
t.Run(table, func(t *testing.T) {
assertSharedTenantQuery(t, db, "SELECT id FROM "+table, nil,
WithTenantIsolation(1), WithSearchScopes([]SearchScope{{TenantID: 2}, {}}))
})
}
// Invalid alternatives must not turn an OR restriction into unrestricted access.
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{TenantID: 2}, {TenantID: 1, KnowledgeBaseID: "own-kb"},
}))
}
func TestStructuredScopePreservesOtherTenantTables(t *testing.T) {
db := sharedTenantFixture(t)
opts := []SQLValidationOption{
WithTenantIsolation(1, "chunks", "sessions", "tenants"),
WithSearchScopes([]SearchScope{
{TenantID: 2, KnowledgeBaseID: "shared-kb", KnowledgeIDs: []string{"shared-doc"}},
}),
}
assertSharedTenantQuery(t, db, "SELECT id FROM sessions ORDER BY id", []string{"own-session"}, opts...)
assertSharedTenantQuery(t, db, "SELECT name FROM tenants ORDER BY name", []string{"own-tenant"}, opts...)
assertSharedTenantQuery(t, db, "SELECT c.id || ':' || s.id FROM chunks c CROSS JOIN sessions s",
[]string{"shared:own-session"}, opts...)
}
func TestStructuredScopePreservesLegacyTenantFiltering(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own", "wrong-owner"},
WithTenantIsolation(1), WithSearchScopeFilter([]string{"own-kb", "shared-kb"}, nil))
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own", "wrong-owner"},
WithTenantIsolation(1), WithSearchScopes(nil))
// A zero-tenant scope without tenant injection retains its original optional semantics.
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own", "swapped-owner"},
WithSearchScopes([]SearchScope{{KnowledgeBaseID: "own-kb"}}))
secured, _, err := ValidateAndSecureSQL("SELECT id FROM chunks",
WithSearchScopes([]SearchScope{{KnowledgeBaseID: "own-kb"}}))
if err != nil || strings.Contains(secured, "tenant_id") {
t.Fatalf("optional scope unexpectedly injected tenant filter: %s; error=%v", secured, err)
}
}
// The owner/KB predicate must remain bound to the outer table when a
// user-chosen alias also occurs inside an injected subquery.
func TestStructuredScopeTagAliasRetainsTenantBinding(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db,
"SELECT ktr.id FROM chunks ktr WHERE ktr.knowledge_id = 'shared-doc' ORDER BY ktr.id", []string{"shared"},
WithTenantIsolation(1), WithSearchScopes([]SearchScope{
{TenantID: 2, KnowledgeBaseID: "shared-kb", TagIDs: []string{"shared-tag"}},
}))
}
func TestStructuredScopeCallerZeroRemainsRestricted(t *testing.T) {
db := sharedTenantFixture(t)
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", nil,
WithTenantIsolation(0), WithSearchScopes([]SearchScope{{KnowledgeBaseID: "own-kb"}}))
// Explicitly authorized ownership also constrains a scope used on its own.
assertSharedTenantQuery(t, db, "SELECT id FROM chunks ORDER BY id", []string{"own"},
WithSearchScopes([]SearchScope{{TenantID: 1, KnowledgeBaseID: "own-kb"}}))
}
func TestStructuredScopeStillRejectsCompoundQuery(t *testing.T) {
_, validation, err := ValidateAndSecureSQL(
"SELECT id FROM chunks UNION SELECT id FROM chunks ORDER BY id",
WithTenantIsolation(1), WithSearchScopes([]SearchScope{{TenantID: 2, KnowledgeBaseID: "shared-kb"}}),
)
if err == nil || validation.Valid {
t.Fatal("structured owner scopes must not permit previously rejected compound queries")
}
}
+675
View File
@@ -0,0 +1,675 @@
package utils
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
func TestParseSQL(t *testing.T) {
tests := []struct {
name string
sql string
wantIsSelect bool
wantTables []string
wantSelect []string
wantWhere []string
wantWhereText string
}{
{
name: "Simple SELECT",
sql: "SELECT id, name, age FROM users WHERE age > 18",
wantIsSelect: true,
wantTables: []string{"users"},
wantSelect: []string{"id", "name", "age"},
wantWhere: []string{"age"},
wantWhereText: "age > 18",
},
{
name: "SELECT with multiple WHERE conditions",
sql: "SELECT u.id, u.name FROM users u WHERE u.age > 18 AND u.status = 'active'",
wantIsSelect: true,
wantTables: []string{"users"},
wantSelect: []string{"id", "name"},
wantWhere: []string{"age", "status"},
wantWhereText: "u.age > 18 AND u.status = 'active'",
},
{
name: "SELECT with JOIN",
sql: "SELECT u.name, o.total FROM users u JOIN orders o ON u.id = o.user_id WHERE o.total > 100",
wantIsSelect: true,
wantTables: []string{"users", "orders"},
wantSelect: []string{"name", "total"},
wantWhere: []string{"total"},
wantWhereText: "o.total > 100",
},
{
name: "SELECT with aggregate functions",
sql: "SELECT COUNT(id), AVG(score) FROM students WHERE grade = 'A'",
wantIsSelect: true,
wantTables: []string{"students"},
wantSelect: []string{"id", "score"},
wantWhere: []string{"grade"},
wantWhereText: "grade = 'A'",
},
{
name: "SELECT with complex WHERE",
sql: "SELECT * FROM products WHERE price BETWEEN 10 AND 100 AND category IN ('electronics', 'books')",
wantIsSelect: true,
wantTables: []string{"products"},
wantSelect: []string{},
wantWhere: []string{"price", "category"},
wantWhereText: "price BETWEEN 10 AND 100 AND category IN ('electronics', 'books')",
},
{
name: "INSERT statement",
sql: "INSERT INTO users (name, age) VALUES ('John', 25)",
wantIsSelect: false,
},
{
name: "UPDATE statement",
sql: "UPDATE users SET age = 26 WHERE id = 1",
wantIsSelect: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ParseSQL(tt.sql)
// Print result for debugging
resultJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("\nTest: %s\nResult:\n%s\n", tt.name, string(resultJSON))
if result.IsSelect != tt.wantIsSelect {
t.Errorf("IsSelect = %v, want %v", result.IsSelect, tt.wantIsSelect)
}
if !tt.wantIsSelect {
// For non-SELECT statements, just check IsSelect
return
}
if result.ParseError != "" {
t.Errorf("ParseError = %v, want empty", result.ParseError)
}
// Check tables
if len(result.TableNames) != len(tt.wantTables) {
t.Errorf("TableNames count = %d, want %d. Got: %v, Want: %v",
len(result.TableNames), len(tt.wantTables), result.TableNames, tt.wantTables)
} else {
for i, table := range tt.wantTables {
if i < len(result.TableNames) && result.TableNames[i] != table {
t.Errorf("TableNames[%d] = %v, want %v", i, result.TableNames[i], table)
}
}
}
// Check SELECT fields
if len(result.SelectFields) != len(tt.wantSelect) {
t.Errorf("SelectFields count = %d, want %d. Got: %v, Want: %v",
len(result.SelectFields), len(tt.wantSelect), result.SelectFields, tt.wantSelect)
}
// Check WHERE fields
if len(result.WhereFields) != len(tt.wantWhere) {
t.Errorf("WhereFields count = %d, want %d. Got: %v, Want: %v",
len(result.WhereFields), len(tt.wantWhere), result.WhereFields, tt.wantWhere)
}
// Check WHERE clause text
if result.WhereClause != tt.wantWhereText {
t.Errorf("WhereClause = %q, want %q", result.WhereClause, tt.wantWhereText)
}
})
}
}
func ExampleParseSQL() {
sql := "SELECT id, name, email FROM users WHERE age > 18 AND status = 'active'"
result := ParseSQL(sql)
fmt.Printf("Is SELECT: %v\n", result.IsSelect)
fmt.Printf("Tables: %v\n", result.TableNames)
fmt.Printf("SELECT fields: %v\n", result.SelectFields)
fmt.Printf("WHERE fields: %v\n", result.WhereFields)
fmt.Printf("WHERE clause: %s\n", result.WhereClause)
// Output:
// Is SELECT: true
// Tables: [users]
// SELECT fields: [id name email]
// WHERE fields: [age status]
// WHERE clause: age > 18 AND status = 'active'
}
func TestValidateSQL_TableNames(t *testing.T) {
tests := []struct {
name string
sql string
allowedTables []string
wantValid bool
wantErrorType string
}{
{
name: "Valid table name",
sql: "SELECT * FROM users WHERE id = 1",
allowedTables: []string{"users", "orders"},
wantValid: true,
},
{
name: "Invalid table name",
sql: "SELECT * FROM products WHERE id = 1",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorType: "table_not_allowed",
},
{
name: "Multiple tables - all valid",
sql: "SELECT * FROM users u JOIN orders o ON u.id = o.user_id",
allowedTables: []string{"users", "orders"},
wantValid: true,
},
{
name: "Multiple tables - one invalid",
sql: "SELECT * FROM users u JOIN products p ON u.id = p.user_id",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorType: "table_not_allowed",
},
{
name: "Case insensitive table names",
sql: "SELECT * FROM USERS WHERE id = 1",
allowedTables: []string{"users", "orders"},
wantValid: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql, WithAllowedTables(tt.allowedTables...))
if validation.Valid != tt.wantValid {
t.Errorf("Valid = %v, want %v", validation.Valid, tt.wantValid)
}
if !tt.wantValid && len(validation.Errors) > 0 {
if validation.Errors[0].Type != tt.wantErrorType {
t.Errorf("Error type = %v, want %v", validation.Errors[0].Type, tt.wantErrorType)
}
}
// Print validation result for debugging
if !validation.Valid {
validationJSON, _ := json.MarshalIndent(validation, "", " ")
fmt.Printf("\nTest: %s\nValidation Result:\n%s\n", tt.name, string(validationJSON))
}
})
}
}
func TestValidateSQL_InjectionRisk(t *testing.T) {
tests := []struct {
name string
sql string
wantValid bool
wantErrorType string
description string
}{
{
name: "Normal WHERE clause",
sql: "SELECT * FROM users WHERE age > 18 AND status = 'active'",
wantValid: true,
description: "Should pass normal conditions",
},
{
name: "SQL injection with 1=1",
sql: "SELECT * FROM users WHERE id = 1 OR 1=1",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 1=1 pattern",
},
{
name: "SQL injection with '1'='1'",
sql: "SELECT * FROM users WHERE username = 'admin' OR '1'='1'",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect '1'='1' pattern",
},
{
name: "SQL injection with 0=0",
sql: "SELECT * FROM users WHERE 0=0",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 0=0 pattern",
},
{
name: "SQL injection with true",
sql: "SELECT * FROM users WHERE true",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 'true' pattern",
},
{
name: "SQL injection with empty string comparison",
sql: "SELECT * FROM users WHERE ''=''",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect empty string comparison",
},
{
name: "SQL injection with 1=0",
sql: "SELECT * FROM users WHERE 1=0",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 1=0 pattern",
},
{
name: "SQL injection with false",
sql: "SELECT * FROM users WHERE false",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 'false' pattern",
},
{
name: "Complex injection with AND",
sql: "SELECT * FROM users WHERE username = 'admin' AND 1=1",
wantValid: false,
wantErrorType: "sql_injection_risk",
description: "Should detect 1=1 even with AND",
},
{
name: "Normal comparison with numbers",
sql: "SELECT * FROM users WHERE status_code = 1",
wantValid: true,
description: "Should allow normal number comparisons",
},
{
name: "Normal string comparison",
sql: "SELECT * FROM users WHERE name = 'John'",
wantValid: true,
description: "Should allow normal string comparisons",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql, WithInjectionRiskCheck())
if validation.Valid != tt.wantValid {
t.Errorf("%s: Valid = %v, want %v", tt.description, validation.Valid, tt.wantValid)
}
if !tt.wantValid && len(validation.Errors) > 0 {
found := false
for _, err := range validation.Errors {
if err.Type == tt.wantErrorType {
found = true
break
}
}
if !found {
t.Errorf("%s: Expected error type %v not found in errors", tt.description, tt.wantErrorType)
}
}
// Print validation result for debugging
if !validation.Valid {
validationJSON, _ := json.MarshalIndent(validation, "", " ")
fmt.Printf("\nTest: %s\nValidation Result:\n%s\n", tt.name, string(validationJSON))
}
})
}
}
func TestValidateSQL_CombinedOptions(t *testing.T) {
tests := []struct {
name string
sql string
allowedTables []string
wantValid bool
wantErrorCnt int
}{
{
name: "Valid SQL with both checks",
sql: "SELECT * FROM users WHERE age > 18",
allowedTables: []string{"users", "orders"},
wantValid: true,
wantErrorCnt: 0,
},
{
name: "Invalid table and injection risk",
sql: "SELECT * FROM products WHERE 1=1",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorCnt: 2, // Both table and injection errors
},
{
name: "Valid table but injection risk",
sql: "SELECT * FROM users WHERE id = 1 OR 1=1",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorCnt: 2, // Injection errors
},
{
name: "Invalid table but no injection",
sql: "SELECT * FROM products WHERE age > 18",
allowedTables: []string{"users", "orders"},
wantValid: false,
wantErrorCnt: 1, // Only table error
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql,
WithAllowedTables(tt.allowedTables...),
WithInjectionRiskCheck(),
)
if validation.Valid != tt.wantValid {
t.Errorf("Valid = %v, want %v", validation.Valid, tt.wantValid)
}
if len(validation.Errors) != tt.wantErrorCnt {
t.Errorf("Error count = %d, want %d", len(validation.Errors), tt.wantErrorCnt)
}
// Print validation result for debugging
validationJSON, _ := json.MarshalIndent(validation, "", " ")
fmt.Printf("\nTest: %s\nValidation Result:\n%s\n", tt.name, string(validationJSON))
})
}
}
func ExampleValidateSQL() {
// Example 1: Validate table names
sql1 := "SELECT * FROM users WHERE age > 18"
_, validation1 := ValidateSQL(sql1, WithAllowedTables("users", "orders"))
fmt.Printf("Example 1 - Valid: %v\n", validation1.Valid)
// Example 2: Detect SQL injection
sql2 := "SELECT * FROM users WHERE id = 1 OR 1=1"
_, validation2 := ValidateSQL(sql2, WithInjectionRiskCheck())
fmt.Printf("Example 2 - Valid: %v\n", validation2.Valid)
if !validation2.Valid {
fmt.Printf("Error: %s\n", validation2.Errors[0].Message)
}
// Example 3: Combined validation
sql3 := "SELECT * FROM products WHERE 1=1"
_, validation3 := ValidateSQL(sql3,
WithAllowedTables("users", "orders"),
WithInjectionRiskCheck(),
)
fmt.Printf("Example 3 - Valid: %v, Error count: %d\n", validation3.Valid, len(validation3.Errors))
// Output:
// Example 1 - Valid: true
// Example 2 - Valid: false
// Error: Potential SQL injection risk detected
// Example 3 - Valid: false, Error count: 2
}
func TestInjectAndConditions(t *testing.T) {
tests := []struct {
name string
sql string
filter string
want string
}{
{
name: "existing WHERE with ORDER BY",
sql: "SELECT id, title FROM knowledges WHERE parse_status = 'completed' ORDER BY created_at DESC LIMIT 10",
filter: "knowledges.tenant_id = 123",
want: "SELECT id, title FROM knowledges WHERE knowledges.tenant_id = 123 " +
"AND parse_status = 'completed' ORDER BY created_at DESC LIMIT 10",
},
{
name: "existing WHERE without tail clauses",
sql: "SELECT id FROM knowledges WHERE enable_status = 'enabled'",
filter: "knowledges.deleted_at IS NULL",
want: "SELECT id FROM knowledges WHERE knowledges.deleted_at IS NULL AND enable_status = 'enabled'",
},
{
name: "no WHERE with ORDER BY",
sql: "SELECT id FROM knowledges ORDER BY created_at DESC",
filter: "knowledges.tenant_id = 123",
want: "SELECT id FROM knowledges WHERE knowledges.tenant_id = 123 ORDER BY created_at DESC",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := InjectAndConditions(tt.sql, tt.filter)
if got != tt.want {
t.Fatalf("InjectAndConditions() = %q, want %q", got, tt.want)
}
})
}
}
func TestValidateAndSecureSQL_WithStructuredSearchScopes(t *testing.T) {
securedSQL, validation, err := ValidateAndSecureSQL(
"SELECT id FROM chunks",
WithSearchScopes([]SearchScope{
{KnowledgeBaseID: "kb-full"},
{KnowledgeBaseID: "kb-doc", KnowledgeIDs: []string{"doc-1"}},
{KnowledgeBaseID: "kb-tag", TagIDs: []string{"tag-a", "tag-b"}},
}),
)
if err != nil {
t.Fatalf("ValidateAndSecureSQL() error = %v", err)
}
if !validation.Valid {
t.Fatalf("expected validation to pass, got %#v", validation.Errors)
}
for _, want := range []string{
"chunks.knowledge_base_id = 'kb-full'",
"chunks.knowledge_base_id = 'kb-doc' AND chunks.knowledge_id IN ('doc-1')",
"chunks.knowledge_base_id = 'kb-tag' AND EXISTS",
"knowledge_tag_relations",
"ktr.knowledge_id = chunks.knowledge_id",
"ktr.tag_id IN ('tag-a', 'tag-b')",
" OR ",
} {
if !strings.Contains(securedSQL, want) {
t.Fatalf("secured SQL missing %q:\n%s", want, securedSQL)
}
}
}
func TestValidateAndSecureSQL_WithChunkEnabledFilter(t *testing.T) {
securedSQL, validation, err := ValidateAndSecureSQL(
"SELECT c.id, c.content FROM chunks c WHERE c.chunk_type = 'faq'",
WithChunkEnabledFilter(),
)
if err != nil {
t.Fatalf("ValidateAndSecureSQL() error = %v", err)
}
if !validation.Valid {
t.Fatalf("expected validation to pass, got %#v", validation.Errors)
}
if !strings.Contains(securedSQL, "c.is_enabled = true") {
t.Fatalf("secured SQL must exclude disabled chunks:\n%s", securedSQL)
}
}
// A scope carrying both a document whitelist and a tag filter must apply both.
// Emitting only one of them would admit rows the other excludes.
func TestValidateAndSecureSQL_ScopeCombinesDocumentAndTagFilters(t *testing.T) {
securedSQL, validation, err := ValidateAndSecureSQL(
"SELECT id FROM chunks",
WithSearchScopes([]SearchScope{
{KnowledgeBaseID: "kb-1", KnowledgeIDs: []string{"doc-1"}, TagIDs: []string{"tag-a"}},
}),
)
if err != nil {
t.Fatalf("ValidateAndSecureSQL() error = %v", err)
}
if !validation.Valid {
t.Fatalf("expected validation to pass, got %#v", validation.Errors)
}
for _, want := range []string{
"chunks.knowledge_base_id = 'kb-1'",
"chunks.knowledge_id IN ('doc-1')",
"ktr.tag_id IN ('tag-a')",
} {
if !strings.Contains(securedSQL, want) {
t.Fatalf("secured SQL missing %q:\n%s", want, securedSQL)
}
}
if strings.Contains(securedSQL, " OR ") {
t.Fatalf("a single scope must not be split into alternatives:\n%s", securedSQL)
}
}
// TestValidateSQL_JSONNodeBypass verifies that PG17 SQL/JSON expression nodes
// cannot be used to smuggle dangerous functions past the blacklist. These were
// previously accepted because validateNode had no handler for them and fell
// through to a permissive `return nil`.
func TestValidateSQL_JSONNodeBypass(t *testing.T) {
dangerous := []struct {
name string
sql string
}{
{"JSON_SCALAR + pg_read_file", "SELECT JSON_SCALAR(pg_read_file('/etc/passwd')) AS data FROM knowledge_bases LIMIT 1"},
{"JSON_VALUE + pg_read_file", "SELECT JSON_VALUE(pg_read_file('/etc/passwd'), '$') AS data FROM knowledge_bases LIMIT 1"},
{"JSON_QUERY + pg_read_file", "SELECT JSON_QUERY(pg_read_file('/etc/passwd'), '$') AS data FROM knowledge_bases LIMIT 1"},
{"JSON scalar + lo_export", "SELECT JSON_SCALAR(lo_export(1, '/tmp/x')) FROM knowledge_bases LIMIT 1"},
{"JSON() parse + pg_read_file", "SELECT JSON(pg_read_file('/etc/passwd')) FROM knowledge_bases LIMIT 1"},
{"JSON_SERIALIZE + pg_read_file", "SELECT JSON_SERIALIZE(pg_read_file('/etc/passwd')) FROM knowledge_bases LIMIT 1"},
}
for _, tt := range dangerous {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql, WithSecurityDefaults(10000))
if validation.Valid {
t.Fatalf("expected SQL to be REJECTED, but it was accepted: %s", tt.sql)
}
})
}
}
// TestValidateSQL_UnknownNodeDefaultDeny ensures a legitimate query still
// passes after the default-deny change, guarding against over-blocking.
func TestValidateSQL_DefaultDenyAllowsLegitimate(t *testing.T) {
legit := []string{
"SELECT id, name FROM knowledge_bases WHERE name LIKE '%test%' ORDER BY created_at DESC LIMIT 10",
"SELECT COUNT(*) AS c FROM knowledges WHERE parse_status = 'completed'",
"SELECT COALESCE(title, 'untitled') FROM knowledges LIMIT 5",
"SELECT CASE WHEN file_size > 100 THEN 'big' ELSE 'small' END FROM knowledges LIMIT 5",
"SELECT kb.name, COUNT(k.id) FROM knowledge_bases kb LEFT JOIN knowledges k ON kb.id = k.knowledge_base_id GROUP BY kb.id, kb.name",
}
for _, sql := range legit {
t.Run(sql, func(t *testing.T) {
_, validation := ValidateSQL(sql, WithSecurityDefaults(10000))
if !validation.Valid {
t.Fatalf("expected legitimate SQL to pass, got errors: %#v\nSQL: %s", validation.Errors, sql)
}
})
}
}
// TestValidateSQL_DuckDBSubqueryFileRead verifies that the data_analysis tool's
// validation options (no WithNoSubqueries) still reject file-reading functions
// hidden inside a FROM subquery, as well as at the top level.
func TestValidateSQL_DuckDBFileRead(t *testing.T) {
// Mirror the exact options used by the data_analysis (DuckDB) tool.
opts := []SQLValidationOption{
WithAllowedTables("k_data"),
WithSingleStatement(),
WithNoDangerousFunctions(),
}
dangerous := []struct {
name string
sql string
}{
{"read_text in FROM subquery", "SELECT * FROM (SELECT * FROM read_text('/etc/passwd')) AS f, k_data AS k LIMIT 1"},
{"read_text in target list", "SELECT read_text('/etc/passwd') FROM k_data LIMIT 1"},
{"read_blob in FROM subquery", "SELECT * FROM (SELECT * FROM read_blob('/etc/passwd')) AS f LIMIT 1"},
{"nested subquery read_csv", "SELECT * FROM (SELECT * FROM (SELECT * FROM read_csv('/etc/passwd')) AS a) AS b LIMIT 1"},
}
for _, tt := range dangerous {
t.Run(tt.name, func(t *testing.T) {
_, validation := ValidateSQL(tt.sql, opts...)
if validation.Valid {
t.Fatalf("expected SQL to be REJECTED, but it was accepted: %s", tt.sql)
}
})
}
t.Run("legitimate table query passes", func(t *testing.T) {
_, validation := ValidateSQL("SELECT product, SUM(amount) FROM k_data GROUP BY product", opts...)
if !validation.Valid {
t.Fatalf("expected legitimate DuckDB query to pass, got: %#v", validation.Errors)
}
})
}
func TestValidateSQLScalarCasts(t *testing.T) {
for _, typ := range []string{
"SMALLINT", "INTEGER", "BIGINT", "REAL", "DOUBLE PRECISION", "DECIMAL(12,2)",
"BOOLEAN", "CHAR(5)", "VARCHAR(20)", "TEXT", "DATE", "TIME", "TIME WITH TIME ZONE",
"TIMESTAMP", "TIMESTAMP WITH TIME ZONE", "INTERVAL", "pg_catalog.int4", "pg_catalog.text", "pg_catalog.date",
} {
for _, query := range []string{
"SELECT CAST(amount AS " + typ + ") FROM dataset",
"SELECT amount::" + typ + " FROM dataset",
} {
t.Run(query, func(t *testing.T) {
_, validation := ValidateSQL(query, WithSelectOnly(), WithSingleStatement(),
WithAllowedTables("dataset"), WithNoDangerousFunctions())
if !validation.Valid {
t.Fatalf("safe scalar cast rejected: %v", validation.Errors)
}
})
}
}
}
func TestValidateSQLCastsRetainSecurityChecks(t *testing.T) {
for _, query := range []string{
"SELECT amount::pg_lsn FROM dataset",
"SELECT amount::pg_catalog.pg_lsn FROM dataset",
"SELECT amount::pg_catalog.regclass FROM dataset",
"SELECT amount::pg_catalog.unknown_type FROM dataset",
`SELECT amount::"pg_catalog.int4" FROM dataset`,
`SELECT amount::pg_catalog."INT4" FROM dataset`,
"SELECT CAST(pg_read_file('/etc/passwd') AS INTEGER) FROM dataset",
"SELECT CAST(read_text('/etc/passwd') AS VARCHAR) FROM dataset",
"SELECT CAST((SELECT amount FROM other_table) AS INTEGER) FROM dataset",
"SELECT CAST(amount AS INTEGER) FROM dataset; DELETE FROM dataset",
"SELECT CAST(amount AS NUMERIC(pg_read_file('/etc/passwd'))) FROM dataset",
} {
t.Run(query, func(t *testing.T) {
_, validation := ValidateSQL(query, WithSelectOnly(), WithSingleStatement(),
WithAllowedTables("dataset"), WithNoDangerousFunctions())
if validation.Valid {
t.Fatal("unsafe query accepted")
}
if strings.Contains(query, "NUMERIC(pg_read_file") &&
!strings.Contains(fmt.Sprint(validation.Errors), "function 'pg_read_file' is not allowed") {
t.Fatalf("type modifier must be checked for dangerous functions: %v", validation.Errors)
}
})
}
}
func BenchmarkInjectAndConditions(b *testing.B) {
const sql = "SELECT id, title FROM docs WHERE status = 'active' ORDER BY created_at LIMIT 50"
for i := 0; i < b.N; i++ {
_ = InjectAndConditions(sql, "tenant_id = 1")
}
}
func BenchmarkCheckSQLInjectionRisks(b *testing.B) {
const where = "status = 'active' AND name LIKE '%foo%' AND (deleted_at IS NULL OR archived = false)"
for i := 0; i < b.N; i++ {
_ = checkSQLInjectionRisks(where)
}
}
+35
View File
@@ -0,0 +1,35 @@
package utils
import (
"encoding/json"
"fmt"
jsonschema "github.com/google/jsonschema-go/jsonschema"
)
// ToJSON converts a value to a JSON string
func ToJSON(v interface{}) string {
json, err := json.Marshal(v)
if err != nil {
return ""
}
return string(json)
}
// GenerateSchema generates JSON schema for type T and returns it as a map
// This is optimized to avoid unnecessary serialization/deserialization
func GenerateSchema[T any]() json.RawMessage {
schema, err := jsonschema.For[T](nil)
if err != nil {
panic(fmt.Sprintf("failed to generate schema: %v", err))
}
// Convert schema to map directly through JSON marshaling
// This is necessary because the schema object doesn't expose its internal structure
schemaBytes, err := json.Marshal(schema)
if err != nil {
panic(fmt.Sprintf("failed to marshal schema: %v", err))
}
return schemaBytes
}
+29
View File
@@ -0,0 +1,29 @@
package utils
import (
"regexp"
"strconv"
)
var imageDataURLPatternForLog = regexp.MustCompile(`data:image\/[a-zA-Z0-9.+-]+;base64,[A-Za-z0-9+/=]+`)
const (
defaultMaxLogChars = 12000
defaultMaxDataURLPreview = 96
)
// CompactImageDataURLForLog shortens large image data URLs for log output.
func CompactImageDataURLForLog(raw string) string {
masked := imageDataURLPatternForLog.ReplaceAllStringFunc(raw, func(match string) string {
if len(match) <= defaultMaxDataURLPreview {
return match
}
hidden := len(match) - defaultMaxDataURLPreview
return match[:defaultMaxDataURLPreview] + "...<omitted " + strconv.Itoa(hidden) + " chars>"
})
if len(masked) <= defaultMaxLogChars {
return masked
}
return masked[:defaultMaxLogChars] + "... (truncated, total " + strconv.Itoa(len(masked)) + " chars)"
}
+27
View File
@@ -0,0 +1,27 @@
package utils
import (
"context"
"net"
"sync"
"github.com/go-sql-driver/mysql"
)
// MySQLSSRFNetwork is the mysql driver network name registered by
// RegisterMySQLSSRFDialer. DSNs must set cfg.Net to this value so every
// database/sql open uses SSRFSafeDialContext at the final TCP sink.
const MySQLSSRFNetwork = "tcp-academy-ssrf"
var mysqlSSRFDialerOnce sync.Once
// RegisterMySQLSSRFDialer installs a go-sql-driver/mysql dialer that routes
// connections through SSRFSafeDialContext. Safe to call from multiple packages;
// registration happens at most once per process.
func RegisterMySQLSSRFDialer() {
mysqlSSRFDialerOnce.Do(func() {
mysql.RegisterDialContext(MySQLSSRFNetwork, func(ctx context.Context, addr string) (net.Conn, error) {
return SSRFSafeDialContext(ctx, "tcp", addr)
})
})
}
+106
View File
@@ -0,0 +1,106 @@
package utils
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
)
const oidcStateMaxAge = 10 * time.Minute
// OIDCStatePayload is the signed OIDC authorization state carried in the
// redirect URL and validated on callback.
type OIDCStatePayload struct {
Nonce string `json:"nonce"`
RedirectURI string `json:"redirect_uri,omitempty"`
IssuedAt int64 `json:"iat"`
}
var (
oidcStateSecretOnce sync.Once
oidcStateSecret string
)
func oidcStateSigningKey() string {
oidcStateSecretOnce.Do(func() {
if envSecret := strings.TrimSpace(os.Getenv("JWT_SECRET")); envSecret != "" {
oidcStateSecret = envSecret
return
}
randomBytes := make([]byte, 32)
if _, err := rand.Read(randomBytes); err != nil {
panic(fmt.Sprintf("failed to generate OIDC state signing key: %v", err))
}
oidcStateSecret = base64.StdEncoding.EncodeToString(randomBytes)
})
return oidcStateSecret
}
// SignOIDCState returns a tamper-evident state token: base64url(payload).base64url(hmac).
func SignOIDCState(payload *OIDCStatePayload) (string, error) {
if payload == nil {
return "", errors.New("oidc state payload is required")
}
if strings.TrimSpace(payload.Nonce) == "" {
return "", errors.New("oidc state nonce is required")
}
if strings.TrimSpace(payload.RedirectURI) == "" {
return "", errors.New("oidc state redirect_uri is required")
}
if payload.IssuedAt == 0 {
payload.IssuedAt = time.Now().Unix()
}
raw, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("marshal oidc state: %w", err)
}
mac := hmac.New(sha256.New, []byte(oidcStateSigningKey()))
mac.Write(raw)
sig := mac.Sum(nil)
return base64.RawURLEncoding.EncodeToString(raw) + "." + base64.RawURLEncoding.EncodeToString(sig), nil
}
// VerifyOIDCState validates the HMAC and freshness of a state token.
func VerifyOIDCState(raw string) (*OIDCStatePayload, error) {
raw = strings.TrimSpace(raw)
parts := strings.Split(raw, ".")
if len(parts) != 2 {
return nil, errors.New("invalid oidc state format")
}
payloadBytes, err := base64.RawURLEncoding.DecodeString(parts[0])
if err != nil {
return nil, fmt.Errorf("decode oidc state payload: %w", err)
}
sigBytes, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, fmt.Errorf("decode oidc state signature: %w", err)
}
mac := hmac.New(sha256.New, []byte(oidcStateSigningKey()))
mac.Write(payloadBytes)
if !hmac.Equal(mac.Sum(nil), sigBytes) {
return nil, errors.New("oidc state signature mismatch")
}
var payload OIDCStatePayload
if err := json.Unmarshal(payloadBytes, &payload); err != nil {
return nil, fmt.Errorf("unmarshal oidc state: %w", err)
}
if strings.TrimSpace(payload.RedirectURI) == "" {
return nil, errors.New("state.redirect_uri is required")
}
if payload.IssuedAt == 0 {
return nil, errors.New("state.iat is required")
}
issuedAt := time.Unix(payload.IssuedAt, 0)
if time.Since(issuedAt) > oidcStateMaxAge || time.Until(issuedAt) > time.Minute {
return nil, errors.New("oidc state expired or invalid timestamp")
}
return &payload, nil
}
+50
View File
@@ -0,0 +1,50 @@
package utils
import (
"strings"
"sync"
"testing"
"time"
)
func TestSignAndVerifyOIDCState(t *testing.T) {
t.Setenv("JWT_SECRET", "test-oidc-state-secret")
oidcStateSecretOnce = sync.Once{}
oidcStateSecret = ""
state, err := SignOIDCState(&OIDCStatePayload{
Nonce: "nonce-abc",
RedirectURI: "http://localhost:5173/login",
IssuedAt: time.Now().Unix(),
})
if err != nil {
t.Fatalf("SignOIDCState: %v", err)
}
got, err := VerifyOIDCState(state)
if err != nil {
t.Fatalf("VerifyOIDCState: %v", err)
}
if got.Nonce != "nonce-abc" || got.RedirectURI != "http://localhost:5173/login" {
t.Fatalf("unexpected payload: %+v", got)
}
}
func TestVerifyOIDCStateRejectsTamperedPayload(t *testing.T) {
t.Setenv("JWT_SECRET", "test-oidc-state-secret")
oidcStateSecretOnce = sync.Once{}
oidcStateSecret = ""
state, err := SignOIDCState(&OIDCStatePayload{
Nonce: "nonce-abc",
RedirectURI: "http://localhost:5173/login",
IssuedAt: time.Now().Unix(),
})
if err != nil {
t.Fatalf("SignOIDCState: %v", err)
}
parts := strings.Split(state, ".")
tampered := parts[0] + ".AAAA"
if _, err := VerifyOIDCState(tampered); err == nil {
t.Fatal("expected tampered state to be rejected")
}
}
+226
View File
@@ -0,0 +1,226 @@
package utils
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
)
const (
// presignPath is the URL path for presigned file access.
presignPath = "/api/v1/files/presigned"
// presignDefaultTTL is the default validity period for presigned URLs.
// Kept short because the HMAC key alone authorizes cross-tenant access —
// a leaked URL should expire before it can be widely abused. IM clients
// typically fetch and cache images within seconds of receipt.
presignDefaultTTL = 2 * time.Hour
)
// SystemHMACKey returns the deployment-wide HMAC key derived from
// SYSTEM_SIGNING_KEY (falling back to SYSTEM_AES_KEY for existing deployments),
// or nil when unset, too short, or a known example value.
// Callers must treat nil as "this deployment cannot sign", not as an empty key.
func SystemHMACKey() []byte {
key := os.Getenv("SYSTEM_SIGNING_KEY")
if key == "" {
key = os.Getenv("SYSTEM_AES_KEY")
}
if len(key) < 16 || key == "academy-system-aes-key-32bytes!!" || key == "your-32-byte-long-encryption-key!" {
return nil
}
return []byte(key)
}
// getPresignKey returns the HMAC key derived from SYSTEM_AES_KEY.
// Returns nil if the key is not configured or invalid.
func getPresignKey() []byte {
return SystemHMACKey()
}
// signPayload computes HMAC-SHA256 over the canonical payload string.
func signPayload(key []byte, filePath string, tenantID uint64, expires int64) string {
payload := fmt.Sprintf("file_path=%s&tenant_id=%d&expires=%d", filePath, tenantID, expires)
mac := hmac.New(sha256.New, key)
mac.Write([]byte(payload))
return hex.EncodeToString(mac.Sum(nil))
}
// SignFileURL generates a presigned HTTP URL for accessing a storage file.
// baseURL is the external URL of the Academy instance (e.g. "https://academy.example.com").
// filePath is the provider:// storage path (e.g. "local://1/abc/img.png").
// tenantID identifies the tenant that owns the file.
// ttl is how long the URL remains valid (0 uses the default presignDefaultTTL).
//
// Returns ("", error) if the signing key is not configured.
func SignFileURL(baseURL, filePath string, tenantID uint64, ttl time.Duration) (string, error) {
key := getPresignKey()
if key == nil {
return "", fmt.Errorf("presign: SYSTEM_AES_KEY not configured")
}
if ttl <= 0 {
ttl = presignDefaultTTL
}
expires := time.Now().Add(ttl).Unix()
sig := signPayload(key, filePath, tenantID, expires)
u, err := url.Parse(strings.TrimRight(baseURL, "/") + presignPath)
if err != nil {
return "", fmt.Errorf("presign: invalid base URL: %w", err)
}
q := u.Query()
q.Set("file_path", filePath)
q.Set("tenant_id", strconv.FormatUint(tenantID, 10))
q.Set("expires", strconv.FormatInt(expires, 10))
q.Set("sig", sig)
u.RawQuery = q.Encode()
return u.String(), nil
}
// VerifyFileURLSig checks the HMAC signature and expiry of a presigned URL.
// Returns true only if the signature is valid and the URL has not expired.
func VerifyFileURLSig(filePath string, tenantID uint64, expiresStr, sig string) bool {
key := getPresignKey()
if key == nil {
return false
}
expires, err := strconv.ParseInt(expiresStr, 10, 64)
if err != nil {
return false
}
// Check expiry.
if time.Now().Unix() > expires {
return false
}
// Verify signature.
expected := signPayload(key, filePath, tenantID, expires)
return hmac.Equal([]byte(expected), []byte(sig))
}
// kbScopedExportsSegment is the only storage prefix served by the KB-scoped
// file proxy. Embedded wiki/chunk images land under exports/; raw knowledge
// uploads use {tenant}/{knowledgeID}/... and are served via
// /knowledge/{id}/download instead.
const kbScopedExportsSegment = "exports"
// ValidateStoragePathTenant ensures the tenant segment embedded in a provider://
// storage path matches the authenticated caller's tenant. Cross-tenant access
// for arbitrary tenant paths uses /api/v1/files/presigned with an HMAC bound to
// the resource owner; KB-scoped shared rendering uses ValidateKBScopedStoragePath.
func ValidateStoragePathTenant(filePath string, tenantID uint64) error {
pathTenant := ParseTenantIDFromStoragePath(filePath)
if pathTenant == 0 {
return fmt.Errorf("storage path has no tenant segment")
}
if pathTenant != tenantID {
return fmt.Errorf("storage path workspace mismatch")
}
return nil
}
// ValidateKBScopedStoragePath is used by GET /knowledge-bases/:id/files. It
// requires the path to belong to the KB owner tenant and to live under the
// exports/ namespace used for embedded images (SaveBytes / multimodal output).
// This prevents borrowers with shared-KB read access from using the proxy to
// fetch arbitrary owner-tenant objects such as raw knowledge uploads.
func ValidateKBScopedStoragePath(filePath string, tenantID uint64) error {
if err := ValidateStoragePathTenant(filePath, tenantID); err != nil {
return err
}
if !storagePathHasExportsScope(filePath, tenantID) {
return fmt.Errorf("storage path is outside KB-scoped exports namespace")
}
return nil
}
// storageBackendScheme wraps a provider:// path with the concrete instance id:
// storage://<backendID>/<provider>://... It is duplicated here (rather than
// reusing types.ParseStorageBackendPath) because internal/types already imports
// internal/utils, so a reverse import would create a cycle.
const storageBackendScheme = "storage://"
// unwrapStorageBackendPath strips a leading storage://<backendID>/ wrapper and
// returns the inner provider:// path. Non-wrapped paths are returned unchanged.
// This keeps tenant/exports parsing anchored on the provider path instead of
// relying on the backend id happening not to look like a tenant segment.
func unwrapStorageBackendPath(filePath string) string {
if !strings.HasPrefix(filePath, storageBackendScheme) {
return filePath
}
rest := strings.TrimPrefix(filePath, storageBackendScheme)
parts := strings.SplitN(rest, "/", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return filePath
}
return parts[1]
}
// storagePathHasExportsScope reports whether tenantID appears next to an
// exports segment in either canonical layout:
// - {tenant}/exports/... (local, minio, s3, most cloud backends)
// - exports/{tenant}/... (OSS temp-bucket layout)
func storagePathHasExportsScope(filePath string, tenantID uint64) bool {
_, rest, ok := strings.Cut(unwrapStorageBackendPath(filePath), "://")
if !ok {
return false
}
tenantSeg := strconv.FormatUint(tenantID, 10)
parts := strings.Split(rest, "/")
for i, part := range parts {
if part != tenantSeg {
continue
}
if i+1 < len(parts) && parts[i+1] == kbScopedExportsSegment {
return true
}
if i > 0 && parts[i-1] == kbScopedExportsSegment {
return true
}
}
return false
}
// ParseTenantIDFromStoragePath extracts the tenant ID from a provider:// storage path.
// Storage paths follow the convention: {scheme}://.../{tenantID}/...
// Returns 0 if the path does not contain a valid tenant ID.
//
// NOTE: For cloud providers whose paths embed numeric bucket or region names
// before the tenant segment, the first numeric segment may not be the tenant.
// Callers that have an authoritative resource-owner tenant ID available
// should pass it directly to SignFileURL instead of relying on this parser.
func ParseTenantIDFromStoragePath(filePath string) uint64 {
// Unwrap storage://<backendID>/ so the tenant scan is anchored on the inner
// provider path, not the (opaque) backend id.
filePath = unwrapStorageBackendPath(filePath)
// Strip scheme: "local://1/abc/img.png" → "1/abc/img.png"
_, rest, ok := strings.Cut(filePath, "://")
if !ok {
return 0
}
// Storage path layouts vary by provider:
// local://TENANT_ID/...
// minio://bucket/TENANT_ID/...
// s3://bucket/prefix/TENANT_ID/...
// cos://bucket/region/prefix/TENANT_ID/...
// tos://bucket/TENANT_ID/...
// oss://bucket/prefix/TENANT_ID/...
// We try each slash-separated segment until we find a numeric tenant ID.
parts := strings.Split(rest, "/")
for _, part := range parts {
if id, err := strconv.ParseUint(part, 10, 64); err == nil {
return id
}
}
return 0
}
+163
View File
@@ -0,0 +1,163 @@
package utils
import (
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSignFileURL_RoundTrip(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", "")
t.Setenv("SYSTEM_AES_KEY", "academy-test-aes-key-32bytes!!!")
baseURL := "https://academy.example.com"
filePath := "local://1/abc/img.png"
var tenantID uint64 = 1
signed, err := SignFileURL(baseURL, filePath, tenantID, 1*time.Hour)
require.NoError(t, err)
assert.Contains(t, signed, "https://academy.example.com/api/v1/files/presigned")
assert.Contains(t, signed, "file_path=")
assert.Contains(t, signed, "tenant_id=1")
assert.Contains(t, signed, "sig=")
}
func TestSignFileURL_NoKey(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", "")
t.Setenv("SYSTEM_AES_KEY", "")
_, err := SignFileURL("https://example.com", "local://1/img.png", 1, 0)
require.Error(t, err)
assert.Contains(t, err.Error(), "SYSTEM_AES_KEY")
}
func TestVerifyFileURLSig_Valid(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", "")
t.Setenv("SYSTEM_AES_KEY", "academy-test-aes-key-32bytes!!!")
filePath := "local://42/knowledge/img.jpg"
var tenantID uint64 = 42
key := getPresignKey()
require.NotNil(t, key)
expires := time.Now().Add(1 * time.Hour).Unix()
sig := signPayload(key, filePath, tenantID, expires)
assert.True(t, VerifyFileURLSig(filePath, tenantID, strconv.FormatInt(expires, 10), sig))
}
func TestVerifyFileURLSig_Expired(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", "")
t.Setenv("SYSTEM_AES_KEY", "academy-test-aes-key-32bytes!!!")
filePath := "local://1/img.png"
var tenantID uint64 = 1
key := getPresignKey()
require.NotNil(t, key)
expires := time.Now().Add(-1 * time.Hour).Unix() // already expired
sig := signPayload(key, filePath, tenantID, expires)
assert.False(t, VerifyFileURLSig(filePath, tenantID, strconv.FormatInt(expires, 10), sig))
}
func TestVerifyFileURLSig_Tampered(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", "")
t.Setenv("SYSTEM_AES_KEY", "academy-test-aes-key-32bytes!!!")
filePath := "local://1/img.png"
var tenantID uint64 = 1
key := getPresignKey()
require.NotNil(t, key)
expires := time.Now().Add(1 * time.Hour).Unix()
sig := signPayload(key, filePath, tenantID, expires)
expiresStr := strconv.FormatInt(expires, 10)
// Tamper with file path
assert.False(t, VerifyFileURLSig("local://1/other.png", tenantID, expiresStr, sig))
// Tamper with tenant ID
assert.False(t, VerifyFileURLSig(filePath, 999, expiresStr, sig))
// Tamper with signature
assert.False(t, VerifyFileURLSig(filePath, tenantID, expiresStr, "deadbeef"))
}
func TestVerifyFileURLSig_NoKey(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", "")
t.Setenv("SYSTEM_AES_KEY", "")
assert.False(t, VerifyFileURLSig("local://1/img.png", 1, "99999999999", "abc"))
}
func TestValidateStoragePathTenant(t *testing.T) {
assert.NoError(t, ValidateStoragePathTenant("local://42/knowledge/file.pdf", 42))
assert.Error(t, ValidateStoragePathTenant("local://7/knowledge/file.pdf", 42))
assert.Error(t, ValidateStoragePathTenant("local://docs/example.txt", 42))
}
func TestValidateKBScopedStoragePath(t *testing.T) {
const tenantID uint64 = 10008
assert.NoError(t, ValidateKBScopedStoragePath("local://10008/exports/img.jpg", tenantID))
assert.NoError(t, ValidateKBScopedStoragePath("minio://bucket/10008/exports/uuid.png", tenantID))
assert.NoError(t, ValidateKBScopedStoragePath("oss://bucket/exports/10008/uuid.png", tenantID))
// storage://<backendID>/ wrapped paths must resolve to the same tenant/exports scope.
assert.NoError(t, ValidateKBScopedStoragePath("storage://backend-a/local://10008/exports/img.jpg", tenantID))
assert.NoError(t, ValidateKBScopedStoragePath("storage://backend-a/cos://bucket/ap-test/10008/exports/a.png", tenantID))
assert.Error(t, ValidateKBScopedStoragePath("local://10008/knowledge-id/123.pdf", tenantID))
assert.Error(t, ValidateKBScopedStoragePath("local://9999/exports/img.jpg", tenantID))
assert.Error(t, ValidateKBScopedStoragePath("local://10008/other/img.jpg", tenantID))
assert.Error(t, ValidateKBScopedStoragePath("storage://backend-a/local://9999/exports/img.jpg", tenantID))
}
func TestParseTenantIDFromStoragePath(t *testing.T) {
tests := []struct {
path string
want uint64
}{
{"local://1/abc/img.png", 1},
{"local://42/knowledge/file.pdf", 42},
{"minio://bucket/1/abc/img.png", 1},
{"s3://bucket/academy/1/abc/img.png", 1},
{"cos://bucket/region/prefix/1/abc/img.png", 1},
{"tos://bucket/1/abc/img.png", 1},
{"oss://bucket/academy/1/abc/img.png", 1},
{"https://example.com/img.png", 0},
{"invalid", 0},
{"local://exports/file.csv", 0},
{"storage://backend-a/local://1/abc/img.png", 1},
{"storage://backend-a/cos://bucket/region/prefix/42/abc/img.png", 42},
}
for _, tt := range tests {
t.Run(tt.path, func(t *testing.T) {
got := ParseTenantIDFromStoragePath(tt.path)
assert.Equal(t, tt.want, got, "ParseTenantIDFromStoragePath(%q)", tt.path)
})
}
}
func TestSystemHMACKeyConfiguration(t *testing.T) {
for _, tc := range []struct {
name, signingKey, aesKey, want string
}{
{
"signing key takes precedence", "test-signing-key-32-bytes-long!!!!", "legacy-aes-key-32-bytes-long!!!!!",
"test-signing-key-32-bytes-long!!!!",
},
{"legacy fallback", "", "legacy-aes-key-32-bytes-long!!!!!", "legacy-aes-key-32-bytes-long!!!!!"},
{"invalid explicit key fails closed", "short", "legacy-aes-key-32-bytes-long!!!!!", ""},
{"no keys", "", "", ""},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", tc.signingKey)
t.Setenv("SYSTEM_AES_KEY", tc.aesKey)
require.Equal(t, tc.want, string(SystemHMACKey()))
})
}
}
@@ -0,0 +1,86 @@
package utils
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
pg_query "github.com/pganalyze/pg_query_go/v6"
"github.com/stretchr/testify/require"
)
func TestSQLNestedQueryBoundaries(t *testing.T) {
for _, query := range []string{
"SELECT * FROM (WITH x AS (SELECT * FROM read_text('/tmp/forbidden')) SELECT * FROM x) q, allowed",
"SELECT * FROM (SELECT * FROM allowed UNION SELECT * FROM read_text('/tmp/forbidden')) q, allowed",
"SELECT * FROM (SELECT * FROM victim_table) q, allowed",
"SELECT (SELECT secret FROM victim_table) FROM allowed",
"SELECT * FROM allowed LIMIT (SELECT secret FROM victim_table)",
`SELECT * FROM "ALLOWED"`,
} {
_, validation := ValidateSQL(query, WithSelectOnly(), WithSingleStatement(),
WithAllowedTables("allowed"), WithNoDangerousFunctions())
require.False(t, validation.Valid, query)
}
_, validation := ValidateSQL("SELECT * FROM (SELECT id FROM allowed) q, allowed",
WithSelectOnly(), WithAllowedTables("allowed"), WithNoDangerousFunctions())
require.True(t, validation.Valid, "%+v", validation.Errors)
}
func TestSQLPredicatesCannotBecomeQuotedText(t *testing.T) {
for _, query := range []string{
`SELECT id AS "ORDER BY fake" FROM knowledges`,
`SELECT 'WHERE ORDER BY' AS name FROM knowledges WHERE title = 'WHERE LIMIT' OR title = 'other'`,
`SELECT "ORDER BY".id FROM knowledges AS "ORDER BY"`,
`SELECT a.id FROM knowledges a JOIN knowledges b ON a.id = b.id`,
} {
secured, validation, err := ValidateAndSecureSQL(query, WithSecurityDefaults(7),
WithSoftDeleteFilter(), WithSearchScopeFilter([]string{"kb-1"}, nil))
require.NoError(t, err, "%+v", validation.Errors)
parsed, err := pg_query.Parse(secured)
require.NoError(t, err)
require.NotNil(t, parsed.Stmts[0].Stmt.GetSelectStmt().WhereClause, secured)
require.Contains(t, secured, "tenant_id = 7")
require.Contains(t, secured, "deleted_at IS NULL")
require.Contains(t, secured, "knowledge_base_id IN ('kb-1')")
if strings.Contains(query, "JOIN") {
require.Contains(t, secured, "a.tenant_id = 7")
require.Contains(t, secured, "b.tenant_id = 7")
}
}
require.Empty(t, InjectAndConditions("invalid", "tenant_id = 1"))
}
func TestSSRFCrossOriginRedirect(t *testing.T) {
reached := false
sink := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { reached = true }))
defer sink.Close()
source := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, sink.URL, http.StatusTemporaryRedirect)
}))
defer source.Close()
// Test the redirect policy with a plain local transport; target blocking
// must happen even when both destinations would otherwise be trusted.
client := &http.Client{CheckRedirect: newSSRFCheckRedirect(10)}
_, err := client.Post(source.URL, "application/json", strings.NewReader(`{"client_secret":"synthetic"}`))
require.Error(t, err)
require.False(t, reached)
req, err := http.NewRequest("GET", "https://example.com", nil)
require.NoError(t, err)
req.Header.Set("Custom-Secret-Header", "synthetic")
req.Header.Set("Authorization", "Bearer synthetic")
req.Header.Set("Accept", "application/json")
stripRedirectSensitiveHeaders(req)
require.Empty(t, req.Header.Get("Custom-Secret-Header"))
require.Empty(t, req.Header.Get("Authorization"))
require.Equal(t, "application/json", req.Header.Get("Accept"))
}
func TestKnownExampleKeyCannotSign(t *testing.T) {
t.Setenv("SYSTEM_SIGNING_KEY", "")
t.Setenv("SYSTEM_AES_KEY", "academy-system-aes-key-32bytes!!")
require.Nil(t, SystemHMACKey())
_, err := SignFileURL("https://example.com", "local://1/a", 1, 0)
require.Error(t, err)
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,32 @@
package utils
import (
"testing"
"time"
)
// TestOutboundDialTimeoutDefault verifies the package default is 5s.
// Keep these tests serial: they read and mutate the same process-wide timeout.
func TestOutboundDialTimeoutDefault(t *testing.T) {
if got := OutboundDialTimeout(); got != 5*time.Second {
t.Fatalf("default dial timeout = %v, want %v", got, 5*time.Second)
}
}
// TestSetOutboundDialTimeout verifies the setter is reflected by the getter and
// that non-positive values are ignored.
func TestSetOutboundDialTimeout(t *testing.T) {
original := OutboundDialTimeout()
t.Cleanup(func() { SetOutboundDialTimeout(original) })
SetOutboundDialTimeout(2 * time.Second)
if got := OutboundDialTimeout(); got != 2*time.Second {
t.Fatalf("after set 2s: got %v, want %v", got, 2*time.Second)
}
// Non-positive must be ignored, leaving the previous value intact.
SetOutboundDialTimeout(0)
if got := OutboundDialTimeout(); got != 2*time.Second {
t.Fatalf("after set 0: got %v, want %v (value should be unchanged)", got, 2*time.Second)
}
}
+55
View File
@@ -0,0 +1,55 @@
package utils
import (
"path/filepath"
"testing"
)
func TestSafeJoinUnderBase(t *testing.T) {
base := t.TempDir()
absBase, err := filepath.Abs(base)
if err != nil {
t.Fatal(err)
}
got, err := SafeJoinUnderBase(base, "a/b")
if err != nil {
t.Fatal(err)
}
if want := filepath.Join(absBase, "a", "b"); got != want {
t.Fatalf("nested prefix: got %q want %q", got, want)
}
got, err = SafeJoinUnderBase(base, "foo/bar/..")
if err != nil {
t.Fatal(err)
}
if want := filepath.Join(absBase, "foo"); got != want {
t.Fatalf("cleaned suffix: got %q want %q", got, want)
}
got, err = SafeJoinUnderBase(base, "")
if err != nil {
t.Fatal(err)
}
if got != absBase {
t.Fatalf("empty suffix: got %q want %q", got, absBase)
}
got, err = SafeJoinUnderBase(base, "/abs")
if err != nil {
t.Fatal(err)
}
if want := filepath.Join(absBase, "abs"); got != want {
t.Fatalf("leading slash stripped: got %q want %q", got, want)
}
for _, rel := range []string{"../outside", "a/../../outside", ".."} {
if _, err := SafeJoinUnderBase(base, rel); err == nil {
t.Fatalf("expected traversal error for %q", rel)
}
}
if _, err := SafeJoinUnderBase(" ", "a"); err == nil {
t.Fatal("expected error for empty baseDir")
}
}
+48
View File
@@ -0,0 +1,48 @@
package utils
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestSSRFSafeClientStripsAuthTokenOnCrossHostRedirect(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1,::1,localhost")
ResetSSRFWhitelistForTest()
t.Cleanup(ResetSSRFWhitelistForTest)
var gotToken string
attacker := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotToken = r.Header.Get("X-Auth-Token")
w.WriteHeader(http.StatusOK)
}))
defer attacker.Close()
mux := http.NewServeMux()
mux.HandleFunc("/start", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, attacker.URL, http.StatusFound)
})
origin := httptest.NewServer(mux)
defer origin.Close()
client := NewSSRFSafeHTTPClient(SSRFSafeHTTPClientConfig{
Timeout: 5 * time.Second,
MaxRedirects: 5,
})
req, err := http.NewRequest(http.MethodGet, origin.URL+"/start", nil)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
req.Header.Set("X-Auth-Token", "super-secret-yuque-token")
resp, err := client.Do(req)
if err != nil {
t.Fatalf("Do: %v", err)
}
resp.Body.Close()
if gotToken != "" {
t.Fatalf("X-Auth-Token leaked on cross-host redirect: %q", gotToken)
}
}
+302
View File
@@ -0,0 +1,302 @@
package utils
import (
"net"
"os"
"strings"
"testing"
)
func TestSSRFSafeURL(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",
},
// --- All direct IPs are blocked by isSSRFSafeURL (strict mode) ---
{
name: "direct IPv4 blocked",
rawURL: "https://8.8.8.8/dns-query",
wantOK: false,
wantReasonSub: "direct IP address access is not allowed",
},
{
name: "direct public IPv6 blocked",
rawURL: "https://[2001:4860:4860::8888]/dns-query",
wantOK: false,
wantReasonSub: "direct IP address access is not allowed",
},
{
name: "loopback IPv6 blocked",
rawURL: "https://[::1]/admin",
wantOK: false,
wantReasonSub: "is restricted",
},
{
name: "link-local IPv6 blocked",
rawURL: "https://[fe80::1]/admin",
wantOK: false,
wantReasonSub: "direct IP address access is not allowed",
},
{
name: "ULA IPv6 blocked",
rawURL: "https://[fd12:3456:789a::1]/admin",
wantOK: false,
wantReasonSub: "direct IP address access is not allowed",
},
{
name: "IPv4-mapped IPv6 blocked",
rawURL: "https://[::ffff:127.0.0.1]/admin",
wantOK: false,
wantReasonSub: "direct IP address access is not allowed",
},
// --- IP obfuscation ---
{
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",
},
// --- Port blocking ---
{
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 TestSSRFSafeURL_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)
}
}
// TestValidateURLForSSRF_IPv6Whitelist verifies that whitelisted IPv6 addresses
// bypass the strict IP block in isSSRFSafeURL.
func TestValidateURLForSSRF_IPv6Whitelist(t *testing.T) {
tests := []struct {
name string
whitelist string
rawURL string
wantErr bool
}{
{
name: "exact IPv6 whitelisted",
whitelist: "2001:4860:4860::8888",
rawURL: "https://[2001:4860:4860::8888]/dns-query",
wantErr: false,
},
{
name: "IPv6 CIDR whitelisted",
whitelist: "2001:db8::/32",
rawURL: "https://[2001:db8::1]/page",
wantErr: false,
},
{
name: "IPv6 not in whitelist still blocked",
whitelist: "2001:db8::/32",
rawURL: "https://[2001:4860:4860::8888]/dns-query",
wantErr: true,
},
{
name: "IPv4 whitelisted",
whitelist: "8.8.8.8",
rawURL: "https://8.8.8.8/dns-query",
wantErr: false,
},
{
name: "wildcard domain whitelisted",
whitelist: "*.example.com",
rawURL: "https://api.example.com/v1",
wantErr: false,
},
{
name: "wildcard domain root whitelisted",
whitelist: "*.example.com",
rawURL: "https://example.com/v1",
wantErr: false,
},
{
name: "bare host without scheme normalised",
whitelist: "internal.service",
rawURL: "internal.service:8080/path",
wantErr: false,
},
{
name: "empty whitelist blocks direct IP",
whitelist: "",
rawURL: "https://8.8.8.8/dns-query",
wantErr: true,
},
{
name: "whitelist does not allow non-http scheme",
whitelist: "example.com",
rawURL: "gopher://example.com/_stats",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset whitelist singleton so we can set a new value
ResetSSRFWhitelistForTest()
os.Setenv("SSRF_WHITELIST", tt.whitelist)
defer func() {
os.Unsetenv("SSRF_WHITELIST")
ResetSSRFWhitelistForTest()
}()
err := ValidateURLForSSRF(tt.rawURL)
if (err != nil) != tt.wantErr {
t.Fatalf("ValidateURLForSSRF(%q) with whitelist=%q: err = %v, wantErr = %v",
tt.rawURL, tt.whitelist, err, tt.wantErr)
}
})
}
}
// TestSSRFWhitelistExtraMerge verifies that SSRF_WHITELIST_EXTRA is merged
// into the effective whitelist alongside SSRF_WHITELIST, so deployment-managed
// defaults (e.g. docker-compose injected sidecar host names) survive when an
// operator overrides SSRF_WHITELIST in their .env.
func TestSSRFWhitelistExtraMerge(t *testing.T) {
cases := []struct {
name string
main string
extra string
host string
want bool
}{
{name: "extra only", main: "", extra: "searxng", host: "searxng", want: true},
{name: "main only does not match extra host", main: "internal", extra: "", host: "searxng", want: false},
{name: "both merged - main host", main: "internal", extra: "searxng", host: "internal", want: true},
{name: "both merged - extra host", main: "internal", extra: "searxng", host: "searxng", want: true},
{name: "neither matches unrelated host", main: "internal", extra: "searxng", host: "evil.example", want: false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
ResetSSRFWhitelistForTest()
os.Setenv("SSRF_WHITELIST", tc.main)
os.Setenv("SSRF_WHITELIST_EXTRA", tc.extra)
defer func() {
os.Unsetenv("SSRF_WHITELIST")
os.Unsetenv("SSRF_WHITELIST_EXTRA")
ResetSSRFWhitelistForTest()
}()
if got := IsSSRFWhitelisted(tc.host); got != tc.want {
t.Fatalf("IsSSRFWhitelisted(%q) main=%q extra=%q = %v, want %v",
tc.host, tc.main, tc.extra, got, tc.want)
}
})
}
}
// TestIsRestrictedIP_IPv6 tests IPv6-specific restricted range detection.
func TestIsRestrictedIP_IPv6(t *testing.T) {
t.Parallel()
tests := []struct {
name string
ip string
wantBlock bool
wantReason string
}{
{"loopback", "::1", true, "loopback"},
{"unspecified", "::", true, "unspecified"},
{"link-local", "fe80::1", true, "link-local"},
{"ULA", "fd12:3456:789a::1", true, ""},
{"site-local", "fec0::1", true, "site-local"},
{"Teredo", "2001:0000:4136:e378:8000:63bf:3fff:fdd2", true, "Teredo"},
{"6to4 private", "2002:c0a8:0101::1", true, "6to4"},
{"6to4 public", "2002:0808:0808::1", false, ""},
{"public IPv6", "2001:4860:4860::8888", false, ""},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ip := parseIPForTest(t, tt.ip)
blocked, reason := isRestrictedIP(ip)
if blocked != tt.wantBlock {
t.Fatalf("isRestrictedIP(%s) = %v, want %v (reason: %s)", tt.ip, blocked, tt.wantBlock, reason)
}
if tt.wantReason != "" && !strings.Contains(strings.ToLower(reason), strings.ToLower(tt.wantReason)) {
t.Fatalf("isRestrictedIP(%s) reason = %q, want contains %q", tt.ip, reason, tt.wantReason)
}
})
}
}
func parseIPForTest(t *testing.T, s string) net.IP {
t.Helper()
ip := net.ParseIP(s)
if ip == nil {
t.Fatalf("invalid test IP: %s", s)
}
return ip
}
+99
View File
@@ -0,0 +1,99 @@
package utils
import (
"context"
"fmt"
"net/http"
"strings"
"testing"
"time"
)
type recordingRoundTripper struct {
called bool
}
func (r *recordingRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
r.called = true
return nil, fmt.Errorf("unexpected network call")
}
func TestSSRFSafeClientValidatesInitialRequestAtFinalSink(t *testing.T) {
base := &recordingRoundTripper{}
client := NewSSRFSafeHTTPClientWithTransport(DefaultSSRFSafeHTTPClientConfig(), base)
req, err := http.NewRequest(http.MethodGet, "http://169.254.169.254/latest/meta-data", nil)
if err != nil {
t.Fatal(err)
}
_, err = client.Do(req)
if err == nil || !strings.Contains(err.Error(), "SSRF") {
t.Fatalf("expected final-sink SSRF rejection, got %v", err)
}
if base.called {
t.Fatal("unsafe request reached the base transport")
}
}
func TestSSRFSafeDialContextRejectsRestrictedPortAtFinalSink(t *testing.T) {
_, err := SSRFSafeDialContext(context.Background(), "tcp", "example.com:6379")
if err == nil || !strings.Contains(err.Error(), "port 6379") {
t.Fatalf("expected restricted-port error, got %v", err)
}
}
// TestNewSSRFSafeTransport_SharedAcrossClients verifies that a single transport
// can back multiple clients (global connection pooling) while each client keeps
// its own timeout and a redirect policy.
func TestNewSSRFSafeTransport_SharedAcrossClients(t *testing.T) {
shared := NewSSRFSafeTransport(DefaultSSRFSafeHTTPClientConfig())
cfg := DefaultSSRFSafeHTTPClientConfig()
cfg.Timeout = 15 * time.Second
first := NewSSRFSafeHTTPClientWithTransport(cfg, shared)
cfg.Timeout = 45 * time.Second
second := NewSSRFSafeHTTPClientWithTransport(cfg, shared)
if first == second {
t.Fatal("expected distinct HTTP clients")
}
firstGuard, ok := first.Transport.(*SSRFValidatingRoundTripper)
if !ok {
t.Fatalf("expected SSRF-validating wrapper, got %T", first.Transport)
}
secondGuard, ok := second.Transport.(*SSRFValidatingRoundTripper)
if !ok {
t.Fatalf("expected SSRF-validating wrapper, got %T", second.Transport)
}
if firstGuard.Base != secondGuard.Base || firstGuard.Base != http.RoundTripper(shared) {
t.Fatal("expected clients to share the supplied base transport")
}
if first.Timeout != 15*time.Second {
t.Fatalf("unexpected first timeout: got %v, want %v", first.Timeout, 15*time.Second)
}
if second.Timeout != 45*time.Second {
t.Fatalf("unexpected second timeout: got %v, want %v", second.Timeout, 45*time.Second)
}
if first.CheckRedirect == nil || second.CheckRedirect == nil {
t.Fatal("expected SSRF redirect policy to be set on both clients")
}
}
// TestNewSSRFSafeHTTPClient_HasDedicatedTransport verifies the convenience
// constructor still builds a working transport + redirect policy.
func TestNewSSRFSafeHTTPClient_HasDedicatedTransport(t *testing.T) {
client := NewSSRFSafeHTTPClient(DefaultSSRFSafeHTTPClientConfig())
if client.Transport == nil {
t.Fatal("expected a transport to be set")
}
guard, ok := client.Transport.(*SSRFValidatingRoundTripper)
if !ok {
t.Fatalf("expected SSRF-validating wrapper, got %T", client.Transport)
}
if _, ok := guard.Base.(*http.Transport); !ok {
t.Fatalf("expected *http.Transport base, got %T", guard.Base)
}
if client.CheckRedirect == nil {
t.Fatal("expected SSRF redirect policy to be set")
}
}
@@ -0,0 +1,179 @@
package utils
import (
"context"
"errors"
"net"
"strings"
"testing"
)
// whitelistOnly configures the whitelist and the whitelist-only switch for
// one test and restores the cached whitelist afterwards.
func whitelistOnly(t *testing.T, whitelist, only string) {
t.Helper()
t.Setenv("SSRF_WHITELIST", whitelist)
t.Setenv("SSRF_WHITELIST_EXTRA", "")
t.Setenv("SSRF_DNS_WHITELIST_ONLY", only)
ResetSSRFWhitelistForTest()
t.Cleanup(ResetSSRFWhitelistForTest)
}
func TestCheckSSRFWhitelistOnly(t *testing.T) {
const whitelist = "api.internal,*.corp.example,10.0.0.0/8,203.0.113.5"
cases := []struct {
name string
only string
host string
refused bool
}{
{"off: any domain passes", "false", "outside.invalid", false},
{"off: any IP passes", "false", "198.51.100.7", false},
{"unset counts as off", "", "outside.invalid", false},
{"on: exact name", "true", "api.internal", false},
{"on: exact name, any case", "true", "API.Internal", false},
{"on: wildcard suffix", "true", "svc.corp.example", false},
{"on: wildcard root", "true", "corp.example", false},
{"on: IP in CIDR", "true", "10.20.30.40", false},
{"on: exact IP", "true", "203.0.113.5", false},
{"on: other domain refused", "true", "outside.invalid", true},
{"on: other IP refused", "true", "198.51.100.7", true},
{"on: loopback refused unless listed", "true", "127.0.0.1", true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
whitelistOnly(t, whitelist, tc.only)
err := CheckSSRFWhitelistOnly(tc.host)
if tc.refused {
if !errors.Is(err, ErrSSRFHostNotWhitelisted) {
t.Fatalf("expected ErrSSRFHostNotWhitelisted, got %v", err)
}
if !strings.Contains(err.Error(), tc.host) {
t.Fatalf("error should name the host: %v", err)
}
return
}
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
})
}
}
// The switch is a security control, so a value that is neither true nor false
// must not silently leave it off.
func TestSSRFWhitelistOnlyEnabledParsesLikeTheRestOfTheRepo(t *testing.T) {
cases := map[string]bool{
"": false,
"false": false,
"FALSE": false,
"0": false,
"f": false,
"true": true,
"TRUE": true,
"True": true,
"1": true,
"t": true,
" true ": true,
"yes": true, // unparsable: fail closed rather than silently off
"on": true,
"enabled": true,
}
for raw, want := range cases {
t.Run("value="+raw, func(t *testing.T) {
whitelistOnly(t, "api.internal", raw)
if got := SSRFWhitelistOnlyEnabled(); got != want {
t.Fatalf("SSRF_DNS_WHITELIST_ONLY=%q: enabled=%v, want %v", raw, got, want)
}
})
}
}
// A dialer sees an address, not always a name: gRPC resolves its own target
// before calling the dialer it was given, so a whitelisted service name
// arrives as an IP. Judging names is the dialer's job; judging IP literals is
// the URL layer's.
func TestCheckDialWhitelistOnlyJudgesNamesOnly(t *testing.T) {
whitelistOnly(t, "api.internal", "true")
if err := CheckDialWhitelistOnly("outside.invalid"); !errors.Is(err, ErrSSRFHostNotWhitelisted) {
t.Fatalf("a non-whitelisted name must be refused, got %v", err)
}
if err := CheckDialWhitelistOnly("api.internal"); err != nil {
t.Fatalf("a whitelisted name must pass, got %v", err)
}
for _, ip := range []string{"198.51.100.7", "10.20.30.40", "2001:db8::1"} {
if err := CheckDialWhitelistOnly(ip); err != nil {
t.Fatalf("%s: an address must be left to the address checks, got %v", ip, err)
}
}
}
// With the mode on the refusal comes from the whitelist, before the name is
// ever resolved; with it off the host takes the ordinary path.
func TestValidateURLForSSRF_WhitelistOnlyRefusesBeforeDNS(t *testing.T) {
whitelistOnly(t, "api.internal", "true")
err := ValidateURLForSSRF("https://outside.invalid/v1")
if !errors.Is(err, ErrSSRFHostNotWhitelisted) {
t.Fatalf("expected the whitelist refusal, got %v", err)
}
if strings.Contains(err.Error(), "DNS resolution failed") {
t.Fatalf("the refusal must come before DNS: %v", err)
}
if err := ValidateURLForSSRF("https://api.internal/v1"); err != nil {
t.Fatalf("a whitelisted host must pass: %v", err)
}
whitelistOnly(t, "api.internal", "false")
if err := ValidateURLForSSRF("https://outside.invalid/v1"); errors.Is(err, ErrSSRFHostNotWhitelisted) {
t.Fatalf("with the mode off nothing is refused for being off-whitelist, got %v", err)
}
}
func TestSSRFSafeDialContext_WhitelistOnlyRefusesNamesBeforeDNS(t *testing.T) {
whitelistOnly(t, "api.internal", "true")
_, err := SSRFSafeDialContext(context.Background(), "tcp", "outside.invalid:443")
if !errors.Is(err, ErrSSRFHostNotWhitelisted) {
t.Fatalf("expected the whitelist refusal, got %v", err)
}
// An address reaches the restricted-IP checks instead of this one.
_, err = SSRFSafeDialContext(context.Background(), "tcp", "198.51.100.7:443")
if errors.Is(err, ErrSSRFHostNotWhitelisted) {
t.Fatalf("an address must be left to the address checks, got %v", err)
}
whitelistOnly(t, "api.internal", "false")
_, err = SSRFSafeDialContext(context.Background(), "tcp", "outside.invalid:443")
if errors.Is(err, ErrSSRFHostNotWhitelisted) {
t.Fatalf("with the mode off the name is resolved as before, got %v", err)
}
}
// With CIDR entries the whitelist resolves a hostname to see whether its
// addresses fall inside; whitelist-only mode matches by name only, because
// that lookup is the query the mode exists to prevent.
func TestIsSSRFWhitelisted_WhitelistOnlyMatchesByNameOnly(t *testing.T) {
ips, err := net.LookupIP("localhost")
loopback := false
for _, ip := range ips {
if ip.IsLoopback() {
loopback = true
}
}
if err != nil || !loopback {
t.Skip("localhost does not resolve to a loopback address here; the CIDR fallback cannot be exercised")
}
whitelistOnly(t, "127.0.0.0/8,::1/128,api.internal", "false")
if !IsSSRFWhitelisted("localhost") {
t.Fatal("with the mode off a hostname is matched by what it resolves to")
}
whitelistOnly(t, "127.0.0.0/8,::1/128,api.internal", "true")
if IsSSRFWhitelisted("localhost") {
t.Fatal("whitelist-only mode must match by name, never by resolved address")
}
if !IsSSRFWhitelisted("api.internal") || !IsSSRFWhitelisted("127.0.0.1") {
t.Fatal("name and IP-literal CIDR entries must still match")
}
}
+41
View File
@@ -0,0 +1,41 @@
package utils
import (
"regexp"
"strings"
)
// shellAssignmentPattern finds NAME=value in a model-built shell command.
// The name must be UPPER_SNAKE_CASE so flags like --model and URLs are not
// treated as environment variables.
var shellAssignmentPattern = regexp.MustCompile(
`(?:^|[;|&\s])(?:export\s+)?([A-Z_][A-Z0-9_]{0,127})=(?:"([^"]*)"|'([^']*)'|([^\s;|&]+))`,
)
// MaskCommandAssignments replaces the value of every NAME=value assignment
// with a placeholder. A command is logged at Info, and passing a credential
// inline is a documented way to hand a skill its key, so the raw string must
// never reach the log.
func MaskCommandAssignments(command string) string {
return shellAssignmentPattern.ReplaceAllStringFunc(command, func(match string) string {
eq := strings.Index(match, "=")
if eq < 0 {
return match
}
return match[:eq+1] + "***"
})
}
// ExtractShellAssignments returns NAME=value pairs from a model-built command.
func ExtractShellAssignments(command string) map[string]string {
out := map[string]string{}
for _, match := range shellAssignmentPattern.FindAllStringSubmatch(command, -1) {
name := match[1]
value := match[2] + match[3] + match[4]
if name == "" || value == "" {
continue
}
out[name] = value
}
return out
}
+29
View File
@@ -0,0 +1,29 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMaskCommandAssignmentsHidesValues(t *testing.T) {
command := `export BIGMODEL_API_KEY="sk-abc.def"; TOKEN=plain ./run --model cogview-4`
got := MaskCommandAssignments(command)
assert.NotContains(t, got, "sk-abc.def")
assert.NotContains(t, got, "plain")
assert.Contains(t, got, "BIGMODEL_API_KEY=***")
assert.Contains(t, got, "TOKEN=***")
assert.Contains(t, got, "--model cogview-4")
}
func TestExtractShellAssignmentsFromExportAndPrefix(t *testing.T) {
command := `unset HTTP_PROXY; export BIGMODEL_API_KEY="sk-abc.def"; cd /workspace && ./run`
got := ExtractShellAssignments(command)
require.Equal(t, "sk-abc.def", got["BIGMODEL_API_KEY"])
assert.NotContains(t, got, "HTTP_PROXY")
}
+135
View File
@@ -0,0 +1,135 @@
package utils
import (
"fmt"
"net/url"
"strings"
"sync"
"sync/atomic"
"time"
"golang.org/x/sync/singleflight"
)
// outboundSSRFValidationTTL bounds how long a successful RoundTrip/redirect
// validation for the same origin may be reused. DNS rebinding at the TCP sink
// is still blocked by SSRFSafeDialContext on every new connection.
const outboundSSRFValidationTTL = 60 * time.Second
var (
ssrfOutboundCacheGen atomic.Uint64
ssrfOutboundCache sync.Map // string -> *ssrfOutboundCacheEntry
ssrfOutboundValidateGroup singleflight.Group
ssrfOutboundValidateMisses atomic.Uint64 // test-only counter
)
type ssrfOutboundCacheEntry struct {
err error
expiresAt time.Time
}
// validateURLForSSRFForOutbound validates URLs on the hot outbound path
// (RoundTripper, redirect checks). Results are cached per origin for a short
// TTL so high-frequency clients do not repeat DNS lookups on every request.
// Handler/input boundaries should keep calling ValidateURLForSSRF directly.
func validateURLForSSRFForOutbound(rawURL string) error {
if rawURL == "" {
return nil
}
cacheKey, ok := outboundSSRFCacheKey(rawURL)
if !ok {
return ValidateURLForSSRF(rawURL)
}
now := time.Now()
if cached, ok := ssrfOutboundCache.Load(cacheKey); ok {
entry := cached.(*ssrfOutboundCacheEntry)
if now.Before(entry.expiresAt) {
return entry.err
}
ssrfOutboundCache.Delete(cacheKey)
}
result, err, _ := ssrfOutboundValidateGroup.Do(cacheKey, func() (any, error) {
if cached, ok := ssrfOutboundCache.Load(cacheKey); ok {
entry := cached.(*ssrfOutboundCacheEntry)
if time.Now().Before(entry.expiresAt) {
return entry.err, entry.err
}
}
ssrfOutboundValidateMisses.Add(1)
validationErr := ValidateURLForSSRF(rawURL)
ssrfOutboundCache.Store(cacheKey, &ssrfOutboundCacheEntry{
err: validationErr,
expiresAt: time.Now().Add(outboundSSRFValidationTTL),
})
return validationErr, validationErr
})
if err != nil {
return err
}
if validationErr, ok := result.(error); ok {
return validationErr
}
return nil
}
func outboundSSRFCacheKey(rawURL string) (string, bool) {
normalized := rawURL
if !strings.Contains(normalized, "://") {
normalized = "https://" + normalized
}
parsed, err := url.Parse(normalized)
if err != nil {
return "", false
}
scheme := strings.ToLower(parsed.Scheme)
if scheme != "http" && scheme != "https" {
return "", false
}
host := strings.ToLower(parsed.Hostname())
if host == "" {
return "", false
}
port := parsed.Port()
if port == "" {
if scheme == "https" {
port = "443"
} else {
port = "80"
}
}
var origin string
if strings.Contains(host, ":") {
origin = fmt.Sprintf("%s://[%s]:%s", scheme, host, port)
} else {
origin = fmt.Sprintf("%s://%s:%s", scheme, host, port)
}
return fmt.Sprintf("%d|%s", ssrfOutboundCacheGen.Load(), origin), true
}
func invalidateSSRFOutboundValidationCache() {
ssrfOutboundCacheGen.Add(1)
ssrfOutboundCache = sync.Map{}
ssrfOutboundValidateGroup = singleflight.Group{}
}
// ResetSSRFOutboundValidationCacheForTest clears the outbound validation cache.
// NOT for production use.
func ResetSSRFOutboundValidationCacheForTest() {
invalidateSSRFOutboundValidationCache()
ssrfOutboundValidateMisses.Store(0)
}
// SSRFOutboundValidationMissesForTest returns how many uncached outbound
// validations have run since the last ResetSSRFOutboundValidationCacheForTest.
func SSRFOutboundValidationMissesForTest() uint64 {
return ssrfOutboundValidateMisses.Load()
}
+101
View File
@@ -0,0 +1,101 @@
package utils
import (
"net/http"
"testing"
"time"
)
func TestOutboundSSRFValidationCacheReusesSameOrigin(t *testing.T) {
ResetSSRFWhitelistForTest()
ResetSSRFOutboundValidationCacheForTest()
SetSSRFWhitelistFromRaw("cache-test.example.com")
if err := validateURLForSSRFForOutbound("https://cache-test.example.com/path/a"); err != nil {
t.Fatalf("first validation failed: %v", err)
}
if err := validateURLForSSRFForOutbound("https://cache-test.example.com/path/b"); err != nil {
t.Fatalf("second validation failed: %v", err)
}
if got := SSRFOutboundValidationMissesForTest(); got != 1 {
t.Fatalf("expected 1 cache miss for same origin, got %d", got)
}
}
func TestOutboundSSRFValidationCacheInvalidatesOnWhitelistChange(t *testing.T) {
ResetSSRFWhitelistForTest()
ResetSSRFOutboundValidationCacheForTest()
SetSSRFWhitelistFromRaw("cache-test.example.com")
if err := validateURLForSSRFForOutbound("https://cache-test.example.com/a"); err != nil {
t.Fatalf("initial validation failed: %v", err)
}
if SSRFOutboundValidationMissesForTest() != 1 {
t.Fatalf("expected one miss before whitelist change, got %d", SSRFOutboundValidationMissesForTest())
}
SetSSRFWhitelistFromRaw("cache-test.example.com,other.example.com")
if err := validateURLForSSRFForOutbound("https://cache-test.example.com/b"); err != nil {
t.Fatalf("validation after whitelist change failed: %v", err)
}
if got := SSRFOutboundValidationMissesForTest(); got != 2 {
t.Fatalf("expected cache miss after whitelist bump, got %d misses", got)
}
}
func TestOutboundSSRFValidationCacheExpires(t *testing.T) {
ResetSSRFWhitelistForTest()
ResetSSRFOutboundValidationCacheForTest()
SetSSRFWhitelistFromRaw("cache-test.example.com")
if err := validateURLForSSRFForOutbound("https://cache-test.example.com/a"); err != nil {
t.Fatalf("initial validation failed: %v", err)
}
cacheKey, ok := outboundSSRFCacheKey("https://cache-test.example.com/a")
if !ok {
t.Fatal("expected cache key")
}
raw, ok := ssrfOutboundCache.Load(cacheKey)
if !ok {
t.Fatal("expected cached entry")
}
entry := raw.(*ssrfOutboundCacheEntry)
entry.expiresAt = time.Now().Add(-time.Second)
ssrfOutboundCache.Store(cacheKey, entry)
if err := validateURLForSSRFForOutbound("https://cache-test.example.com/b"); err != nil {
t.Fatalf("validation after expiry failed: %v", err)
}
if got := SSRFOutboundValidationMissesForTest(); got != 2 {
t.Fatalf("expected revalidation after TTL expiry, got %d misses", got)
}
}
func TestSSRFValidatingRoundTripperUsesOutboundCache(t *testing.T) {
ResetSSRFWhitelistForTest()
ResetSSRFOutboundValidationCacheForTest()
SetSSRFWhitelistFromRaw("cache-test.example.com")
base := &recordingRoundTripper{}
rt := &SSRFValidatingRoundTripper{Base: base}
req1, err := http.NewRequest(http.MethodGet, "https://cache-test.example.com/a", nil)
if err != nil {
t.Fatal(err)
}
req2, err := http.NewRequest(http.MethodGet, "https://cache-test.example.com/b", nil)
if err != nil {
t.Fatal(err)
}
if _, err := rt.RoundTrip(req1); err == nil {
t.Fatal("expected base transport error, validation should pass")
}
if _, err := rt.RoundTrip(req2); err == nil {
t.Fatal("expected base transport error, validation should pass")
}
if got := SSRFOutboundValidationMissesForTest(); got != 1 {
t.Fatalf("round tripper expected one outbound validation miss, got %d", got)
}
}
+35
View File
@@ -0,0 +1,35 @@
package utils
import "strings"
// SanitizeStorageConnectivityError converts a raw storage connectivity error
// into a safe, user-facing message. It deliberately avoids echoing the raw
// driver/network error so responses never leak internal hostnames, IPs, ports
// or TLS/certificate details. Callers that need the full error must log it
// server-side instead of returning it to the client.
func SanitizeStorageConnectivityError(err error) string {
if err == nil {
return ""
}
msg := err.Error()
switch {
case strings.Contains(msg, "Endpoint url cannot have fully qualified paths"):
return "Endpoint 地址格式错误:请去除 http:// 或 https:// 前缀,只填写域名或 IP 地址和端口(例如:minio.example.com:9000)"
case strings.Contains(msg, "no such host"):
return "DNS 解析失败,请检查地址是否正确"
case strings.Contains(msg, "connection refused"):
return "连接被拒绝,请确认服务已启动且端口正确"
case strings.Contains(msg, "no route to host"):
return "无法路由到目标地址,请检查网络配置"
case strings.Contains(msg, "i/o timeout") || strings.Contains(msg, "deadline exceeded") || strings.Contains(msg, "context deadline"):
return "连接超时,请检查网络或服务状态"
case strings.Contains(msg, "403") || strings.Contains(msg, "AccessDenied") || strings.Contains(msg, "access denied"):
return "认证失败,请检查访问凭证是否正确"
case strings.Contains(msg, "certificate") || strings.Contains(msg, "tls") || strings.Contains(msg, "x509"):
return "TLS/SSL 证书错误,请检查 SSL 配置"
case strings.Contains(msg, "404") || strings.Contains(msg, "NoSuchBucket"):
return "Bucket 不存在,请检查名称和 Region"
default:
return "连接失败,请检查配置参数是否正确"
}
}
+151
View File
@@ -0,0 +1,151 @@
package utils
import (
"fmt"
"strconv"
"strings"
"time"
"github.com/google/uuid"
)
// GenerateTaskID generates a unique task ID with multiple collision-resistant elements.
// The format is: <taskType>_<tenantID>_<timestamp>_<uuid>_<businessID>
//
// Parameters:
// - taskType: Type of task (e.g., "faq_import", "kb_clone")
// - tenantID: Tenant ID for multi-tenancy isolation
// - businessID: Optional business-specific ID (e.g., knowledge base ID)
//
// Returns a task ID like: "faq_import_12345_1704628851692_a1b2c3d4_kb789"
func GenerateTaskID(taskType string, tenantID uint64, businessID ...string) string {
// Use current timestamp in milliseconds for temporal uniqueness
timestamp := time.Now().UnixMilli()
// Generate a short UUID (first 8 characters for brevity)
shortUUID := strings.ReplaceAll(uuid.New().String()[:8], "-", "")
// Build the task ID components
components := []string{
sanitizeTaskType(taskType),
strconv.FormatUint(tenantID, 10),
strconv.FormatInt(timestamp, 10),
shortUUID,
}
// Add business ID if provided
if len(businessID) > 0 && businessID[0] != "" {
components = append(components, sanitizeBusinessID(businessID[0]))
}
return strings.Join(components, "_")
}
// GenerateTaskIDWithPrefix generates a task ID with a custom prefix.
// This is useful when you want more control over the task ID format.
func GenerateTaskIDWithPrefix(prefix string, tenantID uint64, businessID ...string) string {
timestamp := time.Now().UnixMilli()
shortUUID := strings.ReplaceAll(uuid.New().String()[:8], "-", "")
components := []string{
sanitizeTaskType(prefix),
strconv.FormatUint(tenantID, 10),
strconv.FormatInt(timestamp, 10),
shortUUID,
}
if len(businessID) > 0 && businessID[0] != "" {
components = append(components, sanitizeBusinessID(businessID[0]))
}
return strings.Join(components, "_")
}
const maxTaskIDLen = 128
// ValidateTaskID reports whether taskID is safe to embed in file names and
// Redis keys: non-empty, bounded, and only [A-Za-z0-9_-] (no separators or "..").
func ValidateTaskID(taskID string) error {
if taskID == "" {
return fmt.Errorf("task ID cannot be empty")
}
if len(taskID) > maxTaskIDLen {
return fmt.Errorf("task ID too long")
}
for i := 0; i < len(taskID); i++ {
c := taskID[i]
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-' {
continue
}
return fmt.Errorf("task ID contains invalid character")
}
return nil
}
// ParseTaskID parses a task ID generated by GenerateTaskID and returns its components.
// Returns taskType, tenantID, timestamp, uuid, businessID, and error.
//
// Task types may contain underscores (e.g. "faq_import", "kb_clone"), so the
// parser locates the tenant/timestamp pair rather than assuming parts[0] is
// the full task type.
func ParseTaskID(taskID string) (taskType string, tenantID uint64, timestamp int64, uuidPart string, businessID string, err error) {
parts := strings.Split(taskID, "_")
if len(parts) < 4 {
err = fmt.Errorf("invalid task ID format: %s", taskID)
return
}
tenantIdx := -1
for i := 1; i < len(parts)-2; i++ {
candidateTenant, parseErr := strconv.ParseUint(parts[i], 10, 64)
if parseErr != nil || candidateTenant == 0 {
continue
}
candidateTS, parseErr := strconv.ParseInt(parts[i+1], 10, 64)
if parseErr != nil || candidateTS < 1_000_000_000_000 {
continue
}
tenantID = candidateTenant
timestamp = candidateTS
tenantIdx = i
break
}
if tenantIdx < 0 {
err = fmt.Errorf("invalid task ID format: %s", taskID)
return
}
taskType = strings.Join(parts[:tenantIdx], "_")
uuidPart = parts[tenantIdx+2]
if len(parts) > tenantIdx+3 {
businessID = strings.Join(parts[tenantIdx+3:], "_")
}
return
}
// TaskTenantID extracts the tenant ID embedded in a GenerateTaskID task ID.
func TaskTenantID(taskID string) (uint64, error) {
_, tenantID, _, _, _, err := ParseTaskID(taskID)
return tenantID, err
}
// sanitizeTaskType ensures task type is safe for use in task ID
func sanitizeTaskType(taskType string) string {
// Replace colons and other special characters with underscores
taskType = strings.ReplaceAll(taskType, ":", "_")
taskType = strings.ReplaceAll(taskType, "-", "_")
taskType = strings.ReplaceAll(taskType, " ", "_")
return strings.ToLower(taskType)
}
// sanitizeBusinessID ensures business ID is safe for use in task ID
func sanitizeBusinessID(businessID string) string {
// Take first 12 characters and replace special characters
if len(businessID) > 12 {
businessID = businessID[:12]
}
businessID = strings.ReplaceAll(businessID, "-", "")
businessID = strings.ReplaceAll(businessID, "_", "")
businessID = strings.ReplaceAll(businessID, ":", "")
return businessID
}
+59
View File
@@ -0,0 +1,59 @@
package utils
import (
"strings"
"testing"
)
func TestParseTaskID_CompoundTaskType(t *testing.T) {
taskID := GenerateTaskID("faq_import", 42, "kb-abc-123")
taskType, tenantID, _, uuidPart, businessID, err := ParseTaskID(taskID)
if err != nil {
t.Fatalf("ParseTaskID failed: %v", err)
}
if taskType != "faq_import" {
t.Fatalf("taskType = %q, want faq_import", taskType)
}
if tenantID != 42 {
t.Fatalf("tenantID = %d, want 42", tenantID)
}
if uuidPart == "" {
t.Fatal("expected uuid part")
}
if businessID == "" {
t.Fatal("expected business ID")
}
}
func TestTaskTenantID(t *testing.T) {
taskID := GenerateTaskID("kb_clone", 7, "source-kb")
got, err := TaskTenantID(taskID)
if err != nil {
t.Fatalf("TaskTenantID failed: %v", err)
}
if got != 7 {
t.Fatalf("TaskTenantID = %d, want 7", got)
}
}
func TestValidateTaskID(t *testing.T) {
if err := ValidateTaskID(GenerateTaskID("faq_import", 42, "kb-abc-123")); err != nil {
t.Fatalf("generated task ID should be valid: %v", err)
}
if err := ValidateTaskID("550e8400-e29b-41d4-a716-446655440000"); err != nil {
t.Fatalf("UUID task ID should be valid: %v", err)
}
invalid := []string{
"",
"../outside",
"a/../../etc",
"foo\\bar",
"id with space",
strings.Repeat("a", 129),
}
for _, id := range invalid {
if err := ValidateTaskID(id); err == nil {
t.Fatalf("expected error for %q", id)
}
}
}