2016-08-05 12:58:27 +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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
2017-12-28 06:54:10 +00:00
|
|
|
"strings"
|
2016-08-05 12:58:27 +01:00
|
|
|
|
2018-07-19 08:40:28 +01:00
|
|
|
"github.com/securego/gas"
|
2016-08-05 12:58:27 +01:00
|
|
|
)
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
type blacklistedImport struct {
|
2016-08-10 12:51:03 +01:00
|
|
|
gas.MetaData
|
2017-12-13 12:35:47 +00:00
|
|
|
Blacklisted map[string]string
|
2016-08-05 12:58:27 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:54:10 +00:00
|
|
|
func unquote(original string) string {
|
|
|
|
copy := strings.TrimSpace(original)
|
|
|
|
copy = strings.TrimLeft(copy, `"`)
|
|
|
|
return strings.TrimRight(copy, `"`)
|
|
|
|
}
|
|
|
|
|
2017-10-05 22:32:03 +01:00
|
|
|
func (r *blacklistedImport) ID() string {
|
|
|
|
return r.MetaData.ID
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:54:10 +00:00
|
|
|
func (r *blacklistedImport) Match(n ast.Node, c *gas.Context) (*gas.Issue, error) {
|
2016-08-05 12:58:27 +01:00
|
|
|
if node, ok := n.(*ast.ImportSpec); ok {
|
2017-12-28 06:54:10 +00:00
|
|
|
if description, ok := r.Blacklisted[unquote(node.Path.Value)]; ok {
|
2018-03-12 08:18:44 +00:00
|
|
|
return gas.NewIssue(c, node, r.ID(), description, r.Severity, r.Confidence), nil
|
2016-08-05 12:58:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewBlacklistedImports reports when a blacklisted import is being used.
|
|
|
|
// Typically when a deprecated technology is being used.
|
2017-10-05 22:32:03 +01:00
|
|
|
func NewBlacklistedImports(id string, conf gas.Config, blacklist map[string]string) (gas.Rule, []ast.Node) {
|
2017-12-13 12:35:47 +00:00
|
|
|
return &blacklistedImport{
|
2016-08-10 12:51:03 +01:00
|
|
|
MetaData: gas.MetaData{
|
2017-10-05 22:32:03 +01:00
|
|
|
ID: id,
|
2017-12-13 12:35:47 +00:00
|
|
|
Severity: gas.Medium,
|
2016-08-10 12:51:03 +01:00
|
|
|
Confidence: gas.High,
|
2016-08-05 12:58:27 +01:00
|
|
|
},
|
2017-12-13 12:35:47 +00:00
|
|
|
Blacklisted: blacklist,
|
2016-11-13 20:55:31 +00:00
|
|
|
}, []ast.Node{(*ast.ImportSpec)(nil)}
|
2016-08-10 12:51:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewBlacklistedImportMD5 fails if MD5 is imported
|
2017-10-05 22:32:03 +01:00
|
|
|
func NewBlacklistedImportMD5(id string, conf gas.Config) (gas.Rule, []ast.Node) {
|
|
|
|
return NewBlacklistedImports(id, conf, map[string]string{
|
2017-12-28 06:54:10 +00:00
|
|
|
"crypto/md5": "Blacklisted import crypto/md5: weak cryptographic primitive",
|
2017-12-13 12:35:47 +00:00
|
|
|
})
|
2016-08-10 12:51:03 +01:00
|
|
|
}
|
2016-08-05 12:58:27 +01:00
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewBlacklistedImportDES fails if DES is imported
|
2017-10-05 22:32:03 +01:00
|
|
|
func NewBlacklistedImportDES(id string, conf gas.Config) (gas.Rule, []ast.Node) {
|
|
|
|
return NewBlacklistedImports(id, conf, map[string]string{
|
2017-12-28 06:54:10 +00:00
|
|
|
"crypto/des": "Blacklisted import crypto/des: weak cryptographic primitive",
|
2017-12-13 12:35:47 +00:00
|
|
|
})
|
2016-08-10 12:51:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 12:35:47 +00:00
|
|
|
// NewBlacklistedImportRC4 fails if DES is imported
|
2017-10-05 22:32:03 +01:00
|
|
|
func NewBlacklistedImportRC4(id string, conf gas.Config) (gas.Rule, []ast.Node) {
|
|
|
|
return NewBlacklistedImports(id, conf, map[string]string{
|
2017-12-28 06:54:10 +00:00
|
|
|
"crypto/rc4": "Blacklisted import crypto/rc4: weak cryptographic primitive",
|
2017-12-13 12:35:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBlacklistedImportCGI fails if CGI is imported
|
2017-10-05 22:32:03 +01:00
|
|
|
func NewBlacklistedImportCGI(id string, conf gas.Config) (gas.Rule, []ast.Node) {
|
|
|
|
return NewBlacklistedImports(id, conf, map[string]string{
|
2017-12-28 06:54:10 +00:00
|
|
|
"net/http/cgi": "Blacklisted import net/http/cgi: Go versions < 1.6.3 are vulnerable to Httpoxy attack: (CVE-2016-5386)",
|
2017-12-13 12:35:47 +00:00
|
|
|
})
|
2016-08-05 12:58:27 +01:00
|
|
|
}
|