# general-injection-kotlin-v1

> general-injection-kotlin-v1: 12 Keryn rules for kotlin (4 error, 8 warning).

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

12 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.kotlin.path-traversal-request-to-file

`error` kotlin data-flow rule kotlin, ktor, spring, android

A request value reaches a file read, write, delete or file response without being reduced to a file name or checked against the base directory. A value such as ../../etc/passwd escapes the intended folder. Use File(x).name, or normalize and verify startsWith(base) / canonicalPath 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:** Reduce the value to File(x).name, or build the File, take canonicalFile (or normalize()), and refuse the request unless it startsWith the canonical base directory. Prefer an id resolved through a lookup instead of a path.

**References:** [https://ktor.io/docs/server-requests.html](https://ktor.io/docs/server-requests.html) · [https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.io/read-text.html](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.io/read-text.html)

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

`error` kotlin data-flow rule kotlin, java-util-zip, android

An archive entry name is used to build the output file of an extraction. A crafted entry such as ../../x writes outside the target directory (Zip Slip). Resolve the entry against the destination, take its canonical path and refuse anything that does not start with the destination.

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:** val target = File(dest, entry.name).canonicalFile; require(target.startsWith(dest.canonicalFile)) before outputStream() / FileOutputStream(target).

**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.kotlin.sql-injection-request-to-query

`error` kotlin data-flow rule kotlin, ktor, spring, jdbc, android-sqlite, exposed

A request, intent or deep-link value is interpolated or concatenated into SQL text executed through JDBC, JdbcTemplate, JPA or Android SQLite. The value can change the query. Keep the SQL constant with ? placeholders and bind values (PreparedStatement.setX, selectionArgs, query parameters).

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 separately (prepareStatement + setString, rawQuery(sql, arrayOf(x)), JdbcTemplate args). Map column or table names to an allow-list.

**References:** [https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#rawQuery(java.lang.String,%20java.lang.String)](https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase#rawQuery(java.lang.String,%20java.lang.String[])) · [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)

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

`error` kotlin data-flow rule kotlin, ktor, spring, android

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). The value can append further commands. Run a fixed program with each argument as a separate list 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:** Use ProcessBuilder("tool", "--flag", arg) or listOf(...) with a constant executable; do not pass input through sh -c or cmd /c. Validate arguments against a strict regex or allow-list.

**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.kotlin.ssrf-request-to-http-client

`warning` kotlin data-flow rule kotlin, ktor-client, spring, okhttp

A request value decides which URL the server fetches. An attacker can reach internal services, cloud metadata or loopback. Parse the URL, require https, compare the host to an allow-list and reject private and loopback addresses before the request.

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:** Do not pass a user supplied string as a request URL. Accept an id mapped to a fixed URL, or parse it, require https, check the host against an allow-list, resolve and reject non-public IPs, and disable or re-validate redirects.

**References:** [https://ktor.io/docs/client-requests.html](https://ktor.io/docs/client-requests.html) · [https://docs.spring.io/spring-framework/reference/integration/rest-clients.html](https://docs.spring.io/spring-framework/reference/integration/rest-clients.html)

## keryn.general.kotlin.xss-request-to-html-response

`warning` kotlin data-flow rule kotlin, ktor, spring, servlet

A request value is written into an HTML response body without encoding (Ktor respondText with text/html, or a servlet writer). The value runs as markup or script in the visitor's browser. Encode with HtmlUtils.htmlEscape, the OWASP Encoder, or build the page with kotlinx.html / a template engine that escapes.

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 request-derived text for the HTML context (HtmlUtils.htmlEscape, Encode.forHtml) or render with kotlinx.html, which escapes text nodes by default. Return text/plain when no markup is intended.

**References:** [https://ktor.io/docs/server-html-dsl.html](https://ktor.io/docs/server-html-dsl.html) · [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)

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

`warning` kotlin kotlin, 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 (CookieCsrfTokenRepository.withHttpOnlyFalse() for SPAs). For a pure bearer-token API, 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.kotlin.sql-built-from-variable

`warning` kotlin kotlin, jdbc, spring, jpa, hibernate, android, exposed

The SQL text passed to this call is assembled from a variable by concatenation, a string template ("$x" / "$&#123;x&#125;") or format(), 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 converted with toInt() / toLong() 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://kotlinlang.org/docs/strings.html#string-templates](https://kotlinlang.org/docs/strings.html#string-templates) · [https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html)

## keryn.general.kotlin.insecure-deserialization-object-input-stream

`warning` kotlin kotlin, java-serialization

A java.io.ObjectInputStream is read with readObject/readUnshared and no ObjectInputFilter or serial filter is visible. If the bytes come from a request, socket, queue or file another party can write, they can instantiate gadget classes. Install an ObjectInputFilter (setObjectInputFilter / setSerialFilter), or use a data format.

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:** Prefer kotlinx.serialization or JSON over Java serialization. When it must stay, call ois.setObjectInputFilter(ObjectInputFilter.Config.createFilter("com.example.\*;!\*")) before the first read.

**References:** [https://docs.oracle.com/en/java/javase/21/core/serialization-filtering1.html](https://docs.oracle.com/en/java/javase/21/core/serialization-filtering1.html)

## keryn.general.kotlin.insecure-deserialization-xstream

`warning` kotlin kotlin, xstream

XStream.fromXML is called on an XStream instance with no allowTypes / allowTypesByWildcard / addPermission or setupDefaultSecurity visible. Untrusted XML can then instantiate arbitrary classes. Configure a type allow-list before parsing.

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:** Call XStream.setupDefaultSecurity(xstream) and xstream.allowTypes(...) / allowTypesByWildcard before the first fromXML. Never feed untrusted XML to an XStream with the default (permissive) security.

**References:** [https://x-stream.github.io/security.html](https://x-stream.github.io/security.html)

## keryn.general.kotlin.insecure-deserialization-snakeyaml

`warning` kotlin kotlin, snakeyaml

A SnakeYAML Yaml built with the default constructor loads a document. Before SnakeYAML 2.0 the default Constructor instantiates any class named by a global tag (!!), so untrusted YAML can run code. Construct it with Yaml(SafeConstructor(LoaderOptions())) or upgrade and restrict tags with a TagInspector.

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:** Use Yaml(SafeConstructor(LoaderOptions())) for untrusted input, or SnakeYAML 2.x with a LoaderOptions TagInspector that allows only the expected types.

**References:** [https://bitbucket.org/snakeyaml/snakeyaml/wiki/Documentation](https://bitbucket.org/snakeyaml/snakeyaml/wiki/Documentation)

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

`warning` kotlin kotlin, 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)
