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

perf(misconf): do not convert contents of a YAML file to string #7292

Merged
merged 2 commits into from
Aug 6, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions pkg/iac/detection/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,15 @@ func init() {
}
data := buf.Bytes()

marker := "\n---\n"
altMarker := "\r\n---\r\n"
if bytes.Contains(data, []byte(altMarker)) {
marker := []byte("\n---\n")
altMarker := []byte("\r\n---\r\n")
if bytes.Contains(data, altMarker) {
marker = altMarker
}

for _, partial := range strings.Split(string(data), marker) {
for _, partial := range bytes.Split(data, marker) {
var result map[string]any
if err := yaml.Unmarshal([]byte(partial), &result); err != nil {
if err := yaml.Unmarshal(partial, &result); err != nil {
continue
}
match := true
Expand Down
11 changes: 5 additions & 6 deletions pkg/iac/scanners/yaml/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"io/fs"
"path/filepath"
"strings"

"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -85,15 +84,15 @@ func (p *Parser) ParseFile(_ context.Context, fsys fs.FS, path string) ([]any, e

var results []any

marker := "\n---\n"
altMarker := "\r\n---\r\n"
if bytes.Contains(contents, []byte(altMarker)) {
marker := []byte("\n---\n")
altMarker := []byte("\r\n---\r\n")
if bytes.Contains(contents, altMarker) {
marker = altMarker
}

for _, partial := range strings.Split(string(contents), marker) {
for _, partial := range bytes.Split(contents, marker) {
var target any
if err := yaml.Unmarshal([]byte(partial), &target); err != nil {
if err := yaml.Unmarshal(partial, &target); err != nil {
return nil, err
}
results = append(results, target)
Expand Down