go-security

A set of Go security rules for writing and running production software safely. Go is a programming language commonly used for services and backend systems.

In plain words
What is it for?
Use it when building Go services, especially those handling user access, tokens, dependencies, secure communication, and security event logs.
Why use it?
It helps prevent common security mistakes such as trusting unchecked input, exposing credentials, using unsafe connections, or skipping authentication and authorization.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/wangqiqi/cursor-ai-rules/go-security
Clone the repo
git clone --depth 1 https://github.com/wangqiqi/cursor-ai-rules

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 2,254 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.02254
Opus 5 $0.00000 $0.01127
Sonnet 5 $0.00000 $0.00451
Haiku 4.5 $0.00000 $0.00225

Measured 3d ago against content hash bc0eaed11006, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

go-security scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/tech/go-security.mdc · 331 lines

How it starts

The opening of the file, as written. The whole thing — 331 lines — stays where its author put it; the contents beside it link to each section on GitHub.

🔒 Go 安全实践和最佳实践

⚠️ 执行原则

MUST 遵循以下Go安全开发准则:

  • MUST 验证所有输入数据
  • NEVER 在代码中硬编码密钥和凭据
  • ALWAYS 使用HTTPS和安全通信
  • DO NOT 忽略依赖的安全漏洞
  • MUST 实施适当的认证和授权
  • ALWAYS 记录安全相关事件

JWT 认证实现

        NotBefore: jwt.NewNumericDate(time.Now()),
        Issuer:    "your-app",
    },
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(j.secretKey)

}

func (j *JWTManager) ValidateToken(tokenString string) (*Claims, error) { token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } return j.secretKey, nil })

if err != nil {
    return nil, err
}

if claims, ok := token.Claims.(*Claims); ok && token.Valid {
    return claims, nil
}

return nil, errors.New("invalid token")

}


## 📊 最佳实践

### 错误处理模式
```go
// ✅ 推荐:自定义错误类型
type Error struct {
    Code    string `json:"code"`
    Message string `json:"message"`
    Details string `json:"details,omitempty"`
}

func (e *Error) Error() string {
    return fmt.Sprintf("[%s] %s", e.Code, e.Message)
}

func NewError(code, message string) *Error {
    return &Error{
        Code:    code,
        Message: message,
    }
}

// 预定义错误
var (
    ErrNotFound     = NewError("NOT_FOUND", "resource not found")
    ErrUnauthorized = NewError("UNAUTHORIZED", "unauthorized access")
    ErrValidation   = NewError("VALIDATION_ERROR", "validation failed")
)

// ✅ 推荐:错误包装和链式调用
func (r *UserRepository) GetUserByID(ctx context.Context, id int64) (*User, error) {
    if id <= 0 {
        return nil, fmt.Errorf("%w: invalid user ID %d", ErrValidation, id)
    }

    var user User
    query := `SELECT id, username, email FROM users WHERE id = $1`

    err := r.db.QueryRowContext(ctx, query, id).Scan(
        &user.ID, &user.Username, &user.Email,
    )

Read the full file on GitHub · 331 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 331 lines · 0 tokens per session scan A bc0eaed11006

Subscribe to this mod's changes

go-security is a cursor rule published in the GitHub repository wangqiqi/cursor-ai-rules (15 stars, last pushed 3mo ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 2,254 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.