@@ -21,8 +21,10 @@ import (
21
21
"database/sql"
22
22
"encoding/json"
23
23
"fmt"
24
+ "io"
24
25
"net/http"
25
26
"os"
27
+ "strings"
26
28
"testing"
27
29
"time"
28
30
@@ -436,6 +438,73 @@ func (s *UnitTestSuite) TestNoopWebhookHandler() {
436
438
assert .Equal (t , http .StatusOK , resp .StatusCode , "unexpected status code" )
437
439
}
438
440
441
+ func (s * UnitTestSuite ) TestHandleWebHookWithTooLargeRequest () {
442
+ t := s .T ()
443
+ t .Parallel ()
444
+
445
+ ctrl := gomock .NewController (t )
446
+ defer ctrl .Finish ()
447
+
448
+ mockStore := mockdb .NewMockStore (ctrl )
449
+ srv , evt := newDefaultServer (t , mockStore , nil )
450
+ defer evt .Close ()
451
+
452
+ pq := testqueue .NewPassthroughQueue (t )
453
+ queued := pq .GetQueue ()
454
+
455
+ evt .Register (events .TopicQueueEntityEvaluate , pq .Pass )
456
+
457
+ go func () {
458
+ err := evt .Run (context .Background ())
459
+ require .NoError (t , err , "failed to run eventer" )
460
+ }()
461
+
462
+ <- evt .Running ()
463
+
464
+ hook := withMaxSizeMiddleware (srv .HandleGitHubWebHook ())
465
+ port , err := rand .GetRandomPort ()
466
+ if err != nil {
467
+ t .Fatal (err )
468
+ }
469
+ addr := fmt .Sprintf ("localhost:%d" , port )
470
+ server := & http.Server {
471
+ Addr : fmt .Sprintf (":%d" , port ),
472
+ Handler : hook ,
473
+ ReadHeaderTimeout : 1 * time .Second ,
474
+ }
475
+ go server .ListenAndServe ()
476
+
477
+ event := github.PackageEvent {
478
+ Action : github .String ("published" ),
479
+ Repo : & github.Repository {
480
+ ID : github .Int64 (12345 ),
481
+ Name : github .String ("stacklok/minder" ),
482
+ },
483
+ Org : & github.Organization {
484
+ Login : github .String ("stacklok" ),
485
+ },
486
+ }
487
+ packageJson , err := json .Marshal (event )
488
+ require .NoError (t , err , "failed to marshal package event" )
489
+
490
+ maliciousBody := strings .NewReader (strings .Repeat ("1337" , 1000000000 ))
491
+ maliciousBodyReader := io .MultiReader (maliciousBody , maliciousBody , maliciousBody , maliciousBody , maliciousBody )
492
+ _ = packageJson
493
+
494
+ client := & http.Client {}
495
+ req , err := http .NewRequest ("POST" , fmt .Sprintf ("http://%s" , addr ), maliciousBodyReader )
496
+ require .NoError (t , err , "failed to create request" )
497
+
498
+ req .Header .Add ("X-GitHub-Event" , "meta" )
499
+ req .Header .Add ("X-GitHub-Delivery" , "12345" )
500
+ req .Header .Add ("Content-Type" , "application/json" )
501
+ resp , err := httpDoWithRetry (client , req )
502
+ require .NoError (t , err , "failed to make request" )
503
+ // We expect OK since we don't want to leak information about registered repositories
504
+ require .Equal (t , http .StatusBadRequest , resp .StatusCode , "unexpected status code" )
505
+ assert .Len (t , queued , 0 )
506
+ }
507
+
439
508
func TestAll (t * testing.T ) {
440
509
t .Parallel ()
441
510
0 commit comments