-
-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Rule G406 responsible for the usage of deprecated MD4 and RIPEMD160 added. * Rules G506, G507 responsible for tracking the usage of the already mentioned libraries added. * Slight changes in the Makefile(`make clean` wasn't removing all expected files) * Added license to `analyzer_test.go`
- Loading branch information
Showing
13 changed files
with
536 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// (c) Copyright 2024 Mercedes-Benz Tech Innovation GmbH | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rules | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/securego/gosec/v2" | ||
"github.com/securego/gosec/v2/issue" | ||
) | ||
|
||
type usesWeakDeprecatedCryptographyHash struct { | ||
issue.MetaData | ||
blocklist map[string][]string | ||
} | ||
|
||
func (r *usesWeakDeprecatedCryptographyHash) ID() string { | ||
return r.MetaData.ID | ||
} | ||
|
||
func (r *usesWeakDeprecatedCryptographyHash) Match(n ast.Node, c *gosec.Context) (*issue.Issue, error) { | ||
for pkg, funcs := range r.blocklist { | ||
if _, matched := gosec.MatchCallByPackage(n, c, pkg, funcs...); matched { | ||
return c.NewIssue(n, r.ID(), r.What, r.Severity, r.Confidence), nil | ||
} | ||
} | ||
return nil, nil | ||
} | ||
|
||
// NewUsesWeakCryptographyHash detects uses of md4.New, ripemd160.New | ||
func NewUsesWeakDeprecatedCryptographyHash(id string, _ gosec.Config) (gosec.Rule, []ast.Node) { | ||
calls := make(map[string][]string) | ||
calls["golang.org/x/crypto/md4"] = []string{"New"} | ||
calls["golang.org/x/crypto/ripemd160"] = []string{"New"} | ||
rule := &usesWeakDeprecatedCryptographyHash{ | ||
blocklist: calls, | ||
MetaData: issue.MetaData{ | ||
ID: id, | ||
Severity: issue.Medium, | ||
Confidence: issue.High, | ||
What: "Use of deprecated weak cryptographic primitive", | ||
}, | ||
} | ||
return rule, []ast.Node{(*ast.CallExpr)(nil)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package testutils | ||
|
||
import "github.com/securego/gosec/v2" | ||
|
||
var ( | ||
// SampleCodeG406 - Use of deprecated weak crypto hash MD4 | ||
SampleCodeG406 = []CodeSample{ | ||
{[]string{` | ||
package main | ||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"golang.org/x/crypto/md4" | ||
) | ||
func main() { | ||
h := md4.New() | ||
h.Write([]byte("test")) | ||
fmt.Println(hex.EncodeToString(h.Sum(nil))) | ||
} | ||
`}, 1, gosec.NewConfig()}, | ||
} | ||
|
||
// SampleCodeG406b - Use of deprecated weak crypto hash RIPEMD160 | ||
SampleCodeG406b = []CodeSample{ | ||
{[]string{` | ||
package main | ||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"golang.org/x/crypto/ripemd160" | ||
) | ||
func main() { | ||
h := ripemd160.New() | ||
h.Write([]byte("test")) | ||
fmt.Println(hex.EncodeToString(h.Sum(nil))) | ||
} | ||
`}, 1, gosec.NewConfig()}, | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package testutils | ||
|
||
import "github.com/securego/gosec/v2" | ||
|
||
// SampleCodeG506 - Blocklisted import MD4 | ||
var SampleCodeG506 = []CodeSample{ | ||
{[]string{` | ||
package main | ||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"golang.org/x/crypto/md4" | ||
) | ||
func main() { | ||
h := md4.New() | ||
h.Write([]byte("test")) | ||
fmt.Println(hex.EncodeToString(h.Sum(nil))) | ||
} | ||
`}, 1, gosec.NewConfig()}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package testutils | ||
|
||
import "github.com/securego/gosec/v2" | ||
|
||
// SampleCodeG507 - Blocklisted import RIPEMD160 | ||
var SampleCodeG507 = []CodeSample{ | ||
{[]string{` | ||
package main | ||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"golang.org/x/crypto/ripemd160" | ||
) | ||
func main() { | ||
h := ripemd160.New() | ||
h.Write([]byte("test")) | ||
fmt.Println(hex.EncodeToString(h.Sum(nil))) | ||
} | ||
`}, 1, gosec.NewConfig()}, | ||
} |