All skills
semgrep avatar

/code-security

@327da93 official
by semgrepsemgrep/skills316 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.

rulesssrf.md

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

Prevent Server-Side Request Forgery (SSRF)

Server-Side Request Forgery (SSRF) occurs when an attacker can make a server-side application send HTTP requests to an arbitrary domain of the attacker's choosing. This can be used to:

  • Access internal services and APIs that are not exposed to the internet
  • Read cloud metadata endpoints (e.g., AWS EC2 metadata at 169.254.169.254)
  • Scan internal networks and ports
  • Bypass firewalls and access controls
  • Exfiltrate sensitive data

Language: Python

Incorrect (user input flows into URL host):

from django.http import HttpResponse
import requests

def fetch_user_data(request):
    host = request.POST.get('host')
    user_id = request.POST.get('user_id')
    response = requests.get(f"https://{host}/api/users/{user_id}")
    return HttpResponse(response.content)

Correct (fixed host, user data only in path):

from django.http import HttpResponse
import requests

def fetch_user_data(request):
    user_id = request.POST.get('user_id')
    response = requests.get(f"https://api.example.com/users/{user_id}")
    return HttpResponse(response.content)

Language: JavaScript / Node.js

Incorrect (user input in URL):

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/fetch', async (req, res) => {
    const url = req.query.url;
    const response = await axios.get(url);
    res.send(response.data);
});

Correct (fixed host, user data only in path):

const express = require('express');
const axios = require('axios');
const app = express();

app.get('/fetch', async (req, res) => {
    const resourceId = req.query.id;
    const response = await axios.get(`https://api.example.com/resources/${resourceId}`);
    res.send(response.data);
});

Language: Java

Incorrect (user-controlled URL):

import java.net.URL;
import java.net.URLConnection;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
public class FetchController {
    @GetMapping("/fetch")
    public byte[] fetchImage(@RequestParam("url") String imageUrl) throws Exception {
        URL u = new URL(imageUrl);
        URLConnection conn = u.openConnection();
        return conn.getInputStream().readAllBytes();
    }
}

Correct (fixed host, user data in path):

import java.net.URL;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
public class FetchController {
    @GetMapping("/fetch")
    public byte[] fetchImage(@RequestParam("id") String imageId) throws Exception {
        String url = String.format("https://images.example.com/%s", imageId);
        URL u = new URL(url);
        return u.openConnection().getInputStream().readAllBytes();
    }
}

Language: Go

Incorrect (user input in URL host):

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    host := r.URL.Query().Get("host")
    url := fmt.Sprintf("https://%s/api/data", host)
    resp, _ := http.Get(url)
    defer resp.Body.Close()
}

Correct (fixed host, user data in path):

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    resourceId := r.URL.Query().Get("id")
    url := fmt.Sprintf("https://api.example.com/data/%s", resourceId)
    resp, _ := http.Get(url)
    defer resp.Body.Close()
}

Language: PHP

Incorrect (user input in URL):

<?php
function fetchData() {
    $url = $_GET['url'];
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
?>

Correct (fixed host, user data in path):

<?php
function fetchData() {
    $resourceId = $_GET['id'];
    $url = 'https://api.example.com/resources/' . $resourceId;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
?>

Language: Ruby

Incorrect (user input in HTTP request):

require 'net/http'

def fetch_data
  url = params[:url]
  uri = URI(url)
  Net::HTTP.get_response(uri)
end

Correct (fixed host, user data in path):

require 'net/http'

def fetch_data
  resource_id = params[:id]
  uri = URI("https://api.example.com/resources/#{resource_id}")
  Net::HTTP.get_response(uri)
end

References:

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.