Merge pull request #22 from owncloud/fix/gosec-issues

fix code smells
This commit is contained in:
David Christofas
2020-05-19 09:32:23 +02:00
committed by GitHub
5 changed files with 18 additions and 7 deletions

View File

@@ -0,0 +1,6 @@
Change: refactor code to remove code smells
Scanning the code using a static code analyzer showed some code smells.
This change fixes them.
https://github.com/owncloud/ocis-thumbnails/issues/21

View File

@@ -40,15 +40,15 @@ func New() *Metrics {
}, []string{}),
}
prometheus.Register(
_ = prometheus.Register(
m.Counter,
)
prometheus.Register(
_ = prometheus.Register(
m.Latency,
)
prometheus.Register(
_ = prometheus.Register(
m.Duration,
)

View File

@@ -40,7 +40,7 @@ func NewService(opts ...Option) grpc.Service {
thumbnail = svc.NewLogging(thumbnail, options.Logger)
}
proto.RegisterThumbnailServiceHandler(
_ = proto.RegisterThumbnailServiceHandler(
service.Server(),
thumbnail,
)

View File

@@ -25,7 +25,7 @@ type FileSystem struct {
// Get retrieves an image from the filesystem.
func (s FileSystem) Get(ctx context.Context, file string) (image.Image, error) {
imgPath := filepath.Join(s.basePath, file)
f, err := os.Open(imgPath)
f, err := os.Open(filepath.Clean(imgPath))
if err != nil {
return nil, fmt.Errorf("failed to load the file %s from %s error %s", file, imgPath, err.Error())
}

View File

@@ -27,7 +27,8 @@ type FileSystem struct {
// Get loads the image from the file system.
func (s FileSystem) Get(key string) []byte {
content, err := ioutil.ReadFile(filepath.Join(s.dir, key))
path := filepath.Join(s.dir, key)
content, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
s.logger.Warn().Err(err).Msgf("could not read file %s", key)
return nil
@@ -48,7 +49,11 @@ func (s FileSystem) Set(key string, img []byte) error {
if err != nil {
return fmt.Errorf("could not create file \"%s\" error: %s", key, err.Error())
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
s.logger.Warn().Err(err).Msg("closing file resulted in an error")
}
}()
_, err = f.Write(img)
if err != nil {
return fmt.Errorf("could not write to file \"%s\" error: %s", key, err.Error())