about summary refs log tree commit diff stats
path: root/gomponents_test.go
diff options
context:
space:
mode:
authorMarkus Wüstenberg2021-05-18 14:21:53 +0200
committerGitHub2021-05-18 14:21:53 +0200
commit3e9e00ca0dc6b58e58694d84c97a1d2f2ab4002b (patch)
tree38b6bfce3f282cd2c758687aaa88fdda88a9f661 /gomponents_test.go
parentac7471aac69fcb4e9cd460dae39b0183ec3d10df (diff)
downloadgomponents-3e9e00ca0dc6b58e58694d84c97a1d2f2ab4002b.tar.lz
gomponents-3e9e00ca0dc6b58e58694d84c97a1d2f2ab4002b.tar.zst
gomponents-3e9e00ca0dc6b58e58694d84c97a1d2f2ab4002b.zip
Escape Attr values (#77)
Because this can be a place of injection if untrusted data is passed, escape all attribute values.

Fixes #74.
Diffstat (limited to 'gomponents_test.go')
-rw-r--r--gomponents_test.go14
1 files changed, 14 insertions, 0 deletions
diff --git a/gomponents_test.go b/gomponents_test.go
index c88a796..dffa64c 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -54,6 +54,11 @@ func TestAttr(t *testing.T) {
 			t.FailNow()
 		}
 	})
+
+	t.Run("escapes attribute values", func(t *testing.T) {
+		a := g.Attr(`id`, `hat"><script`)
+		assert.Equal(t, ` id="hat&#34;&gt;&lt;script"`, a)
+	})
 }
 
 func BenchmarkAttr(b *testing.B) {
@@ -132,6 +137,15 @@ func TestEl(t *testing.T) {
 	})
 }
 
+func BenchmarkEl(b *testing.B) {
+	b.Run("normal elements", func(b *testing.B) {
+		for i := 0; i < b.N; i++ {
+			e := g.El("div")
+			_ = e.Render(&strings.Builder{})
+		}
+	})
+}
+
 type erroringWriter struct{}
 
 func (w *erroringWriter) Write(p []byte) (n int, err error) {