|
| 1 | +/** |
| 2 | + * @(#)HTMLUtils.java 0.01 26/10/2025 |
| 3 | + * Copyright (C) 2025 - 20xx MER-C |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License |
| 7 | + * as published by the Free Software Foundation; either version 3 |
| 8 | + * of the License, or (at your option) any later version. Additionally |
| 9 | + * this file is subject to the "Classpath" exception. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +package org.wikipedia; |
| 22 | + |
| 23 | +/** |
| 24 | + * Utility methods for generating and parsing HTML that don't belong in any of |
| 25 | + * the specialist utility classes. |
| 26 | + * @author MER-C |
| 27 | + * @version 0.01 |
| 28 | + */ |
| 29 | +public class HTMLUtils |
| 30 | +{ |
| 31 | + /** |
| 32 | + * Sanitizes untrusted input for XSS destined for inclusion in the HTML |
| 33 | + * body. |
| 34 | + * @param input an input string |
| 35 | + * @see <a href="https://www.owasp.org/index.php/XSS_Prevention">OWASP XSS |
| 36 | + * Prevention Cheat Sheet Rule 1</a> |
| 37 | + * @return the sanitized input or the empty string if input is null |
| 38 | + */ |
| 39 | + public static String sanitizeForHTML(String input) |
| 40 | + { |
| 41 | + if (input == null) |
| 42 | + return ""; |
| 43 | + return input.replaceAll("&", "&") |
| 44 | + .replaceAll("<", "<") |
| 45 | + .replaceAll(">", ">") |
| 46 | + .replaceAll("'", "'") |
| 47 | + .replaceAll("\"", """) |
| 48 | + .replaceAll("/", "/"); |
| 49 | + } |
| 50 | +} |
0 commit comments