RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(parser): scope TCADP bearer token to API server host only (#17672)
Taranum Wasu0e860e310f0f
diff --git a/internal/parser/parser/tcadp_common.go b/internal/parser/parser/tcadp_common.goindex a17f5a77ea..28770b3cad 100644--- a/internal/parser/parser/tcadp_common.go+++ b/internal/parser/parser/tcadp_common.go@@ -7,6 +7,7 @@ import ("fmt""io""net/http"+ "net/url""os""ragflow/internal/common""strings"@@ -98,7 +99,7 @@ func parseWithTCADP(if err != nil {return ParseResult{Err: fmt.Errorf("parser: TCADP download request: %w", err)}}- if auth := bearer(apiKey); auth != "" {+ if auth := authHeaderForDownload(baseURL, payload.DocumentRecognizeResultURL, apiKey); auth != "" {downloadReq.Header.Set("Authorization", auth)}downloadResp, err := common.GetSSRFHTTPClient().Do(downloadReq)@@ -119,3 +120,39 @@ func parseWithTCADP(}return pdfItemsToResult(filename, items, outputFormat, pageCount)}++// authHeaderForDownload returns the bearer Authorization header value to+// send with a TCADP result-download request — but only when the download+// URL's scheme and host (case-insensitive) both match the configured API+// server's, and neither is empty.+//+// TCADP result URLs are typically presigned object-storage links+// (S3/CDN) that don't require auth; sending the key to a non-matching+// host (or downgrading from https to http) would leak the credential+// outside our trust boundary if the URL is ever attacker-influenced.+// Returns "" when either URL fails to parse, either Host is empty,+// the schemes differ, or the hosts differ case-insensitively — so a+// parse failure defaults to the safer behaviour.+func authHeaderForDownload(apiBaseURL, downloadURL, apiKey string) string {+ du, err := url.Parse(downloadURL)+ if err != nil {+ return ""+ }+ au, err := url.Parse(apiBaseURL)+ if err != nil {+ return ""+ }+ if du.Scheme == "" || au.Scheme == "" {+ return ""+ }+ if !strings.EqualFold(du.Scheme, au.Scheme) {+ return ""+ }+ if du.Host == "" || au.Host == "" {+ return ""+ }+ if !strings.EqualFold(du.Host, au.Host) {+ return ""+ }+ return bearer(apiKey)+}diff --git a/internal/parser/parser/tcadp_common_test.go b/internal/parser/parser/tcadp_common_test.goindex af83d9aa05..467e128aec 100644--- a/internal/parser/parser/tcadp_common_test.go+++ b/internal/parser/parser/tcadp_common_test.go@@ -53,3 +53,131 @@ func TestParseWithTCADPReturnsEmptyWhenNoEnvVarOrArgSet(t *testing.T) {t.Fatalf("expected empty result when no env var and no arg, got %q", got)}}++// TestAuthHeaderForDownload covers the host-scoping rule for the TCADP+// result-URL bearer token. The token should only travel to a URL whose+// host matches the configured API server's host.+func TestAuthHeaderForDownload(t *testing.T) {+ cases := []struct {+ name string+ apiBase string+ download string+ key string+ wantHeader string // "" means no Authorization header should be set+ }{+ {+ name: "matching host sends bearer",+ apiBase: "https://api.tcadp.example.com",+ download: "https://api.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "Bearer tcadp-secret",+ },+ {+ name: "matching host with trailing slash on base still matches",+ apiBase: "https://api.tcadp.example.com/",+ download: "https://api.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "Bearer tcadp-secret",+ },+ {+ name: "matching host with port",+ apiBase: "https://api.tcadp.example.com:8443",+ download: "https://api.tcadp.example.com:8443/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "Bearer tcadp-secret",+ },+ {+ name: "different host — presigned S3 URL — no auth",+ apiBase: "https://api.tcadp.example.com",+ download: "https://bucket.s3.amazonaws.com/results/abc.zip?X-Amz-Signature=...",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "different host — CDN URL — no auth",+ apiBase: "https://api.tcadp.example.com",+ download: "https://cdn.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "subdomain mismatch — no auth",+ apiBase: "https://api.tcadp.example.com",+ download: "https://malicious.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "unparseable download URL — safe default, no auth",+ apiBase: "https://api.tcadp.example.com",+ download: "ht!tp://not a url",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "unparseable API URL — safe default, no auth",+ apiBase: "ht!tp://not a url",+ download: "https://api.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "empty API key — bearer returns empty, no header",+ apiBase: "https://api.tcadp.example.com",+ download: "https://api.tcadp.example.com/results/abc.zip",+ key: "",+ wantHeader: "",+ },+ {+ name: "scheme mismatch (https base, http download) — no auth",+ apiBase: "https://api.tcadp.example.com",+ download: "http://api.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "schemeless base — no auth",+ apiBase: "api.tcadp.example.com/v1",+ download: "https://api.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "schemeless download URL — no auth",+ apiBase: "https://api.tcadp.example.com",+ download: "api.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "host differs only by letter case — match (case-insensitive)",+ apiBase: "https://API.tcadp.example.com",+ download: "https://api.TCADP.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "Bearer tcadp-secret",+ },+ {+ name: "empty download host — no auth",+ apiBase: "https://api.tcadp.example.com",+ download: "https:///results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ {+ name: "empty API host — no auth",+ apiBase: "https://",+ download: "https://api.tcadp.example.com/results/abc.zip",+ key: "tcadp-secret",+ wantHeader: "",+ },+ }+ for _, tc := range cases {+ t.Run(tc.name, func(t *testing.T) {+ got := authHeaderForDownload(tc.apiBase, tc.download, tc.key)+ if got != tc.wantHeader {+ t.Errorf("authHeaderForDownload(%q, %q, %q) = %q, want %q",+ tc.apiBase, tc.download, tc.key, got, tc.wantHeader)+ }+ })+ }+}