Skip to content

Commit fd28036

Browse files
authored
Fix the TLS config rule when parsing the settings from a variable (#911)
1 parent a522ae6 commit fd28036

File tree

4 files changed

+65
-23
lines changed

4 files changed

+65
-23
lines changed

cmd/tlsconfig/rule_template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ func New{{.Name}}TLSCheck(id string, conf gosec.Config) (gosec.Rule, []ast.Node)
1515
{{range $cipherName := .Ciphers }} "{{$cipherName}}",
1616
{{end}}
1717
},
18-
}, []ast.Node{(*ast.CompositeLit)(nil)}
18+
}, []ast.Node{(*ast.CompositeLit)(nil), (*ast.AssignStmt)(nil)}
1919
}
2020
`))

rules/tls.go

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,51 @@ func (t *insecureConfigTLS) processTLSCipherSuites(n ast.Node, c *gosec.Context)
6363
return nil
6464
}
6565

66-
func (t *insecureConfigTLS) processTLSConfVal(n *ast.KeyValueExpr, c *gosec.Context) *gosec.Issue {
67-
if ident, ok := n.Key.(*ast.Ident); ok {
66+
func (t *insecureConfigTLS) processTLSConf(n ast.Node, c *gosec.Context) *gosec.Issue {
67+
if kve, ok := n.(*ast.KeyValueExpr); ok {
68+
issue := t.processTLSConfVal(kve.Key, kve.Value, c)
69+
if issue != nil {
70+
return issue
71+
}
72+
} else if assign, ok := n.(*ast.AssignStmt); ok {
73+
if len(assign.Lhs) < 1 || len(assign.Rhs) < 1 {
74+
return nil
75+
}
76+
if selector, ok := assign.Lhs[0].(*ast.SelectorExpr); ok {
77+
issue := t.processTLSConfVal(selector.Sel, assign.Rhs[0], c)
78+
if issue != nil {
79+
return issue
80+
}
81+
}
82+
}
83+
return nil
84+
}
85+
86+
func (t *insecureConfigTLS) processTLSConfVal(key ast.Expr, value ast.Expr, c *gosec.Context) *gosec.Issue {
87+
if ident, ok := key.(*ast.Ident); ok {
6888
switch ident.Name {
6989
case "InsecureSkipVerify":
70-
if node, ok := n.Value.(*ast.Ident); ok {
90+
if node, ok := value.(*ast.Ident); ok {
7191
if node.Name != "false" {
72-
return gosec.NewIssue(c, n, t.ID(), "TLS InsecureSkipVerify set true.", gosec.High, gosec.High)
92+
return gosec.NewIssue(c, value, t.ID(), "TLS InsecureSkipVerify set true.", gosec.High, gosec.High)
7393
}
7494
} else {
7595
// TODO(tk): symbol tab look up to get the actual value
76-
return gosec.NewIssue(c, n, t.ID(), "TLS InsecureSkipVerify may be true.", gosec.High, gosec.Low)
96+
return gosec.NewIssue(c, value, t.ID(), "TLS InsecureSkipVerify may be true.", gosec.High, gosec.Low)
7797
}
7898

7999
case "PreferServerCipherSuites":
80-
if node, ok := n.Value.(*ast.Ident); ok {
100+
if node, ok := value.(*ast.Ident); ok {
81101
if node.Name == "false" {
82-
return gosec.NewIssue(c, n, t.ID(), "TLS PreferServerCipherSuites set false.", gosec.Medium, gosec.High)
102+
return gosec.NewIssue(c, value, t.ID(), "TLS PreferServerCipherSuites set false.", gosec.Medium, gosec.High)
83103
}
84104
} else {
85105
// TODO(tk): symbol tab look up to get the actual value
86-
return gosec.NewIssue(c, n, t.ID(), "TLS PreferServerCipherSuites may be false.", gosec.Medium, gosec.Low)
106+
return gosec.NewIssue(c, value, t.ID(), "TLS PreferServerCipherSuites may be false.", gosec.Medium, gosec.Low)
87107
}
88108

89109
case "MinVersion":
90-
if d, ok := n.Value.(*ast.Ident); ok {
110+
if d, ok := value.(*ast.Ident); ok {
91111
obj := d.Obj
92112
if obj == nil {
93113
for _, f := range c.PkgFiles {
@@ -118,10 +138,10 @@ func (t *insecureConfigTLS) processTLSConfVal(n *ast.KeyValueExpr, c *gosec.Cont
118138
t.actualMinVersion = ival
119139
}
120140
}
121-
} else if ival, ierr := gosec.GetInt(n.Value); ierr == nil {
141+
} else if ival, ierr := gosec.GetInt(value); ierr == nil {
122142
t.actualMinVersion = ival
123143
} else {
124-
if se, ok := n.Value.(*ast.SelectorExpr); ok {
144+
if se, ok := value.(*ast.SelectorExpr); ok {
125145
if pkg, ok := se.X.(*ast.Ident); ok {
126146
if ip, ok := gosec.GetImportPath(pkg.Name, c); ok && ip == "crypto/tls" {
127147
t.actualMinVersion = t.mapVersion(se.Sel.Name)
@@ -131,10 +151,10 @@ func (t *insecureConfigTLS) processTLSConfVal(n *ast.KeyValueExpr, c *gosec.Cont
131151
}
132152

133153
case "MaxVersion":
134-
if ival, ierr := gosec.GetInt(n.Value); ierr == nil {
154+
if ival, ierr := gosec.GetInt(value); ierr == nil {
135155
t.actualMaxVersion = ival
136156
} else {
137-
if se, ok := n.Value.(*ast.SelectorExpr); ok {
157+
if se, ok := value.(*ast.SelectorExpr); ok {
138158
if pkg, ok := se.X.(*ast.Ident); ok {
139159
if ip, ok := gosec.GetImportPath(pkg.Name, c); ok && ip == "crypto/tls" {
140160
t.actualMaxVersion = t.mapVersion(se.Sel.Name)
@@ -144,7 +164,7 @@ func (t *insecureConfigTLS) processTLSConfVal(n *ast.KeyValueExpr, c *gosec.Cont
144164
}
145165

146166
case "CipherSuites":
147-
if ret := t.processTLSCipherSuites(n.Value, c); ret != nil {
167+
if ret := t.processTLSCipherSuites(value, c); ret != nil {
148168
return ret
149169
}
150170

@@ -192,17 +212,27 @@ func (t *insecureConfigTLS) Match(n ast.Node, c *gosec.Context) (*gosec.Issue, e
192212
actualType := c.Info.TypeOf(complit.Type)
193213
if actualType != nil && actualType.String() == t.requiredType {
194214
for _, elt := range complit.Elts {
195-
if kve, ok := elt.(*ast.KeyValueExpr); ok {
196-
issue := t.processTLSConfVal(kve, c)
197-
if issue != nil {
198-
return issue, nil
199-
}
215+
issue := t.processTLSConf(elt, c)
216+
if issue != nil {
217+
return issue, nil
200218
}
201219
}
202220
issue := t.checkVersion(complit, c)
203221
t.resetVersion()
204222
return issue, nil
205223
}
224+
} else {
225+
if assign, ok := n.(*ast.AssignStmt); ok && len(assign.Lhs) > 0 {
226+
if selector, ok := assign.Lhs[0].(*ast.SelectorExpr); ok {
227+
actualType := c.Info.TypeOf(selector.X)
228+
if actualType != nil && actualType.String() == t.requiredType {
229+
issue := t.processTLSConf(assign, c)
230+
if issue != nil {
231+
return issue, nil
232+
}
233+
}
234+
}
235+
}
206236
}
207237
return nil, nil
208238
}

rules/tls_config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func NewModernTLSCheck(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
1919
"TLS_AES_256_GCM_SHA384",
2020
"TLS_CHACHA20_POLY1305_SHA256",
2121
},
22-
}, []ast.Node{(*ast.CompositeLit)(nil)}
22+
}, []ast.Node{(*ast.CompositeLit)(nil), (*ast.AssignStmt)(nil)}
2323
}
2424

2525
// NewIntermediateTLSCheck creates a check for Intermediate TLS ciphers
@@ -45,7 +45,7 @@ func NewIntermediateTLSCheck(id string, conf gosec.Config) (gosec.Rule, []ast.No
4545
"TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",
4646
"TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",
4747
},
48-
}, []ast.Node{(*ast.CompositeLit)(nil)}
48+
}, []ast.Node{(*ast.CompositeLit)(nil), (*ast.AssignStmt)(nil)}
4949
}
5050

5151
// NewOldTLSCheck creates a check for Old TLS ciphers
@@ -88,5 +88,5 @@ func NewOldTLSCheck(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
8888
"TLS_RSA_WITH_AES_256_CBC_SHA",
8989
"TLS_RSA_WITH_3DES_EDE_CBC_SHA",
9090
},
91-
}, []ast.Node{(*ast.CompositeLit)(nil)}
91+
}, []ast.Node{(*ast.CompositeLit)(nil), (*ast.AssignStmt)(nil)}
9292
}

testutils/source.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2859,6 +2859,18 @@ func main() {
28592859
if err != nil {
28602860
fmt.Println(err)
28612861
}
2862+
}`}, 1, gosec.NewConfig()},
2863+
{[]string{`
2864+
// InsecureSkipVerify from variable
2865+
package main
2866+
2867+
import (
2868+
"crypto/tls"
2869+
)
2870+
2871+
func main() {
2872+
var conf tls.Config
2873+
conf.InsecureSkipVerify = true
28622874
}`}, 1, gosec.NewConfig()},
28632875
{[]string{
28642876
`

0 commit comments

Comments
 (0)