@@ -788,36 +788,123 @@ func main() {
788
788
// SampleCodeG301 - mkdir permission check
789
789
SampleCodeG301 = []CodeSample {{[]string {`
790
790
package main
791
- import "os"
791
+
792
+ import (
793
+ "fmt"
794
+ "os"
795
+ )
796
+
797
+ func main() {
798
+ err := os.Mkdir("/tmp/mydir", 0777)
799
+ if err != nil {
800
+ fmt.Println("Error when creating a directory!")
801
+ return
802
+ }
803
+ }` }, 1 , gosec .NewConfig ()}, {[]string {`
804
+ package main
805
+
806
+ import (
807
+ "fmt"
808
+ "os"
809
+ )
810
+
792
811
func main() {
793
- os.Mkdir("/tmp/mydir", 0777)
794
- os.Mkdir("/tmp/mydir", 0600)
795
- os.MkdirAll("/tmp/mydir/mysubidr", 0775)
796
- }` }, 2 , gosec .NewConfig ()}}
812
+ err := os.MkdirAll("/tmp/mydir", 0777)
813
+ if err != nil {
814
+ fmt.Println("Error when creating a directory!")
815
+ return
816
+ }
817
+ }` }, 1 , gosec .NewConfig ()}, {[]string {`
818
+ package main
819
+
820
+ import (
821
+ "fmt"
822
+ "os"
823
+ )
824
+
825
+ func main() {
826
+ err := os.Mkdir("/tmp/mydir", 0600)
827
+ if err != nil {
828
+ fmt.Println("Error when creating a directory!")
829
+ return
830
+ }
831
+ }` }, 0 , gosec .NewConfig ()}}
797
832
798
833
// SampleCodeG302 - file create / chmod permissions check
799
834
SampleCodeG302 = []CodeSample {{[]string {`
800
835
package main
801
- import "os"
836
+
837
+ import (
838
+ "fmt"
839
+ "os"
840
+ )
841
+
802
842
func main() {
803
- os.Chmod("/tmp/somefile", 0777)
804
- os.Chmod("/tmp/someotherfile", 0600)
805
- os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0666)
806
- os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0600)
807
- }` }, 2 , gosec .NewConfig ()}}
843
+ err := os.Chmod("/tmp/somefile", 0777)
844
+ if err != nil {
845
+ fmt.Println("Error when changing file permissions!")
846
+ return
847
+ }
848
+ }` }, 1 , gosec .NewConfig ()}, {[]string {`
849
+ package main
850
+
851
+ import (
852
+ "fmt"
853
+ "os"
854
+ )
855
+
856
+ func main() {
857
+ _, err := os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0666)
858
+ if err != nil {
859
+ fmt.Println("Error opening a file!")
860
+ return
861
+ }
862
+ }` }, 1 , gosec .NewConfig ()}, {[]string {`
863
+ package main
864
+
865
+ import (
866
+ "fmt"
867
+ "os"
868
+ )
869
+
870
+ func main() {
871
+ err := os.Chmod("/tmp/mydir", 0400)
872
+ if err != nil {
873
+ fmt.Println("Error")
874
+ return
875
+ }
876
+ }` }, 0 , gosec .NewConfig ()}, {[]string {`
877
+ package main
878
+
879
+ import (
880
+ "fmt"
881
+ "os"
882
+ )
883
+
884
+ func main() {
885
+ _, err := os.OpenFile("/tmp/thing", os.O_CREATE|os.O_WRONLY, 0600)
886
+ if err != nil {
887
+ fmt.Println("Error opening a file!")
888
+ return
889
+ }
890
+ }
891
+ ` }, 0 , gosec .NewConfig ()}}
808
892
809
893
// SampleCodeG303 - bad tempfile permissions & hardcoded shared path
810
894
SampleCodeG303 = []CodeSample {{[]string {`
811
895
package samples
896
+
812
897
import (
898
+ "fmt"
813
899
"io/ioutil"
814
- "os"
815
900
)
901
+
816
902
func main() {
817
- file1, _ := os.Create("/tmp/demo1")
818
- defer file1.Close()
819
- ioutil.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
820
- }` }, 2 , gosec .NewConfig ()}}
903
+ err := ioutil.WriteFile("/tmp/demo2", []byte("This is some data"), 0644)
904
+ if err != nil {
905
+ fmt.Println("Error while writing!")
906
+ }
907
+ }` }, 1 , gosec .NewConfig ()}}
821
908
822
909
// SampleCodeG304 - potential file inclusion vulnerability
823
910
SampleCodeG304 = []CodeSample {{[]string {`
@@ -828,12 +915,12 @@ import (
828
915
"log"
829
916
)
830
917
func main() {
831
- f := os.Getenv("tainted_file")
832
- body, err := ioutil.ReadFile(f)
833
- if err != nil {
834
- log.Printf("Error: %v\n", err)
835
- }
836
- log.Print(body)
918
+ f := os.Getenv("tainted_file")
919
+ body, err := ioutil.ReadFile(f)
920
+ if err != nil {
921
+ log.Printf("Error: %v\n", err)
922
+ }
923
+ log.Print(body)
837
924
838
925
}` }, 1 , gosec .NewConfig ()}, {[]string {`
839
926
package main
0 commit comments