All skills
semgrep avatar

/code-security

@327da93 official
by semgrepsemgrep/skills317 stars
31

Security guidelines for writing secure code. Use when writing code, reviewing code for vulnerabilities, or asking about secure coding practices like 'check for SQL injection' or 'review security'. IMPORTANT: Always consult this skill when writing or reviewing any code that handles user input, authentication, file operations, database queries, network requests, cryptography, or infrastructure configuration (Terraform, Kubernetes, Docker, GitHub Actions) — even if the user doesn't explicitly mention security. Also use when users ask to 'review my code', 'check this for bugs', or 'is this safe'.

Use this Skill: https://skilld.dev/gh/semgrep/skills/code-security

This session only. Nothing lands on disk.

rulescorrectness.md

≈955 tokens on demand. Your agent reads this file only when SKILL.md points to it.

Code Correctness Rules

Common coding mistakes that cause runtime errors, unexpected behavior, or logic issues.


Python

Mutable Default Arguments

Python only instantiates default arguments once. Mutating them affects all future calls.

INCORRECT:

def append_func(default=[]):
    default.append(5)

CORRECT:

def append_func(default=None):
    if default is None:
        default = []
    default.append(5)

Modifying Collections While Iterating

INCORRECT:

items = [1, 2, 3, 4]
for i in items:
    items.pop(0)

CORRECT:

for i in list(items):  # Iterate over a copy
    items.pop(0)

Suppressed Exceptions in Finally

Using break, continue, or return in finally suppresses exceptions.

INCORRECT:

try:
    raise ValueError()
finally:
    break  # Suppresses the exception!

CORRECT - Let the exception propagate; use finally only for cleanup:

try:
    raise ValueError()
finally:
    cleanup()  # Cleanup runs, exception still propagates

Raising Non-Exceptions

INCORRECT:

raise "error"

CORRECT:

raise Exception("error")

String Concatenation in Lists

Missing commas cause implicit string concatenation.

INCORRECT:

bad = ["a" "b" "c"]  # Results in ["abc"]

CORRECT:

good = ["a", "b", "c"]

JavaScript

Missing Template String $

INCORRECT:

return `value is {x}`  // Missing $

CORRECT:

return `value is ${x}`

Go

Loop Pointer Export

Note: Go 1.22+ scopes loop variables per-iteration, fixing this issue. The pattern below applies to Go < 1.22.

Loop variables are shared across iterations (Go < 1.22).

INCORRECT:

for _, val := range values {
    funcs = append(funcs, func() {
        fmt.Println(&val)  // Same pointer for all!
    })
}

CORRECT:

for _, val := range values {
    val := val  // Create new variable
    funcs = append(funcs, func() {
        fmt.Println(&val)
    })
}

Integer Overflow from Atoi

INCORRECT:

bigValue, _ := strconv.Atoi("2147483648")
value := int16(bigValue)  // Overflow!

CORRECT:

parsed, err := strconv.ParseInt("2147483648", 10, 32)
if err != nil {
    // handles out-of-range and invalid syntax
    log.Fatal(err)
}
value := int32(parsed)

Java

String Comparison with ==

INCORRECT:

if (a == "hello") return 1;

CORRECT:

if ("hello".equals(a)) return 1;

Assignment in Condition

INCORRECT:

if (myBoolean = true) {  // Assignment, not comparison!

CORRECT:

if (myBoolean) {

C

ato* Functions

The ato*() functions cause undefined behavior on overflow.

INCORRECT:

int i = atoi(buf);

CORRECT:

char *endptr;
errno = 0;
long l = strtol(buf, &endptr, 10);
if (errno != 0 || endptr == buf || *endptr != '\0') {
    // handle conversion error
}

Bash

Unquoted Variable Expansion

Unquoted variables split on whitespace.

INCORRECT:

exec $foo

CORRECT:

exec "$foo"

Other Languages

Scala: indexOf > 0 Bug

INCORRECT:

if (list.indexOf(item) > 0)  // Misses first element!

CORRECT:

if (list.indexOf(item) >= 0)

Elixir: Atom Exhaustion

Atoms are never garbage collected. Use String.to_existing_atom instead of String.to_atom.

OCaml: Physical vs Structural Equality

Use = not == for value comparison, <> not != for inequality.

Source: SKILL.md on GitHub

2 warnings16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides a comprehensive library of security guidelines and code examples to help AI agents write secure code and perform security reviews. It covers OWASP Top 10 vulnerabilities, infrastructure security (Terraform, Kubernetes, Docker), and general best practices. While the files contain examples of vulnerable code (such as SQL injection and hardcoded secrets), these are used exclusively for educational purposes to demonstrate what to avoid and are part of the 'Incorrect' examples within the security rules.

  • Socket16d

    2 alerts: gptSecurity

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer6mo

    4/34 files flagged

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 327da93. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 7 months ago
  • Security
  • Infrastructure
  • code-review
  • owasp
  • sql-injection
  • xss
  • command-injection
  • authentication
  • terraform
  • kubernetes
  • docker

README badge

README badge for semgrep/skills/code-security

Provides security rules across 15+ languages covering OWASP Top 10 vulnerabilities, infrastructure configuration, and secure coding practices. Includes SQL injection, XSS, command injection, cryptography, and Kubernetes/Terraform security with language-specific priority guidelines and rule files for detailed code examples.

Generated from the current SKILL.md.

Does this skill cover infrastructure security like Terraform and Kubernetes?
Yes. The skill includes 28 rule categories covering Terraform (AWS, Azure, GCP), Kubernetes, Docker, and GitHub Actions alongside language-specific rules for Python, JavaScript, Java, Go, C/C++, Ruby, and PHP.
When should I use this skill — only when the user asks about security?
No. The skill is designed for proactive mode: automatically check for vulnerabilities when writing or reviewing any code that handles user input, authentication, databases, file operations, network requests, cryptography, or infrastructure configuration — even if the user doesn't explicitly mention security.
What vulnerabilities does this skill prioritize?
It prioritizes Critical impact rules first: SQL injection, command injection, XSS, XXE, path traversal, insecure deserialization, code injection, hardcoded secrets, and memory safety. High impact rules include insecure crypto, SSRF, JWT issues, and CSRF.
Does this skill provide code examples for each vulnerability type?
Yes. Each rule category (e.g., `rules/sql-injection.md`) contains detailed vulnerable and secure code examples in the relevant language.

Generated from the current SKILL.md. These answers refresh after source changes.