gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/rules/rulelist.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 "github.com/securego/gosec"
    18  
    19  // RuleDefinition contains the description of a rule and a mechanism to
    20  // create it.
    21  type RuleDefinition struct {
    22  	ID          string
    23  	Description string
    24  	Create      gosec.RuleBuilder
    25  }
    26  
    27  // RuleList is a mapping of rule ID's to rule definitions
    28  type RuleList map[string]RuleDefinition
    29  
    30  // Builders returns all the create methods for a given rule list
    31  func (rl RuleList) Builders() map[string]gosec.RuleBuilder {
    32  	builders := make(map[string]gosec.RuleBuilder)
    33  	for _, def := range rl {
    34  		builders[def.ID] = def.Create
    35  	}
    36  	return builders
    37  }
    38  
    39  // RuleFilter can be used to include or exclude a rule depending on the return
    40  // value of the function
    41  type RuleFilter func(string) bool
    42  
    43  // NewRuleFilter is a closure that will include/exclude the rule ID's based on
    44  // the supplied boolean value.
    45  func NewRuleFilter(action bool, ruleIDs ...string) RuleFilter {
    46  	rulelist := make(map[string]bool)
    47  	for _, rule := range ruleIDs {
    48  		rulelist[rule] = true
    49  	}
    50  	return func(rule string) bool {
    51  		if _, found := rulelist[rule]; found {
    52  			return action
    53  		}
    54  		return !action
    55  	}
    56  }
    57  
    58  // Generate the list of rules to use
    59  func Generate(filters ...RuleFilter) RuleList {
    60  	rules := []RuleDefinition{
    61  		// misc
    62  		{"G101", "Look for hardcoded credentials", NewHardcodedCredentials},
    63  		{"G102", "Bind to all interfaces", NewBindsToAllNetworkInterfaces},
    64  		{"G103", "Audit the use of unsafe block", NewUsingUnsafe},
    65  		{"G104", "Audit errors not checked", NewNoErrorCheck},
    66  		{"G105", "Audit the use of big.Exp function", NewUsingBigExp},
    67  		{"G106", "Audit the use of ssh.InsecureIgnoreHostKey function", NewSSHHostKey},
    68  		{"G107", "Url provided to HTTP request as taint input", NewSSRFCheck},
    69  
    70  		// injection
    71  		{"G201", "SQL query construction using format string", NewSQLStrFormat},
    72  		{"G202", "SQL query construction using string concatenation", NewSQLStrConcat},
    73  		{"G203", "Use of unescaped data in HTML templates", NewTemplateCheck},
    74  		{"G204", "Audit use of command execution", NewSubproc},
    75  
    76  		// filesystem
    77  		{"G301", "Poor file permissions used when creating a directory", NewMkdirPerms},
    78  		{"G302", "Poor file permisions used when creation file or using chmod", NewFilePerms},
    79  		{"G303", "Creating tempfile using a predictable path", NewBadTempFile},
    80  		{"G304", "File path provided as taint input", NewReadFile},
    81  		{"G305", "File path traversal when extracting zip archive", NewArchive},
    82  
    83  		// crypto
    84  		{"G401", "Detect the usage of DES, RC4, MD5 or SHA1", NewUsesWeakCryptography},
    85  		{"G402", "Look for bad TLS connection settings", NewIntermediateTLSCheck},
    86  		{"G403", "Ensure minimum RSA key length of 2048 bits", NewWeakKeyStrength},
    87  		{"G404", "Insecure random number source (rand)", NewWeakRandCheck},
    88  
    89  		// blacklist
    90  		{"G501", "Import blacklist: crypto/md5", NewBlacklistedImportMD5},
    91  		{"G502", "Import blacklist: crypto/des", NewBlacklistedImportDES},
    92  		{"G503", "Import blacklist: crypto/rc4", NewBlacklistedImportRC4},
    93  		{"G504", "Import blacklist: net/http/cgi", NewBlacklistedImportCGI},
    94  		{"G505", "Import blacklist: crypto/sha1", NewBlacklistedImportSHA1},
    95  	}
    96  
    97  	ruleMap := make(map[string]RuleDefinition)
    98  
    99  RULES:
   100  	for _, rule := range rules {
   101  		for _, filter := range filters {
   102  			if filter(rule.ID) {
   103  				continue RULES
   104  			}
   105  		}
   106  		ruleMap[rule.ID] = rule
   107  	}
   108  	return ruleMap
   109  }