Skip to content

Commit

Permalink
Merge pull request #2322 from hashicorp/f-escape
Browse files Browse the repository at this point in the history
Fix escaping of HTML characters
  • Loading branch information
dadgar authored Feb 17, 2017
2 parents df9cd3d + c1e9ab6 commit 8b1061c
Show file tree
Hide file tree
Showing 30 changed files with 519 additions and 230 deletions.
9 changes: 7 additions & 2 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ const (
var (
// jsonHandle and jsonHandlePretty are the codec handles to JSON encode
// structs. The pretty handle will add indents for easier human consumption.
jsonHandle = &codec.JsonHandle{}
jsonHandlePretty = &codec.JsonHandle{Indent: 4}
jsonHandle = &codec.JsonHandle{
HTMLCharsAsIs: true,
}
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)

// HTTPServer is used to wrap an Agent and expose it over an HTTP interface
Expand Down
16 changes: 13 additions & 3 deletions command/data_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ package command

import (
"bytes"
"encoding/json"
"fmt"
"io"
"text/template"

"github.com/ugorji/go/codec"
)

var (
jsonHandlePretty = &codec.JsonHandle{
HTMLCharsAsIs: true,
Indent: 4,
}
)

//DataFormatter is a transformer of the data.
Expand Down Expand Up @@ -33,12 +41,14 @@ type JSONFormat struct {

// TransformData returns JSON format string data.
func (p *JSONFormat) TransformData(data interface{}) (string, error) {
out, err := json.MarshalIndent(&data, "", " ")
var buf bytes.Buffer
enc := codec.NewEncoder(&buf, jsonHandlePretty)
err := enc.Encode(data)
if err != nil {
return "", err
}

return string(out), nil
return buf.String(), nil
}

type TemplateFormat struct {
Expand Down
6 changes: 3 additions & 3 deletions command/data_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type testData struct {
}

const expectJSON = `{
"Region": "global",
"ID": "1",
"Name": "example"
"Name": "example",
"Region": "global"
}`

var (
Expand All @@ -36,7 +36,7 @@ func TestDataFormat(t *testing.T) {
}

if result != expectOutput[k] {
t.Fatalf("expected output: %s, actual: %s", expectOutput[k], result)
t.Fatalf("expected output:\n%s\nactual:\n%s", expectOutput[k], result)
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions command/inspect.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package command

import (
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -158,12 +157,17 @@ func (c *InspectCommand) Run(args []string) int {

// Print the contents of the job
req := api.RegisterJobRequest{Job: job}
buf, err := json.MarshalIndent(req, "", " ")
f, err := DataFormat("json", "")
if err != nil {
c.Ui.Error(fmt.Sprintf("Error converting job: %s", err))
c.Ui.Error(fmt.Sprintf("Error getting formatter: %s", err))
return 1
}

c.Ui.Output(string(buf))
out, err := f.TransformData(req)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error formatting the data: %s", err))
return 1
}
c.Ui.Output(out)
return 0
}
4 changes: 2 additions & 2 deletions vendor/github.com/ugorji/go/codec/0doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/ugorji/go/codec/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions vendor/github.com/ugorji/go/codec/binc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion vendor/github.com/ugorji/go/codec/cbor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 57 additions & 10 deletions vendor/github.com/ugorji/go/codec/decode.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/ugorji/go/codec/decode_go.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions vendor/github.com/ugorji/go/codec/decode_go14.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8b1061c

Please sign in to comment.