Debian Codegraph @

jetty-ee10/jetty-ee10-demos/jetty-ee10-demo-jetty-webapp/src/test/java/org/eclipse/jetty/ee10/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.ee10;
15
16 import org.eclipse.jetty.ee10.servlet.DefaultServlet;
17 import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
18 import org.eclipse.jetty.ee10.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("/tests");
48 server.setHandler(context);
49 server.start();
50 }
51
52 @AfterEach
53 public void tearDown()
54 {
55 LifeCycle.stop(server);
56 }
57
58 /**
59 * As filed in JETTY-978.
60 *
61 * Security problems in demo dispatch servlet.
62 *
63 * <blockquote>
64 * <p>
65 * The dispatcher servlet (org.example.DispatchServlet) is prone to a Denial of
66 * Service vulnerability.
67 * </p>
68 * <p>
69 * This example servlet is meant to be used as a resources dispatcher,
70 * however a malicious aggressor may abuse this functionality in order to
71 * cause a recursive inclusion. In details, it is possible to abuse the
72 * method org.example.DispatchServlet.doGet(DispatchServlet.java:203) forcing
73 * the application to recursively include the "Dispatch" servlet.
74 * </p>
75 * <p>
76 * Dispatch org.example.DispatchServlet 1 Dispatch /dispatch/* As a result, it
77 * is possible to trigger a "java.lang.StackOverflowError" and consequently
78 * an internal server error (500).
79 * </p>
80 * <p>
81 * Multiple requests may easily affect the availability of the servlet
82 * container. Since this attack can cause the server to consume resources in
83 * a non-linear relationship to the size of inputs, it should be considered
84 * as a server flaw.
85 * </p>
86 * <p>
87 * The vulnerability seems confined to the example servlet and it does not
88 * afflict the Jetty's core."
89 * </p>
90 * </blockquote>
91 */
92 @Test
93 public void testSelfRefForwardDenialOfService() throws Exception
94 {
95 ServletHolder dispatch = context.addServlet(DispatchServlet.class, "/dispatch/*");
96 context.addServlet(DefaultServlet.class, "/");
97
98 String request = "GET /tests/dispatch/includeN/" + dispatch.getName() + " HTTP/1.1\n" +
99 "Host: tester\n" +
100 "Connection: close\n" +
101 "\n";
102 String response = connector.getResponse(request);
103
104 String msg = "Response code on SelfRefDoS";
105
106 assertFalse(response.startsWith("HTTP/1.1 500 "), msg + " should not be code 500.");
107 assertTrue(response.startsWith("HTTP/1.1 403 "), msg + " should return error code 403 (Forbidden)");
108 }
109
110 @Test
111 public void testSelfRefDeep() throws Exception
112 {
113 context.addServlet(DispatchServlet.class, "/dispatch/*");
114 context.addServlet(DefaultServlet.class, "/");
115
116 String[] selfRefs =
117 {"/dispatch/forward", "/dispatch/includeS", "/dispatch/includeW", "/dispatch/includeN"};
118
119 /*
120 * Number of nested dispatch requests. 220 is a good value, as it won't
121 * trigger an Error 413 response (Entity too large). Anything larger
122 * than 220 will trigger a 413 response.
123 */
124 int nestedDepth = 220;
125
126 for (String selfRef : selfRefs)
127 {
128 String request = "GET /tests" +
129 selfRef.repeat(nestedDepth) +
130 "/ HTTP/1.1\n" +
131 "Host: tester\n" +
132 "Connection: close\n" +
133 "\n";
134 String response = connector.getResponse(request);
135
136 StringBuilder msg = new StringBuilder();
137 msg.append("Response code on nested \"").append(selfRef).append("\"");
138 msg.append(" (depth:").append(nestedDepth).append(")");
139
140 assertFalse(response.startsWith("HTTP/1.1 413 "),
141 msg + " should not be code 413 (Request Entity Too Large)," +
142 "the nestedDepth in the TestCase is too large (reduce it)");
143
144 assertFalse(response.startsWith("HTTP/1.1 500 "), msg + " should not be code 500.");
145 assertThat(response, Matchers.startsWith("HTTP/1.1 403 "));
146 }
147 }
148 }