Skip to content

Commit e1330cd

Browse files
committed
added XSS protection
1 parent fb1f303 commit e1330cd

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

visualize.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"html"
78
"log"
89
"net/http"
910
"os"
@@ -26,12 +27,12 @@ type srcFile struct {
2627
}
2728

2829
func (sf srcFile) MarshalJSON() ([]byte, error) {
29-
return []byte(fmt.Sprintf(`{"name": "%s", "size": %d}`, sf.Path, sf.Size)), nil
30+
return []byte(fmt.Sprintf(`{"name": "%s", "size": %d}`, html.EscapeString(sf.Path), sf.Size)), nil
3031
}
3132

3233
func (sd srcDir) MarshalJSON() ([]byte, error) {
3334
buffer := bytes.NewBufferString("{\n")
34-
_, err := buffer.WriteString(fmt.Sprintf(` "name": "%s"`, strings.ReplaceAll(sd.Path, "\\", "\\\\")))
35+
_, err := buffer.WriteString(fmt.Sprintf(` "name": "%s"`, html.EscapeString(strings.ReplaceAll(sd.Path, "\\", "\\\\"))))
3536
if err != nil {
3637
return nil, err
3738
}
@@ -168,7 +169,7 @@ func netHandleDisplay(w http.ResponseWriter, r *http.Request, hierarchy srcDir,
168169
err = tmpl.Execute(w, struct {
169170
DisplayTitle string
170171
ErrorStr string
171-
}{DisplayTitle: displayTitle, ErrorStr: errorStr})
172+
}{DisplayTitle: html.EscapeString(displayTitle), ErrorStr: html.EscapeString(errorStr)})
172173
if err != nil {
173174
log.Panic(err)
174175
}

0 commit comments

Comments
 (0)