Debian Codegraph @

jetty-ee9/jetty-ee9-demos/jetty-ee9-demo-jetty-webapp/src/main/java/org/example/SecureModeServlet.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.example;
15
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.PrintStream;
19 import java.net.URL;
20 import java.net.URLClassLoader;
21 import java.util.Calendar;
22 import java.util.GregorianCalendar;
23
24 import jakarta.servlet.ServletConfig;
25 import jakarta.servlet.ServletException;
26 import jakarta.servlet.ServletOutputStream;
27 import jakarta.servlet.http.HttpServlet;
28 import jakarta.servlet.http.HttpServletRequest;
29 import jakarta.servlet.http.HttpServletResponse;
30
31 /**
32 * Dump Servlet Request.
33 */
34 @SuppressWarnings("serial")
35 public class SecureModeServlet extends HttpServlet
36 {
37 @Override
38 public void init(ServletConfig config) throws ServletException
39 {
40 super.init(config);
41 }
42
43 @Override
44 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
45 {
46 doGet(request, response);
47 }
48
49 @Override
50 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
51 {
52
53 response.setContentType("text/html");
54 ServletOutputStream out = response.getOutputStream();
55 out.println("<html>");
56 out.println(" <title>Secure Jetty Test Webapp</title>");
57
58 try
59 {
60 runPropertyChecks(out);
61
62 runFileSystemChecks(out);
63
64 runLoggingChecks(out);
65
66 runClassloaderChecks(out);
67 }
68 catch (Exception e)
69 {
70 e.printStackTrace(new PrintStream(out));
71 }
72 out.println("</html>");
73 out.flush();
74
75 try
76 {
77 Thread.sleep(200);
78 }
79 catch (InterruptedException e)
80 {
81 getServletContext().log("exception", e);
82 }
83 }
84
85 private void runClassloaderChecks(ServletOutputStream out) throws Exception
86 {
87 out.println(" <h1>Checking Classloader Setup</h1>");
88 out.println(" <p>");
89
90 System.getProperty("user.dir");
91 try
92 {
93 out.println("check ability to create classloader<br/>");
94 URL url = new URL("http://not.going.to.work");
95 new URLClassLoader(new URL[]{url});
96 out.println("status: <b>SUCCESS - unexpected</b><br/>");
97 }
98 catch (SecurityException e)
99 {
100 out.println("status: <b>FAILURE - expected</b><br/>");
101 }
102
103 out.println(" </p><br/><br/>");
104 }
105
106 private void runLoggingChecks(ServletOutputStream out) throws Exception
107 {
108 out.println(" <h1>Checking File System</h1>");
109 out.println(" <p>");
110
111 String userDir = System.getProperty("user.dir");
112 try
113 {
114 out.println("check ability to log<br/>");
115 getServletContext().log("testing logging");
116 out.println("status: <b>SUCCESS - expected</b><br/>");
117 }
118 catch (SecurityException e)
119 {
120 out.println("status: <b>FAILURE - unexpected</b><br/>");
121 out.println("<table><tr><td>");
122 e.printStackTrace(new PrintStream(out));
123 out.println("</td></tr></table>");
124 }
125
126 try
127 {
128 Calendar c = new GregorianCalendar();
129
130 String logFile = c.get(Calendar.YEAR) + "_" + c.get(Calendar.MONTH) + "_" + c.get(Calendar.DAY_OF_MONTH) + ".request.log";
131
132 out.println("check ability to access log file directly<br/>");
133 File jettyHomeFile = new File(userDir + File.separator + "logs" + File.separator + logFile);
134 jettyHomeFile.canRead();
135 out.println("status: <b>SUCCESS - unexpected</b><br/>");
136 }
137 catch (SecurityException e)
138 {
139 out.println("status: <b>FAILURE - expected</b><br/>");
140 }
141
142 out.println(" </p><br/><br/>");
143 }
144
145 private void runFileSystemChecks(ServletOutputStream out) throws Exception
146 {
147 out.println(" <h1>Checking File System</h1>");
148
149 /*
150 * test the reading and writing of a read only permission
151 */
152 out.println(" <p>");
153
154 String userDir = System.getProperty("user.dir");
155 try
156 {
157 out.println("check read for $jetty.home/lib/policy/jetty.policy<br/>");
158
159 File jettyHomeFile = new File(userDir + File.separator + "lib" + File.separator + "policy" + File.separator + "jetty.policy");
160 jettyHomeFile.canRead();
161 out.println("status: <b>SUCCESS - expected</b><br/>");
162 }
163 catch (SecurityException e)
164 {
165 out.println("status: <b>FAILURE - unexpected</b><br/>");
166 out.println("<table><tr><td>");
167 e.printStackTrace(new PrintStream(out));
168 out.println("</td></tr></table>");
169 }
170
171 try
172 {
173 out.println("check write permission for $jetty.home/lib/policy/jetty.policy<br/>");
174
175 File jettyHomeFile = new File(userDir + File.separator + "lib" + File.separator + "policy" + File.separator + "jetty.policy");
176 jettyHomeFile.canWrite();
177 out.println("status: <b>SUCCESS - unexpected</b><br/>");
178 }
179 catch (SecurityException e)
180 {
181 out.println("status: <b>FAILURE - expected</b><br/>");
182 }
183
184 try
185 {
186 out.println("check read permission for $jetty.home/lib<br/>");
187
188 File jettyHomeFile = new File(userDir + File.separator + "lib");
189 jettyHomeFile.canRead();
190 out.println("status: <b>SUCCESS - unexpected</b><br/>");
191 }
192 catch (SecurityException e)
193 {
194 out.println("status: <b>FAILURE - expected</b><br/>");
195 }
196
197 try
198 {
199 out.println("check write permission for $jetty.home/lib<br/>");
200
201 File jettyHomeFile = new File(userDir + File.separator + "lib");
202 jettyHomeFile.canWrite();
203 out.println("status: <b>SUCCESS - unexpected</b><br/>");
204 }
205 catch (SecurityException e)
206 {
207 out.println("status: <b>FAILURE - expected</b><br/>");
208 }
209
210 try
211 {
212 out.println("check read permission for $jetty.home<br/>");
213
214 File jettyHomeFile = new File(userDir + File.separator);
215 jettyHomeFile.canRead();
216 out.println("status: <b>SUCCESS - unexpected</b><br/>");
217 }
218 catch (SecurityException e)
219 {
220 out.println("status: <b>FAILURE - expected</b><br/>");
221 }
222
223 try
224 {
225 out.println("check write permission for $jetty.home<br/>");
226
227 File jettyHomeFile = new File(userDir + File.separator);
228 jettyHomeFile.canWrite();
229 out.println("status: <b>SUCCESS - unexpected</b><br/>");
230 }
231 catch (SecurityException e)
232 {
233 out.println("status: <b>FAILURE - expected</b><br/>");
234 }
235
236 try
237 {
238 out.println("check read permission for $jetty.home/logs<br/>");
239
240 File jettyHomeFile = new File(userDir + File.separator + "logs" + File.separator);
241 jettyHomeFile.canRead();
242 out.println("status: <b>SUCCESS - unexpected</b><br/>");
243 }
244 catch (SecurityException e)
245 {
246 out.println("status: <b>FAILURE - expected</b><br/>");
247 }
248
249 try
250 {
251 out.println("check read permission for $jetty.home/logs<br/>");
252
253 File jettyHomeFile = new File(userDir + File.separator + "logs");
254 jettyHomeFile.canWrite();
255 out.println("status: <b>SUCCESS - unexpected</b><br/>");
256 }
257 catch (SecurityException e)
258 {
259 out.println("status: <b>FAILURE - expected</b><br/>");
260 }
261
262 out.println(" </p><br/><br/>");
263 }
264
265 private void runPropertyChecks(ServletOutputStream out) throws IOException
266 {
267
268 out.println(" <h1>Checking Properties</h1>");
269
270 /*
271 * test the reading and writing of a read only permission
272 */
273 out.println(" <h3>Declared Property - read</h3>");
274 out.println(" <p>");
275 try
276 {
277 out.println("check read permission for __ALLOWED_READ_PROPERTY <br/>");
278 System.getProperty("__ALLOWED_READ_PROPERTY");
279 out.println("status: <b>SUCCESS - expected</b><br/>");
280 }
281 catch (SecurityException e)
282 {
283 out.println("status: <b>FAILURE - unexpected</b><br/>");
284 out.println("<table><tr><td>");
285 e.printStackTrace(new PrintStream(out));
286 out.println("</td></tr></table>");
287 }
288 try
289 {
290 out.println("check write permission for __ALLOWED_READ_PROPERTY<br/>");
291 System.setProperty("__ALLOWED_READ_PROPERTY", "SUCCESS - unexpected");
292 String value = System.getProperty("__ALLOWED_READ_PROPERTY");
293 out.println("status: <b>" + value + "</b><br/>");
294 }
295 catch (SecurityException e)
296 {
297 out.println("status: <b>FAILURE - expected</b><br/>");
298 }
299
300 out.println(" </p><br/><br/>");
301
302 /*
303 * test the reading and writing of a read/write permission
304 */
305 out.println(" <h3>Declared Property - read/write</h3>");
306 out.println(" <p>");
307 try
308 {
309 out.println("check read permission for __ALLOWED_WRITE_PROPERTY<br/>");
310 System.getProperty("__ALLOWED_WRITE_PROPERTY");
311 out.println("Status: <b>SUCCESS - expected</b><br/>");
312 }
313 catch (SecurityException e)
314 {
315 out.println("status: <b>FAILURE - unexpected</b><br/>");
316 out.println("<table><tr><td>");
317 e.printStackTrace(new PrintStream(out));
318 out.println("</td></tr></table>");
319 }
320 try
321 {
322 out.println("check write permission for __ALLOWED_WRITE_PROPERTY<br/>");
323 System.setProperty("__ALLOWED_WRITE_PROPERTY", "SUCCESS - expected");
324 String value = System.getProperty("__ALLOWED_WRITE_PROPERTY");
325 out.println("status: <b>" + value + "</b><br/>");
326 }
327 catch (SecurityException e)
328 {
329 out.println("status: <b>FAILURE - unexpected</b><br/>");
330 out.println("<table><tr><td>");
331 e.printStackTrace(new PrintStream(out));
332 out.println("</td></tr></table>");
333 }
334
335 out.println(" </p><br/><br/>");
336
337 /*
338 * test the reading and writing of an undeclared property
339 */
340 out.println(" <h3>checking forbidden properties</h3>");
341 out.println(" <p>");
342 try
343 {
344 out.println("check read permission for __UNDECLARED_PROPERTY: <br/>");
345 System.getProperty("__UNDECLARED_PROPERTY");
346 out.println("status: <b>SUCCESS - expected</b><br/>");
347 }
348 catch (SecurityException e)
349 {
350 out.println("status: <b>FAILURE - expected</b><br/>");
351 }
352 try
353 {
354 out.println("check write permission for __UNDECLARED_PROPERTY: <br/>");
355 System.setProperty("__UNDECLARED_PROPERTY", "SUCCESS - unexpected");
356 String value = System.getProperty("__UNDECLARED_PROPERTY");
357 out.println("status: <b>" + value + "</b><br/>");
358 }
359 catch (SecurityException e)
360 {
361 out.println("status: <b>FAILURE - expected</b><br/>");
362 }
363
364 out.println(" </p><br/><br/>");
365 }
366 }