Skip to content

Commit 5b278fa

Browse files
authored
Merge pull request #48 from keep-network/add-gosec
Add GoSec workflow Here we introduce the GoSec scan workflow to the Github Actions pipeline and deal with all discovered problems.
2 parents 84538e8 + 024f151 commit 5b278fa

File tree

7 files changed

+63
-7
lines changed

7 files changed

+63
-7
lines changed

.github/workflows/gosec.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Gosec
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
scan:
13+
runs-on: ubuntu-latest
14+
env:
15+
GO111MODULE: on
16+
steps:
17+
- uses: actions/checkout@v2
18+
- uses: securego/gosec@master
19+
with:
20+
args: ./...

pkg/chain/ethereum/ethutil/ethutil.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func AddressFromHex(hex string) (common.Address, error) {
3535

3636
// DecryptKeyFile reads in a key file and uses the password to decrypt it.
3737
func DecryptKeyFile(keyFile, password string) (*keystore.Key, error) {
38+
// #nosec G304 (file path provided as taint input)
39+
// This line is used to read a local key file. There is no user input.
3840
data, err := ioutil.ReadFile(keyFile)
3941
if err != nil {
4042
return nil, fmt.Errorf("unable to read KeyFile %s [%v]", keyFile, err)
@@ -141,7 +143,7 @@ func CallAtBlock(
141143
// the true gas limit requirement as other transactions may be added or removed by miners,
142144
// but it should provide a basis for setting a reasonable default.
143145
func EstimateGas(
144-
from common.Address,
146+
from common.Address,
145147
to common.Address,
146148
method string,
147149
contractABI *abi.ABI,

pkg/generate/generate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func OrganizeImports(codeBuffer *bytes.Buffer, filePath string) error {
3838
// error writing the file.
3939
func SaveBufferToFile(buffer *bytes.Buffer, filePath string) error {
4040
file, err := os.Create(filePath)
41+
42+
// #nosec G104 G307 (audit errors not checked & deferring unsafe method)
43+
// This line is placed in the auxiliary generator code,
44+
// not in the core application. Also, the Close function returns only
45+
// the error. It doesn't return any other values which can be a security
46+
// threat when used without checking the error.
4147
defer file.Close()
4248
if err != nil {
4349
return fmt.Errorf("output file %s creation failed [%v]", filePath, err)

pkg/persistence/disk_persistence.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,32 @@ func write(filePath string, data []byte) error {
123123
return err
124124
}
125125

126-
defer writeFile.Close()
126+
defer closeFile(writeFile)
127127

128128
_, err = writeFile.Write(data)
129129
if err != nil {
130130
return err
131131
}
132132

133-
writeFile.Sync()
133+
err = writeFile.Sync()
134+
if err != nil {
135+
return err
136+
}
134137

135138
return nil
136139
}
137140

138141
// read a file from a file system
139142
func read(filePath string) ([]byte, error) {
143+
// #nosec G304 (file path provided as taint input)
144+
// This line opens a file from the predefined storage.
145+
// There is no user input.
140146
readFile, err := os.Open(filePath)
141147
if err != nil {
142148
return nil, err
143149
}
144150

145-
defer readFile.Close()
151+
defer closeFile(readFile)
146152

147153
data, err := ioutil.ReadAll(readFile)
148154
if err != nil {
@@ -152,6 +158,13 @@ func read(filePath string) ([]byte, error) {
152158
return data, nil
153159
}
154160

161+
func closeFile(file *os.File) {
162+
err := file.Close()
163+
if err != nil {
164+
logger.Errorf("could not close file [%v]: [%v]", file.Name(), err)
165+
}
166+
}
167+
155168
// readAll reads all files from the provided directoryPath and outputs them
156169
// as DataDescriptors into the first returned output channel. All errors
157170
// occurred during file system reading are sent to the second output channel

tools/generators/ethereum/contract.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ func main() {
6161
contractOutputPath := flag.Arg(1)
6262
commandOutputPath := flag.Arg(2)
6363

64+
// #nosec G304 (file path provided as taint input)
65+
// This line is placed in the auxiliary generator code,
66+
// not in the core application. User input has to be passed to
67+
// provide a path to the contract ABI.
6468
abiFile, err := ioutil.ReadFile(abiPath)
6569
if err != nil {
6670
panic(fmt.Sprintf(
@@ -223,6 +227,12 @@ func organizeImports(outFile string, buf *bytes.Buffer) error {
223227
// Stores the Buffer `buf` content to a file in `filePath`
224228
func saveBufferToFile(buf *bytes.Buffer, filePath string) error {
225229
file, err := os.Create(filePath)
230+
231+
// #nosec G104 G307 (audit errors not checked & deferring unsafe method)
232+
// This line is placed in the auxiliary generator code,
233+
// not in the core application. Also, the Close function returns only
234+
// the error. It doesn't return any other values which can be a security
235+
// threat when used without checking the error.
226236
defer file.Close()
227237
if err != nil {
228238
return fmt.Errorf("output file %s creation failed [%v]", filePath, err)

tools/generators/promise/promise.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ func generatePromisesCode(generationDir string, promisesConfig []promiseConfig)
9696
return fmt.Errorf("template creation failed [%v]", err)
9797
}
9898

99-
for _, promiseConfig := range promisesConfig {
100-
outputFile := promiseConfig.Filename
99+
for i := range promisesConfig {
100+
outputFile := promisesConfig[i].Filename
101101
outputFilePath := path.Join(generationDir, outputFile)
102102

103103
// Generate promise code.
104-
buffer, err := generateCode(promiseTemplate, &promiseConfig, outputFilePath)
104+
buffer, err := generateCode(promiseTemplate, &promisesConfig[i], outputFilePath)
105105
if err != nil {
106106
return fmt.Errorf("promise generation failed [%v]", err)
107107
}

tools/generators/template/template.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func main() {
3030
}
3131

3232
templateFile := os.Args[templateFileArgIndex]
33+
34+
// #nosec G304 (file path provided as taint input)
35+
// This line is placed in the auxiliary generator code,
36+
// not in the core application. User input has to be passed to provide a
37+
// path to the template file.
3338
templateContents, err := ioutil.ReadFile(templateFile)
3439
if err != nil {
3540
errorAndExit(fmt.Sprintf("Failed to open template file: [%v].", err))

0 commit comments

Comments
 (0)