Skip to content

Commit 34d144b

Browse files
authored
Add new rule for Slowloris Attack
1 parent a64cde5 commit 34d144b

File tree

8 files changed

+130
-1
lines changed

8 files changed

+130
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ directory you can supply `./...` as the input argument.
144144
- G109: Potential Integer overflow made by strconv.Atoi result conversion to int16/32
145145
- G110: Potential DoS vulnerability via decompression bomb
146146
- G111: Potential directory traversal
147+
- G112: Potential slowloris attack
147148
- G201: SQL query construction using format string
148149
- G202: SQL query construction using string concatenation
149150
- G203: Use of unescaped data in HTML templates

cwe/data.go

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ var (
8989
Description: "Creating and using insecure temporary files can leave application and system data vulnerable to attack.",
9090
Name: "Insecure Temporary File",
9191
},
92+
{
93+
ID: "400",
94+
Description: "The software does not properly control the allocation and maintenance of a limited resource, thereby enabling an actor to influence the amount of resources consumed, eventually leading to the exhaustion of available resources.",
95+
Name: "Uncontrolled Resource Consumption",
96+
},
9297
{
9398
ID: "409",
9499
Description: "The software does not handle or incorrectly handles a compressed input with a very high compression ratio that produces a large output.",

issue.go

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var ruleToCWE = map[string]string{
6464
"G109": "190",
6565
"G110": "409",
6666
"G111": "22",
67+
"G112": "400",
6768
"G201": "89",
6869
"G202": "89",
6970
"G203": "79",

report/formatter_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ var _ = Describe("Formatter", func() {
277277
Context("When using different report formats", func() {
278278
grules := []string{
279279
"G101", "G102", "G103", "G104", "G106", "G107", "G109",
280-
"G110", "G111", "G201", "G202", "G203", "G204", "G301",
280+
"G110", "G111", "G112", "G201", "G202", "G203", "G204", "G301",
281281
"G302", "G303", "G304", "G305", "G401", "G402", "G403",
282282
"G404", "G501", "G502", "G503", "G504", "G505",
283283
}

rules/rulelist.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func Generate(trackSuppressions bool, filters ...RuleFilter) RuleList {
7474
{"G109", "Converting strconv.Atoi result to int32/int16", NewIntegerOverflowCheck},
7575
{"G110", "Detect io.Copy instead of io.CopyN when decompression", NewDecompressionBombCheck},
7676
{"G111", "Detect http.Dir('/') as a potential risk", NewDirectoryTraversal},
77+
{"G112", "Detect ReadHeaderTimeout not configured as a potential risk", NewSlowloris},
7778

7879
// injection
7980
{"G201", "SQL query construction using format string", NewSQLStrFormat},

rules/rules_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ var _ = Describe("gosec rules", func() {
9494
runner("G111", testutils.SampleCodeG111)
9595
})
9696

97+
It("should detect potential slowloris attack", func() {
98+
runner("G112", testutils.SampleCodeG112)
99+
})
100+
97101
It("should detect sql injection via format strings", func() {
98102
runner("G201", testutils.SampleCodeG201)
99103
})

rules/slowloris.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 (
18+
"go/ast"
19+
20+
"github.com/securego/gosec/v2"
21+
)
22+
23+
type slowloris struct {
24+
gosec.MetaData
25+
}
26+
27+
func (r *slowloris) ID() string {
28+
return r.MetaData.ID
29+
}
30+
31+
func containsReadHeaderTimeout(node *ast.CompositeLit) bool {
32+
if node == nil {
33+
return false
34+
}
35+
for _, elt := range node.Elts {
36+
if kv, ok := elt.(*ast.KeyValueExpr); ok {
37+
if ident, ok := kv.Key.(*ast.Ident); ok {
38+
if ident.Name == "ReadHeaderTimeout" {
39+
return true
40+
}
41+
}
42+
}
43+
}
44+
return false
45+
}
46+
47+
func (r *slowloris) Match(n ast.Node, ctx *gosec.Context) (*gosec.Issue, error) {
48+
switch node := n.(type) {
49+
case *ast.CompositeLit:
50+
actualType := ctx.Info.TypeOf(node.Type)
51+
if actualType != nil && actualType.String() == "net/http.Server" {
52+
if !containsReadHeaderTimeout(node) {
53+
return gosec.NewIssue(ctx, node, r.ID(), r.What, r.Severity, r.Confidence), nil
54+
}
55+
}
56+
}
57+
return nil, nil
58+
}
59+
60+
// NewSlowloris attempts to find the http.Server struct and check if the ReadHeaderTimeout is configured.
61+
func NewSlowloris(id string, conf gosec.Config) (gosec.Rule, []ast.Node) {
62+
return &slowloris{
63+
MetaData: gosec.MetaData{
64+
ID: id,
65+
What: "Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server",
66+
Confidence: gosec.Low,
67+
Severity: gosec.Medium,
68+
},
69+
}, []ast.Node{(*ast.CompositeLit)(nil)}
70+
}

testutils/source.go

+47
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,53 @@ func HelloServer(w http.ResponseWriter, r *http.Request) {
10051005
}`}, 1, gosec.NewConfig()},
10061006
}
10071007

1008+
// SampleCodeG112 - potential slowloris attack
1009+
SampleCodeG112 = []CodeSample{
1010+
{[]string{`
1011+
package main
1012+
1013+
import (
1014+
"fmt"
1015+
"net/http"
1016+
)
1017+
1018+
func main() {
1019+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
1020+
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
1021+
})
1022+
err := (&http.Server{
1023+
Addr: ":1234",
1024+
}).ListenAndServe()
1025+
if err != nil {
1026+
panic(err)
1027+
}
1028+
}
1029+
`}, 1, gosec.NewConfig()},
1030+
{[]string{`
1031+
package main
1032+
1033+
import (
1034+
"fmt"
1035+
"time"
1036+
"net/http"
1037+
)
1038+
1039+
func main() {
1040+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
1041+
fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
1042+
})
1043+
server := &http.Server{
1044+
Addr: ":1234",
1045+
ReadHeaderTimeout: 3 * time.Second,
1046+
}
1047+
err := server.ListenAndServe()
1048+
if err != nil {
1049+
panic(err)
1050+
}
1051+
}
1052+
`}, 0, gosec.NewConfig()},
1053+
}
1054+
10081055
// SampleCodeG201 - SQL injection via format string
10091056
SampleCodeG201 = []CodeSample{
10101057
{[]string{`

0 commit comments

Comments
 (0)