Skip to content

Commit 8b432ca

Browse files
authored
enable gosec (#147)
* enable gosec Signed-off-by: Kim Tsao <[email protected]> * enable gosec - fix errors Signed-off-by: Kim Tsao <[email protected]> * enable gosec - switch to using makefile rule to avoid setting GOROOT Signed-off-by: Kim Tsao <[email protected]> * update PR template Signed-off-by: Kim Tsao <[email protected]> * Switch to Sarif Signed-off-by: Kim Tsao <[email protected]> * Update deprecated action Signed-off-by: Kim Tsao <[email protected]> * Handle gosec warnings Signed-off-by: Kim Tsao <[email protected]> * Handle gosec errors Signed-off-by: Kim Tsao <[email protected]> Signed-off-by: Kim Tsao <[email protected]>
1 parent 4c7c5dc commit 8b432ca

File tree

8 files changed

+43
-15
lines changed

8 files changed

+43
-15
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,8 @@ Testing and documentation do not need to be complete in order for this PR to be
3131

3232
<!-- _Do we have anything that can break our clients? If so, open a notifying issue_ -->
3333

34+
- [ ] Gosec scans
35+
<!-- _Review scan results from the PR. Fix all MEDIUM and higher findings and/or annotate a finding per gosec instructions: https://github.com/securego/gosec#annotating-code to address why a finding is not a security issue_-->
36+
3437

3538
### How to test changes / Special notes to the reviewer:

.github/workflows/go.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,21 @@ jobs:
5858
- name: Run Go Tests
5959
run: make test
6060

61+
- name: Run Gosec Security Scanner
62+
run: |
63+
go install github.com/securego/gosec/v2/cmd/gosec@latest
64+
make gosec
65+
if [[ $? != 0 ]]
66+
then
67+
echo "gosec scanner failed to run "
68+
exit 1
69+
fi
70+
71+
- name: Upload SARIF file
72+
uses: github/codeql-action/upload-sarif@v2
73+
with:
74+
# Path to SARIF file relative to the root of the repository
75+
sarif_file: gosec.sarif
76+
6177
- name: Upload coverage to Codecov
6278
uses: codecov/[email protected]

Makefile

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,24 @@ ifneq ($(shell command -v addlicense 2> /dev/null),)
3131
@echo 'addlicense -v -f license_header.txt **/*.go'
3232
@addlicense -v -f license_header.txt $$(find . -name '*.go')
3333
else
34-
$(error addlicense must be installed for this rule: go get -u github.com/google/addlicense)
34+
$(error "addlicense must be installed for this command: go install github.com/google/addlicense@latest")
3535
endif
3636

3737
### check_fmt: Checks for missing licenses on files in repo
3838
check_license:
3939
ifeq ($(shell command -v addlicense 2> /dev/null),)
40-
$(error "error addlicense must be installed for this rule: go get -u github.com/google/addlicense")
40+
$(error "error addlicense must be installed for this command: go install github.com/google/addlicense@latest")
4141
endif
4242

4343
if ! addlicense -check -f license_header.txt $$(find . -not -path '*/\.*' -name '*.go'); then \
4444
echo "Licenses are not formatted; run 'make fmt_license'"; exit 1 ;\
4545
fi \
4646

4747

48+
49+
### gosec - runs the gosec scanner for non-test files in this repo
50+
.PHONY: gosec
51+
gosec:
52+
# Run this command to install gosec, if not installed:
53+
# go install github.com/securego/gosec/v2/cmd/gosec@latest
54+
gosec -no-fail -fmt=sarif -out=gosec.sarif -exclude-dir pkg/testingutil -exclude-dir tests ./...

pkg/devfile/parser/configurables.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (d DevfileObj) SetMemory(memory string) error {
105105
for _, component := range components {
106106
if component.Container != nil {
107107
component.Container.MemoryLimit = memory
108-
d.Data.UpdateComponent(component)
108+
_ = d.Data.UpdateComponent(component)
109109
}
110110
}
111111
return d.WriteYamlDevfile()

pkg/devfile/parser/data/v2/containers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (d *DevfileV2) AddEnvVars(containerEnvMap map[string][]v1alpha2.EnvVar) err
3636
for _, component := range components {
3737
if component.Container != nil {
3838
component.Container.Env = merge(component.Container.Env, containerEnvMap[component.Name])
39-
d.UpdateComponent(component)
39+
_ = d.UpdateComponent(component)
4040
}
4141
}
4242
return nil
@@ -55,7 +55,7 @@ func (d *DevfileV2) RemoveEnvVars(containerEnvMap map[string][]string) error {
5555
if err != nil {
5656
return err
5757
}
58-
d.UpdateComponent(component)
58+
_ = d.UpdateComponent(component)
5959
}
6060
}
6161
return nil
@@ -76,7 +76,7 @@ func (d *DevfileV2) SetPorts(containerPortsMap map[string][]string) error {
7676
}
7777
if component.Container != nil {
7878
component.Container.Endpoints = addEndpoints(component.Container.Endpoints, endpoints)
79-
d.UpdateComponent(component)
79+
_ = d.UpdateComponent(component)
8080
}
8181
}
8282

