package server
import (
"bytes"
"image/png"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"golang.org/x/net/html"
)
func socialTags(t *testing.T, body string) map[string]string {
t.Helper()
doc, err := html.Parse(strings.NewReader(body))
if err != nil {
t.Fatal(err)
}
out := map[string]string{}
var walk func(*html.Node)
walk = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "head" {
for c := n.FirstChild; c != nil; c = c.NextSibling {
attrs := map[string]string{}
for _, a := range c.Attr {
attrs[a.Key] = a.Val
}
if c.Data == "meta" {
key := attrs["property"]
if key == "" {
key = attrs["name"]
}
if _, exists := out[key]; exists {
t.Fatalf("duplicate meta %s", key)
}
out[key] = attrs["content"]
}
if c.Data == "link" && attrs["rel"] == "canonical" {
out["canonical"] = attrs["href"]
}
}
return
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
walk(c)
}
}
walk(doc)
return out
}
func TestSocialPublicCardsAndPrivacy(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/", s.handleHome)
s.mux.HandleFunc("/v/city", s.handleCityMap)
s.mux.HandleFunc("/v/city/", s.handleCityMap)
s.mux.HandleFunc("/v/raw/", s.handleRawView)
path := "/guide/日本語?#%"
object, err := s.store.CreatePageAtPathInDomain(domain, path)
if err != nil {
t.Fatal(err)
}
body := `
日本語 & "タイトル" <test>
共有用の短い説明です。
TEMPLATE_SECRET{{children:/hidden}}`
if _, err = s.store.SaveRevisionContent(object, []byte(body), "", "ja", "text/html", "guide.html"); err != nil {
t.Fatal(err)
}
call := func(route, auth string) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", "http://localhost"+route, nil)
r.Header.Set("User-Agent", "Twitterbot/1.0")
if auth != "" {
r.Header.Set("Authorization", "Bearer "+auth)
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
route := (&url.URL{Path: path, RawQuery: "token=DO_NOT_SHARE&comic=1"}).String()
w := call(route, "")
if w.Code != 303 || strings.Contains(w.Body.String(), "共有用") || strings.Contains(w.Body.String(), "og:title") {
t.Fatalf("private crawler response=%d", w.Code)
}
w = call(route, token)
tags := socialTags(t, w.Body.String())
if w.Code != 200 || tags["robots"] == "" || tags["og:title"] != "" {
t.Fatalf("private authenticated tags=%v status=%d", tags, w.Code)
}
if err = s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err != nil {
t.Fatal(err)
}
w = call(route, "")
tags = socialTags(t, w.Body.String())
if w.Code != 200 || tags["og:title"] != `日本語 & "タイトル" ` || tags["twitter:title"] != tags["og:title"] {
t.Fatalf("public tags=%v status=%d", tags, w.Code)
}
if !strings.Contains(tags["og:description"], "共有用") {
t.Fatalf("description=%s", tags["og:description"])
}
for _, value := range tags {
for _, secret := range []string{"DO_NOT_SHARE", "SCRIPT_SECRET", "STYLE_SECRET", "TEMPLATE_SECRET", "children", "hidden"} {
if strings.Contains(value, secret) {
t.Fatalf("metadata leaked %s: %s", secret, value)
}
}
}
wantURL := "http://localhost" + cityPageHref(path)
if tags["og:url"] != wantURL || tags["canonical"] != wantURL || tags["og:locale"] != "ja_JP" || tags["twitter:card"] != "summary_large_image" {
t.Fatalf("URL/card tags=%v", tags)
}
imageURL, err := url.Parse(tags["og:image"])
if err != nil {
t.Fatal(err)
}
imageResponse := call(imageURL.RequestURI(), "")
config, err := png.DecodeConfig(bytes.NewReader(imageResponse.Body.Bytes()))
if err != nil || imageResponse.Code != 200 || config.Width != 1200 || config.Height != 630 {
t.Fatalf("image=%+v %v status=%d", config, err, imageResponse.Code)
}
// Original public image documents use their correctly escaped raw address.
imagePath := "/media/image?#%.png"
imageObject, err := s.store.CreatePageAtPathInDomain(domain, imagePath)
if err != nil {
t.Fatal(err)
}
if _, err = s.store.SaveRevisionContent(imageObject, defaultSocialImage(), "", "ja", "image/png", "image.png"); err != nil {
t.Fatal(err)
}
w = call(cityPageHref(imagePath), "")
tags = socialTags(t, w.Body.String())
if tags["og:image"] != "http://localhost"+cityPageHref("/v/raw/path:"+imagePath) || tags["twitter:card"] != "summary" {
t.Fatalf("image card=%v", tags)
}
imageURL, _ = url.Parse(tags["og:image"])
if w = call(imageURL.RequestURI(), ""); w.Code != 200 || !bytes.Equal(w.Body.Bytes(), defaultSocialImage()) {
t.Fatalf("raw image status=%d", w.Code)
}
for _, layout := range []string{"classic", "search", "directory"} {
if err = s.store.SetConfigInDomain(domain, "home_layout", layout); err != nil {
t.Fatal(err)
}
w = call("/", "")
if w.Code != 200 || socialTags(t, w.Body.String())["og:url"] != "http://localhost/" {
t.Fatalf("home %s=%d", layout, w.Code)
}
}
q, err := s.store.CreateZoneAtPath(domain, "actor", "quartier", "", "guide", "Guide", "City description", "/guide")
if err != nil {
t.Fatal(err)
}
w = call("/v/city/q/uuid:"+q, "")
tags = socialTags(t, w.Body.String())
if w.Code != 200 || tags["og:url"] != "http://localhost/v/city/q/guide" || tags["og:description"] != "City description" {
t.Fatalf("City tags=%v status=%d", tags, w.Code)
}
// Revocation is evaluated anew; public metadata is never cached by principal.
if err = s.store.SetConfigInDomain(domain, "authorization_mode", "enforcing"); err != nil {
t.Fatal(err)
}
if w = call(route, ""); w.Code != 303 {
t.Fatalf("revoked crawler=%d", w.Code)
}
}
func TestSocialOriginAndBoundedExtraction(t *testing.T) {
s, _, domain := configFixture(t)
if _, err := s.store.DB.Exec(`UPDATE campuses SET fqdn='wiki.example.test' WHERE uuid=(SELECT campus_uuid FROM domains WHERE uuid=?)`, domain); err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", "http://wiki.example.test/", nil)
r.Header.Set("X-Forwarded-Host", "evil.test")
r.Header.Set("X-Forwarded-Proto", "http")
r.Header.Set("Origin", "https://evil.test")
if got := s.socialOrigin(r); got != "https://wiki.example.test" {
t.Fatalf("origin=%s", got)
}
if got := s.socialOrigin(httptest.NewRequest("GET", "http://evil.test/", nil)); got != "" {
t.Fatalf("unknown host=%s", got)
}
title, description := socialHTML(`` + strings.Repeat("あ", 100) + `
` + strings.Repeat("い", 1<<20) + `
OUTSIDE_BOUND
`)
if len([]rune(title)) > 90 || len([]rune(description)) > 200 || strings.Contains(description, "OUTSIDE_BOUND") {
t.Fatal("unbounded metadata")
}
for _, mime := range []string{"image/svg+xml", "application/pdf"} {
if err := s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err != nil {
t.Fatal(err)
}
m := s.socialMetadataForRequest(r, "page.gohtml", map[string]any{"Path": "/test", "ContentView": contentView{Kind: "image", ContentType: mime, SizeBytes: 20}})
if m.Image != "https://wiki.example.test"+socialImagePath {
t.Fatalf("unsupported image=%s", m.Image)
}
}
}
func TestSocialMetadataTitleAndShareTextUnderEnforcing(t *testing.T) {
s, _, domain := configFixture(t)
if err := s.store.SetConfigInDomain(domain, "authorization_mode", "enforcing"); err != nil {
t.Fatal(err)
}
r := httptest.NewRequest("GET", "http://localhost/notes/article", nil)
m := s.socialMetadataForRequest(r, "page.gohtml", map[string]any{
"Title": "Registered article title",
"SocialSource": `Visible heading
summary
`,
})
if m == nil {
t.Fatal("missing metadata")
}
if !m.NoIndex {
t.Fatal("enforcing metadata must remain noindex")
}
if m.Title != "Visible heading" {
t.Fatalf("title=%q", m.Title)
}
if m.ShareText != "Visible heading - "+s.getWikiName(domain) {
t.Fatalf("share text=%q", m.ShareText)
}
if strings.Contains(m.ShareText, m.URL) || strings.Contains(m.ShareText, "\n") {
t.Fatalf("share text must not duplicate URL: %q", m.ShareText)
}
}
func TestSocialImageHTTP(t *testing.T) {
for _, method := range []string{"GET", "HEAD", "POST"} {
r := httptest.NewRequest(method, socialImagePath, nil)
w := httptest.NewRecorder()
handleSocialImage(w, r)
if method == "POST" {
if w.Code != 405 {
t.Fatal(w.Code)
}
continue
}
if w.Code != 200 || w.Header().Get("Content-Type") != "image/png" {
t.Fatalf("%s=%d", method, w.Code)
}
if method == "HEAD" && w.Body.Len() != 0 {
t.Fatal("HEAD body")
}
}
r := httptest.NewRequest("GET", socialImagePath, nil)
r.Header.Set("If-None-Match", `"social-card-v1"`)
w := httptest.NewRecorder()
handleSocialImage(w, r)
if w.Code != http.StatusNotModified || w.Body.Len() != 0 {
t.Fatalf("conditional=%d", w.Code)
}
}