# general-injection-go-v1

> general-injection-go-v1: 7 Keryn rules for go (4 error, 3 warning).

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

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

`error` go data-flow rule go, net-http, gin, echo, chi, gorilla-mux

A request value reaches a file open, read, write, remove or ServeFile call without being reduced to a base name or confined to a directory. Sequences such as ../ escape the intended folder. Use filepath.Base, or clean a rooted path and verify strings.HasPrefix(full, base) / filepath.Rel 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:** Take only filepath.Base(name), or join with a rooted clean (filepath.Clean("/"+name)) and then confirm the result still has the base directory as prefix (or that filepath.Rel does not start with ..). On Go 1.20+ filepath.IsLocal rejects escaping paths. Prefer an id mapped to a path server-side.

**References:** [https://pkg.go.dev/path/filepath#Clean](https://pkg.go.dev/path/filepath#Clean) · [https://pkg.go.dev/path/filepath#Rel](https://pkg.go.dev/path/filepath#Rel) · [https://pkg.go.dev/net/http#ServeFile](https://pkg.go.dev/net/http#ServeFile)

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

`error` go data-flow rule go, archive-zip, archive-tar

An archive entry name (zip File.Name or tar Header.Name) is used to build the destination of a created file. A crafted name with ../ writes outside the extraction directory (Zip Slip). Join with the destination, then verify the result still starts with the destination directory before creating it.

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 target := filepath.Join(dest, f.Name), then fail unless strings.HasPrefix(target, filepath.Clean(dest)+string(os.PathSeparator)) before os.OpenFile / os.MkdirAll.

**References:** [https://pkg.go.dev/archive/zip#File](https://pkg.go.dev/archive/zip#File) · [https://pkg.go.dev/path/filepath#Rel](https://pkg.go.dev/path/filepath#Rel)

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

`error` go data-flow rule go, database-sql, sqlx, gorm

A request value is formatted or concatenated into SQL text passed to database/sql, sqlx or GORM. The value can change the query. Keep the SQL constant with $1 / ? placeholders and pass values as arguments.

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 db.Query("SELECT ... WHERE id = $1", id) (or ? for MySQL/SQLite) and never build the statement with fmt.Sprintf or +. Identifiers cannot be bound: map them to a fixed allow-list.

**References:** [https://pkg.go.dev/database/sql#DB.Query](https://pkg.go.dev/database/sql#DB.Query) · [https://gorm.io/docs/security.html](https://gorm.io/docs/security.html)

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

`error` go data-flow rule go, os-exec

A request value is run as a command: as the program passed to exec.Command, or as the script of a shell (sh -c, cmd /c). The value can inject further commands. Run a fixed binary and pass each argument as its own exec.Command argument, validating it 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 exec.Command("tool", "--flag", arg) with a constant program and separate arguments; never pass input to sh -c or cmd /c. Validate arguments against a strict pattern and place -- before positional values where supported.

**References:** [https://pkg.go.dev/os/exec#Command](https://pkg.go.dev/os/exec#Command)

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

`warning` go data-flow rule go, net-http, resty

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

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 hand a user supplied URL to http.Get or NewRequest. Accept an id mapped to a fixed URL, or parse with url.Parse, check Scheme == "https" and Host against an allow-list, resolve and reject non-public IPs, and pin the dialled IP in a custom Transport (also covers redirects).

**References:** [https://pkg.go.dev/net/http#Get](https://pkg.go.dev/net/http#Get) · [https://pkg.go.dev/net/http#NewRequestWithContext](https://pkg.go.dev/net/http#NewRequestWithContext)

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

`warning` go data-flow rule go, net-http, gin, echo

A request value is written to the HTTP response without HTML escaping. Go sniffs the content type and serves markup as text/html, so the value runs as script in the visitor's browser. Render through html/template (auto-escaping) or escape with html.EscapeString / template.HTMLEscapeString.

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:** Use html/template (never text/template) for pages, or escape with html.EscapeString before writing; set Content-Type: text/plain and X-Content-Type-Options: nosniff for plain responses. Sanitise rich input with a policy such as bluemonday.

**References:** [https://pkg.go.dev/html/template](https://pkg.go.dev/html/template) · [https://pkg.go.dev/html#EscapeString](https://pkg.go.dev/html#EscapeString)

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

`warning` go go, database-sql, sqlx, gorm

SQL text is assembled from a variable with fmt.Sprintf or + and passed to database/sql, sqlx or GORM, or a format string that reads as SQL has %s / %v holes. Whatever the variable holds becomes part of the statement. Keep the SQL constant with $1 / ? placeholders and pass values as arguments; pick table and column names from a fixed list.

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:** Pass values as query arguments: db.Query("SELECT ... WHERE id = $1", id). For identifiers that cannot be bound (table, column, sort order), map the input to a fixed set of names and quote them with the driver's identifier quoting (pq.QuoteIdentifier).

**References:** [https://go.dev/doc/database/sql-injection](https://go.dev/doc/database/sql-injection) · [https://pkg.go.dev/database/sql#DB.Query](https://pkg.go.dev/database/sql#DB.Query)
