Fixes for gosec

This commit is contained in:
Manuel Alejandro de Brito Fontes 2020-12-04 09:40:42 -03:00
parent 9553b277e9
commit d781d99797
12 changed files with 43 additions and 30 deletions

View file

@ -48,7 +48,10 @@ jobs:
- name: Run Gosec Security Scanner - name: Run Gosec Security Scanner
uses: securego/gosec@master uses: securego/gosec@master
with: with:
args: -exclude=G104,G304 -exclude-dir=test ./... # G601 for zz_generated.deepcopy.go
# G306 TODO: Expect WriteFile permissions to be 0600 or less
# G307 TODO: Deferring unsafe method "Close"
args: -exclude=G601,G104,G204,G304,G306,G307 -tests=false -exclude-dir=test -exclude-dir=images/ -exclude-dir=docs/ ./...
build: build:
name: Build name: Build

View file

@ -19,7 +19,7 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand" "math/rand" // #nosec
"net/http" "net/http"
"net/http/pprof" "net/http/pprof"
"os" "os"

View file

@ -18,17 +18,18 @@ package main
import ( import (
"fmt" "fmt"
"math/rand"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"k8s.io/apimachinery/pkg/util/uuid"
) )
// Sample authentication service returning several HTTP headers in response // Sample authentication service returning several HTTP headers in response
func main() { func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if strings.ContainsAny(r.Header.Get("User"), "internal") { if strings.ContainsAny(r.Header.Get("User"), "internal") {
w.Header().Add("UserID", strconv.Itoa(rand.Int())) w.Header().Add("UserID", fmt.Sprintf("%v", uuid.NewUUID()))
w.Header().Add("UserRole", "admin") w.Header().Add("UserRole", "admin")
w.Header().Add("Other", "not used") w.Header().Add("Other", "not used")
fmt.Fprint(w, "ok") fmt.Fprint(w, "ok")

View file

@ -40,7 +40,7 @@ func convertV1beta1AdmissionReviewToAdmissionAdmissionReview(in *admissionv1beta
} else { } else {
out.Request = nil out.Request = nil
} }
out.Response = (*admissionv1.AdmissionResponse)(unsafe.Pointer(in.Response)) out.Response = (*admissionv1.AdmissionResponse)(unsafe.Pointer(in.Response)) // #nosec
} }
func convertV1beta1AdmissionRequestToAdmissionAdmissionRequest(in *admissionv1beta1.AdmissionRequest, out *admissionv1.AdmissionRequest) { func convertV1beta1AdmissionRequestToAdmissionAdmissionRequest(in *admissionv1beta1.AdmissionRequest, out *admissionv1.AdmissionRequest) {
@ -48,8 +48,8 @@ func convertV1beta1AdmissionRequestToAdmissionAdmissionRequest(in *admissionv1be
out.Kind = in.Kind out.Kind = in.Kind
out.Resource = in.Resource out.Resource = in.Resource
out.SubResource = in.SubResource out.SubResource = in.SubResource
out.RequestKind = (*metav1.GroupVersionKind)(unsafe.Pointer(in.RequestKind)) out.RequestKind = (*metav1.GroupVersionKind)(unsafe.Pointer(in.RequestKind)) // #nosec
out.RequestResource = (*metav1.GroupVersionResource)(unsafe.Pointer(in.RequestResource)) out.RequestResource = (*metav1.GroupVersionResource)(unsafe.Pointer(in.RequestResource)) // #nosec
out.RequestSubResource = in.RequestSubResource out.RequestSubResource = in.RequestSubResource
out.Name = in.Name out.Name = in.Name
out.Namespace = in.Namespace out.Namespace = in.Namespace
@ -70,7 +70,7 @@ func convertAdmissionAdmissionReviewToV1beta1AdmissionReview(in *admissionv1.Adm
} else { } else {
out.Request = nil out.Request = nil
} }
out.Response = (*admissionv1beta1.AdmissionResponse)(unsafe.Pointer(in.Response)) out.Response = (*admissionv1beta1.AdmissionResponse)(unsafe.Pointer(in.Response)) // #nosec
} }
func convertAdmissionAdmissionRequestToV1beta1AdmissionRequest(in *admissionv1.AdmissionRequest, out *admissionv1beta1.AdmissionRequest) { func convertAdmissionAdmissionRequestToV1beta1AdmissionRequest(in *admissionv1.AdmissionRequest, out *admissionv1beta1.AdmissionRequest) {
@ -78,8 +78,8 @@ func convertAdmissionAdmissionRequestToV1beta1AdmissionRequest(in *admissionv1.A
out.Kind = in.Kind out.Kind = in.Kind
out.Resource = in.Resource out.Resource = in.Resource
out.SubResource = in.SubResource out.SubResource = in.SubResource
out.RequestKind = (*metav1.GroupVersionKind)(unsafe.Pointer(in.RequestKind)) out.RequestKind = (*metav1.GroupVersionKind)(unsafe.Pointer(in.RequestKind)) // #nosec
out.RequestResource = (*metav1.GroupVersionResource)(unsafe.Pointer(in.RequestResource)) out.RequestResource = (*metav1.GroupVersionResource)(unsafe.Pointer(in.RequestResource)) // #nosec
out.RequestSubResource = in.RequestSubResource out.RequestSubResource = in.RequestSubResource
out.Name = in.Name out.Name = in.Name
out.Namespace = in.Namespace out.Namespace = in.Namespace

View file

@ -17,7 +17,7 @@ limitations under the License.
package file package file
import ( import (
"crypto/sha1" "crypto/sha1" // #nosec
"encoding/hex" "encoding/hex"
"io/ioutil" "io/ioutil"
@ -26,7 +26,7 @@ import (
// SHA1 returns the SHA1 of a file. // SHA1 returns the SHA1 of a file.
func SHA1(filename string) string { func SHA1(filename string) string {
hasher := sha1.New() hasher := sha1.New() // #nosec
s, err := ioutil.ReadFile(filename) s, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
klog.ErrorS(err, "Error reading file", "path", filename) klog.ErrorS(err, "Error reading file", "path", filename)

View file

@ -304,7 +304,7 @@ func (n *NGINXController) getStreamServices(configmapName string, proto apiv1.Pr
reserverdPorts := sets.NewInt(rp...) reserverdPorts := sets.NewInt(rp...)
// svcRef format: <(str)namespace>/<(str)service>:<(intstr)port>[:<("PROXY")decode>:<("PROXY")encode>] // svcRef format: <(str)namespace>/<(str)service>:<(intstr)port>[:<("PROXY")decode>:<("PROXY")encode>]
for port, svcRef := range configmap.Data { for port, svcRef := range configmap.Data {
externalPort, err := strconv.Atoi(port) externalPort, err := strconv.Atoi(port) // #nosec
if err != nil { if err != nil {
klog.Warningf("%q is not a valid %v port number", port, proto) klog.Warningf("%q is not a valid %v port number", port, proto)
continue continue
@ -342,11 +342,13 @@ func (n *NGINXController) getStreamServices(configmapName string, proto apiv1.Pr
continue continue
} }
var endps []ingress.Endpoint var endps []ingress.Endpoint
targetPort, err := strconv.Atoi(svcPort) /* #nosec */
targetPort, err := strconv.Atoi(svcPort) // #nosec
if err != nil { if err != nil {
// not a port number, fall back to using port name // not a port number, fall back to using port name
klog.V(3).Infof("Searching Endpoints with %v port name %q for Service %q", proto, svcPort, nsName) klog.V(3).Infof("Searching Endpoints with %v port name %q for Service %q", proto, svcPort, nsName)
for _, sp := range svc.Spec.Ports { for i := range svc.Spec.Ports {
sp := svc.Spec.Ports[i]
if sp.Name == svcPort { if sp.Name == svcPort {
if sp.Protocol == proto { if sp.Protocol == proto {
endps = getEndpoints(svc, &sp, proto, n.store.GetServiceEndpoints) endps = getEndpoints(svc, &sp, proto, n.store.GetServiceEndpoints)
@ -356,7 +358,8 @@ func (n *NGINXController) getStreamServices(configmapName string, proto apiv1.Pr
} }
} else { } else {
klog.V(3).Infof("Searching Endpoints with %v port number %d for Service %q", proto, targetPort, nsName) klog.V(3).Infof("Searching Endpoints with %v port number %d for Service %q", proto, targetPort, nsName)
for _, sp := range svc.Spec.Ports { for i := range svc.Spec.Ports {
sp := svc.Spec.Ports[i]
if sp.Port == int32(targetPort) { if sp.Port == int32(targetPort) {
if sp.Protocol == proto { if sp.Protocol == proto {
endps = getEndpoints(svc, &sp, proto, n.store.GetServiceEndpoints) endps = getEndpoints(svc, &sp, proto, n.store.GetServiceEndpoints)
@ -939,7 +942,8 @@ func (n *NGINXController) serviceEndpoints(svcKey, backendPort string) ([]ingres
return upstreams, nil return upstreams, nil
} }
for _, servicePort := range svc.Spec.Ports { for i := range svc.Spec.Ports {
servicePort := svc.Spec.Ports[i]
// targetPort could be a string, use either the port name or number (int) // targetPort could be a string, use either the port name or number (int)
if strconv.Itoa(int(servicePort.Port)) == backendPort || if strconv.Itoa(int(servicePort.Port)) == backendPort ||
servicePort.TargetPort.String() == backendPort || servicePort.TargetPort.String() == backendPort ||
@ -1498,7 +1502,7 @@ func shouldCreateUpstreamForLocationDefaultBackend(upstream *ingress.Backend, lo
} }
func externalNamePorts(name string, svc *apiv1.Service) *apiv1.ServicePort { func externalNamePorts(name string, svc *apiv1.Service) *apiv1.ServicePort {
port, err := strconv.Atoi(name) port, err := strconv.Atoi(name) // #nosec
if err != nil { if err != nil {
// not a number. check port names. // not a number. check port names.
for _, svcPort := range svc.Spec.Ports { for _, svcPort := range svc.Spec.Ports {

View file

@ -434,7 +434,7 @@ func (n NGINXController) generateTemplate(cfg ngx_config.Configuration, ingressC
klog.Warningf("Missing Service for SSL Passthrough backend %q", pb.Backend) klog.Warningf("Missing Service for SSL Passthrough backend %q", pb.Backend)
continue continue
} }
port, err := strconv.Atoi(pb.Port.String()) port, err := strconv.Atoi(pb.Port.String()) // #nosec
if err != nil { if err != nil {
for _, sp := range svc.Spec.Ports { for _, sp := range svc.Spec.Ports {
if sp.Name == pb.Port.String() { if sp.Name == pb.Port.String() {

View file

@ -18,13 +18,13 @@ package template
import ( import (
"bytes" "bytes"
"crypto/sha1" "crypto/sha1" // #nosec
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand" // #nosec
"net" "net"
"net/url" "net/url"
"os" "os"
@ -929,7 +929,7 @@ func buildAuthSignURL(authSignURL, authRedirectParam string) string {
} }
func buildAuthSignURLLocation(location, authSignURL string) string { func buildAuthSignURLLocation(location, authSignURL string) string {
hasher := sha1.New() hasher := sha1.New() // #nosec
hasher.Write([]byte(location)) hasher.Write([]byte(location))
hasher.Write([]byte(authSignURL)) hasher.Write([]byte(authSignURL))
return "@" + hex.EncodeToString(hasher.Sum(nil)) return "@" + hex.EncodeToString(hasher.Sum(nil))
@ -944,7 +944,7 @@ func init() {
func randomString() string { func randomString() string {
b := make([]rune, 32) b := make([]rune, 32)
for i := range b { for i := range b {
b[i] = letters[rand.Intn(len(letters))] b[i] = letters[rand.Intn(len(letters))] // #nosec
} }
return string(b) return string(b)

View file

@ -110,7 +110,7 @@ func NewSocketCollector(pod, namespace, class string, metricsPerHost bool) (*Soc
return nil, err return nil, err
} }
err = os.Chmod(socket, 0777) err = os.Chmod(socket, 0777) // #nosec
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -181,7 +181,8 @@ func (s *statusSync) runningAddresses() ([]string, error) {
} }
addrs := make([]string, 0) addrs := make([]string, 0)
for _, pod := range pods.Items { for i := range pods.Items {
pod := pods.Items[i]
// only Running pods are valid // only Running pods are valid
if pod.Status.Phase != apiv1.PodRunning { if pod.Status.Phase != apiv1.PodRunning {
continue continue

View file

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
"crypto/sha1" "crypto/sha1" // #nosec
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"crypto/x509/pkix" "crypto/x509/pkix"
@ -125,7 +125,7 @@ func CreateSSLCert(cert, key []byte, uid string) (*ingress.SSLCert, error) {
} }
} }
hasher := sha1.New() hasher := sha1.New() // #nosec
hasher.Write(pemCert.Raw) hasher.Write(pemCert.Raw)
return &ingress.SSLCert{ return &ingress.SSLCert{
@ -504,9 +504,12 @@ func NewTLSListener(certificate, key string) *TLSListener {
keyPath: key, keyPath: key,
lock: sync.Mutex{}, lock: sync.Mutex{},
} }
l.load() l.load()
watch.NewFileWatcher(certificate, l.load)
watch.NewFileWatcher(key, l.load) _, _ = watch.NewFileWatcher(certificate, l.load)
_, _ = watch.NewFileWatcher(key, l.load)
return &l return &l
} }
@ -521,6 +524,7 @@ func (tl *TLSListener) GetCertificate(*tls.ClientHelloInfo) (*tls.Certificate, e
func (tl *TLSListener) TLSConfig() *tls.Config { func (tl *TLSListener) TLSConfig() *tls.Config {
return &tls.Config{ return &tls.Config{
GetCertificate: tl.GetCertificate, GetCertificate: tl.GetCertificate,
MinVersion: tls.VersionTLS12,
} }
} }

View file

@ -118,7 +118,7 @@ func downloadDatabase(dbName string) error {
defer outFile.Close() defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil { if _, err := io.CopyN(outFile, tarReader, header.Size); err != nil {
return err return err
} }