41
41
import java .util .Collections ;
42
42
import java .util .List ;
43
43
import java .util .Map ;
44
+ import java .util .Random ;
44
45
import org .junit .jupiter .api .AfterEach ;
45
46
import org .junit .jupiter .api .Test ;
46
47
import org .junit .jupiter .params .ParameterizedTest ;
@@ -185,10 +186,11 @@ void shouldRaiseAlertIfTestedUrlRespondsOkWithRelevantContent()
185
186
false );
186
187
187
188
this .nano .addHandler (new OkResponse (servePath ));
189
+ nano .setHandler404 (new OkWithRndToken ("" ));
188
190
this .nano .addHandler (
189
191
new StaticContentServerHandler (
190
192
'/' + testPath ,
191
- "<html><head></head><H >Awesome Title</H1> Some Text... <html>" ));
193
+ "<html><head></head><H1 >Awesome Title</H1> Some Text... <html>" ));
192
194
193
195
HttpMessage msg = this .getHttpMessage (servePath );
194
196
@@ -426,19 +428,39 @@ void shouldRaiseAlertWithHighConfidenceIfContentStringsAllMatch()
426
428
}
427
429
428
430
@ Test
429
- void shouldRaiseAlertWithLowConfidenceIfTestedUrlRespondsOkToCustomPayload ()
430
- throws HttpMalformedHeaderException {
431
+ void shouldNotRaiseAlertIfMajorityResponsesTooSimilar () throws HttpMalformedHeaderException {
432
+ // Given
433
+ String servePath = "/shouldNotAlert" ;
434
+
435
+ String testPath = "foo/test.php" ;
436
+ List <String > customPaths = Arrays .asList (testPath );
437
+
438
+ nano .setHandler404 (new OkWithRndToken ("" ));
439
+
440
+ HttpMessage msg = this .getHttpMessage (servePath );
441
+
442
+ HiddenFilesScanRule .setPayloadProvider (() -> customPaths );
443
+ rule .init (msg , this .parent );
444
+
445
+ // When
446
+ rule .scan ();
447
+
448
+ // Then
449
+ assertThat (alertsRaised , hasSize (0 ));
450
+ }
451
+
452
+ @ Test
453
+ void shouldtRaiseAlertForMatchWith404As200 () throws HttpMalformedHeaderException {
431
454
// Given
432
455
String servePath = "/shouldAlert" ;
433
456
434
457
String testPath = "foo/test.php" ;
435
458
List <String > customPaths = Arrays .asList (testPath );
436
459
437
460
this .nano .addHandler (new OkResponse (servePath ));
438
- this .nano .addHandler (
439
- new StaticContentServerHandler (
440
- '/' + testPath ,
441
- "<html><head></head><H>Awesome Title</H1> Some Text... <html>" ));
461
+ this .nano .addHandler (new OkResponse ("/" + testPath ));
462
+
463
+ nano .setHandler404 (new OkWithRndToken ("" ));
442
464
443
465
HttpMessage msg = this .getHttpMessage (servePath );
444
466
@@ -447,15 +469,33 @@ void shouldRaiseAlertWithLowConfidenceIfTestedUrlRespondsOkToCustomPayload()
447
469
448
470
// When
449
471
rule .scan ();
472
+
450
473
// Then
451
474
assertThat (alertsRaised , hasSize (1 ));
452
475
Alert alert = alertsRaised .get (0 );
453
- assertEquals ( 1 , httpMessagesSent . size ( ));
476
+ assertThat ( httpMessagesSent , hasSize ( greaterThanOrEqualTo ( 1 ) ));
454
477
assertEquals (Alert .RISK_MEDIUM , alertsRaised .get (0 ).getRisk ());
455
478
assertEquals (Alert .CONFIDENCE_LOW , alertsRaised .get (0 ).getConfidence ());
456
479
assertEquals (rule .getReference (), alert .getReference ());
457
480
}
458
481
482
+ @ ParameterizedTest
483
+ @ MethodSource ("org.zaproxy.zap.extension.ascanrules.HiddenFilesScanRule#getHiddenFiles()" )
484
+ void shouldNotRaiseAlertIfMajorityResponsesTooSimilarForBuiltInCustomPayloads (String fileName )
485
+ throws HttpMalformedHeaderException {
486
+ // Given
487
+ String servePath = "/shouldNotAlert" ;
488
+
489
+ nano .setHandler404 (new OkWithRndToken ("" ));
490
+
491
+ rule .init (getHttpMessage (servePath ), parent );
492
+
493
+ // When
494
+ rule .scan ();
495
+ // Then
496
+ assertThat (alertsRaised , hasSize (0 ));
497
+ }
498
+
459
499
@ Test
460
500
void shouldNotRaiseAlertIfResponseStatusIsNotOkOrAuthRelated ()
461
501
throws HttpMalformedHeaderException {
@@ -756,32 +796,6 @@ void shouldReturnExpectedExampleAlert() {
756
796
assertThat (alert .getConfidence (), is (equalTo (Alert .CONFIDENCE_LOW )));
757
797
}
758
798
759
- @ ParameterizedTest
760
- @ MethodSource ("org.zaproxy.zap.extension.ascanrules.HiddenFilesScanRule#getHiddenFiles()" )
761
- // XXX A very likely FP.
762
- void shouldRaiseAlertIfTestedUrlRespondsOkForCustomPayloads (String fileName )
763
- throws HttpMalformedHeaderException {
764
- // Given
765
- String servePath = "/shouldAlert" ;
766
- nano .addHandler (new OkResponse (servePath ));
767
- nano .addHandler (
768
- new StaticContentServerHandler (
769
- '/' + fileName ,
770
- "<html><head></head><H>Awesome Title</H1> Some Text... <html>" ));
771
- rule .init (getHttpMessage (servePath ), parent );
772
-
773
- // When
774
- rule .scan ();
775
- // Then
776
- assertThat (alertsRaised , hasSize (1 ));
777
- Alert alert = alertsRaised .get (0 );
778
- assertThat (httpMessagesSent , hasSize (greaterThanOrEqualTo (1 )));
779
- assertThat (alert .getRisk (), is (equalTo (Alert .RISK_MEDIUM )));
780
- assertThat (alert .getConfidence (), is (equalTo (Alert .CONFIDENCE_LOW )));
781
- assertThat (alert .getEvidence (), is (equalTo ("HTTP/1.1 200 OK" )));
782
- assertThat (alert .getOtherInfo (), is (equalTo ("" )));
783
- }
784
-
785
799
@ Test
786
800
@ Override
787
801
public void shouldHaveValidReferences () {
@@ -868,4 +882,23 @@ public OkBinResponse(String path, String content) {
868
882
super (path , content );
869
883
}
870
884
}
885
+
886
+ private static class OkWithRndToken extends NanoServerHandler {
887
+
888
+ private Random rnd = new Random ();
889
+
890
+ public OkWithRndToken (String name ) {
891
+ super (name );
892
+ }
893
+
894
+ @ Override
895
+ protected Response serve (IHTTPSession session ) {
896
+ return NanoHTTPD .newFixedLengthResponse (
897
+ Response .Status .OK ,
898
+ "text/html" ,
899
+ "<html><head></head><body><H1>Awesome Title</H1> Some Text... <br>"
900
+ + rnd .nextLong ()
901
+ + "</body></html>" );
902
+ }
903
+ }
871
904
}
0 commit comments