XMLDecoder on Untrusted Input
Using XMLDecoder.readObject() on user-provided XML, allowing arbitrary object instantiation and code execution.
Remote code execution vulnerabilities occur when untrusted XML data is deserialized using XMLDecoder, which can invoke arbitrary constructors and methods during the deserialization process.
@RestControllerpublic class ConfigController { @PostMapping("/api/config") public ResponseEntity<String> updateConfig(@RequestBody String xmlConfig) { try { // VULNERABLE: XMLDecoder with untrusted XML XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(xmlConfig.getBytes())); Object config = decoder.readObject(); decoder.close(); // Process config... return ResponseEntity.ok("Configuration updated"); } catch (Exception e) { return ResponseEntity.status(500).body("Configuration failed"); } }}@RestControllerpublic class SecureConfigController { @PostMapping("/api/config") public ResponseEntity<String> updateConfig(@RequestBody String xmlConfig) { try { // SECURE: Use safe XML parsing with validation DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(xmlConfig.getBytes())); // Manual mapping to safe objects ConfigObject config = parseConfigSafely(doc); return ResponseEntity.ok("Configuration updated safely"); } catch (Exception e) { return ResponseEntity.status(500).body("Invalid configuration"); } } private ConfigObject parseConfigSafely(Document doc) { // Safe manual parsing instead of automatic deserialization Element root = doc.getDocumentElement(); ConfigObject config = new ConfigObject(); // Manually extract and validate each field String name = root.getAttribute("name"); if (isValidConfigName(name)) { config.setName(name); } return config; } private boolean isValidConfigName(String name) { return name != null && name.matches("^[a-zA-Z0-9_-]+$"); }}The vulnerable code was updated to address the security issue.
Using XMLDecoder.readObject() on user-provided XML, allowing arbitrary object instantiation and code execution.
Sourcery automatically identifies remote code execution via xmldecoder deserialization and many other security issues in your codebase.