From 99b41d4f9dc9ecfaa7c5e3f4467d286648e04c47 Mon Sep 17 00:00:00 2001 From: aastha Date: Mon, 13 May 2024 17:13:35 +0530 Subject: [PATCH 1/3] Added missing samples for Go --- .../redundant-equality-check/EqeqIsBad.go | 23 +++++++++++++ .../rule-write-pprof-profile-output.go | 33 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 golang/src/detectors/redundant-equality-check/EqeqIsBad.go create mode 100644 golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go diff --git a/golang/src/detectors/redundant-equality-check/EqeqIsBad.go b/golang/src/detectors/redundant-equality-check/EqeqIsBad.go new file mode 100644 index 0000000..ea0dfe6 --- /dev/null +++ b/golang/src/detectors/redundant-equality-check/EqeqIsBad.go @@ -0,0 +1,23 @@ +package main + +import "fmt" + +// {fact rule=best-practices@v1.0 defects=1} +func eqeqIsBadNoncompliant() { + var s = "hello World !" + // Noncompliant: redundant comparison operation is used. + if s == s { + fmt.Println(s) + } +} + +// {/fact} + +// {fact rule=best-practices@v1.0 defects=0} +func eqeqIsBadCompliant() { + var s = "hello World !" + // Compliant: redundant comparison operation is not used. + fmt.Println(s) +} + +// {/fact} diff --git a/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go b/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go new file mode 100644 index 0000000..9c7d007 --- /dev/null +++ b/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go @@ -0,0 +1,33 @@ +package main + +import( + "net/http" + "runtime/pprof" + "os" +) + +func dumpGoroutines1(w http.ResponseWriter, r *http.Request, t auth.Token) error { + if !permission.Check(t, permission.PermDebug) { + return permission.ErrUnauthorized + } + + // ruleid: rule-write-pprof-profile-output + return pprof.Lookup("goroutine").WriteTo(w, 2) +} + +func dumpGoroutines2(w http.ResponseWriter, r *http.Request, t auth.Token) error { + if !permission.Check(t, permission.PermDebug) { + return permission.ErrUnauthorized + } + + // ruleid: rule-write-pprof-profile-output + return pprof.Lookup("goroutine").WriteTo(os.Stdout, 2) +} +func dumpGoroutines3(w http.ResponseWriter, r *http.Request, t auth.Token) error { + if !permission.Check(t, permission.PermDebug) { + return permission.ErrUnauthorized + } + + // ok: rule-write-pprof-profile-output + return pprof.Lookup("goroutine").WriteTo(r, 0) +} \ No newline at end of file From 8469658228db936a6c171a768399956164073ab0 Mon Sep 17 00:00:00 2001 From: aastha Date: Tue, 14 May 2024 11:05:19 +0530 Subject: [PATCH 2/3] Added missing samples for Go --- .../rule-write-pprof-profile-output.go | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go b/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go index 9c7d007..26e5ff9 100644 --- a/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go +++ b/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go @@ -6,28 +6,24 @@ import( "os" ) +// {fact rule=best-practices@v1.0 defects=1} func dumpGoroutines1(w http.ResponseWriter, r *http.Request, t auth.Token) error { if !permission.Check(t, permission.PermDebug) { return permission.ErrUnauthorized } - // ruleid: rule-write-pprof-profile-output + // Noncompliant: pprof profile is not removed return pprof.Lookup("goroutine").WriteTo(w, 2) } +//{fact} +// {fact rule=write-pprof-profile-output@v1.0 defects=0} func dumpGoroutines2(w http.ResponseWriter, r *http.Request, t auth.Token) error { if !permission.Check(t, permission.PermDebug) { return permission.ErrUnauthorized } - // ruleid: rule-write-pprof-profile-output - return pprof.Lookup("goroutine").WriteTo(os.Stdout, 2) + // Compliant: pprof profile is removed + return pprof.Lookup("goroutine").WriteTo(os.Stdout, 0) } -func dumpGoroutines3(w http.ResponseWriter, r *http.Request, t auth.Token) error { - if !permission.Check(t, permission.PermDebug) { - return permission.ErrUnauthorized - } - - // ok: rule-write-pprof-profile-output - return pprof.Lookup("goroutine").WriteTo(r, 0) -} \ No newline at end of file +//{fact} \ No newline at end of file From 54726acc74992814a94c3ea6591c3d124f3674c3 Mon Sep 17 00:00:00 2001 From: aastha Date: Tue, 28 May 2024 13:44:11 +0530 Subject: [PATCH 3/3] Addressed cosmetic comments --- .../redundant-equality-check/EqeqIsBad.go | 23 ------------------- .../rule-write-pprof-profile-output.go | 14 +++++++---- 2 files changed, 9 insertions(+), 28 deletions(-) delete mode 100644 golang/src/detectors/redundant-equality-check/EqeqIsBad.go diff --git a/golang/src/detectors/redundant-equality-check/EqeqIsBad.go b/golang/src/detectors/redundant-equality-check/EqeqIsBad.go deleted file mode 100644 index ea0dfe6..0000000 --- a/golang/src/detectors/redundant-equality-check/EqeqIsBad.go +++ /dev/null @@ -1,23 +0,0 @@ -package main - -import "fmt" - -// {fact rule=best-practices@v1.0 defects=1} -func eqeqIsBadNoncompliant() { - var s = "hello World !" - // Noncompliant: redundant comparison operation is used. - if s == s { - fmt.Println(s) - } -} - -// {/fact} - -// {fact rule=best-practices@v1.0 defects=0} -func eqeqIsBadCompliant() { - var s = "hello World !" - // Compliant: redundant comparison operation is not used. - fmt.Println(s) -} - -// {/fact} diff --git a/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go b/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go index 26e5ff9..bd6dd60 100644 --- a/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go +++ b/golang/src/detectors/write-pprof-profile-output/rule-write-pprof-profile-output.go @@ -1,3 +1,7 @@ + // +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +// package main import( @@ -6,24 +10,24 @@ import( "os" ) -// {fact rule=best-practices@v1.0 defects=1} -func dumpGoroutines1(w http.ResponseWriter, r *http.Request, t auth.Token) error { +// {fact rule= write-pprof-profile-output@v1.0 defects=1} +func writePprofProfileOutputNoncompliant(w http.ResponseWriter, r *http.Request, t auth.Token) error { if !permission.Check(t, permission.PermDebug) { return permission.ErrUnauthorized } - // Noncompliant: pprof profile is not removed + // Noncompliant: pprof profile is not removed return pprof.Lookup("goroutine").WriteTo(w, 2) } //{fact} // {fact rule=write-pprof-profile-output@v1.0 defects=0} -func dumpGoroutines2(w http.ResponseWriter, r *http.Request, t auth.Token) error { +func writePprofProfileOutputCompliant(w http.ResponseWriter, r *http.Request, t auth.Token) error { if !permission.Check(t, permission.PermDebug) { return permission.ErrUnauthorized } - // Compliant: pprof profile is removed + // Compliant: pprof profile is removed return pprof.Lookup("goroutine").WriteTo(os.Stdout, 0) } //{fact} \ No newline at end of file