Skip to content

Commit

Permalink
Stop HTML-escaping on agent status output (DataDog#4854)
Browse files Browse the repository at this point in the history
  • Loading branch information
prognant committed Feb 14, 2020
1 parent 3ea70de commit 19d64a5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
56 changes: 39 additions & 17 deletions pkg/status/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ package status
import (
"encoding/json"
"fmt"
"html/template"
htemplate "html/template"
"strconv"
"strings"
ttemplate "text/template"
"time"
"unicode"

Expand All @@ -20,14 +21,35 @@ import (
"golang.org/x/text/unicode/norm"
)

// Fmap return a fresh copy of a map of utility functions for templating
func Fmap() template.FuncMap {
return template.FuncMap{
// Fmap return a fresh copy of a map of utility functions for HTML templating
func Fmap() htemplate.FuncMap {
return htemplate.FuncMap{
"doNotEscape": doNotEscape,
"lastError": lastError,
"lastErrorTraceback": func(s string) htemplate.HTML { return doNotEscape(lastErrorTraceback(s)) },
"lastErrorMessage": func(s string) htemplate.HTML { return doNotEscape(lastErrorMessage(s)) },
"configError": configError,
"printDashes": printDashes,
"formatUnixTime": formatUnixTime,
"humanize": mkHuman,
"humanizeDuration": mkHumanDuration,
"toUnsortedList": toUnsortedList,
"formatTitle": formatTitle,
"add": add,
"status": status,
"redText": redText,
"yellowText": yellowText,
"greenText": greenText,
"ntpWarning": ntpWarning,
"version": getVersion,
}
}

// Textfmap return a fresh copy of a map of utility functions for text templating
func Textfmap() ttemplate.FuncMap {
return ttemplate.FuncMap{
"lastErrorTraceback": lastErrorTraceback,
"lastErrorMessage": lastErrorMessage,
"configError": configError,
"printDashes": printDashes,
"formatUnixTime": formatUnixTime,
"humanize": mkHuman,
Expand All @@ -44,40 +66,40 @@ func Fmap() template.FuncMap {
}
}

func doNotEscape(value string) template.HTML {
return template.HTML(value)
func doNotEscape(value string) htemplate.HTML {
return htemplate.HTML(value)
}

func configError(value string) template.HTML {
return template.HTML(value + "\n")
func configError(value string) htemplate.HTML {
return htemplate.HTML(value + "\n")
}

func lastError(value string) template.HTML {
return template.HTML(value)
func lastError(value string) htemplate.HTML {
return htemplate.HTML(value)
}

func lastErrorTraceback(value string) template.HTML {
func lastErrorTraceback(value string) string {
var lastErrorArray []map[string]string

err := json.Unmarshal([]byte(value), &lastErrorArray)
if err != nil || len(lastErrorArray) == 0 {
return template.HTML("No traceback")
return "No traceback"
}
lastErrorArray[0]["traceback"] = strings.Replace(lastErrorArray[0]["traceback"], "\n", "\n ", -1)
lastErrorArray[0]["traceback"] = strings.TrimRight(lastErrorArray[0]["traceback"], "\n\t ")
return template.HTML(lastErrorArray[0]["traceback"])
return lastErrorArray[0]["traceback"]
}

// lastErrorMessage converts the last error message to html
func lastErrorMessage(value string) template.HTML {
func lastErrorMessage(value string) string {
var lastErrorArray []map[string]string
err := json.Unmarshal([]byte(value), &lastErrorArray)
if err == nil && len(lastErrorArray) > 0 {
if msg, ok := lastErrorArray[0]["message"]; ok {
return template.HTML(msg)
return msg
}
}
return template.HTML(value)
return value
}

// formatUnixTime formats the unix time to make it more readable
Expand Down
6 changes: 2 additions & 4 deletions pkg/status/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io"
"text/template"

"github.com/DataDog/datadog-agent/pkg/config"
)

var (
fmap = Fmap()
)
var fmap = Textfmap()

// FormatStatus takes a json bytestring and prints out the formatted statuspage
func FormatStatus(data []byte) (string, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/status/templates/collector.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Collector
{{ $CheckName }}
{{printDashes $CheckName "-"}}
{{- range $idx, $warning := $warnings}}
{{ doNotEscape $warning }}
{{$warning}}
{{- end }}
{{end}}
{{- end }}
Expand All @@ -81,7 +81,7 @@ Collector

instance {{$idx}}:

{{ doNotEscape $err }}
{{$err}}
{{- end }}
{{- end}}
{{- end }}
Expand All @@ -94,7 +94,7 @@ Collector
{{- range $checkname, $error := .ConfigErrors }}
{{$checkname}}
{{printDashes $checkname "-"}}
{{ configError $error }}
{{$error}}
{{- end }}
{{- end}}
{{- end }}
Expand All @@ -109,10 +109,10 @@ Collector
{{- range $kind, $err := $errors -}}
{{- if eq $kind "Python Check Loader" }}
{{$kind}}:
{{ doNotEscape $err }}
{{$err}}
{{ else }}
{{$kind}}:
{{ doNotEscape $err }}
{{$err}}
{{ end }}
{{- end }}
{{- end }}
Expand Down
2 changes: 1 addition & 1 deletion pkg/status/templates/header.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
NOTE: Changes made to this template should be reflected on the following templates, if applicable:
* cmd/agent/gui/views/templates/generalStatus.tmpl
*/}}{{printDashes .title "="}}
{{doNotEscape .title}}
{{.title}}
{{printDashes .title "="}}

Status date: {{.time}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enhancements:
- |
Stop doing HTML escaping on agent status command output
in order to properly display all non-alphanumeric
characters.

0 comments on commit 19d64a5

Please sign in to comment.