WeKnora/internal/infrastructure/docparser/resolve_remote_html_images_...

407 lines
13 KiB
Go
Raw Normal View History

fix(docparser): resolve remote images referenced by HTML img tags (#2355) Remote http(s) images written as <img src="..."> reached no resolver. Markdown images went through ResolveRemoteImages, HTML data-URIs and HTML relative paths each had their own pass, but nothing claimed the remaining combination, so those images were dropped without a log line and the multimodal stage was skipped for documents that rely on HTML for layout. ResolveRemoteImages now scans HTML img tags with a quoted src as a second pass and rewrites only the src attribute value, leaving the tag intact because the surrounding markup is why these documents use HTML at all. The per-image work (whitelist, SSRF, download, icon filter, upload) moves into one helper so the two syntaxes cannot drift apart on policy. Each pass carries its own image budget; sharing one would let HTML images eat into the number of Markdown images a document already gets resolved. An attribute value is trimmed, HTML-unescaped and its scheme lowercased before use, because HTML permits padding, spells a query separator &amp; and tolerates any scheme case, while every fetcher downstream compares the scheme byte-for-byte. Markdown targets get none of that normalization, so no Markdown reference resolves differently than it does today. A whitelisted host keeps serving the image from where it already lives, and its span is rewritten with the normalized URL rather than the raw attribute text: later stages both locate the image by searching the document for its serving URL and fetch that same string, so the two have to agree. The src attribute must now be preceded by whitespace, so a hyphenated attribute name is no longer mistaken for it. Among HTML tags this cuts two ways: one carrying both a hyphenated *-src and a real src now resolves the real src instead of the placeholder, while one carrying only a hyphenated *-src is no longer resolved at all. Recovering the latter needs an attribute parser rather than a regex, and resolving a lazy-loading placeholder while the real image goes unvisited is the worse of the two failures. Both halves are pinned by tests. Image-derived text also has to survive into the answer, which is why this touches internal/searchutil. The chat enricher and the image_info filters matched Markdown links only, so an HTML-only passage looked image-free: every entry was dropped and the model received bare markup with the caption and OCR text stripped, even though the analysis had run. Both now recognise HTML img tags through one shared regex, so a tag the parser stores can always be matched back. Both syntaxes are located against the original passage and spliced in a single pass; scanning one regex over the other's output let a metadata block that itself quotes an image reference be rescanned and duplicated. The chat variant still enriches only images present in the passage, so orphan entries stay out. Known limits: - An unquoted src and a srcset-only tag are not matched, nor is a tag whose attribute values hold a bare > or a quoted src= of their own. - imgHTMLDataURI still matches src without a boundary, so a data-src holding a base64 placeholder is consumed as if it were the src and the whole tag replaced. Worth the same fix, but it changes what an existing lazy-loading document resolves, so it belongs on its own. - A reference shown as a documentation example is rewritten like any other. Recognising code spans needs a Markdown parser; a hand-rolled scanner risks the far worse failure of treating a document's real screenshots as examples. - Rewriting a whitelisted src writes the unescaped URL back into an HTML attribute, so a query parameter whose name spells an entity renders differently than the author wrote it. Keeping the encoded form would mean unescaping on every matcher instead. - Image text reaches the model only when the whole reference falls inside the retrieved window; an img tag is several times longer than a Markdown link and straddles a chunk boundary more often. - A reference repeated in one document is fetched once per occurrence, which is what gives each chunk its own analysed copy. - Nothing bounds how long one document's fetches may take in total. - Parent-context pruning removes only Markdown images. Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-07-28 13:08:31 +00:00
package docparser
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"sync/atomic"
"testing"
"github.com/Tencent/WeKnora/internal/types"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
// remoteImageServer serves a valid PNG for every request and counts the hits so
// that budget and dedup behaviour can be asserted directly.
func remoteImageServer(t *testing.T) (*httptest.Server, *int64) {
t.Helper()
var hits int64
png := createTestPNG(200, 200)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&hits, 1)
w.Header().Set("Content-Type", "image/png")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(png)
}))
t.Cleanup(ts.Close)
return ts, &hits
}
func allowLocalhost(t *testing.T) {
t.Helper()
t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")
secutils.ResetSSRFWhitelistForTest()
}
func resolve(t *testing.T, markdown string) (string, []StoredImage, *mockFileService) {
t.Helper()
fSvc := &mockFileService{}
updated, images, err := NewImageResolver().ResolveRemoteImages(
context.Background(), markdown, fSvc, 42)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
return updated, images, fSvc
}
func TestResolveRemoteImages_HTMLTagRewritesSrcAndKeepsTag(t *testing.T) {
allowLocalhost(t)
ts, _ := remoteImageServer(t)
markdown := fmt.Sprintf(
`<p><img class="screenshot" src="%s/a.png" alt="login screen" width="600"></p>`, ts.URL)
updated, images, _ := resolve(t, markdown)
if len(images) != 1 {
t.Fatalf("expected 1 stored image, got %d", len(images))
}
if strings.Contains(updated, ts.URL) {
t.Errorf("original URL still present: %s", updated)
}
if !strings.Contains(updated, images[0].ServingURL) {
t.Errorf("serving URL missing from output: %s", updated)
}
// The surrounding markup is the reason these documents use HTML at all.
for _, want := range []string{`<img`, `class="screenshot"`, `alt="login screen"`, `width="600"`, `</p>`} {
if !strings.Contains(updated, want) {
t.Errorf("tag structure lost, %q missing from: %s", want, updated)
}
}
if strings.Contains(updated, "![") {
t.Errorf("tag was converted to markdown syntax: %s", updated)
}
}
func TestResolveRemoteImages_MarkdownAndHTMLBothResolved(t *testing.T) {
allowLocalhost(t)
ts, _ := remoteImageServer(t)
markdown := fmt.Sprintf("![md](%s/md.png)\n\n<img src=\"%s/html.png\">", ts.URL, ts.URL)
updated, images, _ := resolve(t, markdown)
if len(images) != 2 {
t.Fatalf("expected 2 stored images, got %d", len(images))
}
if strings.Contains(updated, ts.URL) {
t.Errorf("a remote URL survived: %s", updated)
}
}
func TestResolveRemoteImages_IndependentBudgetPerSyntax(t *testing.T) {
allowLocalhost(t)
ts, _ := remoteImageServer(t)
// Adding HTML support must not reduce how many Markdown images a document
// already gets resolved: each syntax carries its own budget.
var b strings.Builder
for i := 0; i < maxRemoteImages; i++ {
fmt.Fprintf(&b, "![md%d](%s/md%d.png)\n\n", i, ts.URL, i)
}
for i := 0; i < 5; i++ {
fmt.Fprintf(&b, "<img src=\"%s/html%d.png\">\n\n", ts.URL, i)
}
updated, images, _ := resolve(t, b.String())
if len(images) != maxRemoteImages+5 {
t.Fatalf("expected %d stored images, got %d", maxRemoteImages+5, len(images))
}
if strings.Contains(updated, ts.URL) {
t.Errorf("a remote URL survived: %s", updated)
}
}
func TestResolveRemoteImages_HTMLSrcNormalization(t *testing.T) {
allowLocalhost(t)
ts, _ := remoteImageServer(t)
t.Run("uppercase scheme", func(t *testing.T) {
upper := strings.Replace(ts.URL, "http://", "HTTP://", 1)
updated, images, _ := resolve(t, fmt.Sprintf(`<img SRC="%s/up.png">`, upper))
if len(images) != 1 {
t.Fatalf("expected uppercase scheme to be fetched, got %d", len(images))
}
// The scheme is normalized because every fetcher downstream compares it
// byte-for-byte; an image counted as resolved but unfetchable is worse
// than one left alone.
if strings.Contains(images[0].OriginalRef, "HTTP://") {
t.Errorf("scheme was not normalized: %q", images[0].OriginalRef)
}
if strings.Contains(updated, "HTTP://") {
t.Errorf("document still carries the uppercase scheme: %s", updated)
}
})
t.Run("markdown uppercase scheme is left alone", func(t *testing.T) {
// Markdown targets are handed through unchanged, so a document that
// upstream leaves untouched stays untouched.
upper := strings.Replace(ts.URL, "http://", "HTTP://", 1)
markdown := fmt.Sprintf("![x](%s/md-up.png)", upper)
updated, images, _ := resolve(t, markdown)
if len(images) != 0 {
t.Errorf("expected no markdown behaviour change, got %d image(s)", len(images))
}
if updated != markdown {
t.Errorf("markdown content changed:\n got %q\nwant %q", updated, markdown)
}
})
t.Run("padded src", func(t *testing.T) {
_, images, _ := resolve(t, fmt.Sprintf(`<img src=" %s/pad.png ">`, ts.URL))
if len(images) != 1 {
t.Fatalf("expected padded src to be fetched, got %d", len(images))
}
})
t.Run("entity encoded query", func(t *testing.T) {
var got string
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
got = r.URL.RawQuery
w.Header().Set("Content-Type", "image/png")
_, _ = w.Write(createTestPNG(200, 200))
}))
defer srv.Close()
_, images, _ := resolve(t, fmt.Sprintf(`<img src="%s/q.png?a=1&amp;b=2">`, srv.URL))
if len(images) != 1 {
t.Fatalf("expected 1 stored image, got %d", len(images))
}
if got != "a=1&b=2" {
t.Errorf("entities were not decoded before the request: raw query %q", got)
}
})
}
func TestResolveRemoteImages_HTMLSrcFormsOutOfScope(t *testing.T) {
allowLocalhost(t)
ts, hits := remoteImageServer(t)
cases := map[string]string{
"unquoted src": fmt.Sprintf(`<img src=%s/unquoted.png>`, ts.URL),
"srcset only": fmt.Sprintf(`<img srcset="%s/set.png 2x">`, ts.URL),
"relative src": `<img src="./images/local.png">`,
"data uri src": `<img src="data:image/png;base64,AAAA">`,
"provider src": `<img src="local://images/x.png">`,
"non-img tag": fmt.Sprintf(`<video src="%s/v.mp4">`, ts.URL),
}
for name, markdown := range cases {
t.Run(name, func(t *testing.T) {
before := atomic.LoadInt64(hits)
updated, images, _ := resolve(t, markdown)
if len(images) != 0 {
t.Errorf("expected no stored images, got %d", len(images))
}
if updated != markdown {
t.Errorf("content changed:\n got %q\nwant %q", updated, markdown)
}
if got := atomic.LoadInt64(hits) - before; got != 0 {
t.Errorf("expected no fetch, got %d", got)
}
})
}
}
func TestResolveRemoteImages_HyphenatedAttributeIsNotSrc(t *testing.T) {
allowLocalhost(t)
ts, _ := remoteImageServer(t)
// A lazy-loading tag carries a placeholder in src and the real image in
// data-src. Treating data-src as src would capture whichever came first and
// leave the other unvisited, so only src counts.
markdown := fmt.Sprintf(`<img data-src="/placeholder.svg" src="%s/real.png">`, ts.URL)
updated, images, _ := resolve(t, markdown)
if len(images) != 1 {
t.Fatalf("expected the src image to be resolved, got %d", len(images))
}
if images[0].OriginalRef != ts.URL+"/real.png" {
t.Errorf("expected src to be the fetched reference, got %s", images[0].OriginalRef)
}
if !strings.Contains(updated, `data-src="/placeholder.svg"`) {
t.Errorf("data-src must be left alone: %s", updated)
}
if strings.Contains(updated, ts.URL) {
t.Errorf("src was not rewritten: %s", updated)
}
}
func TestResolveRemoteImages_HyphenatedAttributeOnlyIsNotResolved(t *testing.T) {
allowLocalhost(t)
ts, hits := remoteImageServer(t)
// The other half of the same trade-off, pinned deliberately. Requiring
// whitespace before src means a tag carrying only a hyphenated *-src is no
// longer treated as an image at all — the previous boundary did resolve it.
// Recovering that needs an attribute parser rather than a regex; capturing a
// lazy-loading placeholder while the real src goes unvisited is the worse of
// the two failures.
for _, attr := range []string{"data-src", "ng-src", "data-original-src"} {
t.Run(attr, func(t *testing.T) {
before := atomic.LoadInt64(hits)
markdown := fmt.Sprintf(`<img %s="%s/only.png">`, attr, ts.URL)
updated, images, _ := resolve(t, markdown)
if len(images) != 0 {
t.Errorf("expected no stored images, got %d", len(images))
}
if updated != markdown {
t.Errorf("content changed:\n got %q\nwant %q", updated, markdown)
}
if got := atomic.LoadInt64(hits) - before; got != 0 {
t.Errorf("expected no fetch, got %d", got)
}
})
}
}
func TestResolveRemoteImages_WhitelistedHostAgreesWithDocument(t *testing.T) {
var hits int64
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt64(&hits, 1)
if got := r.URL.RawQuery; got != "a=1&b=2" {
t.Errorf("expected entities decoded for the request, got raw query %q", got)
}
w.Header().Set("Content-Type", "image/png")
_, _ = w.Write(createTestPNG(200, 200))
}))
defer ts.Close()
t.Setenv("SSRF_WHITELIST", "127.0.0.1,localhost")
t.Setenv("IMAGE_HOST_KEEP_URL", strings.TrimPrefix(ts.URL, "http://"))
secutils.ResetSSRFWhitelistForTest()
markdown := fmt.Sprintf(`<img src=" %s/q.png?a=1&amp;b=2 " alt="kept">`, ts.URL)
updated, images, fSvc := resolve(t, markdown)
if len(images) != 1 {
t.Fatalf("expected 1 stored image, got %d", len(images))
}
if atomic.LoadInt64(&hits) != 1 {
t.Fatalf("expected the image to be downloaded once, got %d", hits)
}
if len(fSvc.saved) != 0 {
t.Errorf("a whitelisted image must not be uploaded, got %d uploads", len(fSvc.saved))
}
serving := images[0].ServingURL
// ServingURL carries two obligations that have to agree: later stages locate
// the image by searching the document for it, and the multimodal stage fetches
// that same string.
if !strings.Contains(updated, serving) {
t.Errorf("ServingURL %q is not present in the document %q", serving, updated)
}
if serving != ts.URL+"/q.png?a=1&b=2" {
t.Errorf("ServingURL must be the normalized fetchable URL, got %q", serving)
}
if strings.TrimSpace(serving) != serving {
t.Errorf("ServingURL must not carry padding: %q", serving)
}
if strings.Contains(serving, "&amp;") {
t.Errorf("ServingURL must not carry HTML entities: %q", serving)
}
if !strings.Contains(updated, `alt="kept"`) {
t.Errorf("the rest of the tag must survive: %s", updated)
}
}
func TestResolveRemoteImages_CodeExampleIsRewritten(t *testing.T) {
allowLocalhost(t)
ts, _ := remoteImageServer(t)
// Accepted limitation: this resolver does not parse Markdown structure, so a
// tag shown as a documentation example inside a fence is rewritten like any
// other. Recognising code spans reliably needs a Markdown parser; a
// hand-rolled scanner risks the opposite and far worse failure of treating a
// document's real screenshots as examples.
markdown := fmt.Sprintf("```html\n<img src=\"%s/example.png\">\n```", ts.URL)
updated, images, _ := resolve(t, markdown)
if len(images) != 1 {
t.Fatalf("expected the example to be resolved, got %d", len(images))
}
if strings.Contains(updated, ts.URL) {
t.Errorf("expected the example to be rewritten: %s", updated)
}
}
func TestResolveRemoteImages_CancelledContextStops(t *testing.T) {
allowLocalhost(t)
ts, hits := remoteImageServer(t)
var b strings.Builder
for i := 0; i < 5; i++ {
fmt.Fprintf(&b, "<img src=\"%s/c%d.png\">\n\n", ts.URL, i)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, images, err := NewImageResolver().ResolveRemoteImages(ctx, b.String(), &mockFileService{}, 42)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(images) != 0 {
t.Errorf("expected no images after cancellation, got %d", len(images))
}
if got := atomic.LoadInt64(hits); got != 0 {
t.Errorf("expected no fetch after cancellation, got %d", got)
}
}
func TestResolveAndStoreThenResolveRemoteImages(t *testing.T) {
allowLocalhost(t)
ts, _ := remoteImageServer(t)
// Mirrors the real ingest order, where ResolveAndStore runs first and
// ResolveRemoteImages then works on its output. This is the only place the
// two HTML resolvers can interfere with each other.
markdown := fmt.Sprintf(
"<img src=\"images/relative.png\">\n\n<img src=\"%s/remote.png\">", ts.URL)
fSvc := &mockFileService{}
resolver := NewImageResolver()
afterStore, storeImages, err := resolver.ResolveAndStore(context.Background(), &types.ReadResult{
MarkdownContent: markdown,
ImageRefs: []types.ImageRef{{
OriginalRef: "images/relative.png",
Filename: "relative.png",
MimeType: "image/png",
ImageData: createTestPNG(200, 200),
}},
}, fSvc, 42)
if err != nil {
t.Fatalf("ResolveAndStore: %v", err)
}
if len(storeImages) != 1 {
t.Fatalf("expected the relative image stored, got %d", len(storeImages))
}
afterRemote, remoteImages, err := resolver.ResolveRemoteImages(
context.Background(), afterStore, fSvc, 42)
if err != nil {
t.Fatalf("ResolveRemoteImages: %v", err)
}
if len(remoteImages) != 1 {
t.Fatalf("expected the remote image stored, got %d", len(remoteImages))
}
if strings.Contains(afterRemote, ts.URL) {
t.Errorf("remote URL survived: %s", afterRemote)
}
if strings.Contains(afterRemote, "images/relative.png") {
t.Errorf("relative reference was not rewritten by the earlier pass: %s", afterRemote)
}
if n := strings.Count(afterRemote, "<img"); n != 2 {
t.Errorf("expected both tags preserved, found %d", n)
}
}