gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/rules/templates.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  
    20  	"github.com/securego/gosec"
    21  )
    22  
    23  type templateCheck struct {
    24  	gosec.MetaData
    25  	calls gosec.CallList
    26  }
    27  
    28  func (t *templateCheck) ID() string {
    29  	return t.MetaData.ID
    30  }
    31  
    32  func (t *templateCheck) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
    33  	if node := t.calls.ContainsCallExpr(n, c); node != nil {
    34  		for _, arg := range node.Args {
    35  			if _, ok := arg.(*ast.BasicLit); !ok { // basic lits are safe
    36  				return gosec.NewIssue(c, n, t.ID(), t.What, t.Severity, t.Confidence), nil
    37  			}
    38  		}
    39  	}
    40  	return nil, nil
    41  }
    42  
    43  // NewTemplateCheck constructs the template check rule. This rule is used to
    44  // find use of tempaltes where HTML/JS escaping is not being used
    45  func NewTemplateCheck(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
    46  
    47  	calls := gosec.NewCallList()
    48  	calls.Add("html/template", "HTML")
    49  	calls.Add("html/template", "HTMLAttr")
    50  	calls.Add("html/template", "JS")
    51  	calls.Add("html/template", "URL")
    52  	return &templateCheck{
    53  		calls: calls,
    54  		MetaData: gosec.MetaData{
    55  			ID:         id,
    56  			Severity:   gosec.Medium,
    57  			Confidence: gosec.Low,
    58  			What:       "this method will not auto-escape HTML. Verify data is well formed.",
    59  		},
    60  	}, []ast.Node{(*ast.CallExpr)(nil)}
    61  }