package markup
import (
"strings"
"testing"
)
// Every renderer sends an external image through the proxy, hands the
// proxy the author's URL unescaped, and leaves relative images alone.
func TestExternalImagesGoThroughTheProxy(t *testing.T) {
var seen []string
SetImageProxy(func(src string) (string, int, int) {
seen = append(seen, src)
if strings.HasPrefix(src, "https://skip.example/") {
return "", 0, 0
}
if strings.Contains(src, "sized") {
return "/img/sig/" + src, 120, 20
}
return "/img/sig/" + src, 0, 0
})
t.Cleanup(func() { SetImageProxy(nil) })
md, err := Render([]byte("  "), nil)
if err != nil {
t.Fatal(err)
}
got := string(md)
if !strings.Contains(got, `src="/img/sig/https://ci.example/badge.svg?branch=main&x=1"`) {
t.Errorf("markdown image not proxied: %s", got)
}
// A relative image is left alone; one the proxy refuses cannot load
// under img-src 'self', so it is a link to the image instead.
var imgSrcs, hrefs []string
for _, n := range ghFind(ghParse(t, md), ghTag("img")) {
imgSrcs = append(imgSrcs, ghAttr(n, "src"))
}
for _, n := range ghFind(ghParse(t, md), ghTag("a")) {
hrefs = append(hrefs, ghAttr(n, "href"))
}
if !ghIn(imgSrcs, "img/shot.png") || ghIn(imgSrcs, "https://skip.example/a.png") || !ghIn(hrefs, "https://skip.example/a.png") {
t.Errorf("FAIL: a relative or refused image was handled wrongly: imgs=%v links=%v", imgSrcs, hrefs)
}
if len(seen) == 0 || seen[0] != "https://ci.example/badge.svg?branch=main&x=1" {
t.Errorf("the proxy was handed %q, not the author's URL", seen)
}
wiki, err := RenderFossilWiki([]byte(`<img src="https://ci.example/w.png">`), nil)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(wiki), `src="/img/sig/https://ci.example/w.png"`) {
t.Errorf("fossil wiki image not proxied: %s", wiki)
}
if html := Sanitize(`<img src="http://ci.example/h.png">`); !strings.Contains(html, `src="/img/sig/http://ci.example/h.png"`) {
t.Errorf("html document image not proxied: %s", html)
}
if !t.Failed() {
t.Log("ok")
}
}
// Off, nothing is proxied; a remote image cannot load under img-src
// 'self', so it is rendered as a link to the image.
func TestNoProxyRendersRemoteImagesAsLinks(t *testing.T) {
doc, _ := ghRender(t, "")
if n := len(ghFind(doc, ghTag("img"))); n != 0 {
t.Errorf("FAIL: %d remote images left", n)
}
links := ghFind(doc, ghTag("a"))
if len(links) != 1 || ghAttr(links[0], "href") != "https://ci.example/badge.svg" {
t.Errorf("FAIL: a remote image was not turned into a link to it")
}
if !t.Failed() {
t.Log("ok")
}
}
// What the proxy knows goes into the tag: the size it measured, so the
// page does not reflow, and lazy loading for every image after the first
// — the first is usually what the reader came to see.
func TestProxiedImagesCarrySizeAndLazyLoading(t *testing.T) {
SetImageProxy(func(src string) (string, int, int) {
if strings.Contains(src, "unsized") {
return "/img/sig/" + src, 0, 0
}
return "/img/sig/" + src, 120, 20
})
t.Cleanup(func() { SetImageProxy(nil) })
out, err := Render([]byte(
"\n\n\n\n"), nil)
if err != nil {
t.Fatal(err)
}
got := string(out)
imgs := strings.Split(got, "<img ")[1:]
if len(imgs) != 3 {
t.Fatalf("want 3 images, got %d: %s", len(imgs), got)
}
if !strings.Contains(imgs[0], `width="120" height="20"`) {
t.Errorf("the first image has no measured size: %s", imgs[0])
}
if strings.Contains(imgs[0], "loading=") || strings.Contains(imgs[0], "decoding=") {
t.Errorf("the first image was made lazy: %s", imgs[0])
}
for i, img := range imgs[1:] {
if !strings.Contains(img, `loading="lazy"`) || !strings.Contains(img, `decoding="async"`) {
t.Errorf("image %d is not lazy: %s", i+2, img)
}
}
if strings.Contains(imgs[2], "width=") {
t.Errorf("an unmeasured image was given a size: %s", imgs[2])
}
}
// An author who wrote their own width, height or loading keeps it.
func TestAuthorAttributesAreNotOverwritten(t *testing.T) {
SetImageProxy(func(src string) (string, int, int) { return "/img/sig/" + src, 120, 20 })
t.Cleanup(func() { SetImageProxy(nil) })
out := Sanitize(`<img src="https://ci.example/a.png" width="40" height="10"><img src="https://ci.example/b.png" loading="lazy">`)
imgs := strings.Split(out, "<img ")[1:]
if len(imgs) != 2 {
t.Fatalf("want 2 images, got %d: %s", len(imgs), out)
}
// Sized by its author: the measured size is not added on top.
if !strings.Contains(imgs[0], `width="40" height="10"`) || strings.Contains(imgs[0], `width="120"`) {
t.Errorf("the author's size was replaced: %s", imgs[0])
}
// Lazy by its author, and unsized: it keeps the one and is given the
// other, each exactly once.
if strings.Count(imgs[1], `loading="lazy"`) != 1 {
t.Errorf("loading was written twice: %s", imgs[1])
}
if !strings.Contains(imgs[1], `width="120" height="20"`) {
t.Errorf("an unsized image was not measured: %s", imgs[1])
}
}