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.

rulessecrets.md

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

Avoid Hardcoded Secrets

Hardcoded credentials, API keys, tokens, and other secrets in source code pose a critical security risk. When secrets are committed to version control, they can be exposed to unauthorized parties through repository access, leaked in public repositories or through data breaches, difficult to rotate without code changes and redeployment, and discovered by automated secret scanning tools used by attackers. Always use environment variables, secret managers, or secure vaults to provide credentials at runtime.

AWS Credentials

Incorrect (Python - hardcoded AWS credentials):

import boto3

client("s3", aws_secret_access_key="jWnyxxxxxxxxxxxxxxxxX7ZQxxxxxxxxxxxxxxxx")

s3 = boto3.resource(
    "s3",
    aws_access_key_id="AKIAxxxxxxxxxxxxxxxx",
    aws_secret_access_key="jWnyxxxxxxxxxxxxxxxxX7ZQxxxxxxxxxxxxxxxx",
    region_name="us-east-1",
)

Correct (Python - AWS credentials from environment):

import boto3
import os

key = os.environ.get("ACCESS_KEY_ID")
secret = os.environ.get("SECRET_ACCESS_KEY")
s3 = boto3.resource(
    "s3",
    aws_access_key_id=key,
    aws_secret_access_key=secret,
    region_name="us-east-1",
)

API Keys and Tokens

Incorrect (JavaScript - hardcoded JWT secret):

const jsonwt = require('jsonwebtoken')

function signToken() {
  const payload = {foo: 'bar'}
  const token = jsonwt.sign(payload, 'my-secret-key')
  return token
}

Correct (JavaScript - JWT secret from environment):

const jsonwt = require('jsonwebtoken')

function signToken() {
  const payload = {foo: 'bar'}
  const secret = process.env.JWT_SECRET
  const token = jsonwt.sign(payload, secret)
  return token
}

Incorrect (JavaScript - hardcoded express-jwt secret):

var jwt = require('express-jwt');

app.get('/protected', jwt({ secret: 'shhhhhhared-secret' }), function(req, res) {
    if (!req.user.admin) return res.sendStatus(401);
    res.sendStatus(200);
});

Correct (JavaScript - express-jwt secret from environment):

var jwt = require('express-jwt');

app.get('/protected', jwt({ secret: process.env.JWT_SECRET }), function(req, res) {
    if (!req.user.admin) return res.sendStatus(401);
    res.sendStatus(200);
});

Hardcoded Passwords

Incorrect (Python Flask - hardcoded SECRET_KEY):

import flask
app = flask.Flask(__name__)

app.config["SECRET_KEY"] = '_5#y2L"F4Q8z\n\xec]/'

Correct (Python Flask - SECRET_KEY from environment):

import os
import flask
app = flask.Flask(__name__)

app.config["SECRET_KEY"] = os.environ["SECRET_KEY"]

Incorrect (Python - empty password string):

from models import UserProfile

def set_user_password(user_profile: UserProfile) -> None:
    password = ""
    user_profile.set_password(password)
    user_profile.save()

Correct (Python - password from secure source):

from models import UserProfile

def set_user_password(user_profile: UserProfile, password: str) -> None:
    user_profile.set_password(password)
    user_profile.save()

Third-Party Service Tokens

Incorrect (JavaScript - hardcoded Stripe token):

const stripe = require('stripe');

const client = stripe('sk_test_20cbqx6v2hpftsbq203r36yqccazez');

Correct (JavaScript - Stripe token from environment):

const stripe = require('stripe');

const client = stripe(process.env.STRIPE_SECRET_KEY);

Incorrect (Python - hardcoded GitHub token):

import requests

headers = {"Authorization": "token ghp_emmtytndiqky5a98w0s98w36fakekey"}
response = requests.get("https://api.github.com/user", headers=headers)

Correct (Python - GitHub token from environment):

import os
import requests

headers = {"Authorization": f"token {os.environ['GITHUB_TOKEN']}"}
response = requests.get("https://api.github.com/user", headers=headers)

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.