Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/REACT_TEST_PARITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ re-renders/effects are observable inline. SSR behavior is tested via
| ReactDOMServerIntegrationUntrustedURL | javascript: URL sanitization | βœ… | ui/url_sanitization_test.go (+ found v3.4.7) |
| ReactDOMServerIntegrationAttributes | attr boolean/nil/numeric/escaping | βœ… | ui/attribute_serialization_test.go |
| ReactDOMServerIntegrationElements | void elements, text escaping | βœ… | ui/void_element_test.go, ui/text_escaping_test.go |
| ReactDOMServerIntegrationElements (dangerouslySetInnerHTML) | raw HTML in SSR | βœ… | ui/raw_html_ssr_test.go β€” html.RawHTML renders raw but sanitizes (script/js-url/on*= stripped); RawHTMLUnsafe verbatim |
| ReactDOMServerIntegrationBasic | basic SSR render, nested components, nil | βœ… | ui/ssr_components_test.go |
| ReactDOMServerIntegrationFragment | fragment SSR | βœ… | ui/ssr_components_test.go (component->fragment hoist) |
| ReactDOMServerIntegrationSpecialTypes | numbers/bools/null children | 🚫 | Go static typing (ui.Node children) |
Expand Down
50 changes: 50 additions & 0 deletions ui/raw_html_ssr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//go:build !js || !wasm

package ui_test

import (
"strings"
"testing"

"github.com/monstercameron/GoWebComponents/html"
"github.com/monstercameron/GoWebComponents/ui"
)

// Ported from React's ReactDOMServerIntegrationElements dangerouslySetInnerHTML
// tests, adapted to GWC's html.RawHTML β€” which renders raw markup but sanitizes
// it by default (a safety improvement over React's raw injection). html.RawHTMLUnsafe
// renders the markup verbatim.
func TestRawHTMLSSR(t *testing.T) {
render := func(parseNodes []ui.Node) string {
parseOut, parseErr := ui.RenderToString(html.Div(html.Props{}, parseNodes...))
if parseErr != nil {
t.Fatalf("render error: %v", parseErr)
}
return parseOut
}

// Safe markup is preserved.
if parseGot := render(html.RawHTML("<b>bold</b>")); parseGot != "<div><b>bold</b></div>" {
t.Errorf("safe RawHTML = %q, want <div><b>bold</b></div>", parseGot)
}

// A <script> is sanitized out; surrounding text is kept.
if parseGot := render(html.RawHTML("<script>alert(1)</script>keep")); strings.Contains(parseGot, "<script") || !strings.Contains(parseGot, "keep") {
t.Errorf("RawHTML did not sanitize <script>: %q", parseGot)
}

// A javascript: href is stripped by the sanitizer.
if parseGot := render(html.RawHTML(`<a href="javascript:alert(1)">x</a>`)); strings.Contains(strings.ToLower(parseGot), "javascript:") {
t.Errorf("RawHTML did not strip javascript: href: %q", parseGot)
}

// An on*= handler is stripped.
if parseGot := render(html.RawHTML(`<img src=x onerror="alert(1)">`)); strings.Contains(strings.ToLower(parseGot), "onerror") {
t.Errorf("RawHTML did not strip on*= handler: %q", parseGot)
}

// RawHTMLUnsafe renders verbatim (no sanitization).
if parseGot := render(html.RawHTMLUnsafe("<b>raw</b>")); parseGot != "<div><b>raw</b></div>" {
t.Errorf("RawHTMLUnsafe = %q, want <div><b>raw</b></div>", parseGot)
}
}