gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/rules/tempfiles.go (about)

     1  // (c) Copyright 2016 Hewlett Packard Enterprise Development LP
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rules
    16  
    17  import (
    18  	"go/ast"
    19  	"regexp"
    20  
    21  	"github.com/securego/gosec"
    22  )
    23  
    24  type badTempFile struct {
    25  	gosec.MetaData
    26  	calls gosec.CallList
    27  	args  *regexp.Regexp
    28  }
    29  
    30  func (t *badTempFile) ID() string {
    31  	return t.MetaData.ID
    32  }
    33  
    34  func (t *badTempFile) Match(n ast.Node, c *gosec.Context) (gi *gosec.Issue, err error) {
    35  	if node := t.calls.ContainsCallExpr(n, c); node != nil {
    36  		if arg, e := gosec.GetString(node.Args[0]); t.args.MatchString(arg) && e == nil {
    37  			return gosec.NewIssue(c, n, t.ID(), t.What, t.Severity, t.Confidence), nil
    38  		}
    39  	}
    40  	return nil, nil
    41  }
    42  
    43  // NewBadTempFile detects direct writes to predictable path in temporary directory
    44  func NewBadTempFile(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
    45  	calls := gosec.NewCallList()
    46  	calls.Add("io/ioutil", "WriteFile")
    47  	calls.Add("os", "Create")
    48  	return &badTempFile{
    49  		calls: calls,
    50  		args:  regexp.MustCompile(`^/tmp/.*$|^/var/tmp/.*$`),
    51  		MetaData: gosec.MetaData{
    52  			ID:         id,
    53  			Severity:   gosec.Medium,
    54  			Confidence: gosec.High,
    55  			What:       "File creation in shared tmp directory without using ioutil.Tempfile",
    56  		},
    57  	}, []ast.Node{(*ast.CallExpr)(nil)}
    58  }