From b1fa96c17507741630b7ae8711137687fede6518 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 2 Mar 2026 21:19:11 +0800 Subject: [PATCH] refactor: enhance SSRF safety checks in MinerUCloudReader - Removed hardcoded IP address from blocked hosts in `system.go`. - Integrated SSRF safety checks in `MinerUCloudReader` for base URL and zip URL validation. - Updated HTTP client initialization to use a new utility function for SSRF-safe HTTP clients, improving security during API requests. These changes enhance the security of the MinerUCloudReader by ensuring that potentially unsafe URLs are properly validated before use. --- internal/handler/system.go | 1 - .../docparser/mineru_cloud_converter.go | 32 ++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/internal/handler/system.go b/internal/handler/system.go index ab52d5ee..4a52c9e8 100644 --- a/internal/handler/system.go +++ b/internal/handler/system.go @@ -646,7 +646,6 @@ func isBlockedStorageEndpoint(endpoint string) (bool, string) { "metadata.google.internal", "metadata.tencentyun.com", "metadata.aws.internal", - "169.254.169.254", } for _, bh := range blockedHosts { if hostLower == bh { diff --git a/internal/infrastructure/docparser/mineru_cloud_converter.go b/internal/infrastructure/docparser/mineru_cloud_converter.go index 4171684e..87be8a50 100644 --- a/internal/infrastructure/docparser/mineru_cloud_converter.go +++ b/internal/infrastructure/docparser/mineru_cloud_converter.go @@ -16,6 +16,7 @@ import ( "time" "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/utils" "github.com/google/uuid" ) @@ -39,9 +40,14 @@ type MinerUCloudReader struct { // NewMinerUCloudReader creates a reader from ParserEngineOverrides. func NewMinerUCloudReader(overrides map[string]string) *MinerUCloudReader { + baseURL := strings.TrimRight(stringOr(overrides["mineru_api_base_url"], defaultBaseURL), "/") + if safe, reason := utils.IsSSRFSafeURL(baseURL); !safe { + logger.Printf("WARN: [MinerUCloud] baseURL rejected by SSRF check (%s), falling back to default", reason) + baseURL = defaultBaseURL + } return &MinerUCloudReader{ apiKey: strings.TrimSpace(overrides["mineru_api_key"]), - baseURL: strings.TrimRight(stringOr(overrides["mineru_api_base_url"], defaultBaseURL), "/"), + baseURL: baseURL, model: stringOr(overrides["mineru_cloud_model"], "pipeline"), formulaEnable: parseBoolOr(overrides["mineru_cloud_enable_formula"], true), tableEnable: parseBoolOr(overrides["mineru_cloud_enable_table"], true), @@ -134,7 +140,7 @@ func (c *MinerUCloudReader) applyUploadURLs(ctx context.Context, fileName, ext s httpReq.Header.Set("Authorization", "Bearer "+c.apiKey) httpReq.Header.Set("Content-Type", "application/json") - client := &http.Client{Timeout: 30 * time.Second} + client := utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{Timeout: 30 * time.Second, MaxRedirects: 5}) resp, err := client.Do(httpReq) if err != nil { return "", "", fmt.Errorf("HTTP request: %w", err) @@ -167,7 +173,7 @@ func (c *MinerUCloudReader) uploadFile(ctx context.Context, uploadURL string, co return fmt.Errorf("create PUT request: %w", err) } - client := &http.Client{Timeout: 120 * time.Second} + client := utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{Timeout: 120 * time.Second, MaxRedirects: 5}) resp, err := client.Do(httpReq) if err != nil { return fmt.Errorf("PUT upload: %w", err) @@ -262,7 +268,7 @@ func (c *MinerUCloudReader) fetchBatchStatus(ctx context.Context, batchID string httpReq.Header.Set(k, v) } - client := &http.Client{Timeout: 30 * time.Second} + client := utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{Timeout: 30 * time.Second, MaxRedirects: 5}) resp, err := client.Do(httpReq) if err != nil { return nil, err @@ -344,7 +350,10 @@ func (c *MinerUCloudReader) extractDoneResult(_ context.Context, item *extractRe var imgRefPattern = regexp.MustCompile(`!\[[^\]]*\]\(([^)]+)\)`) func downloadAndExtractZip(zipURL string) (string, []types.ImageRef, error) { - client := &http.Client{Timeout: 120 * time.Second} + if safe, reason := utils.IsSSRFSafeURL(zipURL); !safe { + return "", nil, fmt.Errorf("zip URL blocked by SSRF check: %s", reason) + } + client := utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{Timeout: 120 * time.Second, MaxRedirects: 5}) resp, err := client.Get(zipURL) if err != nil { return "", nil, fmt.Errorf("download zip: %w", err) @@ -485,16 +494,23 @@ func PingMinerUCloud(apiKey, baseURL string) (bool, string) { baseURL = defaultBaseURL } - // Light probe: request an empty batch to verify credentials + targetURL := baseURL + "/file-urls/batch" + if safe, reason := utils.IsSSRFSafeURL(targetURL); !safe { + return false, fmt.Sprintf("baseURL 不安全: %s", reason) + } + payload := []byte(`{"files":[],"model_version":"pipeline"}`) - req, err := http.NewRequest(http.MethodPost, baseURL+"/file-urls/batch", bytes.NewReader(payload)) + req, err := http.NewRequest(http.MethodPost, targetURL, bytes.NewReader(payload)) if err != nil { return false, fmt.Sprintf("构建请求失败: %v", err) } req.Header.Set("Authorization", "Bearer "+apiKey) req.Header.Set("Content-Type", "application/json") - client := &http.Client{Timeout: 10 * time.Second} + client := utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{ + Timeout: 10 * time.Second, + MaxRedirects: 5, + }) resp, err := client.Do(req) if err != nil { return false, fmt.Sprintf("MinerU Cloud 不可达: %v", err)