Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved. |
| 3 | * |
| 4 | * This program and the accompanying materials are made available under the |
| 5 | * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | * http://www.eclipse.org/legal/epl-2.0. |
| 7 | * |
| 8 | * This Source Code may also be made available under the following Secondary |
| 9 | * Licenses when the conditions for such availability set forth in the |
| 10 | * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | * version 2 with the GNU Classpath Exception, which is available at |
| 12 | * https://www.gnu.org/software/classpath/license.html. |
| 13 | * |
| 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | */ |
| 16 | |
| 17 | import java.io.*; |
| 18 | import java.net.*; |
| 19 | import java.util.regex.Matcher; |
| 20 | import java.util.regex.Pattern; |
| 21 | import com.sun.ejte.ccl.reporter.*; |
| 22 | |
| 23 | /** |
| 24 | * Unit test for: |
| 25 | * |
| 26 | * ("XSS for HttpServletResponse.sendError()") |
| 27 | * |
| 28 | */ |
| 29 | public class WebTest { |
| 30 | |
| 31 | private static SimpleReporterAdapter stat |
| 32 | = new SimpleReporterAdapter("appserv-tests"); |
| 33 | |
| 34 | private static final String TEST_NAME |
| 35 | = "http-response-error-message"; |
| 36 | |
| 37 | private static final Pattern PATTERN = Pattern.compile("http/\\d\\.\\d 403 .*Hi, there.*", Pattern.CASE_INSENSITIVE); |
| 38 | |
| 39 | private String host; |
| 40 | private String port; |
| 41 | private String contextRoot; |
| 42 | private Socket sock = null; |
| 43 | |
| 44 | public WebTest(String[] args) { |
| 45 | host = args[0]; |
| 46 | port = args[1]; |
| 47 | contextRoot = args[2]; |
| 48 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 49 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 50 | public static void main(String[] args) { |
| 51 | stat.addDescription("Unit test for XSS HttpServletResponse.sendError"); |
| 52 | WebTest webTest = new WebTest(args); |
| 53 | webTest.doTest(); |
| 54 | stat.printSummary(TEST_NAME); |
| 55 | } |
| 56 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 57 | public void doTest() { |
| 58 | try { |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 59 | invoke(); |
| 60 | } catch (Exception ex) { |
| 61 | stat.addStatus(TEST_NAME, stat.FAIL); |
| 62 | ex.printStackTrace(); |
| 63 | } finally { |
| 64 | try { |
| 65 | if (sock != null) { |
| 66 | sock.close(); |
| 67 | } |
| 68 | } catch (IOException ioe) { |
| 69 | // ignore |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private void invoke() throws Exception { |
| 75 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 76 | System.out.println("Host=" + host + ", port=" + port); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 77 | sock = new Socket(host, new Integer(port).intValue()); |
| 78 | OutputStream os = sock.getOutputStream(); |
| 79 | String get = "GET " + contextRoot + "/index.jsp HTTP/1.1\n"; |
| 80 | System.out.println(get); |
| 81 | os.write(get.getBytes()); |
| 82 | os.write("Host: localhost\n".getBytes()); |
| 83 | os.write("Connection: close\n".getBytes()); |
| 84 | os.write("\n".getBytes()); |
| 85 | |
| 86 | InputStream is = null; |
| 87 | BufferedReader bis = null; |
| 88 | boolean isExpected = false; |
| 89 | |
| 90 | try { |
| 91 | is = sock.getInputStream(); |
| 92 | bis = new BufferedReader(new InputStreamReader(is)); |
| 93 | String line = null; |
| 94 | while ((line = bis.readLine()) != null) { |
| 95 | System.out.println(line); |
| 96 | Matcher m = PATTERN.matcher(line); |
| 97 | if (m.matches()) { |
| 98 | isExpected = true; |
| 99 | |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | } finally { |
| 104 | try { |
| 105 | if (is != null) { |
| 106 | is.close(); |
| 107 | } |
| 108 | } catch (IOException ioe) { |
| 109 | // ignore |
| 110 | } |
| 111 | try { |
| 112 | if (bis != null) { |
| 113 | bis.close(); |
| 114 | } |
| 115 | } catch (IOException ioe) { |
| 116 | // ignore |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (isExpected) { |
| 121 | stat.addStatus(TEST_NAME, stat.PASS); |
| 122 | } else { |
| 123 | stat.addStatus(TEST_NAME, stat.FAIL); |
| 124 | System.err.println("Missing expected response: " + PATTERN.toString()); |
| 125 | } |
| 126 | } |
| 127 | } |