Skip to content

Commit

Permalink
Add counting of shell parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris McGehee committed Sep 17, 2021
1 parent 8b7da7c commit 92ab4ed
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions checks/shell_download_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package checks
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"net/url"
Expand All @@ -25,10 +26,12 @@ import (
"regexp"
"strings"

opencensusstats "go.opencensus.io/stats"
"mvdan.cc/sh/v3/syntax"

"github.com/ossf/scorecard/v2/checker"
sce "github.com/ossf/scorecard/v2/errors"
"github.com/ossf/scorecard/v2/stats"
)

var (
Expand Down Expand Up @@ -679,6 +682,8 @@ func validateShellFileAndRecord(pathfn string, content []byte, files map[string]
in := strings.NewReader(string(content))
f, err := syntax.NewParser().Parse(in, "")
if err != nil {
ctx := context.Background()
opencensusstats.Record(ctx, stats.ShellParseErrors.M(1))
// Note: this is caught by internal caller and only printed
// to avoid failing on shell scripts that our parser does not understand.
// Example: https://github.com/openssl/openssl/blob/master/util/shlib_wrap.sh.in
Expand Down
1 change: 1 addition & 0 deletions cron/worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func startMetricsExporter() (monitoring.Exporter, error) {
&stats.CheckErrorCount,
&stats.RepoRuntime,
&stats.OutgoingHTTPRequests,
&stats.ShellParseErrorCount,
&githubstats.GithubTokens); err != nil {
return nil, fmt.Errorf("error during view.Register: %w", err)
}
Expand Down
6 changes: 6 additions & 0 deletions stats/measures.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ var (
stats.UnitSeconds)
// HTTPRequests measures the count of HTTP requests.
HTTPRequests = stats.Int64("HTTPRequests", "Measures the count of HTTP requests", stats.UnitDimensionless)
// ShellParseErrors measures the count of errors when attempting to parse shell code.
ShellParseErrors = stats.Int64(
"ShellParseErrors",
"Measures the count of errors when attempting to parse shell code",
stats.UnitDimensionless,
)
)
9 changes: 9 additions & 0 deletions stats/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,13 @@ var (
TagKeys: []tag.Key{CheckName, RequestTag},
Aggregation: view.Count(),
}

// ShellParseErrorCount tracks error count stats for parsing shell code.
ShellParseErrorCount = view.View{
Name: "ShellParseErrorCount",
Description: "Shell parsing errors",
Measure: ShellParseErrors,
TagKeys: []tag.Key{},
Aggregation: view.Count(),
}
)

0 comments on commit 92ab4ed

Please sign in to comment.