2017-04-28 22:46:26 +01:00
|
|
|
// (c) Copyright 2016 Hewlett Packard Enterprise Development LP
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rules
|
|
|
|
|
2020-04-01 21:18:39 +01:00
|
|
|
import "github.com/securego/gosec/v2"
|
2017-04-28 22:46:26 +01:00
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
// RuleDefinition contains the description of a rule and a mechanism to
|
|
|
|
// create it.
|
2017-04-28 22:46:26 +01:00
|
|
|
type RuleDefinition struct {
|
2017-10-05 22:32:03 +01:00
|
|
|
ID string
|
2017-04-28 22:46:26 +01:00
|
|
|
Description string
|
2018-07-19 17:42:25 +01:00
|
|
|
Create gosec.RuleBuilder
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
// RuleList is a mapping of rule ID's to rule definitions
|
2017-04-28 22:46:26 +01:00
|
|
|
type RuleList map[string]RuleDefinition
|
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
// Builders returns all the create methods for a given rule list
|
2018-07-19 17:42:25 +01:00
|
|
|
func (rl RuleList) Builders() map[string]gosec.RuleBuilder {
|
|
|
|
builders := make(map[string]gosec.RuleBuilder)
|
2017-05-10 05:26:12 +01:00
|
|
|
for _, def := range rl {
|
2017-10-05 22:32:03 +01:00
|
|
|
builders[def.ID] = def.Create
|
2017-05-10 05:26:12 +01:00
|
|
|
}
|
|
|
|
return builders
|
|
|
|
}
|
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
// RuleFilter can be used to include or exclude a rule depending on the return
|
|
|
|
// value of the function
|
2017-04-28 22:46:26 +01:00
|
|
|
type RuleFilter func(string) bool
|
|
|
|
|
2017-12-13 07:39:00 +00:00
|
|
|
// NewRuleFilter is a closure that will include/exclude the rule ID's based on
|
|
|
|
// the supplied boolean value.
|
2017-04-28 22:46:26 +01:00
|
|
|
func NewRuleFilter(action bool, ruleIDs ...string) RuleFilter {
|
|
|
|
rulelist := make(map[string]bool)
|
|
|
|
for _, rule := range ruleIDs {
|
|
|
|
rulelist[rule] = true
|
|
|
|
}
|
|
|
|
return func(rule string) bool {
|
|
|
|
if _, found := rulelist[rule]; found {
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
return !action
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the list of rules to use
|
|
|
|
func Generate(filters ...RuleFilter) RuleList {
|
2017-10-05 22:32:03 +01:00
|
|
|
rules := []RuleDefinition{
|
2017-04-28 22:46:26 +01:00
|
|
|
// misc
|
2018-03-03 00:03:39 +00:00
|
|
|
{"G101", "Look for hardcoded credentials", NewHardcodedCredentials},
|
|
|
|
{"G102", "Bind to all interfaces", NewBindsToAllNetworkInterfaces},
|
|
|
|
{"G103", "Audit the use of unsafe block", NewUsingUnsafe},
|
|
|
|
{"G104", "Audit errors not checked", NewNoErrorCheck},
|
|
|
|
{"G106", "Audit the use of ssh.InsecureIgnoreHostKey function", NewSSHHostKey},
|
2018-09-04 07:55:03 +01:00
|
|
|
{"G107", "Url provided to HTTP request as taint input", NewSSRFCheck},
|
2019-09-20 09:46:06 +01:00
|
|
|
{"G108", "Profiling endpoint is automatically exposed", NewPprofCheck},
|
2020-01-06 08:55:52 +00:00
|
|
|
{"G109", "Converting strconv.Atoi result to int32/int16", NewIntegerOverflowCheck},
|
2020-01-19 19:40:19 +00:00
|
|
|
{"G110", "Detect io.Copy instead of io.CopyN when decompression", NewDecompressionBombCheck},
|
2017-04-28 22:46:26 +01:00
|
|
|
|
|
|
|
// injection
|
2018-03-03 00:03:39 +00:00
|
|
|
{"G201", "SQL query construction using format string", NewSQLStrFormat},
|
|
|
|
{"G202", "SQL query construction using string concatenation", NewSQLStrConcat},
|
|
|
|
{"G203", "Use of unescaped data in HTML templates", NewTemplateCheck},
|
|
|
|
{"G204", "Audit use of command execution", NewSubproc},
|
2017-04-28 22:46:26 +01:00
|
|
|
|
|
|
|
// filesystem
|
2018-03-03 00:03:39 +00:00
|
|
|
{"G301", "Poor file permissions used when creating a directory", NewMkdirPerms},
|
2018-10-11 13:45:31 +01:00
|
|
|
{"G302", "Poor file permissions used when creation file or using chmod", NewFilePerms},
|
2018-03-03 00:03:39 +00:00
|
|
|
{"G303", "Creating tempfile using a predictable path", NewBadTempFile},
|
2018-03-09 02:49:01 +00:00
|
|
|
{"G304", "File path provided as taint input", NewReadFile},
|
2018-07-18 13:31:07 +01:00
|
|
|
{"G305", "File path traversal when extracting zip archive", NewArchive},
|
2020-02-28 11:48:18 +00:00
|
|
|
{"G306", "Poor file permissions used when writing to a file", NewWritePerms},
|
2020-03-01 20:45:37 +00:00
|
|
|
{"G307", "Unsafe defer call of a method returning an error", NewDeferredClosing},
|
2017-04-28 22:46:26 +01:00
|
|
|
|
|
|
|
// crypto
|
2018-08-08 15:41:34 +01:00
|
|
|
{"G401", "Detect the usage of DES, RC4, MD5 or SHA1", NewUsesWeakCryptography},
|
2018-03-03 00:03:39 +00:00
|
|
|
{"G402", "Look for bad TLS connection settings", NewIntermediateTLSCheck},
|
|
|
|
{"G403", "Ensure minimum RSA key length of 2048 bits", NewWeakKeyStrength},
|
|
|
|
{"G404", "Insecure random number source (rand)", NewWeakRandCheck},
|
2017-04-28 22:46:26 +01:00
|
|
|
|
|
|
|
// blacklist
|
2018-03-03 00:03:39 +00:00
|
|
|
{"G501", "Import blacklist: crypto/md5", NewBlacklistedImportMD5},
|
|
|
|
{"G502", "Import blacklist: crypto/des", NewBlacklistedImportDES},
|
|
|
|
{"G503", "Import blacklist: crypto/rc4", NewBlacklistedImportRC4},
|
|
|
|
{"G504", "Import blacklist: net/http/cgi", NewBlacklistedImportCGI},
|
2018-08-08 15:38:57 +01:00
|
|
|
{"G505", "Import blacklist: crypto/sha1", NewBlacklistedImportSHA1},
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
ruleMap := make(map[string]RuleDefinition)
|
|
|
|
|
|
|
|
RULES:
|
|
|
|
for _, rule := range rules {
|
2017-04-28 22:46:26 +01:00
|
|
|
for _, filter := range filters {
|
2017-10-05 22:32:03 +01:00
|
|
|
if filter(rule.ID) {
|
|
|
|
continue RULES
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 22:32:03 +01:00
|
|
|
ruleMap[rule.ID] = rule
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|
2017-10-05 22:32:03 +01:00
|
|
|
return ruleMap
|
2017-04-28 22:46:26 +01:00
|
|
|
}
|