Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to specify HTML escaping for JSON format #72

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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