Skip to content

Commit d7339af

Browse files
committed
add support for DangerousContents
1 parent 06f0090 commit d7339af

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

daz.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,20 @@ var selfClosingTags = map[string]int{
2121
// <a href="#"> => Attr{"href": "#"}
2222
type Attr map[string]string
2323

24+
type HTML func() string
25+
26+
// dangerous contents type
27+
type dangerousContents func() (string, bool)
28+
29+
// UnsafeContent allows injection of JS or HTML from functions
30+
func UnsafeContent(str string) dangerousContents {
31+
return func() (string, bool) {
32+
return str, true
33+
}
34+
}
35+
2436
// H is the base HTML func
25-
func H(el string, attrs ...interface{}) func() string {
37+
func H(el string, attrs ...interface{}) HTML {
2638
contents := []string{}
2739
attributes := ""
2840
for _, v := range attrs {
@@ -34,11 +46,16 @@ func H(el string, attrs ...interface{}) func() string {
3446
case []string:
3547
children := strings.Join(v, "")
3648
contents = append(contents, escape(children))
37-
case []func() string:
49+
case []HTML:
3850
children := subItems(v)
3951
contents = append(contents, children)
40-
case func() string:
52+
case HTML:
4153
contents = append(contents, v())
54+
case dangerousContents:
55+
t, _ := v()
56+
contents = append(contents, t)
57+
case func() string:
58+
contents = append(contents, escape(v()))
4259
default:
4360
contents = append(contents, escape(fmt.Sprintf("%v", v)))
4461
}
@@ -56,7 +73,7 @@ func escape(str string) string {
5673
return html.EscapeString(str)
5774
}
5875

59-
func subItems(attrs []func() string) string {
76+
func subItems(attrs []HTML) string {
6077
res := []string{}
6178
for _, v := range attrs {
6279
res = append(res, v())

daz_test.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var fixture4 = "<div class='bg-grey-50' data-id='div-1'>content</div>"
1111
var fixture5 = "<div>O&#39;Brian<input type='text' value='input value&#39;s' /></div>"
1212
var fixture6 = "<div><img src='https://example.com/image.png' /><br /></div>"
1313
var fixture7 = "<div>&lt;script&gt;alert(&#39;xss&#39;)&lt;/script&gt;</div>"
14+
var fixture8 = "<div><script>alert('xss')</script></div>"
1415

1516
func TestBasicRender(t *testing.T) {
1617
attrs := Attr{"class": "app view"}
@@ -33,11 +34,23 @@ func TestStringItems(t *testing.T) {
3334
}
3435
}
3536

36-
func TestItems(t *testing.T) {
37+
func TestItems1(t *testing.T) {
3738
one := H("div", "one")
3839
two := func() string { return "one" }
3940
three := H("", "text")
40-
items := []func() string{one, two, three}
41+
items := []HTML{one, two, three}
42+
43+
root := H("div", items)
44+
res := root()
45+
if res != fixture3 {
46+
t.Errorf("got: %v wanted: %v", res, fixture3)
47+
}
48+
}
49+
func TestItems2(t *testing.T) {
50+
one := H("div", "one")
51+
two := func() string { return "one" }
52+
three := H("", "text")
53+
items := []HTML{one, two, three}
4154

4255
root := H("div", items)
4356
res := root()
@@ -81,6 +94,15 @@ func TestXSS1(t *testing.T) {
8194
}
8295
}
8396

97+
func TestUnsafeContent(t *testing.T) {
98+
injection := "<script>alert('xss')</script>"
99+
root := H("div", UnsafeContent(injection))
100+
res := root()
101+
if res != fixture8 {
102+
t.Errorf("got: %v wanted: %v", res, fixture8)
103+
}
104+
}
105+
84106
func BenchmarkBasicRender(b *testing.B) {
85107
attrs := Attr{"class": "app view"}
86108
nav := H("nav", "Welcome")

0 commit comments

Comments
 (0)