@@ -97,7 +97,7 @@ func (d *DevfileV2) RemovePorts(containerPortsMap map[string][]string) error {
9797
if err != nil {
9898
return err
9999
}
100-
d.UpdateComponent(component)
100+
_ = d.UpdateComponent(component)
101101
}
102102
}
103103

pkg/devfile/parser/parse.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ func parseKubeResourceFromURI(devObj DevfileObj) error {
742742
}
743743
for _, kubeComp := range kubeComponents {
744744
if kubeComp.Kubernetes != nil && kubeComp.Kubernetes.Uri != "" {
745+
/* #nosec G601 -- not an issue, kubeComp is de-referenced in sequence*/
745746
err := convertK8sLikeCompUriToInlined(&kubeComp, devObj.Ctx)
746747
if err != nil {
747748
return errors.Wrapf(err, "failed to convert Kubernetes Uri to inlined for component '%s'", kubeComp.Name)
@@ -754,6 +755,7 @@ func parseKubeResourceFromURI(devObj DevfileObj) error {
754755
}
755756
for _, openshiftComp := range openshiftComponents {
756757
if openshiftComp.Openshift != nil && openshiftComp.Openshift.Uri != "" {
758+
/* #nosec G601 -- not an issue, openshiftComp is de-referenced in sequence*/
757759
err := convertK8sLikeCompUriToInlined(&openshiftComp, devObj.Ctx)
758760
if err != nil {
759761
return errors.Wrapf(err, "failed to convert Openshift Uri to inlined for component '%s'", openshiftComp.Name)

pkg/util/util.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ func GetIgnoreRulesFromDirectory(directory string) ([]string, error) {
494494
}
495495
}
496496

497-
file, err := os.Open(pathIgnore)
497+
file, err := os.Open(filepath.Clean(pathIgnore))
498498
if err != nil {
499499
return nil, err
500500
}
@@ -707,7 +707,7 @@ func HTTPGetFreePort() (int, error) {
707707
// shamelessly taken from: https://stackoverflow.com/questions/30697324/how-to-check-if-directory-on-path-is-empty
708708
// this helps detect any edge cases where an empty directory is copied over
709709
func IsEmpty(name string) (bool, error) {
710-
f, err := os.Open(name)
710+
f, err := os.Open(filepath.Clean(name))
711711
if err != nil {
712712
return false, err
713713
}
@@ -997,7 +997,7 @@ func Unzip(src, dest, pathToUnzip string) ([]string, error) {
997997
return filenames, err
998998
}
999999

1000-
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, ModeReadWriteFile)
1000+
outFile, err := os.OpenFile(filepath.Clean(fpath), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, ModeReadWriteFile)
10011001
if err != nil {
10021002
return filenames, err
10031003
}
@@ -1015,8 +1015,8 @@ func Unzip(src, dest, pathToUnzip string) ([]string, error) {
10151015
_, err = io.Copy(outFile, limited)
10161016

10171017
// Close the file without defer to close before next iteration of loop
1018-
outFile.Close()
1019-
rc.Close()
1018+
_ = outFile.Close()
1019+
_ = rc.Close()
10201020

10211021
if err != nil {
10221022
return filenames, err
@@ -1214,14 +1214,14 @@ func CopyFile(srcPath string, dstPath string, info os.FileInfo) error {
12141214
}
12151215

12161216
// Open source file
1217-
srcFile, err := os.Open(srcPath)
1217+
srcFile, err := os.Open(filepath.Clean(srcPath))
12181218
if err != nil {
12191219
return err
12201220
}
12211221
defer srcFile.Close() // #nosec G307
12221222

12231223
// Create destination file
1224-
dstFile, err := os.Create(dstPath)
1224+
dstFile, err := os.Create(filepath.Clean(dstPath))
12251225
if err != nil {
12261226
return err
12271227
}

replaceSchemaFile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func ReplaceSchemaFile() {
3838
fmt.Printf("Writing to file: %s\n", filePath)
3939
fileContent := fmt.Sprintf("package %s\n\n// %s\nconst %s = `%s\n`\n", packageVersion, schemaURL, jsonSchemaVersion, newSchema)
4040

41-
if err := ioutil.WriteFile(filePath, []byte(fileContent), 0755); err != nil {
41+
if err := ioutil.WriteFile(filePath, []byte(fileContent), 0644); err != nil {
4242
printErr(err)
4343
os.Exit(1)
4444
}

0 commit comments

Comments
 (0)