Skip to content

Commit

Permalink
feat: option to specify HTML escaping for JSON format (#72)
Browse files Browse the repository at this point in the history
Signed-off-by: Keith Zantow <[email protected]>

Signed-off-by: Keith Zantow <[email protected]>
  • Loading branch information
kzantow authored Dec 22, 2022
1 parent 8b135f6 commit 41a1ac5
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
23 changes: 20 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,22 @@ type BOMEncoder interface {

// SetPretty toggles prettified output.
SetPretty(pretty bool) BOMEncoder

// SetEscapeHTML toggles escaped HTML output.
SetEscapeHTML(escapeHTML bool) BOMEncoder
}

func NewBOMEncoder(writer io.Writer, format BOMFileFormat) BOMEncoder {
if format == BOMFileFormatJSON {
return &jsonBOMEncoder{writer: writer}
return &jsonBOMEncoder{writer: writer, escapeHTML: true}
}
return &xmlBOMEncoder{writer: writer}
}

type jsonBOMEncoder struct {
writer io.Writer
pretty bool
writer io.Writer
pretty bool
escapeHTML bool
}

// Encode implements the BOMEncoder interface.
Expand All @@ -56,6 +60,7 @@ func (j jsonBOMEncoder) Encode(bom *BOM) error {
}

encoder := json.NewEncoder(j.writer)
encoder.SetEscapeHTML(j.escapeHTML)
if j.pretty {
encoder.SetIndent("", " ")
}
Expand All @@ -79,6 +84,12 @@ func (j *jsonBOMEncoder) SetPretty(pretty bool) BOMEncoder {
return j
}

// SetEscapeHTML implements the BOMEncoder interface.
func (j *jsonBOMEncoder) SetEscapeHTML(escapeHTML bool) BOMEncoder {
j.escapeHTML = escapeHTML
return j
}

type xmlBOMEncoder struct {
writer io.Writer
pretty bool
Expand Down Expand Up @@ -113,3 +124,9 @@ func (x *xmlBOMEncoder) SetPretty(pretty bool) BOMEncoder {
x.pretty = pretty
return x
}

// SetEscapeHTML implements the BOMEncoder interface.
func (j *xmlBOMEncoder) SetEscapeHTML(escapeHTML bool) BOMEncoder {
// NOOP -- XML always needs to escape HTML
return j
}
64 changes: 64 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,70 @@ func TestJsonBOMEncoder_SetPretty(t *testing.T) {
`, buf.String())
}

func TestJsonBOMEncoder_SetEscapeHTML_true(t *testing.T) {
buf := new(bytes.Buffer)
encoder := NewBOMEncoder(buf, BOMFileFormatJSON)
encoder.SetPretty(true)
encoder.SetEscapeHTML(true)

bom := NewBOM()
bom.Metadata = &Metadata{
Authors: &[]OrganizationalContact{
{
Name: "some&<\"Name",
},
},
}

require.NoError(t, encoder.Encode(bom))

assert.Equal(t, `{
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"version": 1,
"metadata": {
"authors": [
{
"name": "some\u0026\u003c\"Name"
}
]
}
}
`, buf.String())
}

func TestJsonBOMEncoder_SetEscapeHTML_false(t *testing.T) {
buf := new(bytes.Buffer)
encoder := NewBOMEncoder(buf, BOMFileFormatJSON)
encoder.SetPretty(true)
encoder.SetEscapeHTML(false)

bom := NewBOM()
bom.Metadata = &Metadata{
Authors: &[]OrganizationalContact{
{
Name: "some+<&\"Name",
},
},
}

require.NoError(t, encoder.Encode(bom))

assert.Equal(t, `{
"bomFormat": "CycloneDX",
"specVersion": "1.4",
"version": 1,
"metadata": {
"authors": [
{
"name": "some+<&\"Name"
}
]
}
}
`, buf.String())
}

func TestXmlBOMEncoder_SetPretty(t *testing.T) {
buf := new(bytes.Buffer)
encoder := NewBOMEncoder(buf, BOMFileFormatXML)
Expand Down

0 comments on commit 41a1ac5

Please sign in to comment.