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
feat(parser): hand off Markdown and HTML remote images as media refs (#21764)
jay7772108460530d59e
diff --git a/internal/common/http.go b/internal/common/http.goindex 3d40bfa19d..058e067290 100644--- a/internal/common/http.go+++ b/internal/common/http.go@@ -18,6 +18,7 @@ package commonimport ("bytes"+ "context""encoding/json""fmt""io"@@ -515,6 +516,18 @@ var AssertURLSchemeSafe = func(rawURL string) error {}var AssertURLSafe = func(rawURL string) (hostname, resolvedIP string, err error) {+ return assertURLSafe(rawURL, LookupHost)+}++// AssertURLSafeWithContext validates every resolved address while allowing+// cancellation and deadlines to interrupt DNS resolution.+var AssertURLSafeWithContext = func(ctx context.Context, rawURL string) (hostname, resolvedIP string, err error) {+ return assertURLSafe(rawURL, func(host string) ([]string, error) {+ return net.DefaultResolver.LookupHost(ctx, host)+ })+}++func assertURLSafe(rawURL string, lookup func(string) ([]string, error)) (hostname, resolvedIP string, err error) {parsed, err := url.Parse(strings.TrimSpace(rawURL))if err != nil {return "", "", fmt.Errorf("invalid url")@@ -533,7 +546,7 @@ var AssertURLSafe = func(rawURL string) (hostname, resolvedIP string, err error)}allowAny := allowAnyHost()- addresses, err := LookupHost(hostname)+ addresses, err := lookup(hostname)if err != nil {return "", "", fmt.Errorf("could not resolve hostname '%s': %w", hostname, err)}diff --git a/internal/ingestion/component/chunker/image_upload_test.go b/internal/ingestion/component/chunker/image_upload_test.goindex 78106fe294..794360e076 100644--- a/internal/ingestion/component/chunker/image_upload_test.go+++ b/internal/ingestion/component/chunker/image_upload_test.go@@ -35,6 +35,7 @@ import ("ragflow/internal/ingestion/component/schema"medialib "ragflow/internal/parser/media"parserlib "ragflow/internal/parser/parser"+ "ragflow/internal/utility""gorm.io/gorm")@@ -108,6 +109,70 @@ func TestImageUploadDecorator_ParserUnpaddedImages(t *testing.T) {}}+// TestImageUploadDecorator_MarkdownMediaRefMaterializes pins the ingestion+// seam for Markdown: the parser writes the image bytes to the run media cache+// and emits only a MediaRef, and the chunk uploader resolves that reference+// back to the same bytes.+func TestImageUploadDecorator_MarkdownMediaRefMaterializes(t *testing.T) {+ want, err := base64.StdEncoding.DecodeString(pngBase64)+ if err != nil {+ t.Fatal(err)+ }+ cache := &chunkerMediaCache{}+ ctx := medialib.WithCache(t.Context(), cache)+ p, err := parserlib.GetParser(utility.FileTypeMarkdown)+ if err != nil {+ t.Fatalf("GetParser(FileTypeMarkdown): %v", err)+ }+ parsed := p.ParseWithResult(ctx, "media.md", []byte(""))+ if parsed.Err != nil {+ t.Fatalf("ParseWithResult: %v", parsed.Err)+ }+ items := parsed.JSON+ if len(items) != 1 {+ t.Fatalf("items = %+v, want one image item", items)+ }+ item := items[0].ToMap()+ if entity.MediaRefFromAny(item["media_ref"]) == nil {+ t.Fatalf("parser did not emit a media_ref: %+v", item)+ }+ if _, ok := item["image"]; ok {+ t.Fatalf("parser retained inline image bytes: %+v", item)+ }++ var uploaded []byte+ prev := ChunkImageUploader+ ChunkImageUploader = func(_ context.Context, kbID, chunkID string, data []byte) (string, error) {+ uploaded = append([]byte(nil), data...)+ return kbID + "-" + chunkID, nil+ }+ t.Cleanup(func() { ChunkImageUploader = prev })+ comp, err := NewOneChunker(nil)+ if err != nil {+ t.Fatal(err)+ }+ out, err := (&imageUploadDecorator{inner: comp}).Invoke(ctx, nil, map[string]any{+ "name": "media.md", "kb_id": testKBID, "doc_id": testDocID,+ "output_format": "json", "json": parsed.JSON,+ })+ if err != nil {+ t.Fatalf("markdown media_ref failed at chunk upload: %v", err)+ }+ if !bytes.Equal(uploaded, want) {+ t.Fatalf("uploaded bytes differ from the source PNG: got %d bytes, want %d", len(uploaded), len(want))+ }+ chunks, ok := out["chunks"].([]map[string]any)+ if !ok || len(chunks) != 1 {+ t.Fatalf("expected one chunk, got %#v", out)+ }+ if chunks[0]["img_id"] != testKBID+"-"+common.ChunkID(testDocID, "caption") {+ t.Errorf("unexpected image reference: %v", chunks[0]["img_id"])+ }+ if _, ok := chunks[0]["media_ref"]; ok {+ t.Error("media_ref retained after upload")+ }+}+// TestUploadOneImage_UploadsBytes pins the pure upload contract: it receives// already-decoded bytes (not the raw chunk image field) plus the chunkID, and// returns the img_id — it does NOT touch any chunk map.diff --git a/internal/parser/media/budget.go b/internal/parser/media/budget.goindex 042c6eb57d..07b7226c4b 100644--- a/internal/parser/media/budget.go+++ b/internal/parser/media/budget.go@@ -50,6 +50,10 @@ type Budget struct {oversizedImages inttotalOverflows intcountExhausted bool+ // bytesExhausted records that an unknown-size payload was rejected by the+ // total-byte limit. Later unknown-size sources are rejected before their+ // bytes are read, so a run cannot download past the byte budget.+ bytesExhausted bool}type budgetItem struct {@@ -62,6 +66,9 @@ type budgetItem struct {type Reservation struct {budget *Budgetitem *budgetItem+ // unknown records that the reservation was made without a declared size,+ // so a total-byte rejection here means the byte budget is spent.+ unknown bool}// NewBudget returns a run budget using the current configured limits.@@ -81,6 +88,13 @@ func (b *Budget) Reserve(declared Size) (*Reservation, Admission) {if declared != UnknownSize && declared < 0 {return nil, RejectedInvalid}+ if declared == UnknownSize && b.bytesExhausted {+ // The byte budget is already spent; reject before the caller reads+ // or downloads the payload. The count slot stays available, matching+ // the declared-size rejection below.+ b.noteByteRejection(declared)+ return nil, RejectedByBytes+ }if declared != UnknownSize && !b.canAdmitBytes(declared) {b.noteByteRejection(declared)return nil, RejectedByBytes@@ -91,7 +105,7 @@ func (b *Budget) Reserve(declared Size) (*Reservation, Admission) {}item := &budgetItem{}b.itemsUsed++- return &Reservation{budget: b, item: item}, Admitted+ return &Reservation{budget: b, item: item, unknown: declared == UnknownSize}, Admitted}// ReserveOccurrence admits a source-less payload. The occurrence consumes one@@ -126,6 +140,14 @@ func (r *Reservation) Commit(actual Size) bool {return false}if !r.budget.admitBytes(r.item, actual) {+ // An unknown-size payload that fits the per-image limit but not the+ // run budget spends the budget: later sources of unknown size would+ // only be downloaded to fail the same check. A per-image oversize+ // says nothing about the remaining run budget, so it does not set+ // the flag.+ if r.unknown && int64(actual) <= r.budget.limits.ImageMaxBytes {+ r.budget.bytesExhausted = true+ }return false}r.item.included = truediff --git a/internal/parser/media/budget_test.go b/internal/parser/media/budget_test.goindex e3f482ac34..cde64ea6aa 100644--- a/internal/parser/media/budget_test.go+++ b/internal/parser/media/budget_test.go@@ -182,6 +182,68 @@ func TestBudgetInvalidSizes(t *testing.T) {}}+// TestBudgetUnknownSizeOverflowStopsFurtherReservations pins that once an+// unknown-size payload spends the run byte budget, later unknown-size sources+// are rejected before their bytes are read and do not consume an item slot.+func TestBudgetUnknownSizeOverflowStopsFurtherReservations(t *testing.T) {+ budget := testBudget(4, 8, 5)+ reservation, admission := budget.Reserve(UnknownSize)+ if admission != Admitted {+ t.Fatalf("first reservation = %v, want admitted", admission)+ }+ if reservation.Commit(6) {+ t.Fatal("Commit admitted a payload past the total byte limit")+ }+ before := budget.Count()+ if _, admission := budget.Reserve(UnknownSize); admission != RejectedByBytes {+ t.Fatalf("reservation after byte exhaustion = %v, want byte rejection", admission)+ }+ if budget.Count() != before {+ t.Fatalf("count = %d, want %d (a byte rejection must not consume a slot)", budget.Count(), before)+ }+ if budget.totalOverflows != 2 {+ t.Fatalf("overflows = %d, want 2", budget.totalOverflows)+ }+}++// TestBudgetDeclaredByteRejectionDoesNotSpendRunBudget pins that a rejected+// declared size leaves the byte budget unspent, so a later smaller image is+// still admitted.+func TestBudgetDeclaredByteRejectionDoesNotSpendRunBudget(t *testing.T) {+ budget := testBudget(4, 8, 5)+ if _, admission := budget.Reserve(6); admission != RejectedByBytes {+ t.Fatalf("declared rejection = %v, want byte rejection", admission)+ }+ reservation, admission := budget.Reserve(UnknownSize)+ if admission != Admitted {+ t.Fatalf("small reservation = %v, want admitted", admission)+ }+ if !reservation.Commit(1) {+ t.Fatal("small image should fit after a declared-size rejection")+ }+}++// TestBudgetPerImageOversizeDoesNotSpendRunBudget pins that an unknown-size+// payload larger than the per-image limit does not mark the byte budget as+// spent: later images that fit both limits are still admitted.+func TestBudgetPerImageOversizeDoesNotSpendRunBudget(t *testing.T) {+ budget := testBudget(4, 8, 5)+ reservation, admission := budget.Reserve(UnknownSize)+ if admission != Admitted {+ t.Fatalf("oversized reservation = %v, want admitted", admission)+ }+ if reservation.Commit(9) {+ t.Fatal("Commit admitted a payload past the per-image limit")+ }+ reservation, admission = budget.Reserve(UnknownSize)+ if admission != Admitted {+ t.Fatalf("reservation after an oversized image = %v, want admitted", admission)+ }+ if !reservation.Commit(4) {+ t.Fatal("image within both limits should be admitted after an oversized one")+ }+}+func TestBudgetWarningsAreStable(t *testing.T) {budget := testBudget(3, 4, 5)_, _ = budget.Reserve(5)diff --git a/internal/parser/parser/fetch_image.go b/internal/parser/parser/fetch_image.gonew file mode 100644index 0000000000..78e1c70ece--- /dev/null+++ b/internal/parser/parser/fetch_image.go@@ -0,0 +1,74 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package parser++import (+ "context"+ "fmt"+ "net/http"+ "net/url"+ "strings"+ "time"++ "ragflow/internal/parser/media"+ "ragflow/internal/utility"+)++// isRemoteImageSource reports whether src is an HTTP(S) image source.+func isRemoteImageSource(src string) bool {+ src = strings.TrimSpace(src)+ return strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://")+}++// remoteImageMIME labels downloaded bytes for the run media cache. A response+// that does not identify as an image keeps an opaque label so the image is+// still cached and uploaded rather than silently dropped.+func remoteImageMIME(data []byte) string {+ mime := http.DetectContentType(data)+ if !strings.HasPrefix(mime, "image/") {+ mime = "image/octet-stream"+ }+ return mime+}++// fetchImage fetches a bounded HTTP(S) image payload through the shared+// SSRF-guarded downloader. That downloader validates every hop (private,+// loopback, link-local, CGNAT and metadata addresses are rejected) and pins+// the resolved address for the connection, so redirects cannot reach an+// internal host either.+func fetchImage(ctx context.Context, rawURL string) ([]byte, error) {+ rawURL = strings.TrimSpace(rawURL)+ ctx, cancel := context.WithTimeout(ctx, 30*time.Second)+ defer cancel()++ parsed, err := url.Parse(rawURL)+ if err != nil {+ return nil, fmt.Errorf("image fetch: invalid image URL: %w", err)+ }+ if parsed.User != nil {+ return nil, fmt.Errorf("image fetch: image URL must not carry credentials")+ }++ data, _, _, err := utility.FetchRemoteFileSafelyWithTimeout(ctx, rawURL, int64(media.ImageMaxBytes()), 30*time.Second)+ if err != nil {+ return nil, fmt.Errorf("image fetch: fetch image %s: %w", rawURL, err)+ }+ if len(data) == 0 {+ return nil, fmt.Errorf("image fetch: image %s is empty", rawURL)+ }+ return data, nil+}diff --git a/internal/parser/parser/html_media_test.go b/internal/parser/parser/html_media_test.goindex 9ec4d6eae7..48dc2aac48 100644--- a/internal/parser/parser/html_media_test.go+++ b/internal/parser/parser/html_media_test.go@@ -141,7 +141,7 @@ func TestHTMLParser_EmitsMediaRefsInDocumentOrder(t *testing.T) {}func TestHTMLParser_EmitsTableCellImagesWithParentAndCellOrder(t *testing.T) {- input := `<table><tr><td>A<img alt="first" src="` + htmlMediaDataURI + `"></td><td><img src="https://example.com/second.png"></td></tr></table>`+ input := `<table><tr><td>A<img alt="first" src="` + htmlMediaDataURI + `"></td><td><img src="second.png"></td></tr></table>`ctx, _ := recordingMediaContext()result := NewHTMLParser().ParseWithResult(ctx, "table.html", []byte(input))if result.Err != nil {diff --git a/internal/parser/parser/html_parser.go b/internal/parser/parser/html_parser.goindex 54d385908c..9fe3f56a34 100644--- a/internal/parser/parser/html_parser.go+++ b/internal/parser/parser/html_parser.go@@ -21,6 +21,7 @@ import ("context""fmt""strings"+ "time""golang.org/x/net/html"@@ -30,6 +31,10 @@ import ("ragflow/internal/parser/tableutil")+// htmlImageProcessingBudget bounds the total time one HTML document may spend+// downloading remote images. It is a variable so tests can shorten it.+var htmlImageProcessingBudget = 60 * time.Second+type HTMLParser struct {RemoveHeaderFooter boolRemoveTOC bool@@ -88,13 +93,23 @@ func (p *HTMLParser) ParseWithResult(ctx context.Context, filename string, data}var items []map[string]anystate := &htmlWalkState{- refOnly: true,- ctx: ctx,- mediaCache: media.CacheFromContext(ctx),- budget: media.NewBudget(),+ refOnly: true,+ fetchRemoteImages: true,+ ctx: ctx,+ mediaCache: media.CacheFromContext(ctx),+ budget: media.NewBudget(),}if p.textOnly {- state.resolveImage = func(string) (string, bool) { return "", false }+ // Text-only callers have no run media cache and must not reach the+ // network: every image source is dropped before resolution.+ state.skipImages = true+ } else {+ // Remote downloads are bounded per document. The fetch deadline lives+ // in its own context because state.ctx also scopes the media cache:+ // expiring it would fail every later image write in the document.+ fetchCtx, cancel := context.WithTimeout(ctx, htmlImageProcessingBudget)+ defer cancel()+ state.fetchCtx = fetchCtx}walkHTMLBlocksWithState(doc, &items, state)if state.err != nil {@@ -111,13 +126,18 @@ func (p *HTMLParser) ParseWithResult(ctx context.Context, filename string, dataif items == nil {items = []map[string]any{{"text": "", "doc_type_kwd": "text"}}}+ warnings := state.warnings()+ if state.fetchCtx != nil && state.budget.Count() > 0 &&+ state.fetchCtx.Err() == context.DeadlineExceeded && ctx.Err() == nil {+ warnings = append(warnings, "HTML image processing reached the document time budget")+ }return ParseResult{File: map[string]any{"name": filename,"encoding": encName,},JSON: MapsToItems(items),- Warnings: state.warnings(),+ Warnings: warnings,}}@@ -131,14 +151,22 @@ type htmlWalkState struct {tableSequence intmediaOrder intskippedInlineImages int+ skippedRemoteImages intunresolvedImages intbudget *media.BudgetmediaSink *media.Sink// refOnly defers image bytes to content-addressed references instead of- // carrying the normalized data URI on the item. HTML and EPUB set it;- // formats that still inline image bytes leave it false.- refOnly bool- ctx context.Context+ // carrying the normalized data URI on the item. HTML, EPUB and Markdown+ // ingestion set it; formats that still inline image bytes leave it false.+ refOnly bool+ // fetchRemoteImages downloads HTTP(S) image sources into the run media+ // cache while refOnly is set. The HTML parser enables it; text-only+ // parsing sets skipImages instead so it never reaches the network.+ fetchRemoteImages bool+ ctx context.Context+ // fetchCtx bounds remote image downloads for one document. A nil value+ // falls back to ctx.+ fetchCtx context.ContextmediaCache media.Cache// mediaFormat names the parser family recorded on cache writes. An empty// value means the HTML parser.@@ -161,6 +189,9 @@ func (s *htmlWalkState) warnings() []string {if s.skippedInlineImages > 0 {warnings = append(warnings, fmt.Sprintf("document parser skipped %d invalid or oversized inline image(s)", s.skippedInlineImages))}+ if s.skippedRemoteImages > 0 {+ warnings = append(warnings, fmt.Sprintf("document parser skipped %d remote image(s) that could not be downloaded", s.skippedRemoteImages))+ }if s.unresolvedImages > 0 {warnings = append(warnings, fmt.Sprintf("document parser skipped %d unresolved image reference(s)", s.unresolvedImages))}@@ -186,34 +217,29 @@ func (s *htmlWalkState) imageSource(src string) (string, *entity.MediaRef, bool)if ref, ok := s.resolveMedia(src); ok {return "", ref, true}+ if s.err != nil {+ // The resolver already recorded a fatal cache failure; running the+ // refOnly data-URI fallback would write the same source again.+ return "", nil, false+ }}if s.resolveImage != nil {normalized, ok := s.resolveImage(src)return normalized, nil, ok}if !media.IsDataImageSource(src) {+ if s.refOnly && s.fetchRemoteImages && isRemoteImageSource(src) {+ if ref, ok := s.resolveRemoteMedia(s.remoteFetchContext(), src); ok {+ return "", ref, true+ }+ }return "", nil, false}if s.refOnly {- sink := s.mediaOutput()- key := "data:" + src- if ref, seen := sink.Lookup(key); seen {- return "", ref, ref != nil- }- size, ok := media.DataImageDecodedSize(src)- if !ok {+ ref, invalid := s.storeDataURIMedia("data:"+src, src)+ if invalid {s.skippedInlineImages++- sink.Skip(key)- return "", nil, false}- ref, err := sink.Resolve(key, media.Size(size), func() (string, []byte, bool) {- mime, data, ok := media.DecodeDataImageSource(src)- if !ok {- s.skippedInlineImages++- }- return mime, data, ok- })- s.err = errreturn "", ref, ref != nil}reservation, admission := s.budget.ReserveOccurrence(media.UnknownSize)@@ -231,7 +257,76 @@ func (s *htmlWalkState) imageSource(src string) (string, *entity.MediaRef, bool)return normalized, nil, true}-// mediaOutput shares source admission and writes across data URIs and EPUB entries.+// storeDataURIMedia writes one data-URI image source to the run media cache.+// A repeated source reuses the first outcome without touching the budget or+// the cache again. It reports an invalid or oversized payload so each parser+// can count its own warning.+func (s *htmlWalkState) storeDataURIMedia(key, src string) (*entity.MediaRef, bool) {+ sink := s.mediaOutput()+ if ref, seen := sink.Lookup(key); seen {+ return ref, false+ }+ size, ok := media.DataImageDecodedSize(src)+ if !ok {+ sink.Skip(key)+ return nil, true+ }+ invalid := false+ ref, err := sink.Resolve(key, media.Size(size), func() (string, []byte, bool) {+ mime, data, ok := media.DecodeDataImageSource(src)+ invalid = !ok+ return mime, data, ok+ })+ if err != nil {+ s.err = err+ }+ return ref, invalid+}++// resolveRemoteMedia downloads one HTTP(S) image into the run media cache.+// fetchCtx bounds new downloads; cached sources remain reusable after its+// deadline. A failed download is counted once for the+// parser warning. A cache or task-context failure is returned to the caller+// through the walk state.+func (s *htmlWalkState) resolveRemoteMedia(fetchCtx context.Context, src string) (*entity.MediaRef, bool) {+ src = strings.TrimSpace(src)+ if err := s.ctx.Err(); err != nil {+ s.err = err+ return nil, false+ }+ if ref, seen := s.mediaOutput().Lookup(src); seen {+ return ref, ref != nil+ }+ if fetchCtx.Err() != nil {+ return nil, false+ }+ ref, err := s.mediaOutput().Resolve(src, media.UnknownSize, func() (string, []byte, bool) {+ data, err := fetchImage(fetchCtx, src)+ if err != nil {+ if fetchCtx.Err() == nil {+ s.skippedRemoteImages+++ }+ return "", nil, false+ }+ return remoteImageMIME(data), data, true+ })+ if err != nil {+ s.err = err+ }+ return ref, ref != nil+}++// remoteFetchContext is the context that bounds remote downloads: the+// document fetch budget when one is installed, otherwise the walk context.+func (s *htmlWalkState) remoteFetchContext() context.Context {+ if s.fetchCtx != nil {+ return s.fetchCtx+ }+ return s.ctx+}++// mediaOutput shares source admission and writes across data URIs, remote+// downloads and EPUB entries.func (s *htmlWalkState) mediaOutput() *media.Sink {if s.mediaSink == nil {s.mediaSink = media.NewSink(media.WithCache(s.ctx, s.mediaCache), s.cacheFormat(), s.budget)@@ -500,7 +595,7 @@ func walkHTMLLeaf(n *html.Node, w *leafWriter, out *[]map[string]any, layout strsrc := htmlAttribute(n, "src")normalized, ref, ok := state.imageSource(src)if !ok {- if state.resolveImage != nil {+ if state.resolveImage != nil || state.resolveMedia != nil || state.skipImages {w.writeText(htmlAttribute(n, "alt"))}return@@ -618,7 +713,7 @@ func htmlTableImages(table *html.Node, state *htmlWalkState) []htmlTableImage {if state.err != nil {return}- if state.resolveImage != nil {+ if state.resolveImage != nil || state.resolveMedia != nil || state.skipImages {// ParseTableHTML's cellText ignores <img>, so replace// an unresolved table image with its Markdown reference.alt := htmlAttribute(node, "alt")diff --git a/internal/parser/parser/html_remote_media_test.go b/internal/parser/parser/html_remote_media_test.gonew file mode 100644index 0000000000..7c01301907--- /dev/null+++ b/internal/parser/parser/html_remote_media_test.go@@ -0,0 +1,376 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package parser++import (+ "context"+ "net"+ "net/http"+ "net/http/httptest"+ "net/url"+ "strings"+ "sync/atomic"+ "testing"+ "time"++ "ragflow/internal/common"+ "ragflow/internal/entity"+ "ragflow/internal/parser/media"+ "ragflow/internal/utility"+)++// allowLoopbackImages lets one test reach a loopback httptest server through+// the shared SSRF guard. Every other URL still goes through the production+// guard, so a direct or redirected reference to a private address stays+// rejected.+func allowLoopbackImages(t *testing.T) {+ t.Helper()+ previous := common.AssertURLSafeWithContext+ common.AssertURLSafeWithContext = func(ctx context.Context, rawURL string) (string, string, error) {+ parsed, err := url.Parse(strings.TrimSpace(rawURL))+ if err == nil {+ if host := parsed.Hostname(); host != "" {+ if ip := net.ParseIP(host); ip != nil && ip.IsLoopback() {+ return host, ip.String(), nil+ }+ }+ }+ return previous(ctx, rawURL)+ }+ t.Cleanup(func() { common.AssertURLSafeWithContext = previous })+}++func imageTestServer(t *testing.T, body string, hits *atomic.Int32) *httptest.Server {+ t.Helper()+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ if hits != nil {+ hits.Add(1)+ }+ w.Header().Set("Content-Type", "image/png")+ _, _ = w.Write([]byte(body))+ }))+ t.Cleanup(server.Close)+ return server+}++func TestHTMLParser_FetchesRemoteImageIntoRunCache(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ const body = "\x89PNG\r\n\x1a\nremote-bytes"+ server := imageTestServer(t, body, &hits)+ ctx, cache := recordingMediaContext()++ result := NewHTMLParser().ParseWithResult(ctx, "remote.html", []byte(`<p>before <img alt="remote alt" src="`+server.URL+`/image.png"> after</p>`))+ if result.Err != nil {+ t.Fatalf("ParseWithResult: %v", result.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 1 || cache.puts[0].format != "html" || cache.puts[0].mime != "image/png" || string(cache.puts[0].data) != body {+ t.Fatalf("cache puts = %+v", cache.puts)+ }+ var imageItem map[string]any+ for _, item := range result.JSON {+ m := item.ToMap()+ if m["doc_type_kwd"] == DocTypeImage {+ imageItem = m+ }+ }+ if imageItem == nil {+ t.Fatalf("no image item emitted: %+v", ItemsToMaps(result.JSON))+ }+ ref := entity.MediaRefFromAny(imageItem["media_ref"])+ if ref == nil || ref.Format != "html" || ref.MIME != "image/png" || ref.Size != int64(len(body)) {+ t.Fatalf("media_ref = %+v", ref)+ }+ if _, ok := imageItem["image"]; ok {+ t.Fatalf("image bytes retained: %+v", imageItem)+ }+ if strings.Join(result.Warnings, "\n") != "" {+ t.Fatalf("unexpected warnings: %v", result.Warnings)+ }+}++func TestHTMLParser_RemoteImageDownloadFailureWarns(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ hits.Add(1)+ w.WriteHeader(http.StatusNotFound)+ }))+ defer server.Close()+ ctx, cache := recordingMediaContext()++ result := NewHTMLParser().ParseWithResult(ctx, "missing.html", []byte(`<p>before</p><img alt="gone" src="`+server.URL+`/missing.png"><p>after</p>`))+ if result.Err != nil {+ t.Fatalf("ParseWithResult: %v", result.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ for _, item := range result.JSON {+ if item.ToMap()["doc_type_kwd"] == DocTypeImage {+ t.Fatalf("image item emitted for a failed download: %+v", item.ToMap())+ }+ }+ if !strings.Contains(strings.Join(result.Warnings, "\n"), "remote image(s) that could not be downloaded") {+ t.Fatalf("missing remote download warning: %v", result.Warnings)+ }+}++func TestHTMLParser_RepeatedRemoteImagesFetchOnce(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := imageTestServer(t, "shared-bytes", &hits)+ img := `<img src="` + server.URL + `/same.png">`+ ctx, cache := recordingMediaContext()++ result := NewHTMLParser().ParseWithResult(ctx, "repeat.html", []byte("<p>"+img+img+"</p>"))+ if result.Err != nil {+ t.Fatalf("ParseWithResult: %v", result.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1 for a repeated source", hits.Load())+ }+ if len(cache.puts) != 1 {+ t.Fatalf("cache puts = %d, want 1", len(cache.puts))+ }+ images := 0+ var firstRef *entity.MediaRef+ for _, item := range result.JSON {+ m := item.ToMap()+ if m["doc_type_kwd"] != DocTypeImage {+ continue+ }+ images+++ ref := entity.MediaRefFromAny(m["media_ref"])+ if ref == nil {+ t.Fatalf("image item without media_ref: %+v", m)+ }+ if firstRef == nil {+ firstRef = ref+ } else if *ref != *firstRef {+ t.Fatalf("repeated source produced different refs: %+v vs %+v", ref, firstRef)+ }+ }+ if images != 2 {+ t.Fatalf("image items = %d, want 2", images)+ }+}++func TestHTMLParser_RejectsRemoteImageToInternalAddress(t *testing.T) {+ ctx, cache := recordingMediaContext()+ // Private and loopback literals are rejected before any connection is+ // attempted, so this test needs no network.+ for _, source := range []string{"http://10.0.0.1/image.png", "http://127.0.0.1/image.png"} {+ result := NewHTMLParser().ParseWithResult(ctx, "ssrf.html", []byte(`<img alt="blocked" src="`+source+`">`))+ if result.Err != nil {+ t.Fatalf("ParseWithResult(%s): %v", source, result.Err)+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if !strings.Contains(strings.Join(result.Warnings, "\n"), "could not be downloaded") {+ t.Fatalf("missing SSRF rejection warning for %s: %v", source, result.Warnings)+ }+ }+}++func TestHTMLParser_RemoteRedirectToPrivateAddressIsRejected(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ hits.Add(1)+ http.Redirect(w, r, "http://10.0.0.1/image.png", http.StatusFound)+ }))+ defer server.Close()+ ctx, cache := recordingMediaContext()++ result := NewHTMLParser().ParseWithResult(ctx, "redirect.html", []byte(`<img src="`+server.URL+`/redirect.png">`))+ if result.Err != nil {+ t.Fatalf("ParseWithResult: %v", result.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("first-hop fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if !strings.Contains(strings.Join(result.Warnings, "\n"), "could not be downloaded") {+ t.Fatalf("missing redirect rejection warning: %v", result.Warnings)+ }+}++func TestHTMLParser_BoundsRemoteImageFetchingPerDocument(t *testing.T) {+ allowLoopbackImages(t)+ previous := htmlImageProcessingBudget+ htmlImageProcessingBudget = 50 * time.Millisecond+ t.Cleanup(func() { htmlImageProcessingBudget = previous })++ var hits atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ hits.Add(1)+ time.Sleep(200 * time.Millisecond)+ w.Header().Set("Content-Type", "image/png")+ _, _ = w.Write([]byte("slow-image"))+ }))+ defer server.Close()+ ctx, _ := recordingMediaContext()++ input := `<p>before</p><img src="` + server.URL + `/slow.png"><p>after</p>` ++ `<img src="` + server.URL + `/second.png"><p>done</p>`+ result := NewHTMLParser().ParseWithResult(ctx, "slow.html", []byte(input))+ if result.Err != nil {+ t.Fatalf("ParseWithResult: %v", result.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want only the first image to reach the network", hits.Load())+ }+ texts := make([]string, 0, len(result.JSON))+ for _, item := range result.JSON {+ if text, ok := item.ToMap()["text"].(string); ok {+ texts = append(texts, text)+ }+ }+ joined := strings.Join(texts, "\n")+ if !strings.Contains(joined, "after") || !strings.Contains(joined, "done") {+ t.Fatalf("text after the timed-out image was lost: %q", joined)+ }+ if !strings.Contains(strings.Join(result.Warnings, "\n"), "document time budget") {+ t.Fatalf("missing document time-budget warning: %v", result.Warnings)+ }+}++func TestHTMLParser_FetchesRemoteTableImage(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ const body = "\x89PNG\r\n\x1a\ncell-bytes"+ server := imageTestServer(t, body, &hits)+ ctx, cache := recordingMediaContext()++ result := NewHTMLParser().ParseWithResult(ctx, "table.html", []byte(`<table><tr><td><img alt="cell" src="`+server.URL+`/cell.png"></td></tr></table>`))+ if result.Err != nil {+ t.Fatalf("ParseWithResult: %v", result.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 1 || string(cache.puts[0].data) != body {+ t.Fatalf("cache puts = %+v", cache.puts)+ }+ var imageItem map[string]any+ for _, item := range result.JSON {+ m := item.ToMap()+ if m["doc_type_kwd"] == DocTypeImage {+ imageItem = m+ }+ }+ if imageItem == nil {+ t.Fatalf("no image item emitted: %+v", ItemsToMaps(result.JSON))+ }+ ref := entity.MediaRefFromAny(imageItem["media_ref"])+ if ref == nil || ref.Format != "html" || ref.Size != int64(len(body)) {+ t.Fatalf("media_ref = %+v", ref)+ }+ if imageItem["parent_table_id"] == nil {+ t.Fatalf("table image lost its parent table: %+v", imageItem)+ }+}++func TestHTMLTextParserSkipsRemoteImages(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := imageTestServer(t, "remote-bytes", &hits)++ p, err := GetTextOnlyParser(utility.FileTypeHTML)+ if err != nil {+ t.Fatalf("GetTextOnlyParser: %v", err)+ }+ input := `<p>before <img alt="alt text" src="` + server.URL + `/image.png"> after</p>` ++ `<table><tr><td><img alt="cell alt" src="` + server.URL + `/cell.png"></td></tr></table>`+ result := p.ParseWithResult(context.Background(), "text.html", []byte(input))+ if result.Err != nil {+ t.Fatalf("ParseWithResult: %v", result.Err)+ }+ if hits.Load() != 0 {+ t.Fatalf("text-only HTML reached the network %d time(s)", hits.Load())+ }+ var texts []string+ for _, item := range result.JSON {+ m := item.ToMap()+ if _, ok := m["image"]; ok {+ t.Fatalf("text-only HTML retained inline image bytes: %+v", m)+ }+ if _, ok := m["media_ref"]; ok {+ t.Fatalf("text-only HTML emitted a media_ref: %+v", m)+ }+ if text, ok := m["text"].(string); ok && text != "" {+ texts = append(texts, text)+ }+ if table, ok := m["table"].(*entity.TableData); ok {+ for _, row := range table.CellTexts() {+ texts = append(texts, strings.Join(row, " "))+ }+ }+ }+ joined := strings.Join(texts, "\n")+ if !strings.Contains(joined, "before") || !strings.Contains(joined, "after") {+ t.Fatalf("surrounding prose lost: %q", joined)+ }+ if !strings.Contains(joined, "alt text") {+ t.Fatalf("inline image alt text lost: %q", joined)+ }+ if !strings.Contains(joined, "cell alt") {+ t.Fatalf("table image alt text lost: %q", joined)+ }+ if len(result.Warnings) != 0 {+ t.Fatalf("warnings = %v, want none for a text-only parse", result.Warnings)+ }+}++func TestRemoteMediaReusesCachedSourceAfterDeadline(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := imageTestServer(t, "cached-image", &hits)+ ctx, cache := recordingMediaContext()+ ctx, cancelTask := context.WithCancel(ctx)+ defer cancelTask()+ state := &htmlWalkState{ctx: ctx, mediaCache: cache, budget: media.NewBudget()}+ ref, ok := state.resolveRemoteMedia(ctx, server.URL)+ if !ok {+ t.Fatal("initial image was not cached")+ }+ expired, cancel := context.WithDeadline(ctx, time.Now().Add(-time.Second))+ defer cancel()+ reused, ok := state.resolveRemoteMedia(expired, server.URL)+ if !ok || reused == nil || *reused != *ref {+ t.Fatalf("cached image was lost after deadline: %+v", reused)+ }+ if _, ok := state.resolveRemoteMedia(expired, server.URL+"/new"); ok {+ t.Fatal("new source resolved after deadline")+ }+ if hits.Load() != 1 || len(cache.puts) != 1 {+ t.Fatalf("fetches=%d, cache writes=%d; want one each", hits.Load(), len(cache.puts))+ }+ cancelTask()+ if _, ok := state.resolveRemoteMedia(expired, server.URL); ok || state.err != context.Canceled {+ t.Fatalf("task cancellation was ignored: %v", state.err)+ }+}diff --git a/internal/parser/parser/markdown_media_test.go b/internal/parser/parser/markdown_media_test.gonew file mode 100644index 0000000000..92dba00cc7--- /dev/null+++ b/internal/parser/parser/markdown_media_test.go@@ -0,0 +1,643 @@+//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.++package parser++import (+ "context"+ "crypto/sha256"+ "encoding/hex"+ "errors"+ "fmt"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync/atomic"+ "testing"++ "ragflow/internal/entity"+ "ragflow/internal/parser/media"+ "ragflow/internal/utility"+)++const markdownMediaDataURI = "data:image/png;base64,aGVsbG8="++func markdownIngestionParser(t *testing.T) ParseResultProducer {+ t.Helper()+ p, err := GetParser(utility.FileTypeMarkdown)+ if err != nil {+ t.Fatalf("GetParser(FileTypeMarkdown): %v", err)+ }+ return p+}++func markdownImageItems(t *testing.T, res ParseResult) []map[string]any {+ t.Helper()+ var images []map[string]any+ for _, item := range ItemsToMaps(res.JSON) {+ if item["doc_type_kwd"] == DocTypeImage {+ images = append(images, item)+ }+ }+ return images+}++func markdownText(res ParseResult) string {+ var texts []string+ for _, item := range ItemsToMaps(res.JSON) {+ if text, ok := item["text"].(string); ok {+ texts = append(texts, text)+ }+ }+ return strings.Join(texts, "\n")+}++func TestMarkdownParserIngestionStoresBlockDataURI(t *testing.T) {+ ctx, cache := recordingMediaContext()+ res := markdownIngestionParser(t).ParseWithResult(ctx, "doc.md", []byte("Before\n\n\n\nAfter\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 1 || cache.puts[0].format != "markdown" || cache.puts[0].mime != "image/png" || string(cache.puts[0].data) != "hello" {+ t.Fatalf("cache puts = %+v", cache.puts)+ }+ images := markdownImageItems(t, res)+ if len(images) != 1 {+ t.Fatalf("image items = %+v, want one", ItemsToMaps(res.JSON))+ }+ item := images[0]+ if _, ok := item["image"]; ok {+ t.Fatalf("image bytes retained on the item: %+v", item)+ }+ ref := entity.MediaRefFromAny(item["media_ref"])+ if ref == nil || ref.Format != "markdown" || ref.Kind != entity.MediaKindKV || ref.MIME != "image/png" || ref.Size != int64(len("hello")) {+ t.Fatalf("media_ref = %+v", ref)+ }+ sum := sha256.Sum256([]byte("hello"))+ if want := hex.EncodeToString(sum[:]); ref.SHA256 != want {+ t.Fatalf("sha256 = %s, want %s", ref.SHA256, want)+ }+ if _, err := cache.Get(ctx, *ref); err != nil {+ t.Fatalf("resolve media_ref: %v", err)+ }+ if texts := markdownText(res); !strings.Contains(texts, "Before") || !strings.Contains(texts, "After") {+ t.Fatalf("surrounding text lost: %q", texts)+ }+ if len(res.Warnings) != 0 {+ t.Fatalf("warnings = %v, want none", res.Warnings)+ }+}++func TestMarkdownParserIngestionStoresTableDataURI(t *testing.T) {+ ctx, cache := recordingMediaContext()+ md := "| Field | Value |\n|---|---|\n| Logo |  |\n"+ res := markdownIngestionParser(t).ParseWithResult(ctx, "table.md", []byte(md))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 1 || string(cache.puts[0].data) != "hello" {+ t.Fatalf("cache puts = %+v", cache.puts)+ }+ images := markdownImageItems(t, res)+ if len(images) != 1 {+ t.Fatalf("image items = %+v, want one", ItemsToMaps(res.JSON))+ }+ item := images[0]+ if entity.MediaRefFromAny(item["media_ref"]) == nil {+ t.Fatalf("table image without media_ref: %+v", item)+ }+ if item["parent_table_id"] == nil {+ t.Fatalf("table image lost its parent table: %+v", item)+ }+ if _, ok := item["image"]; ok {+ t.Fatalf("image bytes retained on the item: %+v", item)+ }+}++func TestMarkdownParserIngestionFetchesRemoteImage(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ const body = "\x89PNG\r\n\x1a\nremote-bytes"+ server := imageTestServer(t, body, &hits)+ ctx, cache := recordingMediaContext()++ res := markdownIngestionParser(t).ParseWithResult(ctx, "remote.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 1 || cache.puts[0].format != "markdown" || cache.puts[0].mime != "image/png" || string(cache.puts[0].data) != body {+ t.Fatalf("cache puts = %+v", cache.puts)+ }+ images := markdownImageItems(t, res)+ if len(images) != 1 {+ t.Fatalf("image items = %+v, want one", ItemsToMaps(res.JSON))+ }+ ref := entity.MediaRefFromAny(images[0]["media_ref"])+ if ref == nil || ref.Format != "markdown" || ref.Size != int64(len(body)) {+ t.Fatalf("media_ref = %+v", ref)+ }+ if _, ok := images[0]["image"]; ok {+ t.Fatalf("image bytes retained on the item: %+v", images[0])+ }+ if len(res.Warnings) != 0 {+ t.Fatalf("warnings = %v, want none", res.Warnings)+ }+}++func TestMarkdownParserIngestionSharesRepeatedSources(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := imageTestServer(t, "\x89PNG\r\n\x1a\nshared", &hits)+ ctx, cache := recordingMediaContext()++ md := "\n\n\n\n" ++ "\n\n\n"+ res := markdownIngestionParser(t).ParseWithResult(ctx, "repeat.md", []byte(md))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1 for a repeated URL", hits.Load())+ }+ if len(cache.puts) != 2 {+ t.Fatalf("cache puts = %d, want one data URI and one remote source", len(cache.puts))+ }+ images := markdownImageItems(t, res)+ if len(images) != 4 {+ t.Fatalf("image items = %d, want 4", len(images))+ }+ ref := entity.MediaRefFromAny(images[0]["media_ref"])+ if ref == nil {+ t.Fatalf("first image without media_ref: %+v", images[0])+ }+ for i, item := range images[1:] {+ other := entity.MediaRefFromAny(item["media_ref"])+ if other == nil {+ t.Fatalf("image %d without media_ref: %+v", i+1, item)+ }+ }+}++func TestMarkdownParserIngestionSkipsFailedRemoteDownload(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ hits.Add(1)+ w.WriteHeader(http.StatusNotFound)+ }))+ defer server.Close()+ ctx, cache := recordingMediaContext()++ res := markdownIngestionParser(t).ParseWithResult(ctx, "missing.md", []byte("Before\n\n\n\nAfter\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if images := markdownImageItems(t, res); len(images) != 0 {+ t.Fatalf("image items = %+v, want none", images)+ }+ texts := markdownText(res)+ if !strings.Contains(texts, "Before") || !strings.Contains(texts, "After") || !strings.Contains(texts, "gone") {+ t.Fatalf("text item lost the failed image or its neighbours: %q", texts)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "remote image(s) that could not be downloaded") {+ t.Fatalf("missing remote download warning: %v", res.Warnings)+ }+}++func TestMarkdownParserIngestionRejectsInternalAddress(t *testing.T) {+ ctx, cache := recordingMediaContext()+ for _, source := range []string{"http://10.0.0.1/image.png", "http://127.0.0.1/image.png"} {+ res := markdownIngestionParser(t).ParseWithResult(ctx, "ssrf.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult(%s): %v", source, res.Err)+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "could not be downloaded") {+ t.Fatalf("missing SSRF rejection warning for %s: %v", source, res.Warnings)+ }+ }+}++func TestMarkdownParserIngestionRejectsRedirectToPrivateAddress(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ hits.Add(1)+ http.Redirect(w, r, "http://10.0.0.1/image.png", http.StatusFound)+ }))+ defer server.Close()+ ctx, cache := recordingMediaContext()++ res := markdownIngestionParser(t).ParseWithResult(ctx, "redirect.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("first-hop fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "could not be downloaded") {+ t.Fatalf("missing redirect rejection warning: %v", res.Warnings)+ }+}++func TestMarkdownParserIngestionSkipsInvalidDataURI(t *testing.T) {+ ctx, cache := recordingMediaContext()+ res := markdownIngestionParser(t).ParseWithResult(ctx, "bad.md", []byte("Before\n\n\n\nAfter\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if images := markdownImageItems(t, res); len(images) != 0 {+ t.Fatalf("image items = %+v, want none", images)+ }+ texts := markdownText(res)+ if !strings.Contains(texts, "Before") || !strings.Contains(texts, "After") || !strings.Contains(texts, "broken") {+ t.Fatalf("text item lost the failed image or its neighbours: %q", texts)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "unresolved image reference") {+ t.Fatalf("missing invalid-data-URI warning: %v", res.Warnings)+ }+}++func TestMarkdownParserIngestionNeverReadsLocalPaths(t *testing.T) {+ ctx, cache := recordingMediaContext()+ res := markdownIngestionParser(t).ParseWithResult(ctx, "local.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if images := markdownImageItems(t, res); len(images) != 0 {+ t.Fatalf("image items = %+v, want none", images)+ }+ if texts := markdownText(res); !strings.Contains(texts, "") {+ t.Fatalf("local image reference lost from text: %q", texts)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "unresolved image reference") {+ t.Fatalf("missing unresolved-source warning: %v", res.Warnings)+ }+}++func TestMarkdownParserIngestionFailsWhenCacheWriteFails(t *testing.T) {+ writeErr := errors.New("kvrocks unavailable")+ cache := &recordingMediaCache{putErr: writeErr}+ ctx := media.WithCache(context.Background(), cache)++ res := markdownIngestionParser(t).ParseWithResult(ctx, "failed.md", []byte("\n"))+ if !errors.Is(res.Err, writeErr) {+ t.Fatalf("ParseWithResult error = %v, want %v", res.Err, writeErr)+ }+}++func TestMarkdownParserIngestionFailsWithoutMediaCache(t *testing.T) {+ res := markdownIngestionParser(t).ParseWithResult(context.Background(), "nocache.md", []byte("\n"))+ if res.Err == nil || !strings.Contains(res.Err.Error(), "media cache is not configured") {+ t.Fatalf("ParseWithResult error = %v, want a missing media cache error", res.Err)+ }+}++func TestMarkdownParserIngestionSkipsImagesAfterRunCacheLimit(t *testing.T) {+ cache := &recordingMediaCache{putErr: fmt.Errorf("mediacache: run image count limit 1 exceeded: %w", media.ErrMediaLimitExceeded)}+ ctx := media.WithCache(context.Background(), cache)++ res := markdownIngestionParser(t).ParseWithResult(ctx, "limit.md", []byte("Before\n\n\n\n\n\nAfter\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if images := markdownImageItems(t, res); len(images) != 0 {+ t.Fatalf("image items = %+v, want none", images)+ }+ texts := markdownText(res)+ if !strings.Contains(texts, "Before") || !strings.Contains(texts, "After") {+ t.Fatalf("text after skipped images lost: %q", texts)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "run media cache limit") {+ t.Fatalf("missing run-limit warning: %v", res.Warnings)+ }+}++func TestMarkdownParserIngestionCancellationFailsTask(t *testing.T) {+ ctx, cancel := context.WithCancel(media.WithCache(context.Background(), &recordingMediaCache{}))+ cancel()+ res := markdownIngestionParser(t).ParseWithResult(ctx, "canceled.md", []byte("\n"))+ if !errors.Is(res.Err, context.Canceled) {+ t.Fatalf("ParseWithResult error = %v, want context.Canceled", res.Err)+ }+}++// TestPDFMarkdownKeepsInlineDataURIImage pins that the PDF backend Markdown+// normalization path still inlines data-URI images: media-ref mode is an+// ingestion-only opt-in, and the PDF path must not change.+func TestPDFMarkdownKeepsInlineDataURIImage(t *testing.T) {+ res := parsePDFMarkdown(context.Background(), "ocr.pdf", "\n")+ if res.Err != nil {+ t.Fatalf("parsePDFMarkdown: %v", res.Err)+ }+ images := markdownImageItems(t, res)+ if len(images) != 1 {+ t.Fatalf("image items = %+v, want one inline image", ItemsToMaps(res.JSON))+ }+ if images[0]["image"] != "aGVsbG8=" {+ t.Fatalf("image payload = %v, want the inline base64 payload", images[0]["image"])+ }+ if _, ok := images[0]["media_ref"]; ok {+ t.Fatalf("PDF Markdown normalization emitted a media_ref: %+v", images[0])+ }+}++func TestMarkdownParserIngestionFlattenKeepsImageAsText(t *testing.T) {+ ctx, cache := recordingMediaContext()+ p := markdownIngestionParser(t)+ markdown, ok := p.(*MarkdownParser)+ if !ok {+ t.Fatalf("GetParser(FileTypeMarkdown) = %T", p)+ }+ markdown.FlattenMediaToText = true++ res := p.ParseWithResult(ctx, "flatten.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 1 || string(cache.puts[0].data) != "hello" {+ t.Fatalf("cache puts = %+v", cache.puts)+ }+ items := ItemsToMaps(res.JSON)+ if len(items) != 1 || items[0]["doc_type_kwd"] != DocTypeText {+ t.Fatalf("flattened items = %+v, want one text item", items)+ }+ if entity.MediaRefFromAny(items[0]["media_ref"]) == nil {+ t.Fatalf("flattened image lost its media_ref: %+v", items[0])+ }+}++// TestMarkdownParserIngestionCountsInvalidDataURIOnce pins the shared source+// key between the Markdown resolver and the walker's ref-only data-URI+// fallback: one broken data URI must produce one warning, not an+// invalid-payload warning plus an unresolved-source warning.+func TestMarkdownParserIngestionCountsInvalidDataURIOnce(t *testing.T) {+ ctx, cache := recordingMediaContext()+ res := markdownIngestionParser(t).ParseWithResult(ctx, "bad.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ warnings := strings.Join(res.Warnings, "\n")+ if strings.Contains(warnings, "invalid or oversized inline image") {+ t.Fatalf("broken data URI was resolved twice: %v", res.Warnings)+ }+ if got := strings.Count(warnings, "unresolved image reference"); got != 1 {+ t.Fatalf("unresolved warnings = %d, want 1: %v", got, res.Warnings)+ }+ if len(res.Warnings) != 1 {+ t.Fatalf("warnings = %v, want exactly one", res.Warnings)+ }+}++// TestMarkdownParserIngestionCacheLimitCountedOnce pins that a data URI the+// run cache rejects is attempted once and reported once.+func TestMarkdownParserIngestionCacheLimitCountedOnce(t *testing.T) {+ cache := &recordingMediaCache{putErr: fmt.Errorf("mediacache: run image count limit 1 exceeded: %w", media.ErrMediaLimitExceeded)}+ ctx := media.WithCache(context.Background(), cache)++ res := markdownIngestionParser(t).ParseWithResult(ctx, "limit.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if cache.putCalls != 1 {+ t.Fatalf("cache Put calls = %d, want 1", cache.putCalls)+ }+ warnings := strings.Join(res.Warnings, "\n")+ if !strings.Contains(warnings, "skipped 1 image(s) after the run media cache limit") {+ t.Fatalf("cache-limit warning count is not 1: %v", res.Warnings)+ }+ if strings.Contains(warnings, "skipped 2 image(s)") {+ t.Fatalf("cache-limit warning double counted: %v", res.Warnings)+ }+}++// TestMarkdownParserIngestionBudgetRejectionCountedOnce pins that a data URI+// rejected by the run byte budget is reserved once, not once per fallback.+func TestMarkdownParserIngestionBudgetRejectionCountedOnce(t *testing.T) {+ t.Setenv("RAGFLOW_MEDIA_MAX_BYTES_PER_RUN", "1")+ ctx, cache := recordingMediaContext()+ res := markdownIngestionParser(t).ParseWithResult(ctx, "budget.md", []byte("\n"))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ warnings := strings.Join(res.Warnings, "\n")+ if !strings.Contains(warnings, "omitted 1 embedded image payload(s) after reaching the 1-byte run image budget") {+ t.Fatalf("budget warning count is not 1: %v", res.Warnings)+ }+}++// markdownTableTexts returns the cell texts of every table item in res.+func markdownTableTexts(t *testing.T, res ParseResult) []string {+ t.Helper()+ var texts []string+ for _, item := range ItemsToMaps(res.JSON) {+ table, ok := item["table"].(*entity.TableData)+ if !ok {+ continue+ }+ for _, row := range table.CellTexts() {+ texts = append(texts, row...)+ }+ }+ return texts+}++// TestMarkdownParserIngestionKeepsLocalTableImageReference pins that an+// unresolved table image keeps its Markdown reference: ParseTableHTML's+// cellText ignores <img>, so dropping the image without a text fallback+// silently loses the cell's content.+func TestMarkdownParserIngestionKeepsLocalTableImageReference(t *testing.T) {+ ctx, cache := recordingMediaContext()+ md := "| Field | Value |\n|---|---|\n| Figure |  |\n"+ res := markdownIngestionParser(t).ParseWithResult(ctx, "table.md", []byte(md))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if images := markdownImageItems(t, res); len(images) != 0 {+ t.Fatalf("image items = %+v, want none", images)+ }+ joined := strings.Join(markdownTableTexts(t, res), "\n")+ if !strings.Contains(joined, "") {+ t.Fatalf("table cell lost the image reference: %q", joined)+ }+}++// TestMarkdownParserIngestionKeepsFailedRemoteTableImageAlt pins the text+// fallback for a table image that could not be downloaded.+func TestMarkdownParserIngestionKeepsFailedRemoteTableImageAlt(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ hits.Add(1)+ w.WriteHeader(http.StatusNotFound)+ }))+ defer server.Close()+ ctx, cache := recordingMediaContext()++ md := "| Field | Value |\n|---|---|\n| Figure |  |\n"+ res := markdownIngestionParser(t).ParseWithResult(ctx, "table.md", []byte(md))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1", hits.Load())+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ joined := strings.Join(markdownTableTexts(t, res), "\n")+ if !strings.Contains(joined, "diagram") {+ t.Fatalf("table cell lost the image alt text: %q", joined)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "could not be downloaded") {+ t.Fatalf("missing remote download warning: %v", res.Warnings)+ }+}++// TestMarkdownParserIngestionKeepsInvalidDataURITableImageAlt pins the text+// fallback for a table image whose data URI cannot be decoded, and that the+// invalid source is counted once.+func TestMarkdownParserIngestionKeepsInvalidDataURITableImageAlt(t *testing.T) {+ ctx, cache := recordingMediaContext()+ md := "| Field | Value |\n|---|---|\n| Figure |  |\n"+ res := markdownIngestionParser(t).ParseWithResult(ctx, "table.md", []byte(md))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if cache.putCalls != 0 {+ t.Fatalf("cache Put calls = %d, want 0", cache.putCalls)+ }+ joined := strings.Join(markdownTableTexts(t, res), "\n")+ if !strings.Contains(joined, "broken") || strings.Contains(joined, "base64") {+ t.Fatalf("table cell text = %q, want the alt text without the data URI", joined)+ }+ warnings := strings.Join(res.Warnings, "\n")+ if got := strings.Count(warnings, "unresolved image reference"); got != 1 {+ t.Fatalf("unresolved warnings = %d, want 1: %v", got, res.Warnings)+ }+}++// TestMarkdownParserIngestionStopsRemoteDownloadsAfterByteBudget pins that an+// unknown-size download rejected by the run byte budget stops later downloads+// before they reach the network.+func TestMarkdownParserIngestionStopsRemoteDownloadsAfterByteBudget(t *testing.T) {+ allowLoopbackImages(t)+ t.Setenv("RAGFLOW_MEDIA_MAX_BYTES_PER_RUN", "1")+ var hits atomic.Int32+ server := imageTestServer(t, "\x89PNG\r\n\x1a\nremote", &hits)+ ctx, cache := recordingMediaContext()++ md := "\n\n\n"+ res := markdownIngestionParser(t).ParseWithResult(ctx, "budget.md", []byte(md))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if hits.Load() != 1 {+ t.Fatalf("remote fetches = %d, want 1 once the byte budget is spent", hits.Load())+ }+ if len(cache.puts) != 0 {+ t.Fatalf("cache puts = %+v, want none", cache.puts)+ }+ if !strings.Contains(strings.Join(res.Warnings, "\n"), "after reaching the 1-byte run image budget") {+ t.Fatalf("missing byte-budget warning: %v", res.Warnings)+ }+}++// TestMarkdownTextParserSkipsImages pins the text-only contract: preview, CLI+// and attachment parsing must not download remote images, decode data URIs or+// emit image payloads, while keeping the alt text and surrounding prose.+func TestMarkdownTextParserSkipsImages(t *testing.T) {+ allowLoopbackImages(t)+ var hits atomic.Int32+ server := imageTestServer(t, "\x89PNG\r\n\x1a\nremote", &hits)++ p, err := GetTextOnlyParser(utility.FileTypeMarkdown)+ if err != nil {+ t.Fatalf("GetTextOnlyParser(FileTypeMarkdown): %v", err)+ }+ markdown, ok := p.(*MarkdownParser)+ if !ok || !markdown.textOnly || markdown.mediaRefs {+ t.Fatalf("GetTextOnlyParser(FileTypeMarkdown) = %#v, want the text-only parser", p)+ }++ input := "Before\n\n\n\n" ++ "\n\n" ++ "\n\n" ++ "| A | B |\n|---|---|\n| x |  |\n\n" ++ "After\n"+ // No media cache is attached: a text-only parse must not need one.+ res := p.ParseWithResult(context.Background(), "text.md", []byte(input))+ if res.Err != nil {+ t.Fatalf("ParseWithResult: %v", res.Err)+ }+ if hits.Load() != 0 {+ t.Fatalf("text-only Markdown reached the network %d time(s)", hits.Load())+ }+ var texts []string+ for _, item := range ItemsToMaps(res.JSON) {+ if _, ok := item["image"]; ok {+ t.Fatalf("text-only Markdown retained inline image bytes: %+v", item)+ }+ if _, ok := item["media_ref"]; ok {+ t.Fatalf("text-only Markdown emitted a media_ref: %+v", item)+ }+ if text, ok := item["text"].(string); ok && text != "" {+ texts = append(texts, text)+ }+ if table, ok := item["table"].(*entity.TableData); ok {+ for _, row := range table.CellTexts() {+ texts = append(texts, strings.Join(row, " "))+ }+ }+ }+ joined := strings.Join(texts, "\n")+ for _, want := range []string{"Before", "After", "alt text", "inline", "broken", "cell alt"} {+ if !strings.Contains(joined, want) {+ t.Fatalf("text-only Markdown lost %q: %q", want, joined)+ }+ }+ if len(res.Warnings) != 0 {+ t.Fatalf("warnings = %v, want none for a text-only parse", res.Warnings)+ }+}diff --git a/internal/parser/parser/markdown_parser.go b/internal/parser/parser/markdown_parser.goindex 891154e237..2371af0fe1 100644--- a/internal/parser/parser/markdown_parser.go+++ b/internal/parser/parser/markdown_parser.go@@ -21,12 +21,7 @@ import ("context""encoding/base64""fmt"- "io"- "net"- "net/http"- "net/url""strings"- "sync""time"markdownlib "github.com/gomarkdown/markdown"@@ -35,6 +30,7 @@ import (mdparser "github.com/gomarkdown/markdown/parser""golang.org/x/net/html"+ "ragflow/internal/entity""ragflow/internal/parser/charset""ragflow/internal/parser/media")@@ -45,11 +41,6 @@ const dataURIPrefix = "data:image/"// GoMarkdown is the lib_type identifier for the pure-Go Markdown backend.const GoMarkdown = "go_markdown"-// ssrfAllowLoopback lets tests exercise the HTTP image fetch path against a-// loopback httptest server. It stays false in production so loopback-// addresses are rejected (SSRF protection).-var ssrfAllowLoopback bool-type MarkdownParser struct {libType stringParseMethod string@@ -59,6 +50,13 @@ type MarkdownParser struct {// downloaded. NewMarkdownParser enables it for direct Markdown parsing;// callers normalizing untrusted backend responses may disable it.FetchRemoteImages bool+ // textOnly skips every image source: no download, no data-URI decode and+ // no media reference. Preview, CLI and attachment paths use it.+ textOnly bool+ // mediaRefs stores image bytes in the run media cache and emits MediaRefs+ // instead of inline payloads. newParser enables it for ingestion; callers+ // normalizing backend Markdown keep inline payloads.+ mediaRefs bool}func NewMarkdownParser(libType string) (*MarkdownParser, error) {@@ -92,11 +90,24 @@ func (p *MarkdownParser) ConfigureFromSetup(setup map[string]any) {// returns a structured JSON payload. Each top-level block// emits one item with `text` + `doc_type_kwd: "text"`. When the// block contains a Markdown image reference (), the image-// data is resolved and the item carries `doc_type_kwd: "image"` with-// the base64-encoded image payload. The legacy debug-print path has+// source is resolved and the item carries `doc_type_kwd: "image"`.+// Ingestion parsing stores the bytes in the run media cache and emits a+// `media_ref`; direct callers that normalize backend Markdown keep the+// base64-encoded payload on `image`. The legacy debug-print path has// been removed; callers consume ParseResult directly.func (p *MarkdownParser) ParseWithResult(ctx context.Context, filename string, data []byte) ParseResult {state := &htmlWalkState{budget: media.NewBudget()}+ switch {+ case p.textOnly:+ // Text-only callers have no run media cache and must not reach the+ // network or decode inline payloads.+ state.skipImages = true+ case p.mediaRefs:+ state.refOnly = true+ state.ctx = ctx+ state.mediaCache = media.CacheFromContext(ctx)+ state.mediaFormat = "markdown"+ }result := p.parseWithState(ctx, filename, data, state)result.Warnings = append(state.warnings(), result.Warnings...)return result@@ -157,9 +168,21 @@ func (p *MarkdownParser) parseWithState(ctx context.Context, filename string, davar items []map[string]anymediaBudget := state.budget- imageCtx, cancel := context.WithTimeout(ctx, 60*time.Second)- defer cancel()- unresolvedImages := walkMarkdownBlocksWithImages(imageCtx, doc, &items, p.FlattenMediaToText, p.FetchRemoteImages, state)+ walkCtx := ctx+ if !state.skipImages {+ var cancel context.CancelFunc+ walkCtx, cancel = context.WithTimeout(ctx, 60*time.Second)+ defer cancel()+ }+ previousMediaResolver := state.resolveMedia+ if state.refOnly {+ state.resolveMedia = markdownMediaResolver(state, walkCtx, p.FetchRemoteImages)+ defer func() { state.resolveMedia = previousMediaResolver }()+ }+ unresolvedImages := walkMarkdownBlocksWithImages(walkCtx, doc, &items, p.FlattenMediaToText, p.FetchRemoteImages, state)+ if state.err != nil {+ return ParseResult{Err: state.err}+ }if err := ctx.Err(); err != nil {return ParseResult{Err: err}}@@ -167,7 +190,7 @@ func (p *MarkdownParser) parseWithState(ctx context.Context, filename string, daitems = []map[string]any{{"text": "", "doc_type_kwd": "text"}}}var warnings []string- if mediaBudget.Count() > 0 && imageCtx.Err() == context.DeadlineExceeded && ctx.Err() == nil {+ if !state.skipImages && mediaBudget.Count() > 0 && walkCtx.Err() == context.DeadlineExceeded && ctx.Err() == nil {warnings = append(warnings, "Markdown image processing reached the document time budget")}if unresolvedImages > 0 {@@ -436,10 +459,11 @@ func markdownTableCells(line string) []string {// walkMarkdownBlocksWithImages emits one normalized item per// top-level block. Headings, paragraphs, lists, and code blocks are// emitted with their text. When a block contains a Markdown image-// reference (), the image data is resolved via+// reference (), the image source is resolved via// findBlockImage (per-block AST walk) and the item carries-// `doc_type_kwd: "image"` together with the base64-encoded image-// payload. When flatten is true, image items use doc_type_kwd="text".+// `doc_type_kwd: "image"` together with either a run-cache reference+// (refOnly), the base64-encoded payload (inline mode), or nothing when the+// source was skipped. When flatten is true, image items use doc_type_kwd="text".//// Tables are emitted through the HTML walker as structured items in source// order, with embedded images linked to their parent table.@@ -457,29 +481,30 @@ func walkMarkdownBlocksWithImages(ctx context.Context, doc ast.Node, out *[]map[}()unresolvedImages := 0previousResolver := htmlState.resolveImage- htmlState.resolveImage = func(src string) (string, bool) {- remote := strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://")- if remote && !fetchRemoteImages {- return "", false- }- encoded, raw, omitted, resolved := resolveImageURL(ctx, src, budget)- if !resolved {- if !omitted {- unresolvedImages+++ if !htmlState.refOnly && !htmlState.skipImages {+ htmlState.resolveImage = func(src string) (string, bool) {+ remote := strings.HasPrefix(src, "http://") || strings.HasPrefix(src, "https://")+ if remote && !fetchRemoteImages {+ return "", false}- return "", false- }- if raw != nil {- mime := http.DetectContentType(raw)- if !strings.HasPrefix(mime, "image/") {- mime = "image/octet-stream"+ encoded, raw, omitted, resolved := resolveImageURL(ctx, src, budget)+ if !resolved {+ if !omitted {+ unresolvedImages+++ }+ return "", false}- return "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(raw), true+ if raw != nil {+ return "data:" + remoteImageMIME(raw) + ";base64," + base64.StdEncoding.EncodeToString(raw), true+ }+ return strings.ToLower(src[:strings.IndexByte(src, ',')+1]) + encoded, true}- return strings.ToLower(src[:strings.IndexByte(src, ',')+1]) + encoded, true+ defer func() { htmlState.resolveImage = previousResolver }()}- defer func() { htmlState.resolveImage = previousResolver }()for _, child := range doc.GetChildren() {+ if htmlState.err != nil {+ break+ }var layout stringvar docTypeKwd stringvar txt string@@ -569,20 +594,31 @@ func walkMarkdownBlocksWithImages(ctx context.Context, doc ast.Node, out *[]map[// every block as an image whenever any image was present. When// flatten is true, keep doc_type_kwd="text" (Python// parser.py:1034: flatten_media_to_text overrides image).- if imgURL, ok := findBlockImage(child); ok {- isRemote := strings.HasPrefix(imgURL, "http://") || strings.HasPrefix(imgURL, "https://")- if fetchRemoteImages || !isRemote {- encoded, raw, omitted, resolved := resolveImageURL(ctx, imgURL, budget)- if omitted {- item["media_omitted"] = true- } else if !resolved {- unresolvedImages+++ if !htmlState.skipImages {+ if imgURL, ok := findBlockImage(child); ok {+ if htmlState.refOnly {+ // Media-ref mode stores the bytes in the run cache; the+ // item carries only the reference.+ if _, ref, ok := htmlState.imageSource(imgURL); ok && ref != nil {+ item["media_ref"] = *ref+ item["doc_type_kwd"] = "image"+ }} else {- if raw != nil {- encoded = base64.StdEncoding.EncodeToString(raw)+ isRemote := strings.HasPrefix(imgURL, "http://") || strings.HasPrefix(imgURL, "https://")+ if fetchRemoteImages || !isRemote {+ encoded, raw, omitted, resolved := resolveImageURL(ctx, imgURL, budget)+ if omitted {+ item["media_omitted"] = true+ } else if !resolved {+ unresolvedImages+++ } else {+ if raw != nil {+ encoded = base64.StdEncoding.EncodeToString(raw)+ }+ item["image"] = encoded+ item["doc_type_kwd"] = "image"+ }}- item["image"] = encoded- item["doc_type_kwd"] = "image"}}}@@ -592,6 +628,39 @@ func walkMarkdownBlocksWithImages(ctx context.Context, doc ast.Node, out *[]map[return unresolvedImages}+// markdownMediaResolver resolves one Markdown image source against the run+// media cache. A data URI is decoded and stored directly; an HTTP(S) source is+// downloaded through the shared SSRF-guarded fetch when remote images are+// enabled. Local paths and other schemes are never read: they are reported as+// unresolved so the block text keeps the Markdown reference and the parser+// warns about the skipped source.+func markdownMediaResolver(state *htmlWalkState, walkCtx context.Context, fetchRemote bool) func(string) (*entity.MediaRef, bool) {+ return func(src string) (*entity.MediaRef, bool) {+ if state.err != nil {+ return nil, false+ }+ src = strings.TrimSpace(src)+ if src == "" {+ return nil, false+ }+ if media.IsDataImageSource(src) {+ ref, invalid := state.storeDataURIMedia("data:"+src, src)+ if invalid {+ state.unresolvedImages+++ }+ return ref, ref != nil+ }+ if isRemoteImageSource(src) {+ if !fetchRemote {+ return nil, false+ }+ return state.resolveRemoteMedia(walkCtx, src)+ }+ state.unresolvedImages+++ return nil, false+ }+}+// isTableHTML reports whether block text starts with a table element.func isTableHTML(s string) bool {s = strings.TrimSpace(strings.ToLower(s))@@ -665,133 +734,6 @@ func resolveImageURL(ctx context.Context, imageURL string, budget *media.Budget)return encoded, raw, false, true}-// fetchImage fetches a bounded HTTP(S) image payload. Local/private addresses-// and redirects to them are rejected (SSRF guard). Hostnames are resolved-// once and the validated IP is pinned in a custom DialContext to prevent-// DNS-rebinding TOCTOU attacks.-func fetchImage(ctx context.Context, rawURL string) ([]byte, error) {- rawURL = strings.TrimSpace(rawURL)- ctx, cancel := context.WithTimeout(ctx, 30*time.Second)- defer cancel()-- parsed, err := url.Parse(rawURL)- if err != nil {- return nil, fmt.Errorf("markdown: invalid image URL: %w", err)- }-- // pinned maps hostname (without port) → validated IP. The hostname- // is resolved once per host, and the transport dials the pinned IP- // directly instead of re-resolving DNS.- var pinnedMu sync.Mutex- pinned := make(map[string]net.IP)-- pinHost := func(host string) error {- ip, err := resolveAndValidateHost(ctx, host)- if err != nil {- return err- }- h, _, _ := net.SplitHostPort(host)- if h == "" {- h = host- }- pinnedMu.Lock()- pinned[h] = ip- pinnedMu.Unlock()- return nil- }-- if err := pinHost(parsed.Host); err != nil {- return nil, err- }-- transport := &http.Transport{- DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {- host, port, err := net.SplitHostPort(addr)- if err != nil {- return nil, err- }- pinnedMu.Lock()- ip, ok := pinned[host]- pinnedMu.Unlock()- if ok {- return (&net.Dialer{}).DialContext(ctx, network, net.JoinHostPort(ip.String(), port))- }- return (&net.Dialer{}).DialContext(ctx, network, addr)- },- }-- client := &http.Client{- Transport: transport,- Timeout: 30 * time.Second,- CheckRedirect: func(req *http.Request, via []*http.Request) error {- if len(via) >= 5 {- return fmt.Errorf("markdown: too many redirects")- }- return pinHost(req.URL.Host)- },- }-- req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)- if err != nil {- return nil, fmt.Errorf("markdown: create image request: %w", err)- }-- resp, err := client.Do(req)- if err != nil {- return nil, fmt.Errorf("markdown: fetch image %s: %w", rawURL, err)- }- defer resp.Body.Close()- if resp.StatusCode < 200 || resp.StatusCode >= 300 {- return nil, fmt.Errorf("markdown: fetch image %s: HTTP %d", rawURL, resp.StatusCode)- }- body, err := io.ReadAll(io.LimitReader(resp.Body, int64(media.ImageMaxBytes())+1))- if err != nil {- return nil, fmt.Errorf("markdown: read image %s: %w", rawURL, err)- }- if len(body) > media.ImageMaxBytes() {- return nil, fmt.Errorf("markdown: image %s exceeds %d-byte limit", rawURL, media.ImageMaxBytes())- }- if len(body) == 0 {- return nil, fmt.Errorf("markdown: image %s is empty", rawURL)- }- return body, nil-}--// resolveAndValidateHost resolves a host (which may include a port),-// validates none of its IPs are internal/private, and returns the-// first public IP for connection pinning.-func resolveAndValidateHost(ctx context.Context, host string) (net.IP, error) {- hostname := host- if h, _, err := net.SplitHostPort(host); err == nil {- hostname = h- }-- ip := net.ParseIP(hostname)- if ip != nil {- if (ip.IsLoopback() && !ssrfAllowLoopback) || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() ||- ip.IsPrivate() || ip.IsUnspecified() {- return nil, fmt.Errorf("markdown: rejected image URL to internal address: %s", host)- }- return ip, nil- }-- addrs, err := net.DefaultResolver.LookupIPAddr(ctx, hostname)- if err != nil {- return nil, fmt.Errorf("markdown: cannot resolve image host: %s", hostname)- }- for _, addr := range addrs {- ip = addr.IP- if (ip.IsLoopback() && !ssrfAllowLoopback) || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() ||- ip.IsPrivate() || ip.IsUnspecified() {- return nil, fmt.Errorf("markdown: rejected image URL resolving to internal address: %s (%s)", host, ip)- }- }- if len(addrs) == 0 {- return nil, fmt.Errorf("markdown: no addresses resolved for host: %s", hostname)- }- return addrs[0].IP, nil-}-// headingText returns a canonical ATX heading (marker plus inline text).// Keeping the marker in the parser payload preserves the Markdown structure// for GeneralChunker and downstream LLM prompts; layout alone does not carrydiff --git a/internal/parser/parser/markdown_parser_test.go b/internal/parser/parser/markdown_parser_test.goindex 6a7e25e7b1..b43966133e 100644--- a/internal/parser/parser/markdown_parser_test.go+++ b/internal/parser/parser/markdown_parser_test.go@@ -302,9 +302,7 @@ func TestMarkdownParser_TableDataURIWithLeadingSpace(t *testing.T) {}func TestMarkdownParser_ResolvedRemoteImageKeepsAltOnly(t *testing.T) {- previous := ssrfAllowLoopback- ssrfAllowLoopback = true- t.Cleanup(func() { ssrfAllowLoopback = previous })+ allowLoopbackImages(t)server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {w.Header().Set("Content-Type", "image/png")@@ -578,10 +576,8 @@ func TestResolveImageURL_LocalPathNotFetched(t *testing.T) {func TestResolveImageURL_HTTPImage(t *testing.T) {// httptest servers bind loopback, which the SSRF guard rejects by// default. Allow loopback for this test so the HTTP fetch path is- // exercised (production keeps ssrfAllowLoopback == false).- prev := ssrfAllowLoopback- ssrfAllowLoopback = true- defer func() { ssrfAllowLoopback = prev }()+ // exercised (production keeps the loopback rejection).+ allowLoopbackImages(t)ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {w.Header().Set("Content-Type", "image/png")@@ -606,9 +602,7 @@ func TestResolveImageURL_HTTPImage(t *testing.T) {}func TestMarkdownImageBudgetSkipsFetchAfterCountLimit(t *testing.T) {- previous := ssrfAllowLoopback- ssrfAllowLoopback = true- t.Cleanup(func() { ssrfAllowLoopback = previous })+ allowLoopbackImages(t)var fetches atomic.Int32server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {diff --git a/internal/parser/parser/parser_type.go b/internal/parser/parser/parser_type.goindex 30d8490ab8..915744fbd4 100644--- a/internal/parser/parser/parser_type.go+++ b/internal/parser/parser/parser_type.go@@ -77,7 +77,13 @@ func newParser(fileType utility.FileType, textOnly bool) (ParseResultProducer, ep.textOnly = textOnlyreturn p, nilcase utility.FileTypeMarkdown:- return NewMarkdownParser(GoMarkdown)+ p, err := NewMarkdownParser(GoMarkdown)+ if err != nil {+ return nil, err+ }+ p.textOnly = textOnly+ p.mediaRefs = !textOnly+ return p, nilcase utility.FileTypeTXT:return NewTextParser(), nilcase utility.FileTypeEPUB:diff --git a/internal/parser/parser/pdf_result_normalize_markdown_test.go b/internal/parser/parser/pdf_result_normalize_markdown_test.goindex f2fec16302..655d4eb406 100644--- a/internal/parser/parser/pdf_result_normalize_markdown_test.go+++ b/internal/parser/parser/pdf_result_normalize_markdown_test.go@@ -46,14 +46,12 @@ func TestPDFParser_NormalizeMarkdown_ConvertsToJSON(t *testing.T) {// TestPDFParser_NormalizeMarkdown_DoesNotFetchRemoteImages pins that backend// markdown normalization never pulls remote image URLs (untrusted OCR response,-// deterministic local normalization only). ssrfAllowLoopback is enabled so the-// SSRF guard does not reject the loopback request before the handler runs; that+// deterministic local normalization only). Loopback is allowed so the SSRF+// guard does not reject the loopback request before the handler runs; that// would make requests stay 0 regardless and mask a regression where NormalizeMarkdown// started fetching remote images.func TestPDFParser_NormalizeMarkdown_DoesNotFetchRemoteImages(t *testing.T) {- prev := ssrfAllowLoopback- ssrfAllowLoopback = true- t.Cleanup(func() { ssrfAllowLoopback = prev })+ allowLoopbackImages(t)var requests intsrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {diff --git a/internal/parser/parser/pdf_result_test.go b/internal/parser/parser/pdf_result_test.goindex 7eee494b48..3beddc0ffb 100644--- a/internal/parser/parser/pdf_result_test.go+++ b/internal/parser/parser/pdf_result_test.go@@ -46,9 +46,7 @@ func TestPDFNormalizeResultPositionsAndMetadata(t *testing.T) {}func TestPDFNormalizeResultMarkdownAndCancellation(t *testing.T) {- previous := ssrfAllowLoopback- ssrfAllowLoopback = true- t.Cleanup(func() { ssrfAllowLoopback = previous })+ allowLoopbackImages(t)requests := 0server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requests++ }))defer server.Close()@@ -78,9 +76,7 @@ func TestPDFNormalizeResultMarkdownAndCancellation(t *testing.T) {func TestPDFMarkdownBackendDoesNotFetchRemoteImages(t *testing.T) {withSSRFBypass(t)- previous := ssrfAllowLoopback- ssrfAllowLoopback = true- t.Cleanup(func() { ssrfAllowLoopback = previous })+ allowLoopbackImages(t)var requests atomic.Int32server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { requests.Add(1); w.WriteHeader(http.StatusNotFound) }))defer server.Close()diff --git a/internal/parser/parser/table_structure_test.go b/internal/parser/parser/table_structure_test.goindex 03dfac66a2..bc95630196 100644--- a/internal/parser/parser/table_structure_test.go+++ b/internal/parser/parser/table_structure_test.go@@ -110,9 +110,7 @@ func TestMarkdownParserPreservesImageBesideInlineTable(t *testing.T) {_, _ = w.Write([]byte("hello"))}))defer server.Close()- old := ssrfAllowLoopback- ssrfAllowLoopback = true- defer func() { ssrfAllowLoopback = old }()+ allowLoopbackImages(t)p, _ := NewMarkdownParser(GoMarkdown)res := p.ParseWithResult(t.Context(), "table.md", []byte(" <table><tr><td>Owner</td><td>RET</td></tr></table>"))if res.Err != nil {diff --git a/internal/service/file/file_test.go b/internal/service/file/file_test.goindex 9666b6d1ad..1eda6fcb8c 100644--- a/internal/service/file/file_test.go+++ b/internal/service/file/file_test.go@@ -432,16 +432,16 @@ func TestFileService_UploadFromURL_PDFAddsExtensionAndStoresToDownloads(t *testi}))defer server.Close()- origAssert := common.AssertURLSafe+ origAssert := common.AssertURLSafeWithContextorigPinned := utility.PinnedHTTPClient- common.AssertURLSafe = func(rawURL string) (string, string, error) {+ common.AssertURLSafeWithContext = func(_ context.Context, rawURL string) (string, string, error) {return "127.0.0.1", "127.0.0.1", nil}utility.PinnedHTTPClient = func(hostname, resolvedIP string, timeout time.Duration) *http.Client {return server.Client()}t.Cleanup(func() {- common.AssertURLSafe = origAssert+ common.AssertURLSafeWithContext = origAssertutility.PinnedHTTPClient = origPinned})@@ -479,16 +479,16 @@ func TestFileService_UploadFromURL_HTMLNormalizesReadableContent(t *testing.T) {}))defer server.Close()- origAssert := common.AssertURLSafe+ origAssert := common.AssertURLSafeWithContextorigPinned := utility.PinnedHTTPClient- common.AssertURLSafe = func(rawURL string) (string, string, error) {+ common.AssertURLSafeWithContext = func(_ context.Context, rawURL string) (string, string, error) {return "127.0.0.1", "127.0.0.1", nil}utility.PinnedHTTPClient = func(hostname, resolvedIP string, timeout time.Duration) *http.Client {return server.Client()}t.Cleanup(func() {- common.AssertURLSafe = origAssert+ common.AssertURLSafeWithContext = origAssertutility.PinnedHTTPClient = origPinned})@@ -553,16 +553,16 @@ func TestFileService_UploadFromURL_ShortensLongNamesWithinTheLimit(t *testing.T)}))defer server.Close()- origAssert := common.AssertURLSafe+ origAssert := common.AssertURLSafeWithContextorigPinned := utility.PinnedHTTPClient- common.AssertURLSafe = func(rawURL string) (string, string, error) {+ common.AssertURLSafeWithContext = func(_ context.Context, rawURL string) (string, string, error) {return "127.0.0.1", "127.0.0.1", nil}utility.PinnedHTTPClient = func(hostname, resolvedIP string, timeout time.Duration) *http.Client {return server.Client()}t.Cleanup(func() {- common.AssertURLSafe = origAssert+ common.AssertURLSafeWithContext = origAssertutility.PinnedHTTPClient = origPinned})diff --git a/internal/utility/upload.go b/internal/utility/upload.goindex 07394b4617..c40e08ab3c 100644--- a/internal/utility/upload.go+++ b/internal/utility/upload.go@@ -50,7 +50,7 @@ func FetchRemoteFileSafelyWithTimeout(ctx context.Context, rawURL string, maxSiz}currentURL := rawURLfor redirects := 0; redirects < 10; redirects++ {- hostname, resolvedIP, err := common.AssertURLSafe(currentURL)+ hostname, resolvedIP, err := common.AssertURLSafeWithContext(ctx, currentURL)if err != nil {return nil, nil, "", err}diff --git a/internal/utility/upload_test.go b/internal/utility/upload_test.goindex 96914df965..98f6f5157a 100644--- a/internal/utility/upload_test.go+++ b/internal/utility/upload_test.go@@ -18,6 +18,10 @@ package utilityimport ("bytes"+ "context"+ "errors"+ "fmt"+ "net""net/http""net/http/httptest""ragflow/internal/common"@@ -43,16 +47,16 @@ func TestFetchRemoteFileSafely_PDFAddsExtension(t *testing.T) {}))defer server.Close()- origAssert := common.AssertURLSafe+ origAssert := common.AssertURLSafeWithContextorigPinned := PinnedHTTPClient- common.AssertURLSafe = func(rawURL string) (string, string, error) {+ common.AssertURLSafeWithContext = func(ctx context.Context, rawURL string) (string, string, error) {return "127.0.0.1", "127.0.0.1", nil}PinnedHTTPClient = func(hostname, resolvedIP string, timeout time.Duration) *http.Client {return server.Client()}t.Cleanup(func() {- common.AssertURLSafe = origAssert+ common.AssertURLSafeWithContext = origAssertPinnedHTTPClient = origPinned})@@ -76,16 +80,16 @@ func TestFetchRemoteFileSafely_ReturnsContentAndHeaders(t *testing.T) {}))defer server.Close()- origAssert := common.AssertURLSafe+ origAssert := common.AssertURLSafeWithContextorigPinned := PinnedHTTPClient- common.AssertURLSafe = func(rawURL string) (string, string, error) {+ common.AssertURLSafeWithContext = func(ctx context.Context, rawURL string) (string, string, error) {return "127.0.0.1", "127.0.0.1", nil}PinnedHTTPClient = func(hostname, resolvedIP string, timeout time.Duration) *http.Client {return server.Client()}t.Cleanup(func() {- common.AssertURLSafe = origAssert+ common.AssertURLSafeWithContext = origAssertPinnedHTTPClient = origPinned})@@ -104,3 +108,56 @@ func TestFetchRemoteFileSafely_ReturnsContentAndHeaders(t *testing.T) {t.Fatalf("data = %q", string(data))}}++func TestFetchRemoteFileSafelyCancelsDNS(t *testing.T) {+ for _, redirect := range []bool{false, true} {+ t.Run(fmt.Sprintf("redirect=%t", redirect), func(t *testing.T) {+ started := make(chan struct{}, 4)+ release := make(chan struct{})+ previousResolver := net.DefaultResolver+ net.DefaultResolver = &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {+ select {+ case started <- struct{}{}:+ default:+ }+ select {+ case <-ctx.Done():+ return nil, ctx.Err()+ case <-release:+ return nil, errors.New("DNS test released")+ }+ }}+ t.Cleanup(func() { net.DefaultResolver = previousResolver })+ defer close(release)+ previousAllow := common.AllowAnyHostForTest+ common.AllowAnyHostForTest = true+ t.Cleanup(func() { common.AllowAnyHostForTest = previousAllow })+ target := "http://blocked-dns.invalid/image.png"+ if redirect {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ http.Redirect(w, r, "http://blocked-dns.invalid/image.png", http.StatusFound)+ }))+ defer server.Close()+ target = server.URL+ }+ ctx, cancel := context.WithCancel(t.Context())+ defer cancel()+ done := make(chan error, 1)+ go func() { _, _, _, err := FetchRemoteFileSafely(ctx, target, 1024); done <- err }()+ select {+ case <-started:+ case <-time.After(time.Second):+ t.Fatal("DNS lookup did not start")+ }+ cancel()+ select {+ case err := <-done:+ if !errors.Is(err, context.Canceled) {+ t.Fatalf("fetch error = %v, want cancellation", err)+ }+ case <-time.After(time.Second):+ t.Fatal("fetch did not stop during DNS lookup")+ }+ })+ }+}