golang-general-backend-security

This document is designed as a security spec that supports: 1) Secure-by-default code generation for new Go code.

Go (Golang) Security Spec (Go 1.25.x, Standard Library, net/http)

This document is designed as a security spec that supports: 1) Secure-by-default code generation for new Go code. 2) Security review / vulnerability hunting in existing Go code (passive “notice issues while working” and active “scan the repo and report findings”).

It is intentionally written as a set of normative requirements (“MUST/SHOULD/MAY”) plus audit rules (what bad patterns look like, how to detect them, and how to fix/mitigate them).


0) Safety, boundaries, and anti-abuse constraints (MUST FOLLOW)


1) Operating modes

1.1 Generation mode (default)

When asked to write new Go code or modify existing code:

1.2 Passive review mode (always on while editing)

While working anywhere in a Go repo (even if the user did not ask for a security scan):

1.3 Active audit mode (explicit scan request)

When the user asks to “scan”, “audit”, or “hunt for vulns”:

Recommended audit order: 1) Build/deploy entrypoints: main.go, cmd/*, Dockerfiles, Kubernetes manifests, systemd units, CI workflows. 2) Go toolchain & dependency policy: Go version, modules, go.mod/go.sum, proxy/sumdb settings, govulncheck usage. 3) Secret management and config loading (env, files, secret stores) + logging patterns. 4) HTTP server configuration (timeouts, body limits, proxy trust, security headers). 5) AuthN/AuthZ boundaries, session/cookie settings, token validation. 6) CSRF protections for cookie-authenticated state-changing endpoints. 7) Template usage and output encoding (XSS), and any “render template from string” behavior (SSTI). 8) File handling (uploads/downloads/path traversal/temp files), static file serving. 9) Injection sinks: SQL, OS command execution, SSRF/outbound fetch, open redirects. 10) Concurrency/resource exhaustion (unbounded goroutines/queues, missing timeouts/contexts). 11) Use of unsafe / cgo / reflect in security-sensitive paths. 12) Debug/diagnostic endpoints (pprof/expvar/metrics) exposure. 13) Cryptography usage (randomness, password hashing).


2) Definitions and review guidance

2.1 Untrusted input (treat as attacker-controlled unless proven otherwise)

Examples include:

2.2 State-changing request

A request is state-changing if it can create/update/delete data, change auth/session state, trigger side effects (purchase, email send, webhook send), or initiate privileged actions.

2.3 Required audit finding format

For each issue found, output:


3) Secure baseline: minimum production configuration (MUST in production)

This is the smallest “production baseline” that prevents common Go misconfigurations.

3.1 Toolchain, patching, and dependency hygiene (MUST)

3.2 HTTP server baseline (MUST for network-facing services)

If the program serves HTTP (directly or via a framework built on net/http):

Illustrative baseline skeleton (adjust to your project):


4) Rules (generation + audit)

Each rule contains: required practice, insecure patterns, detection hints, and remediation.

GO-DEPLOY-001: Keep the Go toolchain and standard library updated (security releases)

Severity: Medium

NOTE: Upgrading dependencies and the core Go version can break projects in unexpected ways. Focus on only security-critical dependencies and if noticed, let the user know rather than upgrading automatically.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-SUPPLY-001: Go module authenticity MUST NOT be disabled for public dependencies

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-CONFIG-001: Secrets must be externalized and never logged or committed

Severity: High (Critical if credentials are committed)

Required:

Insecure patterns:

Detection hints:

Fix:


GO-HTTP-001: HTTP servers MUST set timeouts and MaxHeaderBytes

Severity: High (DoS risk)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-HTTP-002: Request body and multipart parsing MUST be size-bounded

Severity: Medium (DoS risk; can be High for upload-heavy apps)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-DEPLOY-002: Diagnostic endpoints (pprof/expvar/metrics) MUST NOT be publicly exposed

Severity: High

NOTE: This only applies to production configurations. These endpoints are often used for debug or dev endpoints. If found, confirm that it would be reachable from the actual production deployment.

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-HTTP-003: Reverse proxy and forwarded header trust MUST be explicit

Severity: High (auth, URL generation, logging/auditing correctness)

Required:

Insecure patterns:

Detection hints:

Fix:


GO-HTTP-004: Security headers SHOULD be set (in app or at the edge)

Severity: Medium

Required (typical web app serving browsers):

NOTE:

Insecure patterns:

Detection hints:

Fix:


GO-HTTP-005: Cookies MUST use secure attributes in production

Severity: Medium

Required (production, HTTPS):

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-HTTP-006: Cookie-authenticated state-changing endpoints MUST be CSRF-protected

Severity: High

Required:

If tokens are impractical, or for small applications:

Insecure patterns:

Detection hints:

Fix:


GO-HTTP-007: CORS must be explicit and least-privilege

Severity: Medium (High if misconfigured with credentials)

Required:

Insecure patterns:

Detection hints:

Fix:


GO-XSS-001: Use html/template and avoid bypassing auto-escaping with untrusted data

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


GO-SSTI-001: Never parse/execute templates from untrusted input (SSTI)

Severity: Critical

Required:

Insecure patterns:

Detection hints:

Fix:


GO-PATH-001: Prevent path traversal and unsafe file serving

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


GO-UPLOAD-001: File uploads must be validated, stored safely, and served safely

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


GO-INJECT-001: Prevent SQL injection (parameterized queries / ORM)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


GO-INJECT-002: Prevent OS command injection; avoid shelling out with untrusted input

Severity: Critical to High (depends on exposure)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-SSRF-001: Prevent SSRF in outbound HTTP requests

Severity: Medium (High in cloud/LAN environments)

Required:

Insecure patterns:

Detection hints:

Fix:


GO-HTTPCLIENT-001: Outbound HTTP clients MUST set timeouts and close bodies

Severity: High (DoS and resource exhaustion)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-REDIRECT-001: Prevent open redirects

Severity: Medium (can be High with auth flows)

Required:

Insecure patterns:

Detection hints:

Fix:


GO-CRYPTO-001: Cryptographic randomness MUST come from crypto/rand

Severity: High (Critical if used for auth/session tokens or keys)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-AUTH-001: Password storage MUST use adaptive hashing (bcrypt/argon2id) and safe comparisons

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-CONC-001: Data races and concurrency hazards MUST be treated as security-relevant

Severity: Medium to High (depends on what races affect)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


GO-UNSAFE-001: Use of unsafe/cgo MUST be minimized and audited like memory-unsafe code

Severity: High (Critical in high-risk code paths)

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


5) Practical scanning heuristics (how to “hunt”)

When actively scanning, use these high-signal patterns:

Toolchain & dependencies:

HTTP server hardening:

Body parsing / DoS:

Debug exposure:

Templates / XSS / SSTI:

Files:

Injection:

SSRF / outbound HTTP:

Crypto:

Concurrency:

Always try to confirm:


6) Sources (accessed 2026-01-28)

Primary Go documentation:

OWASP Cheat Sheet Series (general web security):