Go security

From RedHatProductSecurity/prodsec-skills · View interactive version

Go security is an AI agent skill from the GitHub repository RedHatProductSecurity/prodsec-skills. Enforce secure coding practices in Go applications. Use when building, reviewing, or auditing Go code that handles user input, database access, cryptography, TLS, or uses unsafe/cgo.

How to use this skill

Skill file

Go Security

Dependency Management

Input Validation

XSS Prevention

Use html/template (not text/template) for rendering HTML. It applies contextual autoescaping for HTML, CSS, JavaScript, and URL contexts.

name := r.FormValue("name")
tmpl := template.Must(template.ParseGlob("page.html"))
data["Name"] = name
err := tmpl.ExecuteTemplate(w, "page", data)

Use nosurf for CSRF prevention in HTTP handlers.

SQL Injection Prevention

Use parameterized queries. In Go, statements are prepared on the DB, not the connection:

customerName := r.URL.Query().Get("name")
db.Exec("UPDATE creditcards SET name=? WHERE customerId=?", customerName, 233)

When using db.Query() with string formatting, sanitize all input first.

Cryptography

HTTPS and TLS

Enforce HTTPS with HSTS headers and explicit TLS server configuration:

w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
config := &tls.Config{ServerName: "yourServiceName"}

Always encrypt in-transit communication, even for internal services.

unsafe and cgo

unsafe Package

cgo

Avoid cgo when possible. Known issues:

Error Handling and Logging

nil Pointer Safety

When unmarshalling JSON into structs with pointer fields, unset pointers remain nil. Dereferencing them causes a panic:

type Foo struct {
    Bar *Bar
}

var f Foo
json.Unmarshal([]byte(`{"other":"data"}`), &f)
// f.Bar is nil -- accessing f.Bar.Field will panic

Always check pointer fields for nil before dereferencing.

Tooling

| Tool | Purpose |

|---|---|

| govulncheck | Scan dependencies for known vulnerabilities |

| golangci-lint | Aggregated linter collection |

| gosec | Go security checker |

Implementation Checklist

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.