Debian Codegraph @

jetty-ee9/jetty-ee9-demos/jetty-ee9-demo-jetty-webapp/src/test/java/org/eclipse/jetty/ee9/DispatchServletTest.java

raw
1 //
2 // ========================================================================
3 // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
4 //
5 // This program and the accompanying materials are made available under the
6 // terms of the Eclipse Public License v. 2.0 which is available at
7 // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
8 // which is available at https://www.apache.org/licenses/LICENSE-2.0.
9 //
10 // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
11 // ========================================================================
12 //
13
14 package org.eclipse.jetty.ee9;
15
16 import org.eclipse.jetty.ee9.servlet.DefaultServlet;
17 import org.eclipse.jetty.ee9.servlet.ServletContextHandler;
18 import org.eclipse.jetty.ee9.servlet.ServletHolder;
19 import org.eclipse.jetty.server.LocalConnector;
20 import org.eclipse.jetty.server.Server;
21 import org.eclipse.jetty.util.component.LifeCycle;
22 import org.example.DispatchServlet;
23 import org.hamcrest.Matchers;
24 import org.junit.jupiter.api.AfterEach;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27
28 import static org.hamcrest.MatcherAssert.assertThat;
29 import static org.junit.jupiter.api.Assertions.assertFalse;
30 import static org.junit.jupiter.api.Assertions.assertTrue;
31
32 /**
33 * Simple tests against DispatchServlet.
34 */
35 public class DispatchServletTest
36 {
37 private Server server;
38 private LocalConnector connector;
39 private ServletContextHandler context;
40
41 @BeforeEach
42 public void setUp() throws Exception
43 {
44 server = new Server();
45 connector = new LocalConnector(server);
46 server.addConnector(connector);
47 context = new ServletContextHandler(server, "/tests");
48 server.start();
49 }
50
51 @AfterEach
52 public void tearDown()
53 {
54 LifeCycle.stop(server);
55 }
56
57 /**
58 * As filed in JETTY-978.
59 *
60 * Security problems in demo dispatch servlet.
61 *
62 * <blockquote>
63 * <p>
64 * The dispatcher servlet (org.example.DispatchServlet) is prone to a Denial of
65 * Service vulnerability.
66 * </p>
67 * <p>
68 * This example servlet is meant to be used as a resources dispatcher,
69 * however a malicious aggressor may abuse this functionality in order to
70 * cause a recursive inclusion. In details, it is possible to abuse the
71 * method org.example.DispatchServlet.doGet(DispatchServlet.java:203) forcing
72 * the application to recursively include the "Dispatch" servlet.
73 * </p>
74 * <p>
75 * Dispatch org.example.DispatchServlet 1 Dispatch /dispatch/* As a result, it
76 * is possible to trigger a "java.lang.StackOverflowError" and consequently
77 * an internal server error (500).
78 * </p>
79 * <p>
80 * Multiple requests may easily affect the availability of the servlet
81 * container. Since this attack can cause the server to consume resources in
82 * a non-linear relationship to the size of inputs, it should be considered
83 * as a server flaw.
84 * </p>
85 * <p>
86 * The vulnerability seems confined to the example servlet and it does not
87 * afflict the Jetty's core."
88 * </p>
89 * </blockquote>
90 */
91 @Test
92 public void testSelfRefForwardDenialOfService() throws Exception
93 {
94 ServletHolder dispatch = context.addServlet(DispatchServlet.class, "/dispatch/*");
95 context.addServlet(DefaultServlet.class, "/");
96
97 String request = "GET /tests/dispatch/includeN/" + dispatch.getName() + " HTTP/1.1\n" +
98 "Host: tester\n" +
99 "Connection: close\n" +
100 "\n";
101 String response = connector.getResponse(request);
102
103 String msg = "Response code on SelfRefDoS";
104
105 assertFalse(response.startsWith("HTTP/1.1 500 "), msg + " should not be code 500.");
106 assertTrue(response.startsWith("HTTP/1.1 403 "), msg + " should return error code 403 (Forbidden)");
107 }
108
109 @Test
110 public void testSelfRefDeep() throws Exception
111 {
112 context.addServlet(DispatchServlet.class, "/dispatch/*");
113 context.addServlet(DefaultServlet.class, "/");
114
115 String[] selfRefs =
116 {"/dispatch/forward", "/dispatch/includeS", "/dispatch/includeW", "/dispatch/includeN"};
117
118 /*
119 * Number of nested dispatch requests. 220 is a good value, as it won't
120 * trigger an Error 413 response (Entity too large). Anything larger
121 * than 220 will trigger a 413 response.
122 */
123 int nestedDepth = 220;
124
125 for (String selfRef : selfRefs)
126 {
127 String request = "GET /tests" +
128 selfRef.repeat(nestedDepth) +
129 "/ HTTP/1.1\n" +
130 "Host: tester\n" +
131 "Connection: close\n" +
132 "\n";
133 String response = connector.getResponse(request);
134
135 StringBuilder msg = new StringBuilder();
136 msg.append("Response code on nested \"").append(selfRef).append("\"");
137 msg.append(" (depth:").append(nestedDepth).append(")");
138
139 assertFalse(response.startsWith("HTTP/1.1 413 "),
140 msg + " should not be code 413 (Request Entity Too Large)," +
141 "the nestedDepth in the TestCase is too large (reduce it)");
142
143 assertFalse(response.startsWith("HTTP/1.1 500 "), msg + " should not be code 500.");
144 assertThat(response, Matchers.startsWith("HTTP/1.1 403 "));
145 }
146 }
147 }