Skip to content

Commit

Permalink
ensure null JS and valuecounts data is omitted from result struct ins…
Browse files Browse the repository at this point in the history
…tead of having empty arrays

Signed-off-by: Max Fisher <[email protected]>
  • Loading branch information
maxfisher-g committed Oct 17, 2023
1 parent ac3f708 commit 9083cea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
16 changes: 12 additions & 4 deletions internal/staticanalysis/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ func (r *Result) ToAPIResults() *staticanalysis.Results {
fr.DetectedType = f.Basic.DetectedType
fr.Size = f.Basic.Size
fr.SHA256 = f.Basic.SHA256
fr.LineLengths = f.Basic.LineLengths
// only populate value counts if nonempty
if f.Basic.LineLengths.Len() > 0 {
fr.LineLengths = &f.Basic.LineLengths
}
}
if f.Parsing != nil && f.Parsing.Language == parsing.JavaScript {
fr.Js = staticanalysis.JsData{
fr.Js = &staticanalysis.JsData{
Identifiers: f.Parsing.Identifiers,
StringLiterals: f.Parsing.StringLiterals,
IntLiterals: f.Parsing.IntLiterals,
Expand All @@ -67,8 +70,13 @@ func (r *Result) ToAPIResults() *staticanalysis.Results {
}
}
if f.Signals != nil {
fr.IdentifierLengths = f.Signals.IdentifierLengths
fr.StringLengths = f.Signals.StringLengths
// only populate value counts if nonempty
if f.Signals.IdentifierLengths.Len() > 0 {
fr.IdentifierLengths = &f.Signals.IdentifierLengths
}
if f.Signals.StringLengths.Len() > 0 {
fr.StringLengths = &f.Signals.StringLengths
}
fr.Base64Strings = f.Signals.Base64Strings
fr.HexStrings = f.Signals.HexStrings
fr.IPAddresses = f.Signals.IPAddresses
Expand Down
10 changes: 5 additions & 5 deletions internal/staticanalysis/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestResult_ToAPIResults(t *testing.T) {
DetectedType: "plain text",
Size: 10,
SHA256: "aabbbcc",
LineLengths: valuecounts.Count([]int{1, 2, 3, 4}),
LineLengths: valuecounts.Count([]int{1, 2, 3, 4}).Ptr(),
},
}},
},
Expand Down Expand Up @@ -153,8 +153,8 @@ func TestResult_ToAPIResults(t *testing.T) {
DetectedType: "javascript source file",
Size: 100,
SHA256: "abc123def456",
LineLengths: valuecounts.Count([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}),
Js: staticanalysis.JsData{
LineLengths: valuecounts.Count([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).Ptr(),
Js: &staticanalysis.JsData{
Identifiers: []token.Identifier{
{
Name: "myvar",
Expand Down Expand Up @@ -207,8 +207,8 @@ func TestResult_ToAPIResults(t *testing.T) {
},
},
},
IdentifierLengths: valuecounts.Count([]int{5}),
StringLengths: valuecounts.Count([]int{5}),
IdentifierLengths: valuecounts.Count([]int{5}).Ptr(),
StringLengths: valuecounts.Count([]int{5}).Ptr(),
SuspiciousIdentifiers: []staticanalysis.SuspiciousIdentifier{
{
Name: "a",
Expand Down
28 changes: 14 additions & 14 deletions pkg/api/staticanalysis/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ func CreateRecord(r *Results, k analysisrun.Key) *Record {
// mandatory field, and holds the path to the file relative to the package root.
// Other fields may be present or missing depending on whether relevant data was collected.
type FileResult struct {
Filename string `json:"filename"`
DetectedType string `json:"detected_type,omitempty"`
Size int64 `json:"size,omitempty"`
SHA256 string `json:"sha256,omitempty"`
LineLengths valuecounts.ValueCounts `json:"line_lengths,omitempty"`
Js JsData `json:"js,omitempty"`
IdentifierLengths valuecounts.ValueCounts `json:"identifier_lengths,omitempty"`
StringLengths valuecounts.ValueCounts `json:"string_lengths,omitempty"`
Base64Strings []string `json:"base64_strings,omitempty"`
HexStrings []string `json:"hex_strings,omitempty"`
IPAddresses []string `json:"ip_addresses,omitempty"`
URLs []string `json:"urls,omitempty"`
SuspiciousIdentifiers []SuspiciousIdentifier `json:"suspicious_identifiers,omitempty"`
EscapedStrings []EscapedString `json:"escaped_strings,omitempty"`
Filename string `json:"filename"`
DetectedType string `json:"detected_type,omitempty"`
Size int64 `json:"size,omitempty"`
SHA256 string `json:"sha256,omitempty"`
LineLengths *valuecounts.ValueCounts `json:"line_lengths,omitempty"`
Js *JsData `json:"js,omitempty"`
IdentifierLengths *valuecounts.ValueCounts `json:"identifier_lengths,omitempty"`
StringLengths *valuecounts.ValueCounts `json:"string_lengths,omitempty"`
Base64Strings []string `json:"base64_strings,omitempty"`
HexStrings []string `json:"hex_strings,omitempty"`
IPAddresses []string `json:"ip_addresses,omitempty"`
URLs []string `json:"urls,omitempty"`
SuspiciousIdentifiers []SuspiciousIdentifier `json:"suspicious_identifiers,omitempty"`
EscapedStrings []EscapedString `json:"escaped_strings,omitempty"`
}

type JsData struct {
Expand Down
7 changes: 7 additions & 0 deletions pkg/valuecounts/value_counts.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ func Count(data []int) ValueCounts {
return vc
}

// Ptr returns a pointer to this ValueCounts. This function is useful
// constructing a ValueCounts using one of the above functions and the
// result needs to be assigned directly to a pointer variable.
func (vc ValueCounts) Ptr() *ValueCounts {
return &vc
}

// Len returns the number of values stored by this ValueCounts.
// It is equivalent to the length of the slice returned by ToPairs()
func (vc ValueCounts) Len() int {
Expand Down

0 comments on commit 9083cea

Please sign in to comment.