# general-injection-java-v1

> general-injection-java-v1: 9 Keryn rules for java (4 error, 5 warning).

Source: https://keryn.dev/rules/general-injection-java-v1/

9 rules · Keryn Software Licence · rules 0.8.0 · [all packs](https://keryn.dev/rules/)

**How to read a rule**

- **Severity**: How bad the finding is if it is real: `error` fails a default gate, `warning` and `info` do not.

- **Confidence**: **P0** exploitable as written; **P1** dangerous in context; **P2** a heuristic or a posture check: confirm by reading the code.

- **False-positive risk**: **low**: an exact API or flag, so hits are almost always real; **medium**: the pattern is legitimate in many programs, so check each hit; **high**: it finds a lead to confirm, so expect benign hits.

- **Edge**: The relationship the finding sits on. **reaches**: content or traffic gets to a thing; **calls**: one thing invokes another; **reads**: one thing reads from another; **writes**: one thing writes to another; **sends**: data leaves for another party; **grants**: authority is given or left unchecked; **depends_on**: something relies on a component it does not verify; **trusts**: one party believes another without evidence. The boundary it crosses, when there is one: **user->prompt** (untrusted input reaching a prompt); **model->code** (model output reaching code, a shell, a query or a page); **secret->outside** (a secret leaving the trusted side); **supply-chain** (code, models or prompts pulled in from outside).

- **Taxonomy chips**: References for readers who work in OWASP LLM 2025, OWASP Agentic, CWE or NIST AI RMF. They say where a rule belongs, not that a framework is satisfied.

## keryn.general.java.path-traversal-request-to-file

`error` java data-flow rule java, spring, servlet, jax-rs

A request parameter, path variable or uploaded file name reaches a file read, write or delete without being reduced to a file name or checked against the base directory. A value such as ../../etc/passwd escapes the intended folder. Take only the file name (FilenameUtils.getName) or resolve, normalize() and verify startsWith(base) before opening.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [reaches](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-22](https://keryn.dev/taxonomies/cwe/#cwe-22-path-traversal) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Never build a path from a request value directly. Reduce it to a bare file name with FilenameUtils.getName or Paths.get(x).getFileName(), or resolve it against the base directory, call normalize() (or getCanonicalPath()) and reject the request unless the result startsWith the base directory. Prefer an opaque id looked up in a map or database.

**References:** [https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Path.html#normalize()](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Path.html#normalize()) · [https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/requestparam.html](https://docs.spring.io/spring-framework/reference/web/webmvc/mvc-controller/ann-methods/requestparam.html)

## keryn.general.java.zip-slip-entry-name

`error` java data-flow rule java, zip, tar

An archive entry name is used to build the output path of a file that is created or written. A crafted entry such as ../../x writes outside the extraction directory (Zip Slip). Resolve the entry against the destination, normalize it and refuse any path that does not start with the destination directory.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [reads](https://keryn.dev/docs/edges/edge-kinds/#reads) one thing reads from another.

[CWE-22](https://keryn.dev/taxonomies/cwe/#cwe-22-path-traversal) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Compute File target = new File(dest, entry.getName()) (or dest.resolve(name).normalize()), then fail unless target.getCanonicalPath() / target.startsWith(dest) holds, before opening any stream on it.

**References:** [https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/zip/ZipEntry.html#getName()](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/zip/ZipEntry.html#getName())

## keryn.general.java.sql-injection-request-to-query

`error` java data-flow rule java, spring, jdbc, jpa, hibernate

A request value is concatenated or formatted into SQL text that is passed to JDBC, JdbcTemplate or JPA. The value can change the query. Use a PreparedStatement with ? placeholders and setString/setInt, or named parameters, and keep the SQL text constant.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Write the SQL as a constant string with ? or :name placeholders and bind values through PreparedStatement.setX, JdbcTemplate args, or Query.setParameter. For column or table names, map user input to a fixed allow-list.

**References:** [https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/PreparedStatement.html](https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/PreparedStatement.html) · [https://docs.spring.io/spring-framework/reference/data-access/jdbc/core.html](https://docs.spring.io/spring-framework/reference/data-access/jdbc/core.html)

## keryn.general.java.xss-request-to-response-writer

`warning` java data-flow rule java, servlet, spring

A request value is written into the HTTP response body without HTML encoding. Servlet writers default to text/html, so markup in the value runs in the visitor's browser. Encode with HtmlUtils.htmlEscape, StringEscapeUtils.escapeHtml4 or the OWASP Encoder (Encode.forHtml) before writing.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-79](https://keryn.dev/taxonomies/cwe/#cwe-79-cross-site-scripting) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Escape every request-derived value for the HTML context it lands in (Encode.forHtmlContent / forHtmlAttribute, HtmlUtils.htmlEscape) or render through a template engine with auto-escaping. Set an explicit Content-Type of text/plain when no markup is intended.

**References:** [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/HtmlUtils.html](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/HtmlUtils.html) · [https://owasp.org/projects/java-encoder](https://owasp.org/projects/java-encoder)

## keryn.general.java.ssrf-request-to-http-client

`warning` java data-flow rule java, spring, resttemplate, webclient, httpclient, okhttp

A request value decides which URL the server fetches. An attacker can point the call at internal services, cloud metadata or loopback. Check the parsed host against an allow-list (and block private ranges) before making the request, or accept only an id that maps to a fixed URL.

Confidence **P1**: dangerous in context. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [reaches](https://keryn.dev/docs/edges/edge-kinds/#reaches) content or traffic gets to a thing; it crosses the [`user->prompt`](https://keryn.dev/docs/edges/trust-boundaries/#user-to-prompt) boundary (untrusted input reaching a prompt).

[CWE-918](https://keryn.dev/taxonomies/cwe/#cwe-918-server-side-request-forgery) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Never pass a user-supplied string as the request URL. Parse it, require https, compare the host against a fixed allow-list, resolve it and reject loopback, link-local and private addresses, and disable redirects or re-validate them.

**References:** [https://docs.spring.io/spring-framework/reference/integration/rest-clients.html](https://docs.spring.io/spring-framework/reference/integration/rest-clients.html) · [https://docs.oracle.com/en/java/javase/21/docs/api/java.net.http/java/net/http/HttpRequest.html](https://docs.oracle.com/en/java/javase/21/docs/api/java.net.http/java/net/http/HttpRequest.html)

## keryn.general.java.command-injection-request-to-shell

`error` java data-flow rule java, spring, servlet

A request value is run as an operating system command, either as the program itself or as the command string of a shell (sh -c, cmd /c). The value can append further commands. Run a fixed program with each argument as a separate array element and validate arguments against an allow-list.

Confidence **P1**: dangerous in context. False-positive risk **low**: an exact API or flag, so hits are almost always real.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-78](https://keryn.dev/taxonomies/cwe/#cwe-78-os-command-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Call ProcessBuilder with a hard-coded executable and each argument as its own element; never route input through sh -c or cmd /c. Validate arguments against a strict allow-list or regex and pass -- before positional values where the tool supports it.

**References:** [https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ProcessBuilder.html](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ProcessBuilder.html)

## keryn.general.java.csrf-protection-disabled

`warning` java java, spring-security

Spring Security CSRF protection is switched off for the whole filter chain. Cookie or session authenticated state-changing requests can then be forged from another site. Keep CSRF enabled, or disable it only for a stateless token-authenticated API and say so.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **high**: it finds a lead to confirm, so expect benign hits.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another.

[CWE-352](https://keryn.dev/rules/?cwe=CWE-352) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Leave csrf() enabled for browser sessions and use CookieCsrfTokenRepository.withHttpOnlyFalse() for SPAs. If the API is purely bearer-token authenticated and holds no cookie auth, restrict the exemption with ignoringRequestMatchers for those paths instead of disabling it globally.

**References:** [https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html](https://docs.spring.io/spring-security/reference/servlet/exploits/csrf.html)

## keryn.general.java.sql-built-from-variable

`warning` java java, jdbc, spring, jpa, hibernate, android

The SQL text passed to this call is assembled from a variable by concatenation, String.format or formatted(), so whatever the variable holds becomes part of the statement. Keep the SQL constant and bind values as ? or named parameters instead.

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [calls](https://keryn.dev/docs/edges/edge-kinds/#calls) one thing invokes another; it crosses the [`model->code`](https://keryn.dev/docs/edges/trust-boundaries/#model-to-code) boundary (model output reaching code, a shell, a query or a page).

[CWE-89](https://keryn.dev/taxonomies/cwe/#cwe-89-sql-injection) [LLM05](https://keryn.dev/taxonomies/owasp-llm/#llm05-improper-output-handling)

**Fix:** Write the SQL as a constant string with ? or :name placeholders and bind values through PreparedStatement.setX, JdbcTemplate arguments, or Query.setParameter. Table and column names cannot be bound: map them from an allow-list. Numeric values read through Integer.parseInt are not flagged.

**References:** [https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/PreparedStatement.html](https://docs.oracle.com/en/java/javase/21/docs/api/java.sql/java/sql/PreparedStatement.html) · [https://docs.spring.io/spring-framework/reference/data-access/jdbc/core.html](https://docs.spring.io/spring-framework/reference/data-access/jdbc/core.html) · [https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html)

## keryn.general.java.insecure-deserialization-jackson-default-typing

`warning` java java, jackson

Jackson default typing is switched on. Class names in the JSON then choose the instantiated type, which is a deserialization gadget vector for untrusted input. Use @JsonTypeInfo on specific base types, or pass a restrictive PolymorphicTypeValidator built with BasicPolymorphicTypeValidator.builder().allowIfSubType(...).

Confidence **P2**: a heuristic or a posture check: confirm by reading the code. False-positive risk **medium**: the pattern is legitimate in many programs, so check each hit.

Edge inspected: [depends_on](https://keryn.dev/docs/edges/edge-kinds/#depends-on) something relies on a component it does not verify; it crosses the [`supply-chain`](https://keryn.dev/docs/edges/trust-boundaries/#supply-chain) boundary (code, models or prompts pulled in from outside).

[CWE-502](https://keryn.dev/taxonomies/cwe/#cwe-502-deserialization-of-untrusted-data) [LLM03](https://keryn.dev/taxonomies/owasp-llm/#llm03-supply-chain)

**Fix:** Remove enableDefaultTyping. If polymorphism is required, call activateDefaultTyping with a BasicPolymorphicTypeValidator that allows only your own base package, never LaissezFaireSubTypeValidator.

**References:** [https://github.com/FasterXML/jackson-docs/wiki/JacksonPolymorphicDeserialization](https://github.com/FasterXML/jackson-docs/wiki/JacksonPolymorphicDeserialization) · [https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/latest/com/fasterxml/jackson/databind/jsontype/BasicPolymorphicTypeValidator.html](https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/latest/com/fasterxml/jackson/databind/jsontype/BasicPolymorphicTypeValidator.html)
