Skip to content

Commit

Permalink
fix: static analysis findings (#341)
Browse files Browse the repository at this point in the history
* fix: reset eslintrc

* feat: log points for when different linters run to make it easier to know where you're at

* chore: resolve error level SCA findings

* chore: clean up SCA warning level findings

* chore: update community edition with a newly minted .golangci-lint.json
  • Loading branch information
superlinkx authored Jan 24, 2024
1 parent 3eea4a6 commit 544e4d7
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 34 deletions.
14 changes: 4 additions & 10 deletions .golangci.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,17 @@
"path": "cache_test\\.go",
"text": "SA1026:"
},
{
"path": "empty\\.go",
"text": "SA5010:"
},
{
"path": "foldr_test\\.go",
"text": "SA4000:"
},
{
"path": ".go",
"text": "((neo4j(.+)(NewDriver|Result))|Id|jwt\\.StandardClaims|database.Database|(.+)Deprecated) is deprecated"
"text": "((neo4j(.+)(NewDriver|Result))|Id|database.Database|(.+)Deprecated) is deprecated"
},
{
"path": "(.+)_test\\.go",
"linters": [
"errcheck"
]
"path": "expected_ingest.go",
"text": "ST1022:"
}
]
},
Expand All @@ -54,7 +48,7 @@
"default-severity": "error",
"rules": [
{
"text": "(ST\\d{4}|S\\d{4})",
"text": "(ST\\d{4}|S\\d{4}|SA1019)",
"severity": "warning"
}
]
Expand Down
6 changes: 1 addition & 5 deletions cmd/api/src/analysis/ad/adcs_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,13 @@ func TestEnrollOnBehalfOf(t *testing.T) {
}, func(harness integration.HarnessDetails, db graph.Database) {
certTemplates, err := ad2.FetchNodesByKind(context.Background(), db, ad.CertTemplate)
v1Templates := make([]*graph.Node, 0)
v2Templates := make([]*graph.Node, 0)
for _, template := range certTemplates {
if version, err := template.Properties.Get(ad.SchemaVersion.String()).Float64(); err != nil {
continue
} else if version == 1 {
v1Templates = append(v1Templates, template)
} else if version >= 2 {
v2Templates = append(v2Templates, template)
continue
}
}

Expand Down Expand Up @@ -430,16 +429,13 @@ func TestEnrollOnBehalfOf(t *testing.T) {
return nil
}, func(harness integration.HarnessDetails, db graph.Database) {
certTemplates, err := ad2.FetchNodesByKind(context.Background(), db, ad.CertTemplate)
// TODO: v1Templates are never used in any assertions and should either have assertions added or be removed from the test entirely
//v1Templates := make([]*graph.Node, 0)
v2Templates := make([]*graph.Node, 0)

for _, template := range certTemplates {
if version, err := template.Properties.Get(ad.SchemaVersion.String()).Float64(); err != nil {
continue
} else if version == 1 {
continue
//v1Templates = append(v1Templates, template)
} else if version >= 2 {
v2Templates = append(v2Templates, template)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/api/src/api/middleware/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/specterops/bloodhound/src/api"
)

var unsupportedEncodingError = errors.New("content encoding is not supported")
var errUnsupportedEncoding = errors.New("content encoding is not supported")

type GzipResponseWriter struct {
http.ResponseWriter
Expand Down Expand Up @@ -66,7 +66,7 @@ func CompressionMiddleware(next http.Handler) http.Handler {
if err != nil {
errMsg := fmt.Sprintf("failed to create reader for %s encoding: %v", encoding, err)
log.Warnf(errMsg)
if errors.Is(err, unsupportedEncodingError) {
if errors.Is(err, errUnsupportedEncoding) {
api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusUnsupportedMediaType, fmt.Sprintf("Error trying to read request: %s", errMsg), request), responseWriter)
} else {
api.WriteErrorResponse(request.Context(), api.BuildErrorResponse(http.StatusBadRequest, fmt.Sprintf("Error trying to read request: %s", errMsg), request), responseWriter)
Expand Down Expand Up @@ -107,7 +107,7 @@ func wrapBody(encoding string, body io.ReadCloser) (io.ReadCloser, error) {
newBody, err = zlib.NewReader(body)
default:
log.Infof("unsupported encoding detected: %s", encoding)
err = unsupportedEncodingError
err = errUnsupportedEncoding
}
return newBody, err
}
4 changes: 2 additions & 2 deletions cmd/api/src/bootstrap/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ package bootstrap
import (
"context"
"fmt"
"os"

"github.com/specterops/bloodhound/dawgs"
"github.com/specterops/bloodhound/dawgs/drivers/neo4j"
_ "github.com/specterops/bloodhound/dawgs/drivers/neo4j"
"github.com/specterops/bloodhound/dawgs/drivers/pg"
"github.com/specterops/bloodhound/dawgs/graph"
"github.com/specterops/bloodhound/dawgs/util/size"
"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/src/api/tools"
"github.com/specterops/bloodhound/src/config"
"os"
)

func ensureDirectory(path string) error {
Expand Down
11 changes: 6 additions & 5 deletions cmd/api/src/cmd/dawgs-harness/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ import (
"context"
"flag"
"fmt"
"github.com/specterops/bloodhound/dawgs/drivers/neo4j"
"github.com/specterops/bloodhound/dawgs/drivers/pg"
"github.com/specterops/bloodhound/dawgs/util/size"
schema "github.com/specterops/bloodhound/graphschema"
_ "net/http/pprof"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"time"

"github.com/specterops/bloodhound/dawgs/drivers/neo4j"
"github.com/specterops/bloodhound/dawgs/drivers/pg"
"github.com/specterops/bloodhound/dawgs/util/size"
schema "github.com/specterops/bloodhound/graphschema"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/specterops/bloodhound/dawgs"
"github.com/specterops/bloodhound/dawgs/graph"
Expand Down Expand Up @@ -71,7 +72,7 @@ func RunTestSuite(ctx context.Context, connectionStr, driverName string) tests.T
func newContext() context.Context {
var (
ctx, done = context.WithCancel(context.Background())
sigchnl = make(chan os.Signal)
sigchnl = make(chan os.Signal, 1)
)

signal.Notify(sigchnl)
Expand Down
1 change: 0 additions & 1 deletion cmd/ui/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-inferrable-types': 'off',
'@typescript-eslint/no-empty-function': 'off',
'prefer-const': 'warn',
},
};
4 changes: 0 additions & 4 deletions cmd/ui/src/views/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,3 @@ const Home: React.FC = () => {
};

export default Home;

// TODO: Remove before merge to fix linter messages
let arr = [].reduce(function(prev: never, curr: never, idx: number, arr: never[]) {})

4 changes: 0 additions & 4 deletions packages/go/dawgs/graph/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ func castNumericSlice[R numeric, T any](src []T) ([]R, error) {
}

func AsNumericSlice[T numeric](rawValue any) ([]T, error) {
var numericSlice []T

switch typedValue := rawValue.(type) {
case []any:
return castNumericSlice[T](typedValue)
Expand Down Expand Up @@ -117,8 +115,6 @@ func AsNumericSlice[T numeric](rawValue any) ([]T, error) {
default:
return nil, fmt.Errorf("unable to convert raw value %T as a numeric slice", rawValue)
}

return numericSlice, nil
}

func AsKinds(rawValue any) (Kinds, error) {
Expand Down
4 changes: 4 additions & 0 deletions packages/go/stbernard/analyzers/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ func Run(cwd string, modPaths []string, env []string) ([]codeclimate.Entry, erro
cmd.Stdout = &outb
cmd.Args = append(cmd.Args, args...)

log.Infof("Running golangci-lint")

err := cmd.Run()
if _, ok := err.(*exec.ExitError); ok {
err = ErrNonZeroExit
} else if err != nil {
return result, fmt.Errorf("unexpected failure: %w", err)
}

log.Infof("Completed golangci-lint")

if err := json.NewDecoder(&outb).Decode(&result); err != nil {
return result, fmt.Errorf("failed to decode output: %w", err)
}
Expand Down
5 changes: 5 additions & 0 deletions packages/go/stbernard/analyzers/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"os/exec"

"github.com/specterops/bloodhound/log"
"github.com/specterops/bloodhound/packages/go/stbernard/analyzers/codeclimate"
)

Expand Down Expand Up @@ -51,6 +52,8 @@ func Run(jsPaths []string, env []string) ([]codeclimate.Entry, error) {
result = make([]codeclimate.Entry, 0, len(jsPaths))
)

log.Infof("Running eslint")

for _, path := range jsPaths {
entries, err := runEslint(path, env)
if errors.Is(err, ErrNonZeroExit) {
Expand All @@ -61,6 +64,8 @@ func Run(jsPaths []string, env []string) ([]codeclimate.Entry, error) {
result = append(result, entries...)
}

log.Infof("Completed eslint")

return result, exitError
}

Expand Down

0 comments on commit 544e4d7

Please sign in to comment.