gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/rules/bind.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  // Looks for net.Listen("0.0.0.0") or net.Listen(":8080")
    25  type bindsToAllNetworkInterfaces struct {
    26  	gosec.MetaData
    27  	calls   gosec.CallList
    28  	pattern *regexp.Regexp
    29  }
    30  
    31  func (r *bindsToAllNetworkInterfaces) ID() string {
    32  	return r.MetaData.ID
    33  }
    34  
    35  func (r *bindsToAllNetworkInterfaces) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, error) {
    36  	callExpr := r.calls.ContainsCallExpr(n, c)
    37  	if callExpr == nil {
    38  		return nil, nil
    39  	}
    40  	if arg, err := gosec.GetString(callExpr.Args[1]); err == nil {
    41  		if r.pattern.MatchString(arg) {
    42  			return gosec.NewIssue(c, n, r.ID(), r.What, r.Severity, r.Confidence), nil
    43  		}
    44  	}
    45  	return nil, nil
    46  }
    47  
    48  // NewBindsToAllNetworkInterfaces detects socket connections that are setup to
    49  // listen on all network interfaces.
    50  func NewBindsToAllNetworkInterfaces(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
    51  	calls := gosec.NewCallList()
    52  	calls.Add("net", "Listen")
    53  	calls.Add("crypto/tls", "Listen")
    54  	return &bindsToAllNetworkInterfaces{
    55  		calls:   calls,
    56  		pattern: regexp.MustCompile(`^(0.0.0.0|:).*$`),
    57  		MetaData: gosec.MetaData{
    58  			ID:         id,
    59  			Severity:   gosec.Medium,
    60  			Confidence: gosec.High,
    61  			What:       "Binds to all network interfaces",
    62  		},
    63  	}, []ast.Node{(*ast.CallExpr)(nil)}
    64  }