forked from securego/gosec
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg118_samples.go
More file actions
1940 lines (1557 loc) · 32.3 KB
/
g118_samples.go
File metadata and controls
1940 lines (1557 loc) · 32.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package testutils
import "github.com/securego/gosec/v2"
// SampleCodeG118 - Context propagation failures that may leak goroutines/resources
var SampleCodeG118 = []CodeSample{
// Vulnerable: goroutine uses context.Background while request context exists
{[]string{`
package main
import (
"context"
"net/http"
"time"
)
func handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
_ = ctx
go func() {
child, _ := context.WithTimeout(context.Background(), time.Second)
_ = child
}()
}
`}, 2, gosec.NewConfig()},
// Vulnerable: cancel function from context.WithTimeout is never called
{[]string{`
package main
import (
"context"
"time"
)
func work(ctx context.Context) {
child, _ := context.WithTimeout(ctx, time.Second)
_ = child
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with blocking call and no ctx.Done guard
{[]string{`
package main
import (
"context"
"time"
)
func run(ctx context.Context) {
for {
time.Sleep(time.Second)
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: complex infinite multi-block loop without ctx.Done guard
{[]string{`
package main
import (
"context"
"time"
)
func complexInfinite(ctx context.Context, ch <-chan int) {
_ = ctx
for {
select {
case <-ch:
time.Sleep(time.Millisecond)
default:
time.Sleep(time.Millisecond)
}
}
}
`}, 1, gosec.NewConfig()},
// Safe: goroutine propagates request context and checks cancellation
{[]string{`
package main
import (
"context"
"net/http"
"time"
)
func handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
go func(ctx2 context.Context) {
for {
select {
case <-ctx2.Done():
return
case <-time.After(time.Millisecond):
}
}
}(ctx)
}
`}, 0, gosec.NewConfig()},
// Safe: cancel is always called
{[]string{`
package main
import (
"context"
"time"
)
func work(ctx context.Context) {
child, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
_ = child
}
`}, 0, gosec.NewConfig()},
// Safe: cancel is forwarded then deferred (regression for SSA store/load flow)
{[]string{`
package main
import "context"
func forwarded(ctx context.Context) {
child, cancel := context.WithCancel(ctx)
_ = child
cancelCopy := cancel
defer cancelCopy()
}
`}, 0, gosec.NewConfig()},
// Safe: loop has explicit ctx.Done guard
{[]string{`
package main
import (
"context"
"time"
)
func run(ctx context.Context) {
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
}
}
}
`}, 0, gosec.NewConfig()},
// Safe: bounded loop with blocking call (finite by condition)
{[]string{`
package main
import (
"context"
"time"
)
func bounded(ctx context.Context) {
_ = ctx
for i := 0; i < 3; i++ {
time.Sleep(time.Millisecond)
}
}
`}, 0, gosec.NewConfig()},
// Safe: complex loop with explicit non-context exit path
{[]string{`
package main
import (
"context"
"time"
)
func worker(ctx context.Context, max int) {
_ = ctx
i := 0
for {
if i >= max {
break
}
time.Sleep(time.Millisecond)
i++
}
}
`}, 0, gosec.NewConfig()},
// Vulnerable: context.WithCancel variant (not just WithTimeout)
{[]string{`
package main
import "context"
func work(ctx context.Context) {
child, _ := context.WithCancel(ctx)
_ = child
}
`}, 1, gosec.NewConfig()},
// Vulnerable: context.WithDeadline variant
{[]string{`
package main
import (
"context"
"time"
)
func work(ctx context.Context) {
child, _ := context.WithDeadline(ctx, time.Now().Add(time.Second))
_ = child
}
`}, 1, gosec.NewConfig()},
// Vulnerable: goroutine uses context.TODO instead of request context
{[]string{`
package main
import (
"context"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
_ = ctx
go func() {
bg := context.TODO()
_ = bg
}()
}
`}, 1, gosec.NewConfig()},
// Note: nested goroutines are not detected by current implementation
{[]string{`
package main
import (
"context"
"net/http"
)
func handler(r *http.Request) {
_ = r.Context()
go func() {
go func() {
ctx := context.Background()
_ = ctx
}()
}()
}
`}, 0, gosec.NewConfig()},
// Vulnerable: function parameter ignored in goroutine
{[]string{`
package main
import (
"context"
"time"
)
func worker(ctx context.Context) {
_ = ctx
go func() {
newCtx := context.Background()
_, _ = context.WithTimeout(newCtx, time.Second)
}()
}
`}, 2, gosec.NewConfig()},
// Note: channel range loops are not detected as blocking by current implementation
{[]string{`
package main
import "context"
func consume(ctx context.Context, ch <-chan int) {
_ = ctx
for val := range ch {
_ = val
}
}
`}, 0, gosec.NewConfig()},
// Note: select loops without ctx.Done are not detected by current implementation
{[]string{`
package main
import (
"context"
"time"
)
func selectLoop(ctx context.Context, ch <-chan int) {
_ = ctx
for {
select {
case <-ch:
case <-time.After(time.Second):
}
}
}
`}, 0, gosec.NewConfig()},
// Vulnerable: multiple context creations, one missing cancel
{[]string{`
package main
import "context"
func multiContext(ctx context.Context) {
ctx1, cancel1 := context.WithCancel(ctx)
defer cancel1()
_ = ctx1
ctx2, _ := context.WithCancel(ctx)
_ = ctx2
}
`}, 1, gosec.NewConfig()},
// Safe: cancel returned to caller — responsibility is transferred
{[]string{`
package main
import "context"
func createContext(ctx context.Context) (context.Context, context.CancelFunc) {
return context.WithCancel(ctx)
}
`}, 0, gosec.NewConfig()},
// Note: simple goroutines with Background() not detected when request param unused
{[]string{`
package main
import (
"context"
"net/http"
)
func simpleHandler(w http.ResponseWriter, r *http.Request) {
go func() {
ctx := context.Background()
_ = ctx
}()
}
`}, 0, gosec.NewConfig()},
// Vulnerable: loop with http.Get blocking call (no ctx.Done guard)
{[]string{`
package main
import (
"context"
"net/http"
"time"
)
func pollAPI(ctx context.Context) {
for {
resp, _ := http.Get("https://api.example.com")
if resp != nil {
resp.Body.Close()
}
time.Sleep(time.Second)
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with database query (no ctx.Done guard)
{[]string{`
package main
import (
"context"
"database/sql"
"time"
)
func pollDB(ctx context.Context, db *sql.DB) {
for {
db.Query("SELECT 1")
time.Sleep(time.Second)
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with os.ReadFile blocking call
{[]string{`
package main
import (
"context"
"os"
"time"
)
func watchFile(ctx context.Context) {
for {
os.ReadFile("config.txt")
time.Sleep(time.Second)
}
}
`}, 1, gosec.NewConfig()},
// Safe: loop with blocking call AND ctx.Done guard
{[]string{`
package main
import (
"context"
"net/http"
"time"
)
func safePoller(ctx context.Context) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
resp, _ := http.Get("https://api.example.com")
if resp != nil {
resp.Body.Close()
}
}
}
}
`}, 0, gosec.NewConfig()},
// Vulnerable: goroutine with TODO instead of passed context
{[]string{`
package main
import (
"context"
"time"
)
func startWorker(ctx context.Context) {
go func() {
newCtx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()
_ = newCtx
}()
}
`}, 1, gosec.NewConfig()},
// Vulnerable: WithTimeout in loop, cancel never called (reports once per location)
{[]string{`
package main
import (
"context"
"time"
)
func leakyLoop(ctx context.Context) {
for i := 0; i < 10; i++ {
child, _ := context.WithTimeout(ctx, time.Second)
_ = child
}
}
`}, 1, gosec.NewConfig()},
// Safe: WithTimeout in loop WITH defer cancel
{[]string{`
package main
import (
"context"
"time"
)
func properLoop(ctx context.Context) {
for i := 0; i < 10; i++ {
child, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
_ = child
}
}
`}, 0, gosec.NewConfig()},
// Vulnerable: cancel assigned to variable but never called
{[]string{`
package main
import "context"
func storeCancel(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
_ = cancel
}
`}, 1, gosec.NewConfig()},
// Safe: cancel assigned to interface and called
{[]string{`
package main
import "context"
func interfaceCancel(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
var fn func() = cancel
defer fn()
}
`}, 0, gosec.NewConfig()},
// Vulnerable: nested WithCancel calls, inner one not canceled
{[]string{`
package main
import "context"
func nestedContext(ctx context.Context) {
ctx1, cancel1 := context.WithCancel(ctx)
defer cancel1()
ctx2, _ := context.WithCancel(ctx1)
_ = ctx2
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with goroutine launch (hasBlocking=true)
{[]string{`
package main
import (
"context"
"time"
)
func spawnWorkers(ctx context.Context) {
for {
go func() {
time.Sleep(time.Millisecond)
}()
time.Sleep(time.Second)
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with defer that has blocking call
{[]string{`
package main
import (
"context"
"os"
"time"
)
func deferredWrites(ctx context.Context) {
for {
defer func() {
os.WriteFile("log.txt", []byte("data"), 0644)
}()
time.Sleep(time.Second)
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: infinite loop with blocking interface method call
{[]string{`
package main
import (
"context"
"io"
"time"
)
func readLoop(ctx context.Context, r io.Reader) {
buf := make([]byte, 1024)
for {
r.Read(buf)
time.Sleep(time.Millisecond)
}
}
`}, 1, gosec.NewConfig()},
// Safe: loop with http.Client.Do has external exit via error
{[]string{`
package main
import (
"context"
"net/http"
)
func fetchWithBreak(ctx context.Context) error {
client := &http.Client{}
for i := 0; i < 5; i++ {
req, _ := http.NewRequest("GET", "https://example.com", nil)
_, err := client.Do(req)
if err != nil {
return err
}
}
return nil
}
`}, 0, gosec.NewConfig()},
// Safe: cancel stored in struct field and called via method (tests isCancelCalledViaStructField)
{[]string{`
package main
import "context"
type Job struct {
cancelFn context.CancelFunc
}
func NewJob(ctx context.Context) *Job {
childCtx, cancel := context.WithCancel(ctx)
job := &Job{cancelFn: cancel}
_ = childCtx
return job
}
func (j *Job) Close() {
if j.cancelFn != nil {
j.cancelFn()
}
}
`}, 0, gosec.NewConfig()},
// Vulnerable: cancel stored in struct field but Close method never defined
{[]string{`
package main
import "context"
type Worker struct {
cancel context.CancelFunc
}
func NewWorker(ctx context.Context) *Worker {
childCtx, cancel := context.WithCancel(ctx)
w := &Worker{cancel: cancel}
_ = childCtx
return w
}
`}, 1, gosec.NewConfig()},
// Safe: cancel stored and called via pointer receiver method (tests reachesParam)
{[]string{`
package main
import "context"
type Service struct {
stopFn func()
}
func (s *Service) Start(ctx context.Context) {
childCtx, cancel := context.WithCancel(ctx)
s.stopFn = cancel
_ = childCtx
}
func (s *Service) Stop() {
if s.stopFn != nil {
s.stopFn()
}
}
`}, 0, gosec.NewConfig()},
// Safe: cancel via phi node - assigned conditionally then called (tests Phi case)
{[]string{`
package main
import "context"
func conditionalCancel(ctx context.Context, useTimeout bool) {
var cancel context.CancelFunc
if useTimeout {
_, cancel = context.WithCancel(ctx)
} else {
_, cancel = context.WithCancel(ctx)
}
defer cancel()
}
`}, 0, gosec.NewConfig()},
// Safe: cancel through Store/UnOp chain (tests Store case in isCancelCalled)
{[]string{`
package main
import "context"
func storeAndLoad(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
var holder func()
holder = cancel
defer holder()
}
`}, 0, gosec.NewConfig()},
// Safe: cancel via ChangeType conversion (tests ChangeType case)
{[]string{`
package main
import "context"
func changeType(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
fn := (func())(cancel)
defer fn()
}
`}, 0, gosec.NewConfig()},
// Note: cancel via MakeInterface + type assertion not tracked by current implementation
{[]string{`
package main
import "context"
func makeInterface(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
var iface interface{} = cancel
defer iface.(func())()
}
`}, 1, gosec.NewConfig()},
// Safe: cancel field accessed via nested pointer dereference (tests UnOp in reachesParamImpl)
{[]string{`
package main
import "context"
type Container struct {
cleanup func()
}
func (c *Container) Setup(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
c.cleanup = cancel
}
func (c *Container) Teardown() {
if c.cleanup != nil {
c.cleanup()
}
}
`}, 0, gosec.NewConfig()},
// Vulnerable: cancel stored but method that calls it is on wrong receiver type
{[]string{`
package main
import "context"
type TaskA struct {
cancelFn func()
}
type TaskB struct {
cancelFn func()
}
func NewTaskA(ctx context.Context) *TaskA {
_, cancel := context.WithCancel(ctx)
return &TaskA{cancelFn: cancel}
}
func (t *TaskB) Close() {
if t.cancelFn != nil {
t.cancelFn()
}
}
`}, 1, gosec.NewConfig()},
// Safe: cancel stored in field with index tracking (tests fieldIdx matching)
{[]string{`
package main
import "context"
type MultiField struct {
name string
cancelFn context.CancelFunc
data []byte
}
func (m *MultiField) Init(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
m.cancelFn = cancel
}
func (m *MultiField) Cleanup() {
if m.cancelFn != nil {
m.cancelFn()
}
}
`}, 0, gosec.NewConfig()},
// Safe: cancel passed as argument to helper function (tests isUsedInCall)
{[]string{`
package main
import "context"
func helper(fn func()) {
defer fn()
}
func useHelper(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
helper(cancel)
}
`}, 0, gosec.NewConfig()},
// Safe: cancel used in Call.Value position (tests isUsedInCall Value branch)
{[]string{`
package main
import "context"
func callAsValue(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
(func(f func()) { defer f() })(cancel)
}
`}, 0, gosec.NewConfig()},
// Safe: multiple Phi edges with cancel (tests reachesParamImpl Phi case)
{[]string{`
package main
import "context"
func multiPhiEdges(ctx context.Context, a, b, c bool) {
var cancel context.CancelFunc
if a {
_, cancel = context.WithCancel(ctx)
} else if b {
_, cancel = context.WithCancel(ctx)
} else if c {
_, cancel = context.WithCancel(ctx)
} else {
_, cancel = context.WithCancel(ctx)
}
defer cancel()
}
`}, 0, gosec.NewConfig()},
// Safe: nested field cancel called via outer method on Inner type (fixed by isFieldCalledInAnyFunc)
{[]string{`
package main
import "context"
type Outer struct {
inner Inner
}
type Inner struct {
cancel func()
}
func (o *Outer) Setup(ctx context.Context) {
_, cancel := context.WithCancel(ctx)
o.inner.cancel = cancel
}
func (o *Outer) Teardown() {
if o.inner.cancel != nil {
o.inner.cancel()
}
}
`}, 0, gosec.NewConfig()},
// Vulnerable: loop with interface method Do (tests analyzeBlockFeatures invoke)
{[]string{`
package main
import (
"context"
"net/http"
)
type HTTPClient interface {
Do(*http.Request) (*http.Response, error)
}
func pollWithInterface(ctx context.Context, client HTTPClient) {
for {
req, _ := http.NewRequest("GET", "https://example.com", nil)
client.Do(req)
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with Send interface method (tests analyzeBlockFeatures invoke Send)
{[]string{`
package main
import "context"
type Sender interface {
Send(interface{}) error
}
func sendLoop(ctx context.Context, s Sender) {
for {
s.Send("data")
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with Recv interface method (tests analyzeBlockFeatures invoke Recv)
{[]string{`
package main
import "context"
type Receiver interface {
Recv() (interface{}, error)
}
func recvLoop(ctx context.Context, r Receiver) {
for {
r.Recv()
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with QueryContext method (tests analyzeBlockFeatures invoke QueryContext)
{[]string{`
package main
import (
"context"
"database/sql"
)
type Querier interface {
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
}
func queryLoop(ctx context.Context, q Querier) {
for {
q.QueryContext(ctx, "SELECT 1")
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with ExecContext method (tests analyzeBlockFeatures invoke ExecContext)
{[]string{`
package main
import (
"context"
"database/sql"
)
type Executor interface {
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}
func execLoop(ctx context.Context, e Executor) {
for {
e.ExecContext(ctx, "UPDATE foo SET bar = 1")
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with RoundTrip interface method (tests analyzeBlockFeatures invoke RoundTrip)
{[]string{`
package main
import (
"context"
"net/http"
)
func roundTripLoop(ctx context.Context, rt http.RoundTripper) {
for {
req, _ := http.NewRequest("GET", "https://example.com", nil)
rt.RoundTrip(req)
}
}
`}, 1, gosec.NewConfig()},
// Vulnerable: loop with http.Head blocking call (tests looksLikeBlockingCall Head)
{[]string{`
package main
import (
"context"
"net/http"
)