Skip to content

Commit e55ee31

Browse files
authored
fixing gosec alerts (#12)
Signed-off-by: Michael Hoang <mhoang@redhat.com>
1 parent 4370f0a commit e55ee31

File tree

10 files changed

+64
-38
lines changed

10 files changed

+64
-38
lines changed

pkg/apis/enricher/framework/java/quarkus_detector.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ package enricher
1414
import (
1515
"context"
1616
"errors"
17-
"io/ioutil"
1817
"os"
1918
"path/filepath"
2019

@@ -135,7 +134,7 @@ func getServerPortsFromQuarkusPropertiesFile(file string) ([]int, error) {
135134
}
136135

137136
func getServerPortsFromQuarkusApplicationYamlFile(file string) ([]int, error) {
138-
yamlFile, err := ioutil.ReadFile(file)
137+
yamlFile, err := os.ReadFile(filepath.Clean(file))
139138
if err != nil {
140139
return []int{}, err
141140
}

pkg/apis/enricher/framework/java/spring_detector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ package enricher
1414
import (
1515
"context"
1616
"errors"
17-
"io/ioutil"
17+
"os"
1818
"path/filepath"
1919

2020
"github.com/devfile/alizer/pkg/apis/model"
@@ -122,7 +122,7 @@ func getPortFromMap(props map[string]string, key string) int {
122122
}
123123

124124
func getServerPortsFromYamlFile(file string) ([]int, error) {
125-
yamlFile, err := ioutil.ReadFile(file)
125+
yamlFile, err := os.ReadFile(filepath.Clean(file))
126126
if err != nil {
127127
return []int{}, err
128128
}

pkg/apis/enricher/go_enricher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ package enricher
1414
import (
1515
"context"
1616
"errors"
17-
"io/ioutil"
18-
1917
framework "github.com/devfile/alizer/pkg/apis/enricher/framework/go"
2018
"github.com/devfile/alizer/pkg/apis/model"
2119
"github.com/devfile/alizer/pkg/utils"
2220
"golang.org/x/mod/modfile"
21+
"os"
22+
"path/filepath"
2323
)
2424

2525
type GoEnricher struct{}
@@ -108,7 +108,7 @@ func (g GoEnricher) IsConfigValidForComponentDetection(language string, config s
108108
}
109109

110110
func getGoModFile(filePath string) (*modfile.File, error) {
111-
b, err := ioutil.ReadFile(filePath)
111+
b, err := os.ReadFile(filepath.Clean(filePath))
112112
if err != nil {
113113
return nil, errors.New("unable to read go.mod file")
114114
}

pkg/utils/detector.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
"encoding/xml"
2020
"errors"
2121
"fmt"
22-
"io/ioutil"
22+
"io"
2323
"os"
2424
"path/filepath"
2525
"regexp"
@@ -83,7 +83,7 @@ func IsPathOfWantedFile(path string, wantedFile string) bool {
8383

8484
// IsTagInFile checks if the file contains the tag.
8585
func IsTagInFile(file string, tag string) (bool, error) {
86-
contentInByte, err := ioutil.ReadFile(file)
86+
contentInByte, err := os.ReadFile(filepath.Clean(file))
8787
if err != nil {
8888
return false, err
8989
}
@@ -143,7 +143,7 @@ func GetPomFileContent(pomFilePath string) (schema.Pom, error) {
143143
if err != nil {
144144
return schema.Pom{}, err
145145
}
146-
byteValue, _ := ioutil.ReadAll(xmlFile)
146+
byteValue, _ := io.ReadAll(xmlFile)
147147

148148
var pom schema.Pom
149149
err = xml.Unmarshal(byteValue, &pom)
@@ -298,7 +298,7 @@ func isFileInRoot(root string, file string) bool {
298298

299299
// GetFilePathsInRoot returns a slice of all files in the root.
300300
func GetFilePathsInRoot(root string) ([]string, error) {
301-
fileInfos, err := ioutil.ReadDir(root)
301+
fileInfos, err := os.ReadDir(root)
302302
if err != nil {
303303
return nil, err
304304
}
@@ -310,7 +310,7 @@ func GetFilePathsInRoot(root string) ([]string, error) {
310310
}
311311

312312
func ConvertPropertiesFileAsPathToMap(path string) (map[string]string, error) {
313-
bytes, err := ioutil.ReadFile(path)
313+
bytes, err := os.ReadFile(filepath.Clean(path))
314314
if err != nil {
315315
return nil, err
316316
}
@@ -426,7 +426,7 @@ func readAnyApplicationFile(root string, propsFiles []model.ApplicationFileInfo,
426426
path = GetAnyApplicationFilePath(root, propsFiles, ctx)
427427
}
428428
if path != "" {
429-
return ioutil.ReadFile(path)
429+
return os.ReadFile(filepath.Clean(path))
430430
}
431431
return nil, errors.New("no file found")
432432
}

resources/projects/golang-gin-app/articles/models.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package articles
22

33
import (
44
_ "fmt"
5-
"github.com/jinzhu/gorm"
65
"github.com/gothinkster/golang-gin-realworld-example-app/common"
76
"github.com/gothinkster/golang-gin-realworld-example-app/users"
7+
"github.com/jinzhu/gorm"
88
"strconv"
99
)
1010

@@ -184,7 +184,7 @@ func FindManyArticle(tag, author, limit, offset, favorited string) ([]ArticleMod
184184
count = tx.Model(&articleUserModel).Association("FavoriteModels").Count()
185185
for _, favorite := range favoriteModels {
186186
var model ArticleModel
187-
tx.Model(&favorite).Related(&model, "Favorite")
187+
tx.Model(&favorite).Related(&model, "Favorite") // #nosec G601
188188
models = append(models, model)
189189
}
190190
}

resources/projects/golang-gin-app/articles/validators.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package articles
22

33
import (
4+
"github.com/gin-gonic/gin"
45
"github.com/gosimple/slug"
56
"github.com/gothinkster/golang-gin-realworld-example-app/common"
67
"github.com/gothinkster/golang-gin-realworld-example-app/users"
7-
"github.com/gin-gonic/gin"
88
)
99

1010
type ArticleModelValidator struct {
@@ -44,7 +44,10 @@ func (s *ArticleModelValidator) Bind(c *gin.Context) error {
4444
s.articleModel.Description = s.Article.Description
4545
s.articleModel.Body = s.Article.Body
4646
s.articleModel.Author = GetArticleUserModel(myUserModel)
47-
s.articleModel.setTags(s.Article.Tags)
47+
err = s.articleModel.setTags(s.Article.Tags)
48+
if err != nil {
49+
return err
50+
}
4851
return nil
4952
}
5053

resources/projects/golang-gin-app/common/utils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/dgrijalva/jwt-go"
1010
"gopkg.in/go-playground/validator.v8"
1111

12-
"github.com/gin-gonic/gin/binding"
1312
"github.com/gin-gonic/gin"
13+
"github.com/gin-gonic/gin/binding"
1414
)
1515

1616
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
@@ -19,14 +19,14 @@ var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345
1919
func RandString(n int) string {
2020
b := make([]rune, n)
2121
for i := range b {
22-
b[i] = letters[rand.Intn(len(letters))]
22+
b[i] = letters[rand.Intn(len(letters))] // #nosec G404
2323
}
2424
return string(b)
2525
}
2626

2727
// Keep this two config private, it should not expose to open source
28-
const NBSecretPassword = "A String Very Very Very Strong!!@##$!@#$"
29-
const NBRandomPassword = "A String Very Very Very Niubilty!!@##$!@#4"
28+
const NBSecretPassword = "A String Very Very Very Strong!!@##$!@#$" // #nosec G101
29+
const NBRandomPassword = "A String Very Very Very Niubilty!!@##$!@#4" // #nosec G101
3030

3131
// A Util function to generate jwt_token which can be used in the request header
3232
func GenToken(id uint) string {
@@ -42,7 +42,8 @@ func GenToken(id uint) string {
4242
}
4343

4444
// My own Error type that will help return my customized Error info
45-
// {"database": {"hello":"no such table", error: "not_exists"}}
45+
//
46+
// {"database": {"hello":"no such table", error: "not_exists"}}
4647
type CommonError struct {
4748
Errors map[string]interface{} `json:"errors"`
4849
}

resources/projects/golang-gin-app/users/models.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package users
22

33
import (
44
"errors"
5-
"github.com/jinzhu/gorm"
65
"github.com/gothinkster/golang-gin-realworld-example-app/common"
6+
"github.com/jinzhu/gorm"
77
"golang.org/x/crypto/bcrypt"
88
)
99

@@ -27,8 +27,9 @@ type UserModel struct {
2727
// DB schema looks like: id, created_at, updated_at, deleted_at, following_id, followed_by_id.
2828
//
2929
// Retrieve them by:
30-
// db.Where(FollowModel{ FollowingID: v.ID, FollowedByID: u.ID, }).First(&follow)
31-
// db.Where(FollowModel{ FollowedByID: u.ID, }).Find(&follows)
30+
//
31+
// db.Where(FollowModel{ FollowingID: v.ID, FollowedByID: u.ID, }).First(&follow)
32+
// db.Where(FollowModel{ FollowedByID: u.ID, }).Find(&follows)
3233
//
3334
// More details about gorm.Model: http://jinzhu.me/gorm/models.html#conventions
3435
type FollowModel struct {
@@ -50,7 +51,8 @@ func AutoMigrate() {
5051
// What's bcrypt? https://en.wikipedia.org/wiki/Bcrypt
5152
// Golang bcrypt doc: https://godoc.org/golang.org/x/crypto/bcrypt
5253
// You can change the value in bcrypt.DefaultCost to adjust the security index.
53-
// err := userModel.setPassword("password0")
54+
//
55+
// err := userModel.setPassword("password0")
5456
func (u *UserModel) setPassword(password string) error {
5557
if len(password) == 0 {
5658
return errors.New("password should not be empty!")
@@ -63,15 +65,17 @@ func (u *UserModel) setPassword(password string) error {
6365
}
6466

6567
// Database will only save the hashed string, you should check it by util function.
66-
// if err := serModel.checkPassword("password0"); err != nil { password error }
68+
//
69+
// if err := serModel.checkPassword("password0"); err != nil { password error }
6770
func (u *UserModel) checkPassword(password string) error {
6871
bytePassword := []byte(password)
6972
byteHashedPassword := []byte(u.PasswordHash)
7073
return bcrypt.CompareHashAndPassword(byteHashedPassword, bytePassword)
7174
}
7275

7376
// You could input the conditions and it will return an UserModel in database with error info.
74-
// userModel, err := FindOneUser(&UserModel{Username: "username0"})
77+
//
78+
// userModel, err := FindOneUser(&UserModel{Username: "username0"})
7579
func FindOneUser(condition interface{}) (UserModel, error) {
7680
db := common.GetDB()
7781
var model UserModel
@@ -80,23 +84,26 @@ func FindOneUser(condition interface{}) (UserModel, error) {
8084
}
8185

8286
// You could input an UserModel which will be saved in database returning with error info
83-
// if err := SaveOne(&userModel); err != nil { ... }
87+
//
88+
// if err := SaveOne(&userModel); err != nil { ... }
8489
func SaveOne(data interface{}) error {
8590
db := common.GetDB()
8691
err := db.Save(data).Error
8792
return err
8893
}
8994

9095
// You could update properties of an UserModel to database returning with error info.
91-
// err := db.Model(userModel).Update(UserModel{Username: "wangzitian0"}).Error
96+
//
97+
// err := db.Model(userModel).Update(UserModel{Username: "wangzitian0"}).Error
9298
func (model *UserModel) Update(data interface{}) error {
9399
db := common.GetDB()
94100
err := db.Model(model).Update(data).Error
95101
return err
96102
}
97103

98104
// You could add a following relationship as userModel1 following userModel2
99-
// err = userModel1.following(userModel2)
105+
//
106+
// err = userModel1.following(userModel2)
100107
func (u UserModel) following(v UserModel) error {
101108
db := common.GetDB()
102109
var follow FollowModel
@@ -108,7 +115,8 @@ func (u UserModel) following(v UserModel) error {
108115
}
109116

110117
// You could check whether userModel1 following userModel2
111-
// followingBool = myUserModel.isFollowing(self.UserModel)
118+
//
119+
// followingBool = myUserModel.isFollowing(self.UserModel)
112120
func (u UserModel) isFollowing(v UserModel) bool {
113121
db := common.GetDB()
114122
var follow FollowModel
@@ -120,7 +128,8 @@ func (u UserModel) isFollowing(v UserModel) bool {
120128
}
121129

122130
// You could delete a following relationship as userModel1 following userModel2
123-
// err = userModel1.unFollowing(userModel2)
131+
//
132+
// err = userModel1.unFollowing(userModel2)
124133
func (u UserModel) unFollowing(v UserModel) error {
125134
db := common.GetDB()
126135
err := db.Where(FollowModel{
@@ -131,7 +140,8 @@ func (u UserModel) unFollowing(v UserModel) error {
131140
}
132141

133142
// You could get a following list of userModel
134-
// followings := userModel.GetFollowings()
143+
//
144+
// followings := userModel.GetFollowings()
135145
func (u UserModel) GetFollowings() []UserModel {
136146
db := common.GetDB()
137147
tx := db.Begin()
@@ -142,7 +152,7 @@ func (u UserModel) GetFollowings() []UserModel {
142152
}).Find(&follows)
143153
for _, follow := range follows {
144154
var userModel UserModel
145-
tx.Model(&follow).Related(&userModel, "Following")
155+
tx.Model(&follow).Related(&userModel, "Following") // #nosec G601
146156
followings = append(followings, userModel)
147157
}
148158
tx.Commit()

resources/projects/golang-gin-app/users/validators.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package users
22

33
import (
4-
"github.com/gothinkster/golang-gin-realworld-example-app/common"
54
"github.com/gin-gonic/gin"
5+
"github.com/gothinkster/golang-gin-realworld-example-app/common"
66
)
77

88
// *ModelValidator containing two parts:
@@ -33,7 +33,10 @@ func (self *UserModelValidator) Bind(c *gin.Context) error {
3333
self.userModel.Bio = self.User.Bio
3434

3535
if self.User.Password != common.NBRandomPassword {
36-
self.userModel.setPassword(self.User.Password)
36+
err := self.userModel.setPassword(self.User.Password)
37+
if err != nil {
38+
return err
39+
}
3740
}
3841
if self.User.Image != "" {
3942
self.userModel.Image = &self.User.Image

resources/projects/golang-runtime/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ import (
44
"fmt"
55
"net/http"
66
"os"
7+
"time"
78
)
89

910
func main() {
1011
http.HandleFunc("/", HelloHandler)
1112
fmt.Println("Listening on localhost:8080")
12-
http.ListenAndServe(":8080", nil)
13+
14+
server := &http.Server{
15+
Addr: ":8080",
16+
ReadHeaderTimeout: 3 * time.Second,
17+
}
18+
19+
err := server.ListenAndServe()
20+
if err != nil {
21+
panic(err)
22+
}
1323
}
1424

1525
func HelloHandler(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)