When you review Java code, the most dangerous bugs rarely look broken. They look like clean, standard object-oriented code, normal configuration classes, or convenient framework helpers.

Finding these flaws means following data from an untrusted source, past type boundaries, straight into runtime sinks. A sink is an API method that performs a sensitive operation, such as parsing an archive, executing a command, or running a database query.

Here is how to spot and fix the most common high-risk Java sinks during a pull request review.

The Default Parser Trap: XML and XXE

Java's standard XML parsing APIs date back decades. By default, parsers such as DocumentBuilderFactory enable Document Type Definitions (DTDs) and external entity resolution.

When an attacker supplies XML containing an external entity, the parser tries to resolve it. This can lead to local file disclosure, Server-Side Request Forgery (SSRF), or denial of service through recursive entity expansion as DeepSource outlines.

Take a look at this typical parser setup:

// Default factory allows external DTDs DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(untrustedInputStream); 

Many developers assume running an up-to-date JDK fixes this behaviour automatically. It does not. To harden the parser, you must explicitly instruct the factory to disallow doctype declarations completely under OWASP XXE guidance:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, ""); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(untrustedInputStream); 

During review, confirm that setFeature runs before newDocumentBuilder(). Configuring properties after creating the builder leaves the parser vulnerable.

Deserialization and Polymorphic Typing

Native Java deserialization via ObjectInputStream.readObject() reconstructs an entire object graph from raw bytes using reflection. If vulnerable classes (known as gadget chains) exist on the application classpath, reading untrusted bytes can lead directly to remote code execution as explained in Safeguard's deserialization research.

If you see raw readObject() in a pull request, verify that an ObjectInputFilter from JEP 290 restricts which classes the stream can instantiate.

Modern web services often use JSON or YAML instead of binary streams. However, similar risks occur in Jackson if developers enable polymorphic type handling globally with enableDefaultTyping(). That method tells Jackson to trust class names supplied directly in incoming JSON fields.

Instead of permissive global typing, look for an explicit BasicPolymorphicTypeValidator that confines allowed subtypes to safe, application-owned packages. To build intuition for how parsers turn data into active execution paths, check out recognising dangerous code patterns in security reviews.

ORM Sorting and Dynamic Queries

Most teams know that concatenating strings into JPQL queries causes SQL injection. Parameter binding (:parameter) prevents that in WHERE clauses. Dynamic sorting, however, creates a subtle bypass in Spring Data JPA.

Normally, Sort.by("propertyName") validates input against your entity metamodel. If a client submits a property name that does not exist, the framework throws an exception. But when developers struggle with sorting on complex joins or computed aliases, they often switch to JpaSort.unsafe() as highlighted in JDriven's Spring Data review:

// Unsafe dynamic order clause Sort sort = JpaSort.unsafe(direction, userSuppliedColumn); return userRepository.findAll(sort); 

JpaSort.unsafe bypasses metamodel property validation and appends raw strings directly into the generated SQL query's ORDER BY clause. This reopens the door to SQL injection.

When reviewing dynamic sorting, check that sort columns are matched against a strict allowlist of known fields before reaching the repository query.

Command Execution on Windows

In Java 18, Runtime.getRuntime().exec(String) was deprecated because its whitespace tokenization breaks complex commands and encourages argument injection per OWASP command injection guidance.

Using ProcessBuilder with a list of arguments is much safer on POSIX platforms because Java calls execve() directly without a shell. On Windows, however, CreateProcess takes a single flat command-line string.

If an application uses ProcessBuilder to invoke .bat or .cmd scripts, or calls cmd.exe /c, the command processor still interprets shell metacharacters (&, |, %, ") regardless of Java's separated argument list as detailed in Code White's process research. During code review, check whether the external command is necessary at all. Pure Java APIs such as java.nio.file.Files replace most shell commands cleanly.

Path Traversal in Archive Extraction

When an application extracts uploaded ZIP files, it inspects entries with ZipEntry.getName(). An archive can store file names with relative path segments, such as ../../etc/cron.d/job.

If the extraction code naively joins the destination directory with entry.getName(), files are written outside the target directory. To prevent this, canonicalise both paths and verify the boundary as documented in Android's Zip Slip guidance:

ZipEntry entry = zis.getNextEntry(); File target = new File(destDir, entry.getName()); String canonicalDest = destDir.getCanonicalPath() + File.separator; String canonicalTarget = target.getCanonicalPath(); if (!canonicalTarget.startsWith(canonicalDest)) { throw new SecurityException("Illegal relative path in zip: " + entry.getName()); } 

Notice that trailing File.separator. Without it, an entry pointing to /app/target_extra could pass a check meant to protect /app/target.

Review Checklist

When skimming a Java pull request, keep these checks in mind:

  • XML Parsers: Check that disallow-doctype-decl is set to true on the parser factory.
  • Object Streams: Flag readObject() without an ObjectInputFilter, and look out for Jackson's enableDefaultTyping().
  • Database Sorting: Verify that dynamic sort columns use Sort.by() with an allowlist rather than JpaSort.unsafe().
  • Process Creation: Check whether OS calls can be replaced with native Java methods, especially when running on Windows.
  • Archive Unpacking: Ensure Zip entry extraction canonicalises target paths and appends a trailing separator.

For a structured method to trace how request parameters reach sensitive operations across multiple classes, read through how to review code for security.

If you want to practise spotting these vulnerabilities on realistic codebases, take a look at the interactive challenges on Code Review Lab.