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

Fix test that checks the overridden nosec directive #1051

Merged
merged 2 commits into from
Oct 18, 2023
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
7 changes: 6 additions & 1 deletion analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,12 @@ func (gosec *Analyzer) ignore(n ast.Node) map[string]issue.SuppressionInfo {
if groups, ok := gosec.context.Comments[n]; ok && !gosec.ignoreNosec {

// Checks if an alternative for #nosec is set and, if not, uses the default.
noSecDefaultTag := NoSecTag(string(Nosec))
noSecDefaultTag, err := gosec.config.GetGlobal(Nosec)
if err != nil {
noSecDefaultTag = NoSecTag(string(Nosec))
} else {
noSecDefaultTag = NoSecTag(noSecDefaultTag)
}
noSecAlternativeTag, err := gosec.config.GetGlobal(NoSecAlternative)
if err != nil {
noSecAlternativeTag = noSecDefaultTag
Expand Down
6 changes: 3 additions & 3 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,21 @@ var _ = Describe("Analyzer", func() {
Expect(nosecIssues).Should(HaveLen(sample.Errors))
})

XIt("should be possible to overwrite nosec comments, and report issues but the should not be counted", func() {
It("should be possible to overwrite nosec comments, and report issues but they should not be counted", func() {
// Rule for MD5 weak crypto usage
sample := testutils.SampleCodeG401[0]
source := sample.Code[0]

// overwrite nosec option
nosecIgnoreConfig := gosec.NewConfig()
nosecIgnoreConfig.SetGlobal(gosec.Nosec, "true")
nosecIgnoreConfig.SetGlobal(gosec.Nosec, "mynosec")
nosecIgnoreConfig.SetGlobal(gosec.ShowIgnored, "true")
customAnalyzer := gosec.NewAnalyzer(nosecIgnoreConfig, tests, false, false, 1, logger)
customAnalyzer.LoadRules(rules.Generate(false, rules.NewRuleFilter(false, "G401")).RulesInfo())

nosecPackage := testutils.NewTestPackage()
defer nosecPackage.Close()
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() //#nosec", 1)
nosecSource := strings.Replace(source, "h := md5.New()", "h := md5.New() // #mynosec", 1)
nosecPackage.AddFile("md5.go", nosecSource)
err := nosecPackage.Build()
Expect(err).ShouldNot(HaveOccurred())
Expand Down
8 changes: 5 additions & 3 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ import (
var _ = Describe("Cli", func() {
Context("vflag test", func() {
It("value must be empty as parameter value contains invalid character", func() {
os.Args = []string{"gosec", "-test1=-incorrect"}
os.Args = []string{"gosec", "-flag1=-incorrect"}
f := vflag.ValidatedFlag{}
flag.Var(&f, "test1", "")
flag.CommandLine.Init("test1", flag.ContinueOnError)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
flag.Var(&f, "falg1", "")
flag.CommandLine.Init("flag1", flag.ContinueOnError)
flag.Parse()
Expect(flag.Parsed()).Should(BeTrue())
Expect(f.Value).Should(Equal(``))
})
It("value must be empty as parameter value contains invalid character without equal sign", func() {
os.Args = []string{"gosec", "-test2= -incorrect"}
f := vflag.ValidatedFlag{}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
flag.Var(&f, "test2", "")
flag.CommandLine.Init("test2", flag.ContinueOnError)
flag.Parse()
Expand Down