|
| 1 | +package sanitize |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// entityPayload is the canonical finding payload: the < and > around an |
| 9 | +// <img onerror> tag are HTML-entity-encoded, so bluemonday tokenizes them |
| 10 | +// as character data and StrictPolicy passes them through untouched. The |
| 11 | +// post-sanitize html.UnescapeString then reconstitutes a live tag. |
| 12 | +const entityPayload = `<img src=x onerror=alert(1)>` |
| 13 | + |
| 14 | +// stripAllTags removes every remaining <...> tag from s; used as an |
| 15 | +// assertion helper (the sanitizer's contract is "no HTML in output"). |
| 16 | +func stripAllTags(t *testing.T, s string) string { |
| 17 | + t.Helper() |
| 18 | + var b strings.Builder |
| 19 | + inTag := false |
| 20 | + for _, r := range s { |
| 21 | + switch r { |
| 22 | + case '<': |
| 23 | + inTag = true |
| 24 | + case '>': |
| 25 | + inTag = false |
| 26 | + continue |
| 27 | + } |
| 28 | + if !inTag { |
| 29 | + b.WriteRune(r) |
| 30 | + } |
| 31 | + } |
| 32 | + return b.String() |
| 33 | +} |
| 34 | + |
| 35 | +func assertNoRawTag(t *testing.T, policy Policy, label string) { |
| 36 | + t.Helper() |
| 37 | + out := policy.Sanitize(entityPayload) |
| 38 | + if out == entityPayload { |
| 39 | + t.Errorf("%s: payload passed through unmodified: %q", label, out) |
| 40 | + } |
| 41 | + if strings.Contains(strings.ToLower(out), "<img") { |
| 42 | + t.Errorf("%s: live <img tag reconstituted after sanitize: %q", label, out) |
| 43 | + } |
| 44 | + if strings.Contains(strings.ToLower(out), "onerror") { |
| 45 | + t.Errorf("%s: onerror handler survived sanitize: %q", label, out) |
| 46 | + } |
| 47 | + // After stripping any tag residue, no raw tag markup should remain. |
| 48 | + if got := stripAllTags(t, out); got != out && strings.Contains(strings.ToLower(out), "<img") { |
| 49 | + t.Errorf("%s: output still contained tag markup after tag-strip: %q", label, out) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestPlainTextFieldEntityEncodedXSS(t *testing.T) { |
| 54 | + assertNoRawTag(t, PlainTextField, "PlainTextField") |
| 55 | +} |
| 56 | + |
| 57 | +func TestShortIdentifierEntityEncodedXSS(t *testing.T) { |
| 58 | + assertNoRawTag(t, ShortIdentifier, "ShortIdentifier") |
| 59 | +} |
| 60 | + |
| 61 | +func TestRichTextEntityEncodedXSS(t *testing.T) { |
| 62 | + assertNoRawTag(t, RichText, "RichText") |
| 63 | +} |
| 64 | + |
| 65 | +func TestLongDocumentEntityEncodedXSS(t *testing.T) { |
| 66 | + assertNoRawTag(t, LongDocument, "LongDocument") |
| 67 | +} |
| 68 | + |
| 69 | +func TestCommentEntityEncodedXSS(t *testing.T) { |
| 70 | + assertNoRawTag(t, Comment, "Comment") |
| 71 | +} |
| 72 | + |
| 73 | +// Legit entity-encoded prose (e.g. "5 < 6 > 4") must survive as |
| 74 | +// plain decoded text, not be eaten. Regression guard for the |
| 75 | +// second-sanitize pass turning into over-stripping. |
| 76 | +func TestPlainTextFieldPreservesDecodedEntities(t *testing.T) { |
| 77 | + got := PlainTextField.Sanitize("5 < 6 > 4") |
| 78 | + if want := "5 < 6 > 4"; got != want { |
| 79 | + t.Errorf("PlainTextField decoded-prose: got %q want %q", got, want) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestRichTextPreservesDecodedEntities(t *testing.T) { |
| 84 | + got := RichText.Sanitize("5 < 6 > 4") |
| 85 | + if want := "5 < 6 > 4"; got != want { |
| 86 | + t.Errorf("RichText decoded-prose: got %q want %q", got, want) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// brOnly path must still keep a real <br /> on round-trip (its whole |
| 91 | +// reason for existing — Milkdown blank-line preservation). |
| 92 | +func TestRichTextPreservesBreakTag(t *testing.T) { |
| 93 | + got := RichText.Sanitize("line one<br />line two") |
| 94 | + if !strings.Contains(got, "<br />") { |
| 95 | + t.Errorf("RichText lost <br />: got %q", got) |
| 96 | + } |
| 97 | + if strings.Contains(strings.ToLower(got), "<img") { |
| 98 | + t.Errorf("RichText leaked img: %q", got) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// A javascript: Markdown link is neutralized regardless of HTML. |
| 103 | +func TestCommentNeutralizesDangerousMarkdownURL(t *testing.T) { |
| 104 | + got := Comment.Sanitize("[click](javascript:alert(1))") |
| 105 | + if strings.Contains(strings.ToLower(got), "javascript:") { |
| 106 | + t.Errorf("Comment kept dangerous scheme: %q", got) |
| 107 | + } |
| 108 | +} |
0 commit comments