-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
DefaultServletTest.java
2293 lines (1905 loc) · 106 KB
/
DefaultServletTest.java
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
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.servlet;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.DateGenerator;
import org.eclipse.jetty.http.HttpContent;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.http.UriCompliance;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.LocalConnector;
import org.eclipse.jetty.server.ResourceContentFactory;
import org.eclipse.jetty.server.ResourceService;
import org.eclipse.jetty.server.SameFileAliasChecker;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AllowSymLinkAliasChecker;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jetty.http.tools.matchers.HttpFieldsMatchers.containsHeader;
import static org.eclipse.jetty.http.tools.matchers.HttpFieldsMatchers.containsHeaderValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@ExtendWith(WorkDirExtension.class)
public class DefaultServletTest
{
public WorkDir workDir;
public Path docRoot;
// The name of the odd-jar used for testing "jar:file://" based resource access.
private static final String ODD_JAR = "jar-resource-odd.jar";
private Server server;
private LocalConnector connector;
private ServletContextHandler context;
@BeforeEach
public void init() throws Exception
{
docRoot = workDir.getEmptyPathDir().resolve("docroot");
FS.ensureDirExists(docRoot);
server = new Server();
connector = new LocalConnector(server);
connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false);
connector.getBean(HttpConnectionFactory.class).getHttpConfiguration().setUriCompliance(UriCompliance.STRICT); // allow ambiguous path segments
File extraJarResources = MavenTestingUtils.getTestResourceFile(ODD_JAR);
URL[] urls = new URL[]{extraJarResources.toURI().toURL()};
ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader();
URLClassLoader extraClassLoader = new URLClassLoader(urls, parentClassLoader);
context = new ServletContextHandler();
context.setBaseResource(new PathResource(docRoot));
context.setContextPath("/context");
context.setWelcomeFiles(new String[]{"index.html", "index.jsp", "index.htm"});
context.setClassLoader(extraClassLoader);
server.setHandler(context);
server.addConnector(connector);
server.start();
}
@AfterEach
public void destroy() throws Exception
{
server.stop();
server.join();
}
@Test
public void testListingWithSession() throws Exception
{
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("gzip", "false");
/* create some content in the docroot */
FS.ensureDirExists(docRoot.resolve("one"));
FS.ensureDirExists(docRoot.resolve("two"));
FS.ensureDirExists(docRoot.resolve("three"));
String rawResponse = connector.getResponse("GET /context/;JSESSIONID=1234567890 HTTP/1.0\n\n");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
String body = response.getContent();
assertThat(body, containsString("/one/;JSESSIONID=1234567890"));
assertThat(body, containsString("/two/;JSESSIONID=1234567890"));
assertThat(body, containsString("/three/;JSESSIONID=1234567890"));
assertThat(body, not(containsString("<script>")));
}
@Test
public void testListingXSS() throws Exception
{
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("gzip", "false");
/* create some content in the docroot */
Path one = docRoot.resolve("one");
FS.ensureDirExists(one);
FS.ensureDirExists(docRoot.resolve("two"));
FS.ensureDirExists(docRoot.resolve("three"));
Path alert = one.resolve("onmouseclick='alert(oops)'");
FS.touch(alert);
/*
* Intentionally bad request URI. Sending a non-encoded URI with typically
* encoded characters '<', '>', and '"'.
*/
String req1 = "GET /context/;<script>window.alert(\"hi\");</script> HTTP/1.0\r\n" +
"\r\n";
String rawResponse = connector.getResponse(req1);
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
String body = response.getContent();
assertThat(body, not(containsString("<script>")));
req1 = "GET /context/one/;\"onmouseover='alert(document.location)' HTTP/1.0\r\n" +
"\r\n";
rawResponse = connector.getResponse(req1);
response = HttpTester.parseResponse(rawResponse);
body = response.getContent();
assertThat(body, not(containsString(";\"onmouseover")));
}
@Test
public void testListingWithQuestionMarks() throws Exception
{
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("gzip", "false");
/* create some content in the docroot */
FS.ensureDirExists(docRoot.resolve("one"));
FS.ensureDirExists(docRoot.resolve("two"));
FS.ensureDirExists(docRoot.resolve("three"));
// Creating dir 'f??r' (Might not work in Windows)
assumeMkDirSupported(docRoot, "f??r");
String rawResponse = connector.getResponse("GET /context/ HTTP/1.0\r\n\r\n");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
String body = response.getContent();
assertThat(body, containsString("f??r"));
}
/**
* A regression on windows allowed the directory listing show
* the fully qualified paths within the directory listing.
* This test ensures that this behavior will not arise again.
*/
@Test
public void testListingFilenamesOnly() throws Exception
{
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("gzip", "false");
/* create some content in the docroot */
FS.ensureDirExists(docRoot);
Path one = docRoot.resolve("one");
FS.ensureDirExists(one);
Path deep = one.resolve("deep");
FS.ensureDirExists(deep);
FS.touch(deep.resolve("foo"));
FS.ensureDirExists(docRoot.resolve("two"));
FS.ensureDirExists(docRoot.resolve("three"));
String resBasePath = docRoot.toAbsolutePath().toString();
defholder.setInitParameter("resourceBase", resBasePath);
StringBuffer req1 = new StringBuffer();
req1.append("GET /context/one/deep/ HTTP/1.0\n");
req1.append("\n");
String rawResponse = connector.getResponse(req1.toString());
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
String body = response.getContent();
assertThat(body, containsString("/foo"));
assertThat(body, not(containsString(resBasePath)));
}
/**
* A regression on windows allowed the directory listing show
* the fully qualified paths within the directory listing.
* This test ensures that this behavior will not arise again.
*/
@Test
public void testListingFilenamesOnlyUrlResource() throws Exception
{
URL extraResource = context.getClassLoader().getResource("rez/one");
assertNotNull(extraResource, "Must have extra jar resource in classloader");
String extraResourceBaseString = extraResource.toURI().toASCIIString();
extraResourceBaseString = extraResourceBaseString.substring(0, extraResourceBaseString.length() - "/one".length());
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/extra/*");
defholder.setInitParameter("resourceBase", extraResourceBaseString);
defholder.setInitParameter("pathInfoOnly", "true");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("gzip", "false");
StringBuffer req1;
String rawResponse;
HttpTester.Response response;
String body;
// Test that GET works first.
req1 = new StringBuffer();
req1.append("GET /context/extra/one HTTP/1.0\n");
req1.append("\n");
rawResponse = connector.getResponse(req1.toString());
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
body = response.getContent();
assertThat(body, containsString("is this the one?"));
// Typical directory listing of location in jar:file:// URL
req1 = new StringBuffer();
req1.append("GET /context/extra/deep/ HTTP/1.0\r\n");
req1.append("\r\n");
rawResponse = connector.getResponse(req1.toString());
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
body = response.getContent();
assertThat(body, containsString("/xxx"));
assertThat(body, containsString("/yyy"));
assertThat(body, containsString("/zzz"));
assertThat(body, not(containsString(extraResourceBaseString)));
assertThat(body, not(containsString(ODD_JAR)));
// Get deep resource
req1 = new StringBuffer();
req1.append("GET /context/extra/deep/yyy HTTP/1.0\r\n");
req1.append("\r\n");
rawResponse = connector.getResponse(req1.toString());
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
body = response.getContent();
assertThat(body, containsString("a file named yyy"));
// Convoluted directory listing of location in jar:file:// URL
// This exists to test proper encoding output
req1 = new StringBuffer();
req1.append("GET /context/extra/oddities/ HTTP/1.0\r\n");
req1.append("\r\n");
rawResponse = connector.getResponse(req1.toString());
response = HttpTester.parseResponse(rawResponse);
assertThat(response.getStatus(), is(HttpStatus.OK_200));
body = response.getContent();
assertThat(body, containsString(">#hashcode <")); // text on page
assertThat(body, containsString("/oddities/%23hashcode")); // generated link
assertThat(body, containsString(">other%2fkind%2Fof%2fslash <")); // text on page
assertThat(body, containsString("/oddities/other%252fkind%252Fof%252fslash")); // generated link
assertThat(body, containsString(">a file with a space <")); // text on page
assertThat(body, containsString("/oddities/a%20file%20with%20a%20space")); // generated link
assertThat(body, not(containsString(extraResourceBaseString)));
assertThat(body, not(containsString(ODD_JAR)));
}
@Test
public void testListingProperUrlEncoding() throws Exception
{
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("gzip", "false");
/* create some content in the docroot */
Path wackyDir = docRoot.resolve("dir;"); // this should not be double-encoded.
FS.ensureDirExists(wackyDir);
FS.ensureDirExists(wackyDir.resolve("four"));
FS.ensureDirExists(wackyDir.resolve("five"));
FS.ensureDirExists(wackyDir.resolve("six"));
/* At this point we have the following
* testListingProperUrlEncoding/
* `-- docroot
* `-- dir;
* |-- five
* |-- four
* `-- six
*/
// First send request in improper, unencoded way.
String rawResponse = connector.getResponse("GET /context/dir;/ HTTP/1.0\r\n\r\n");
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.NOT_FOUND_404));
// Now send request in proper, encoded format.
rawResponse = connector.getResponse("GET /context/dir%3B/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
String body = response.getContent();
// Should not see double-encoded ";"
// First encoding: ";" -> "%3b"
// Second encoding: "%3B" -> "%253B" (BAD!)
assertThat(body, not(containsString("%253B")));
assertThat(body, containsString("/dir%3B/"));
assertThat(body, containsString("/dir%3B/four/"));
assertThat(body, containsString("/dir%3B/five/"));
assertThat(body, containsString("/dir%3B/six/"));
}
@SuppressWarnings("Duplicates")
public static Stream<Arguments> contextBreakoutScenarios()
{
Scenarios scenarios = new Scenarios();
scenarios.addScenario(
"GET normal",
"GET /context/ HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("<h1>Hello Index</h1>"))
);
scenarios.addScenario(
"GET /context/index.html",
"GET /context/index.html HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("Hello Index"))
);
List<String> notEncodedPrefixes = new ArrayList<>();
if (!OS.WINDOWS.isCurrentOs())
{
notEncodedPrefixes.add("/context/dir?");
}
notEncodedPrefixes.add("/context/dir;");
for (String prefix : notEncodedPrefixes)
{
scenarios.addScenario(
"GET " + prefix,
"GET " + prefix + " HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404
);
scenarios.addScenario(
"GET " + prefix + "/",
"GET " + prefix + "/ HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404
);
scenarios.addScenario(
"GET " + prefix + "/../../sekret/pass",
"GET " + prefix + "/../../sekret/pass HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404,
(response) -> assertThat(response.getContent(), not(containsString("Sssh")))
);
scenarios.addScenario(
"GET " + prefix + "/%2E%2E/%2E%2E/sekret/pass",
"GET " + prefix + "/ HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404,
(response) -> assertThat(response.getContent(), not(containsString("Sssh")))
);
// A Raw Question mark in the prefix can be interpreted as a query section
if (prefix.contains("?"))
{
scenarios.addScenario(
"GET " + prefix + "/../index.html",
"GET " + prefix + "/../index.html HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404
);
scenarios.addScenario(
"GET " + prefix + "/%2E%2E/index.html",
"GET " + prefix + "/%2E%2E/index.html HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404
);
}
else
{
scenarios.addScenario(
"GET " + prefix + "/../index.html",
"GET " + prefix + "/../index.html HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("Hello Index"))
);
scenarios.addScenario(
"GET " + prefix + "/%2E%2E/index.html",
"GET " + prefix + "/%2E%2E/index.html HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("Hello Index"))
);
}
scenarios.addScenario(
"GET " + prefix + "/../../",
"GET " + prefix + "/../../ HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404,
(response) ->
{
String body = response.getContent();
assertThat(body, not(containsString("Directory: ")));
}
);
}
List<String> encodedPrefixes = new ArrayList<>();
if (!OS.WINDOWS.isCurrentOs())
{
encodedPrefixes.add("/context/dir%3F");
}
encodedPrefixes.add("/context/dir%3B");
for (String prefix : encodedPrefixes)
{
scenarios.addScenario(
"GET " + prefix,
"GET " + prefix + " HTTP/1.0\r\n\r\n",
HttpStatus.MOVED_TEMPORARILY_302,
(response) -> assertThat("Location header", response.get(HttpHeader.LOCATION), endsWith(prefix + "/"))
);
scenarios.addScenario(
"GET " + prefix + "/",
"GET " + prefix + "/ HTTP/1.0\r\n\r\n",
HttpStatus.OK_200
);
scenarios.addScenario(
"GET " + prefix + "/.%2E/.%2E/sekret/pass",
"GET " + prefix + "/ HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), not(containsString("Sssh")))
);
scenarios.addScenario(
"GET " + prefix + "/../index.html",
"GET " + prefix + "/../index.html HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("Hello Index"))
);
scenarios.addScenario(
"GET " + prefix + "/../../",
"GET " + prefix + "/../../ HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404,
(response) ->
{
String body = response.getContent();
assertThat(body, containsString("/../../"));
assertThat(body, not(containsString("Directory: ")));
}
);
scenarios.addScenario(
"GET " + prefix + "/../../sekret/pass",
"GET " + prefix + "/../../sekret/pass HTTP/1.0\r\n\r\n",
HttpStatus.NOT_FOUND_404,
(response) -> assertThat(response.getContent(), not(containsString("Sssh")))
);
scenarios.addScenario(
"GET " + prefix + "/../index.html",
"GET " + prefix + "/../index.html HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("Hello Index"))
);
}
return scenarios.stream();
}
@ParameterizedTest
@MethodSource("contextBreakoutScenarios")
public void testListingContextBreakout(Scenario scenario) throws Exception
{
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "true");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("gzip", "false");
defholder.setInitParameter("aliases", "true");
/* create some content in the docroot */
Path index = docRoot.resolve("index.html");
createFile(index, "<h1>Hello Index</h1>");
if (!OS.WINDOWS.isCurrentOs())
{
// FileSystem should support question dirs
assumeMkDirSupported(docRoot, "dir?");
}
// FileSystem should support semicolon dirs
assumeMkDirSupported(docRoot, "dir;");
/* create some content outside of the docroot */
Path sekret = workDir.getPath().resolve("sekret");
FS.ensureDirExists(sekret);
Path pass = sekret.resolve("pass");
createFile(pass, "Sssh, you shouldn't be seeing this");
/* At this point we have the following
* testListingContextBreakout/
* |-- docroot
* | |-- index.html
* | |-- dir? (Might be missing on Windows)
* | |-- dir;
* `-- sekret
* `-- pass
*/
String rawResponse = connector.getResponse(scenario.rawRequest);
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(scenario.expectedStatus));
if (scenario.extraAsserts != null)
scenario.extraAsserts.accept(response);
}
private static void addBasicWelcomeScenarios(Scenarios scenarios)
{
scenarios.addScenario(
"GET /context/one/ (index.htm match)",
"GET /context/one/ HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("<h1>Hello Inde</h1>"))
);
scenarios.addScenario(
"GET /context/two/ (index.html match)",
"GET /context/two/ HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("<h1>Hello Index</h1>"))
);
scenarios.addScenario(
"GET /context/three/ (index.html wins over index.htm)",
"GET /context/three/ HTTP/1.0\r\n\r\n",
HttpStatus.OK_200,
(response) -> assertThat(response.getContent(), containsString("<h1>Three Index</h1>"))
);
}
public static Stream<Arguments> welcomeScenarios()
{
Scenarios scenarios = new Scenarios();
scenarios.addScenario(
"GET /context/ - (no match)",
"GET /context/ HTTP/1.0\r\n\r\n",
HttpStatus.FORBIDDEN_403
);
addBasicWelcomeScenarios(scenarios);
return scenarios.stream();
}
@ParameterizedTest
@MethodSource("welcomeScenarios")
public void testWelcome(Scenario scenario) throws Exception
{
Path one = docRoot.resolve("one");
Path two = docRoot.resolve("two");
Path three = docRoot.resolve("three");
FS.ensureDirExists(one);
FS.ensureDirExists(two);
FS.ensureDirExists(three);
createFile(one.resolve("index.htm"), "<h1>Hello Inde</h1>");
createFile(two.resolve("index.html"), "<h1>Hello Index</h1>");
createFile(three.resolve("index.html"), "<h1>Three Index</h1>");
createFile(three.resolve("index.htm"), "<h1>Three Inde</h1>");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("welcomeServlets", "false");
defholder.setInitParameter("gzip", "false");
defholder.setInitParameter("maxCacheSize", "1024000");
defholder.setInitParameter("maxCachedFileSize", "512000");
defholder.setInitParameter("maxCachedFiles", "100");
ServletHolder jspholder = context.addServlet(NoJspServlet.class, "*.jsp");
context.addServlet(jspholder, "/index.jsp");
String rawResponse = connector.getResponse(scenario.rawRequest);
HttpTester.Response response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(scenario.expectedStatus));
if (scenario.extraAsserts != null)
scenario.extraAsserts.accept(response);
}
@Test
public void testWelcomeMultipleBasesBase() throws Exception
{
Path dir = docRoot.resolve("dir");
FS.ensureDirExists(dir);
Path inde = dir.resolve("index.htm");
Path index = dir.resolve("index.html");
Path altRoot = workDir.getPath().resolve("altroot");
Path altDir = altRoot.resolve("dir");
FS.ensureDirExists(altDir);
Path altInde = altDir.resolve("index.htm");
Path altIndex = altDir.resolve("index.html");
ServletHolder altholder = context.addServlet(DefaultServlet.class, "/alt/*");
altholder.setInitParameter("resourceBase", altRoot.toUri().toASCIIString());
altholder.setInitParameter("pathInfoOnly", "true");
altholder.setInitParameter("dirAllowed", "false");
altholder.setInitParameter("redirectWelcome", "false");
altholder.setInitParameter("welcomeServlets", "false");
altholder.setInitParameter("gzip", "false");
ServletHolder otherholder = context.addServlet(DefaultServlet.class, "/other/*");
otherholder.setInitParameter("resourceBase", altRoot.toUri().toASCIIString());
otherholder.setInitParameter("pathInfoOnly", "true");
otherholder.setInitParameter("dirAllowed", "true");
otherholder.setInitParameter("redirectWelcome", "false");
otherholder.setInitParameter("welcomeServlets", "false");
otherholder.setInitParameter("gzip", "false");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("welcomeServlets", "false");
defholder.setInitParameter("gzip", "false");
@SuppressWarnings("unused")
ServletHolder jspholder = context.addServlet(NoJspServlet.class, "*.jsp");
String rawResponse;
HttpTester.Response response;
// Test other redirect
rawResponse = connector.getResponse("GET /context/other HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/other/"));
// Test alt default
rawResponse = connector.getResponse("GET /context/alt/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.FORBIDDEN_403));
createFile(altIndex, "<h1>Alt Index</h1>");
rawResponse = connector.getResponse("GET /context/alt/dir/index.html HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Alt Index</h1>"));
rawResponse = connector.getResponse("GET /context/alt/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Alt Index</h1>"));
createFile(altInde, "<h1>Alt Inde</h1>");
rawResponse = connector.getResponse("GET /context/alt/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Alt Index</h1>"));
if (deleteFile(altIndex))
{
rawResponse = connector.getResponse("GET /context/alt/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Alt Inde</h1>"));
if (deleteFile(altInde))
{
rawResponse = connector.getResponse("GET /context/alt/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.FORBIDDEN_403));
}
}
// Test normal default
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.FORBIDDEN_403));
createFile(index, "<h1>Hello Index</h1>");
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Hello Index</h1>"));
createFile(inde, "<h1>Hello Inde</h1>");
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Hello Index</h1>"));
if (deleteFile(index))
{
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Hello Inde</h1>"));
if (deleteFile(inde))
{
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.FORBIDDEN_403));
}
}
}
@Test
public void testIncludedWelcomeDifferentBase() throws Exception
{
Path altRoot = workDir.getPath().resolve("altroot");
FS.ensureDirExists(altRoot);
Path altIndex = altRoot.resolve("index.html");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/alt/*");
defholder.setInitParameter("resourceBase", altRoot.toUri().toASCIIString());
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("welcomeServlets", "true");
defholder.setInitParameter("pathInfoOnly", "true");
ServletHolder gwholder = new ServletHolder("gateway", new HttpServlet()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
req.getRequestDispatcher("/alt/").include(req, resp);
}
});
context.addServlet(gwholder, "/gateway");
String rawResponse;
HttpTester.Response response;
// Test included alt default
rawResponse = connector.getResponse("GET /context/gateway HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
// 9.3 "The Include Method" - when include() is used, FileNotFoundException (and HTTP 500)
// should be used
assertThat(response.toString(), response.getStatus(), is(HttpStatus.INTERNAL_SERVER_ERROR_500));
createFile(altIndex, "<h1>Alt Index</h1>");
rawResponse = connector.getResponse("GET /context/gateway HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContent(), containsString("<h1>Alt Index</h1>"));
}
@Test
public void testWelcomeRedirect() throws Exception
{
Path dir = docRoot.resolve("dir");
FS.ensureDirExists(dir);
Path inde = dir.resolve("index.htm");
Path index = dir.resolve("index.html");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "true");
defholder.setInitParameter("welcomeServlets", "false");
defholder.setInitParameter("gzip", "false");
defholder.setInitParameter("maxCacheSize", "1024000");
defholder.setInitParameter("maxCachedFileSize", "512000");
defholder.setInitParameter("maxCachedFiles", "100");
@SuppressWarnings("unused")
ServletHolder jspholder = context.addServlet(NoJspServlet.class, "*.jsp");
String rawResponse;
HttpTester.Response response;
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.FORBIDDEN_403));
createFile(index, "<h1>Hello Index</h1>");
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/dir/index.html"));
createFile(inde, "<h1>Hello Inde</h1>");
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/dir/index.html"));
if (deleteFile(index))
{
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/dir/index.htm"));
if (deleteFile(inde))
{
rawResponse = connector.getResponse("GET /context/dir/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.FORBIDDEN_403));
}
}
}
/**
* Ensure that oddball directory names are served with proper escaping
*/
@Test
public void testWelcomeRedirectDirWithQuestion() throws Exception
{
FS.ensureDirExists(docRoot);
context.setBaseResource(new PathResource(docRoot));
Path dir = assumeMkDirSupported(docRoot, "dir?");
Path index = dir.resolve("index.html");
createFile(index, "<h1>Hello Index</h1>");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "true");
defholder.setInitParameter("welcomeServlets", "false");
defholder.setInitParameter("gzip", "false");
String rawResponse;
HttpTester.Response response;
rawResponse = connector.getResponse("GET /context/dir%3F HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/dir%3F/"));
rawResponse = connector.getResponse("GET /context/dir%3F/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/dir%3F/index.html"));
}
/**
* Ensure that oddball directory names are served with proper escaping
*/
@Test
public void testWelcomeRedirectDirWithSemicolon() throws Exception
{
FS.ensureDirExists(docRoot);
context.setBaseResource(new PathResource(docRoot));
Path dir = assumeMkDirSupported(docRoot, "dir;");
Path index = dir.resolve("index.html");
createFile(index, "<h1>Hello Index</h1>");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "true");
defholder.setInitParameter("welcomeServlets", "false");
defholder.setInitParameter("gzip", "false");
String rawResponse;
HttpTester.Response response;
rawResponse = connector.getResponse("GET /context/dir%3B HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/dir%3B/"));
rawResponse = connector.getResponse("GET /context/dir%3B/ HTTP/1.0\r\n\r\n");
response = HttpTester.parseResponse(rawResponse);
assertThat(response.toString(), response.getStatus(), is(HttpStatus.MOVED_TEMPORARILY_302));
assertThat(response, containsHeaderValue("Location", "http://0.0.0.0/context/dir%3B/index.html"));
}
@Test
public void testWelcomeServlet() throws Exception
{
Path inde = docRoot.resolve("index.htm");
Path index = docRoot.resolve("index.html");
ServletHolder defholder = context.addServlet(DefaultServlet.class, "/");
defholder.setInitParameter("dirAllowed", "false");
defholder.setInitParameter("redirectWelcome", "false");
defholder.setInitParameter("welcomeServlets", "true");
defholder.setInitParameter("gzip", "false");
@SuppressWarnings("unused")
ServletHolder jspholder = context.addServlet(NoJspServlet.class, "*.jsp");
String rawResponse;
HttpTester.Response response;
rawResponse = connector.getResponse("GET /context/ HTTP/1.0\r\n\r\n");