package postcard
import (
"context"
"errors"
"strings"
"testing"
)
// schemeFiller surrounds a case's markup, so a snippet that points at the
// offending markup is told apart from one that starts the document.
var schemeFiller = strings.Repeat("plain words here ", 6)
// TestURLSchemeCheck covers the anchored, decoded URL-scheme check of
// rendered HTML (WR-05): prose and URLs that merely contain a scheme word
// pass; every URL-bearing attribute, CSS, meta refresh and every
// character-reference or whitespace-split spelling of a script or data URL
// is refused, on a send with inlining on and off and in the editor.
func TestURLSchemeCheck(t *testing.T) {
t.Parallel()
accepts := []struct{ name, html string }{
{"prose_metadata", `
See the metadata: field
`},
{"prose_big_data", `
Big Data: Summit
`},
{"img_alt", ``},
{"title_text", `
Tips
`},
{"scheme_in_query", `x`},
// Escaped markup in a raw-text element is text to every reader.
{"textarea_escaped_link", ``},
{"title_escaped_link", ``},
{"noscript_safe_link", ``},
{"outlook_conditional_vml", ``},
}
for _, tc := range accepts {
t.Run("accept/"+tc.name, func(t *testing.T) {
t.Parallel()
if err := validateHTML("
" + schemeFiller + tc.html + schemeFiller + "
"); err != nil {
t.Errorf("validateHTML(%s) = %v, want nil", tc.html, err)
}
})
}
const schemeMsg = "dangerous URL scheme"
refuses := []struct {
name, html string
// marker is a raw text of the offending markup the snippet holds.
marker string
msg string
}{
{"href_javascript", `x`, `href="javascript:alert(1)"`, schemeMsg},
{"href_space_mixed_case", `x`, `JavaScript:alert(1)`, schemeMsg},
{"src_data", ``, `data:image/png`, schemeMsg},
{"href_vbscript", `x`, `vbscript:msgbox(1)`, schemeMsg},
{"form_action", ``, `action="javascript:x"`, schemeMsg},
{"button_formaction", ``, `formaction="javascript:x"`, schemeMsg},
{"table_background", `