|
| 1 | +package com.jbytescanner.engine; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.w3c.dom.Document; |
| 6 | +import org.w3c.dom.Element; |
| 7 | +import org.w3c.dom.Node; |
| 8 | +import org.w3c.dom.NodeList; |
| 9 | + |
| 10 | +import javax.xml.parsers.DocumentBuilder; |
| 11 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 12 | +import java.io.File; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.util.*; |
| 15 | +import java.util.jar.JarEntry; |
| 16 | +import java.util.jar.JarFile; |
| 17 | + |
| 18 | +public class WebXmlParser { |
| 19 | + private static final Logger logger = LoggerFactory.getLogger(WebXmlParser.class); |
| 20 | + |
| 21 | + /** |
| 22 | + * Parse web.xml inside a JAR file and return a map of Servlet Class -> List of URL Patterns |
| 23 | + */ |
| 24 | + public Map<String, List<String>> parse(File jarFile) { |
| 25 | + Map<String, List<String>> routes = new HashMap<>(); |
| 26 | + |
| 27 | + if (!jarFile.exists() || !jarFile.getName().endsWith(".jar")) { |
| 28 | + return routes; |
| 29 | + } |
| 30 | + |
| 31 | + try (JarFile jar = new JarFile(jarFile)) { |
| 32 | + Enumeration<JarEntry> entries = jar.entries(); |
| 33 | + while (entries.hasMoreElements()) { |
| 34 | + JarEntry entry = entries.nextElement(); |
| 35 | + String name = entry.getName(); |
| 36 | + |
| 37 | + // Recursively search for web.xml (e.g., WEB-INF/web.xml, webapp/web.xml) |
| 38 | + // Using exact name match or suffix match to be safe |
| 39 | + if (name.endsWith("web.xml") && !name.contains("classes/")) { // Avoid resources inside classes if any |
| 40 | + logger.debug("Found web.xml in {}: {}", jarFile.getName(), name); |
| 41 | + try (InputStream is = jar.getInputStream(entry)) { |
| 42 | + parseWebXml(is, routes); |
| 43 | + } catch (Exception e) { |
| 44 | + logger.error("Failed to parse web.xml in {}", jarFile.getName(), e); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } catch (Exception e) { |
| 49 | + logger.error("Failed to process JAR for web.xml: {}", jarFile.getName(), e); |
| 50 | + } |
| 51 | + |
| 52 | + return routes; |
| 53 | + } |
| 54 | + |
| 55 | + private void parseWebXml(InputStream is, Map<String, List<String>> routes) throws Exception { |
| 56 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 57 | + // Secure processing to prevent XXE |
| 58 | + dbFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 59 | + dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); |
| 60 | + dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); |
| 61 | + dbFactory.setNamespaceAware(true); // Handle namespaces if present |
| 62 | + |
| 63 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 64 | + Document doc = dBuilder.parse(is); |
| 65 | + doc.getDocumentElement().normalize(); |
| 66 | + |
| 67 | + // 1. Extract Servlets: Name -> Class |
| 68 | + Map<String, String> servletNameMap = new HashMap<>(); |
| 69 | + NodeList servletNodes = doc.getElementsByTagName("servlet"); // Simple tag name search ignores namespace |
| 70 | + if (servletNodes.getLength() == 0) { |
| 71 | + // Try with namespace awareness if simple search fails (though getElementsByTagName usually works) |
| 72 | + servletNodes = doc.getElementsByTagNameNS("*", "servlet"); |
| 73 | + } |
| 74 | + |
| 75 | + for (int i = 0; i < servletNodes.getLength(); i++) { |
| 76 | + Element element = (Element) servletNodes.item(i); |
| 77 | + String servletName = getTagValue("servlet-name", element); |
| 78 | + String servletClass = getTagValue("servlet-class", element); |
| 79 | + |
| 80 | + if (servletName != null && servletClass != null) { |
| 81 | + servletNameMap.put(servletName, servletClass); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // 2. Extract Mappings: Name -> URL Pattern |
| 86 | + NodeList mappingNodes = doc.getElementsByTagName("servlet-mapping"); |
| 87 | + if (mappingNodes.getLength() == 0) { |
| 88 | + mappingNodes = doc.getElementsByTagNameNS("*", "servlet-mapping"); |
| 89 | + } |
| 90 | + |
| 91 | + for (int i = 0; i < mappingNodes.getLength(); i++) { |
| 92 | + Element element = (Element) mappingNodes.item(i); |
| 93 | + String servletName = getTagValue("servlet-name", element); |
| 94 | + String urlPattern = getTagValue("url-pattern", element); |
| 95 | + |
| 96 | + if (servletName != null && urlPattern != null) { |
| 97 | + String servletClass = servletNameMap.get(servletName); |
| 98 | + if (servletClass != null) { |
| 99 | + routes.computeIfAbsent(servletClass, k -> new ArrayList<>()).add(urlPattern); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private String getTagValue(String tag, Element element) { |
| 106 | + NodeList nodeList = element.getElementsByTagName(tag); |
| 107 | + if (nodeList.getLength() == 0) { |
| 108 | + nodeList = element.getElementsByTagNameNS("*", tag); |
| 109 | + } |
| 110 | + |
| 111 | + if (nodeList.getLength() > 0) { |
| 112 | + Node node = nodeList.item(0).getFirstChild(); |
| 113 | + if (node != null) { |
| 114 | + return node.getNodeValue().trim(); |
| 115 | + } |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | +} |
0 commit comments