go-security
/go-security
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.
- Author
- redhatproductsecurity ↗
- Repository
- prodsec-skills ↗
- Licence
- Apache-2.0
- Last repository change
- 15.07.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 9
No signals detected. Repository changed on 15.07.2026, without references to retired models or APIs.
Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.
What it does and when to use it
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.
Original author description
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 it works
- Dependency Management
- Input Validation
- XSS Prevention
- SQL Injection Prevention
- Cryptography
- HTTPS and TLS
- unsafe and cgo
- Error Handling and Logging
- nil Pointer Safety
- Tooling
SKILL.md preview
---
name: go-security
description: >
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.
category: "secure_development"
subcategory: "languages"
---
# Go Security
## Dependency Management
- Use **Go modules** for all dependency management
- The `go.sum` checksum database provides assurance against module mutation
- Pin dependency versions, including transitive modules
- Run `govulncheck` regularly to scan for known vulnerabilities in dependencies
## Input Validation
- Validate all user input using Go native packages (`strconv`, `regexp`) or third-party validators like [go-playground/validator](https://github.com/go-playground/validator)
- Never trust input from external sources without validation
## XSS Prevention
Use `html/template` (not `text/template`) for rendering HTML. It applies contextual autoescaping for HTML, CSS, JavaScript, and URL contexts.
```go
name := r.FormValue("name")
tmpl := template.Must(template.ParseGlob("page.html"))
data["Name"] = name
err := tmpl.ExecuteTemplate(w, "page", data)
```
Use [nosurf](https://github.com/justinas/nosurf) for CSRF prevention in HTTP handlers.
## SQL Injection Prevention
Use **parameterized queries**. In Go, statements are prepared on the DB, not the connection:
```go
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
- Use Go's standard `crypto` and `golang.org/x/crypto` packages
- Do not implement custom cryptographic algorithms
- Use well-established algorithms (AES-GCM, ChaCha20-Poly1305, Ed25519)
## HTTPS and TLS
Enforce HTTPS with HSTS headers and explicit TLS server configuration:
```go
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
```
```go
config := &tls.Config{ServerName: "yourServiceName"}
```
Always encrypt in-transit communication, even for internal services.
## unsafe and cgo
### unsafe Package
- The `unsafe` package bypasses Go's type system; subtle mistakes are common
- Avoid `unsafe` unless there is no alternative (low-level system calls, performance-critical FFI)
- Verify safe usage with Go's vet tooling
### cgo
Avoid cgo when possible. Known issues:
- Breaks Go's cross-compilation
- Increases binary size and reduces portability
- cgo calls are significantly slower than native Go calls
- Puts Go's concurrency model at risk
- May break static binary linking
## Error Handling and Logging
- Handle every error explicitly (`if err != nil`)
- Never log secrets, tokens, API keys, or PII
- Sanitize data before including it in log output
- Do not expose internal error details in user-facing responses
## nil Pointer Safety
When unmarshalling JSON into structs with pointer fields, unset pointers remain `nil`. Dereferencing them causes a panic:
```go
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](https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck) | Scan dependencies for known vulnerabilities |
| [golangci-lint](https://golangci-lint.run/) | Aggregated linter collection |
| [gosec](https://github.com/securego/gosec) | Go security checker |
## Implementation Checklist
- [ ] Go modules are used with `go.sum` integrity checking
- [ ] `govulncheck` runs in CI
- [ ] All user input is validated before use
- [ ] `html/template` is used for HTML rendering (not `text/template`)
- [ ] SQL queries use parameterized statements
- [ ] Standard `crypto` packages are used (no custom crypto)
- [ ] HSTS is set and TLS is configured with explicit `ServerName`
- [ ] `unsafe` and `cgo` usage is justified, minimized, and reviewed
- [ ] Errors are handled explicitly; no secrets in logs or error responses
- [ ] Pointer fields are checked for nil before dereferencing
## References
- [Go Modules Reference](https://golang.org/ref/mod)
- [govulncheck](https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck)
- [OWASP Go Secure Coding Practices](https://github.com/OWASP/Go-SCP)
- [Snyk: Go Security Cheatsheet](https://snyk.io/blog/go-security-cheatsheet-for-go-developers/)
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · redhatproductsecurity/prodsec-skills | 100 | Apache-2.0 |
| h0rn3t/golang-skills | 100 | Apache-2.0 |
| claude-dev-suite/claude-dev-suite | 95 | MIT |
Database snapshot: 13.11.2026 · package version 73c8c52fe921 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100secure-coding-practices
Secure coding practices and defensive programming patterns for building security-first applications. Use when implementing authentication, handling user input, managing sensitive data, or conducting secure code reviews. - 100security-and-hardening
Use when writing or reviewing code that handles user input, authentication, access control, cryptography, error handling, file uploads, or dependency management. Also activates when a pull request touches any security-sensitive component. - 100secure-coding
Secure-coding discipline mapped to the OWASP Top 10, checked at the point of writing code, not after. Covers input validation and injection classes, broken access control, secrets handling, dependency/supply-chain risk, and safe error handling/logging. Use when writing code that… - 100owasp-security
Reviews code for security vulnerabilities and guides secure implementation using OWASP Top 10:2025, ASVS 5.0, the OWASP Top 10 for LLM Applications (2026), and the OWASP Top 10 for Agentic Applications (2026). Use when reviewing code or a diff for security issues, implementing… - 81security-guidance
Security-first development guidance based on OWASP ASVS (Application Security Verification Standard). Use this skill automatically when planning or implementing any code that touches user input, authentication, data persistence, network communication, file I/O, cryptography, or… - 100secure-code-review
Perform a security-focused code review of a change or codebase — authentication, authorization (object/function level), input handling and injection, output encoding, cryptography, secrets, deserialization, SSRF, file handling, logging — with language-specific sinks and a…