Skip to content

Commit 2b2077e

Browse files
authored
Add G118 SSA analyzer for context propagation failures that can cause goroutine/resource leaks (#1516)
* Add G118 SSA analyzer for context propagation failures that can cause goroutine/resource leaks This PR introduces G118, a new SSA-based gosec rule that detects high-risk context misuse patterns: goroutines using context.Background/TODO when request context exists, missing cancel() calls from WithCancel/WithTimeout/WithDeadline, and unbounded blocking loop regions without ctx.Done() guards. These patterns can leak goroutines and I/O resources, leading to resource exhaustion/DoS in production services. The rule is mapped to CWE-400, integrated into analyzer registration and docs, and includes positive/negative samples (including complex loop CFG cases) to reduce false positives while preserving detection quality. Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch> * Fix false pasitive Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch> --------- Signed-off-by: Cosmin Cojocar <cosmin@cojocar.ch>
1 parent a7666f3 commit 2b2077e

File tree

6 files changed

+984
-0
lines changed

6 files changed

+984
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ directory you can supply `./...` as the input argument.
196196
- G115: Potential integer overflow when converting between integer types
197197
- G116: Detect Trojan Source attacks using bidirectional Unicode control characters
198198
- G117: Potential exposure of secrets via JSON marshaling
199+
- G118: Context propagation failure leading to goroutine/resource leaks
199200
- G201: SQL query construction using format string
200201
- G202: SQL query construction using string concatenation
201202
- G203: Use of unescaped data in HTML templates

analyzers/analyzers_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ var _ = Describe("gosec analyzers", func() {
5151
})
5252

5353
Context("report correct errors for all samples", func() {
54+
It("should detect context propagation failures", func() {
55+
runner("G118", testutils.SampleCodeG118)
56+
})
57+
5458
It("should detect HTTP request smuggling", func() {
5559
runner("G113", testutils.SampleCodeG113)
5660
})

analyzers/analyzerslist.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ func NewAnalyzerFilter(action bool, analyzerIDs ...string) AnalyzerFilter {
113113
}
114114

115115
var defaultAnalyzers = []AnalyzerDefinition{
116+
{"G118", "Context propagation failure leading to goroutine/resource leaks", newContextPropagationAnalyzer},
116117
{"G113", "HTTP request smuggling via conflicting headers or bare LF in body parsing", newRequestSmugglingAnalyzer},
117118
{"G115", "Type conversion which leads to integer overflow", newConversionOverflowAnalyzer},
118119
{"G602", "Possible slice bounds out of range", newSliceBoundsAnalyzer},

0 commit comments

Comments
 (0)