samber/cc-skills-golang / skills/golang-security
golang-security
FlaggedSecurity best practices and vulnerability prevention for Golang — injection (SQL, command, XSS), cryptography, path traversal, SSRF and HTTP security headers, cookies, secrets management, memory safety, PII in logs, STRIDE/DREAD threat modeling, plus `gosec` SAST, race detection, and fuzz testing. Apply when writing, reviewing, or auditing Go code for security, or when touching crypto, file or network I/O, secrets, user input, or authentication. Not for non-exploitable defensive bugs such as nil panics or slice aliasing (→ See `samber/cc-skills-golang@golang-safety` skill), dependency vulnerability scanning with govulncheck (→ See `samber/cc-skills-golang@golang-dependency-management` skill), or wiring security scanners into CI pipelines (→ See `samber/cc-skills-golang@golang-continuous-integration` skill).
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 14.0 KB |
| evals/evals.json | config | 49.0 KB |
| references/architecture.md | markdown | 10.4 KB |
| references/checklist.md | markdown | 4.0 KB |
| references/cookies.md | markdown | 4.8 KB |
| references/cryptography.md | markdown | 11.0 KB |
| references/filesystem.md | markdown | 6.8 KB |
| references/injection.md | markdown | 7.3 KB |
| references/logging.md | markdown | 3.9 KB |
| references/memory-safety.md | markdown | 5.1 KB |
| references/network.md | markdown | 6.3 KB |
| references/secrets.md | markdown | 4.8 KB |
| references/third-party.md | markdown | 4.9 KB |
| references/threat-modeling.md | markdown | 8.4 KB |
references/logging.md
View on GitHub# Logging Security Rules
Logging sensitive information can lead to data exposure and compliance violations.
**Rules:**
1. PII MUST NEVER be logged — filter passwords, tokens, emails, and personal data.
2. Log injection MUST be prevented — sanitize user input before logging.
3. Error messages MUST NOT expose internals to users — log details server-side, return generic messages.
---
## Table of Contents
- [Sensitive Data in Logs — Medium](#sensitive-data-in-logs--medium)
- [Log Injection — Low](#log-injection--low)
- [Information Leakage in Error Messages — Medium](#information-leakage-in-error-messages--medium)
- [General Logger Security — Low](#general-logger-security--low)
- [Log Security Checklist](#log-security-checklist)
- [CWE References](#cwe-references)
## Sensitive Data in Logs — Medium
**Bad:**
```go
type User struct {
ID string
Username string
Password string
Token string
}
func logUserLogin(user *User) {
log.Printf("User logged in: %+v\n", user) // DON'T: Logs password, token
}
```
**Good:**
```go
import "log/slog"
func logUserLogin(logger *slog.Logger, user *User) {
logger.Info("user_login",
"user_id", user.ID,
"username", user.Username,
// Don't log: password, token
)
}
```
---
## Log Injection — Low
User input in logs can lead to log injection attacks.
**Bad:**
```go
log.Printf("User logged in: %s\n", username) // DON'T: No sanitization
```
**Good:**
```go
import "log/slog"
// Sanitize user input before logging
func sanitizeLogInput(input string) string {
// Remove control characters
var result strings.Builder
for _, r := range input {
if !unicode.IsControl(r) || r == '\n' || r == '\t' {
result.WriteRune(r)
}
}
return result.String()
}
func logUsername(logger *slog.Logger, username string) {
sanitized := sanitizeLogInput(username)
logger.Info("user_login", "username", sanitized)
}
```
---
## Information Leakage in Error Messages — Medium
**Bad:**
```go
func handleDatabaseError(err error) error {
return fmt.Errorf("database error: %v", err) // DON'T: Leaks internal details
}
func dbErrorToHTTP(err error) {
http.Error(w, "Error: "+err.Error(), 500) // DON'T
}
```
**Good:**
```go
func handleDatabaseError(logger *slog.Logger, err error) error {
// Log detailed error for debugging
logger.Error("database_error", "error", err.Error())
// Return generic message to client
return errors.New("database operation failed")
}
func dbErrorToHTTP(w http.ResponseWriter, logger *slog.Logger, err error) {
logger.Error("database_error", "error", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
```
---
## General Logger Security — Low
**Bad:**
```go
import "log"
log.Println("User logged in:", user.ID, password) // DON'T: Logs password
fmt.Printf("DEBUG: %+v\n", data) // DON'T: Raw data
```
**Good:**
```go
import "log/slog"
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelInfo,
AddSource: false,
})
logger := slog.New(handler)
logger.Info("user_login",
"user_id", userID,
"ip_address", request.RemoteIP,
)
```
---
## Log Security Checklist
- [ ] No passwords, tokens, or secrets in logs
- [ ] No PII/PHI in production logs
- [ ] Sanitize user input before logging
- [ ] Use structured logging (JSON)
- [ ] Implement log level strategy
- [ ] Separate access logs from error logs
- [ ] Log file permissions restricted (e.g., 600)
- [ ] Log rotation prevents disk exhaustion
- [ ] Generic error messages to clients
- [ ] Detailed errors only in internal logs
---
## CWE References
- **CWE-532**: Insertion of Sensitive Information into Log File
- **CWE-117**: Improper Output Neutralization for Logs
- **CWE-209**: Information Exposure Through an Error Message
- **CWE-200**: Exposure of Sensitive Information
- **CWE-312**: Cleartext Storage of Sensitive Information
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |