package render import ( "strings" "testing" "github.com/gomarkdown/markdown/parser" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" xhtml "golang.org/x/net/html" ) // permissive is the grammar notificationExtensions used to enable. Tests that // must exercise the escaper rather than the allowlist render against it. const permissive = parser.CommonExtensions | parser.HardLineBreak // suspendedBody mirrors the live TemplateUserAccountSuspended body: the // untrusted value sits mid-paragraph with trusted text on both sides. func suspendedBody(value string) string { return "The account belongs to **" + value + "** and it was suspended by **rob**." } // TestEscapeMarkdownFenceInfo covers the info-string sink that made backtick // inlineCritical: a `"` closes the class attribute and a `>` closes the tag. func TestEscapeMarkdownFenceInfo(t *testing.T) { t.Parallel() const info = `">Click here` // Each payload needs a line after the closing fence, or the template's // trailing text lands on it and the fence stops being one. for name, value := range map[string]string{ "Anchor": "Eve\n\n```" + info + "\nhidden\n```\nmore", "Image": "Eve\n\n```\">\nhidden\n```\nmore", "Tilde": "Eve\n\n~~~" + info + "\nhidden\n~~~\nmore", "AtStart": "```" + info + "\nhidden\n```\nmore", } { t.Run(name, func(t *testing.T) { t.Parallel() html := HTMLFromNotificationMarkdown(suspendedBody(EscapeMarkdown(value))) assert.NotContains(t, html, `class="language-`, "an info string reached the class attribute: %s", html) assert.NotContains(t, html, "foo"}, {"Resources:\n\n- one\n- two\n", "
  • "}, {"marked as [**dormant**](https://coder.com/docs/y) because", ` next", "~tilde name", "= x", "~~strike~~", "=?utf-8?q?x?=", "~", "~~", "=== and more", "a\n===", } { plain, err := PlaintextFromMarkdown(EscapeMarkdown(value) + " end") require.NoError(t, err) assert.NotContains(t, plain, `\`, "value %q was escaped when it is not a fold construct", value) } }) } // TestRecoverToEscapedSource drives renderHTML's panic guard directly: safeURL // closed the only input known to panic it. func TestRecoverToEscapedSource(t *testing.T) { t.Parallel() const src = ` & "quoted"` got := recoverToEscapedSource(src, func() string { panic("boom") }) assert.Equal(t, xhtml.EscapeString(src), got) // The point of escaping rather than returning the source: no markup escapes. assert.NotContains(t, got, "