### Start Claude Code Instance Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/guardian.mdx Start a Claude Code instance to begin the setup process. This command is part of the initial setup for integrating Semgrep Guardian with Claude Code. ```bash claude ``` -------------------------------- ### Setup Semgrep Dependencies Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Installs all necessary dependencies for Semgrep using the root Makefile. ```bash make setup ``` -------------------------------- ### Install Semgrep CLI (macOS/Linux) Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/getting-started/quickstart.mdx Install the Semgrep CLI using pipx or uv. pipx and uv are the preferred installation methods. Confirm installation by checking the version. ```bash # preferred: install through pipx (https://pipx.pypa.io/stable/how-to/install-pipx/) pipx install semgrep # preferred: install through uv (https://docs.astral.sh/uv/) uv tool install semgrep # best-effort: install through homebrew (maintained on a best-effort basis; often lags behind the latest release) brew install semgrep # confirm installation succeeded by printing the currently installed version semgrep --version ``` ```bash # install through pipx (https://pipx.pypa.io/stable/how-to/install-pipx/) pipx install semgrep # or, install through uv (https://docs.astral.sh/uv/) uv tool install semgrep # confirm installation succeeded by printing the currently installed version semgrep --version ``` -------------------------------- ### Create and activate a Python virtual environment Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/kb/semgrep-appsec-platform/error-externally-managed-environment.mdx This example demonstrates how to create a Python virtual environment using `venv` and activate it. This is a standard approach to isolate Python package installations and avoid the 'externally-managed-environment' error. ```bash python3 -m venv path/to/venv source path/to/venv/bin/activate ``` -------------------------------- ### Install Semgrep Core Binary Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Install the semgrep-core executable using 'make install'. This makes the 'sc' command available. ```bash make install ``` -------------------------------- ### Sample environment variable setup for cross-linking Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/deployment/local-to-scp-scans.mdx An example demonstrating how to set environment variables for SEMGREP_REPO_URL, SEMGREP_REPO_NAME, SEMGREP_BRANCH, and SEMGREP_COMMIT. This setup is used to generate correct hyperlinks for local scans in Semgrep AppSec Platform. ```bash # Set the repository URL export SEMGREP_REPO_URL=https://github.com/corporation/s_juiceshop # Set the repository name export SEMGREP_REPO_NAME=corporation/s_juiceshop # Retrieve the branch git rev-parse --abbrev-ref HEAD s_update # Set the branch export SEMGREP_BRANCH=s_update # Retrieve the commit hash git log -n 1 commit fa4e36b9369e5b039bh2220b5h9R61a38b077f29 (HEAD -> s_juiceshop, origin/main, origin/HEAD, master) # Set the commit hash export SEMGREP_COMMIT=fa4e36b9369e5b039bh2220b5h9R61a38b077f29 ``` -------------------------------- ### Running OS Processes with ProcessBuilder in Java Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/cheat-sheets/java-command-injection.mdx Shows how to use ProcessBuilder to create and start OS processes. Highlights vulnerable examples where user input is directly included in the command string or passed as the entire command, risking command injection. ```java // ProcessBuilder example Process builder = new ProcessBuilder("ls", "-la").start(); ``` ```java // Vulnerable String input = "cat /etc/passwd"; // value supplied by user input ProcessBuilder builder = new ProcessBuilder(); builder.command("bash", "-c", "some_tool -t param1 param2 " + input) ``` ```java // Vulnerable String input = "cat /etc/passwd"; // value supplied by user input ProcessBuilder builder = new ProcessBuilder(); builder.command(input); ``` -------------------------------- ### Install Semgrep CLI using uv Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/for-developers/signin.mdx Install the Semgrep CLI tool using uv. This is an alternative installation method for users who prefer uv. ```bash # Or, if you use uv (https://docs.astral.sh/uv/) uv tool install semgrep ``` -------------------------------- ### Run Build Scripts and Start Dev Server Source: https://github.com/semgrep/semgrep-docs/blob/main/README.md Execute the build scripts and then start the local development server for the documentation using Mintlify. ```bash bash run-build-scripts cd docs && npx mintlify@latest dev ``` -------------------------------- ### Example HTTP Proxy Configuration Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/cli-reference.mdx An example of how to set the HTTP_PROXY environment variable with a specific IP address and port. ```bash export HTTP_PROXY="http://10.10.1.10:3128" ``` -------------------------------- ### Example Pipfile Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/kb/semgrep-supply-chain/ssc-python-lockfiles.mdx This is an example of a Pipfile, which defines direct Python dependencies and their versions. ```text [[source]] url = "https://pypi.org/simple" verify_ssl = true name = "pypi" [packages] flasgger = "==0.9.5" flask = "==2.2.2" flask-cors = "==3.0.10" marshmallow = "==3.18.0" requests = "==2.25.1" ssqlalchemy = "==1.4.41" waitress = "==2.1.2" psycopg2 = "==2.9.5" defusedxml = "==0.7.1" [dev-packages] [requires] python_version = "3.9" ``` -------------------------------- ### Install Base Development Packages Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Installs essential system packages required for building Semgrep Core. ```bash brew install pkg-config bash ``` -------------------------------- ### Install Semgrep CLI (Windows) Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/getting-started/quickstart.mdx Install the Semgrep CLI on Windows using pipx or uv. Confirm the installation by checking the version. ```powershell # install through pipx (https://pipx.pypa.io/stable/how-to/install-pipx/) pipx install semgrep # or, install through uv (https://docs.astral.sh/uv/) uv tool install semgrep # confirm installation succeeded by printing the currently installed version semgrep --version ``` -------------------------------- ### Example HTTPS Proxy Configuration Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/cli-reference.mdx An example of how to set the HTTPS_PROXY environment variable with a specific IP address and port. ```bash export HTTPS_PROXY="http://10.10.1.10:1080" ``` -------------------------------- ### Install pre-commit Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/contributing-code.mdx Installs the pre-commit tool, which is used to ensure files are well-formatted and check for basic linting bugs before each commit. ```bash python -m pip install pre-commit ``` -------------------------------- ### Build and Install ocaml-tree-sitter Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/updating-a-grammar.mdx Build and install the ocaml-tree-sitter library following the standard procedure. This is a prerequisite for language-specific grammar updates. ```bash ./configure make setup make make install ``` -------------------------------- ### Install Semgrep Core Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Builds and installs the Semgrep Core component after dependencies are set up. ```bash make core ``` -------------------------------- ### Example requirements.txt generated from requirements.in Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/kb/semgrep-supply-chain/ssc-python-lockfiles.mdx This is an example of a `requirements.txt` file generated from a `requirements.in` file, containing all direct and transitive dependencies for Semgrep Supply Chain scanning. ```text # # This file is autogenerated by pip-compile with Python 3.10 # by the following command: # # pip-compile --output-file=requirements.txt # contourpy==1.0.7 # via matplotlib cycler==0.11.0 # via matplotlib fonttools==4.39.4 # via matplotlib kiwisolver==1.4.4 # via matplotlib matplotlib==3.7.1 # via # -r requirements.in # seaborn numpy==1.24.3 # via # -r requirements.in # contourpy # matplotlib # pandas # scipy # seaborn packaging==23.1 # via matplotlib pandas==2.0.2 # via # -r requirements.in # seaborn pillow==9.5.0 # via matplotlib pyparsing==3.0.9 # via matplotlib python-dateutil==2.8.2 # via # matplotlib # pandas pytz==2023.3 # via pandas scipy==1.10.1 # via seaborn seaborn==0.10.1 # via -r requirements.in six==1.16.0 # via python-dateutil tzdata==2023.3 # via pandas ``` -------------------------------- ### Install Semgrep CLI Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/getting-started/cli.mdx Install the Semgrep CLI using package managers like Homebrew, pipx, or uv. Ensure the installation is confirmed by checking the version. ```bash # macOS users only - ensure that you've added Homebrew to your PATH # https://docs.brew.sh/FAQ#my-mac-apps-dont-find-homebrew-utilities brew install semgrep # macOS, Linux, Windows users - using pipx (recommended) # See https://pipx.pypa.io/stable/how-to/install-pipx/ to install pipx pipx install semgrep # Or, if you use uv (https://docs.astral.sh/uv/) uv tool install semgrep # confirm semgrep --version ``` -------------------------------- ### Test Semgrep Core Installation Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Verifies the Semgrep Core installation by running its help command. ```bash bin/semgrep-core -help ``` -------------------------------- ### fyi.list Example Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/adding-a-language.mdx Example content for the `fyi.list` file, specifying informational files relative to the `ocaml-tree-sitter-semgrep/lang` directory. Comments and blank lines are permitted. ```text # Comments are allowed on their own line. # Blank lines are ok. # Each path is relative to ocaml-tree-sitter-semgrep/lang semgrep-grammars/src/tree-sitter-ruby/LICENSE semgrep-grammars/src/tree-sitter-ruby/grammar.js semgrep-grammars/src/semgrep-ruby/grammar.js ``` -------------------------------- ### Install Semgrep Pro cross-file binary Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/semgrep-code/semgrep-pro-engine-intro.mdx Install the Semgrep Pro binary required for cross-file analysis. ```bash semgrep install-semgrep-pro ``` -------------------------------- ### Public Key Example Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/semgrep-ci/network-broker.mdx Example of a WireGuard public key format. This value is used in older versions of the Network Broker configuration. ```bash 4EqJwDZ8X/qXB5u3Wpo2cxnKlysec93uhRvGWPix0lg= ``` -------------------------------- ### Providing Code Examples in Memories Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/semgrep-multimodal/best-practices-for-memories.mdx Instead of using links, provide code examples directly within memories for Multimodal to reference. ```text Recommend a fix similar to this code: `Sample code...` ``` -------------------------------- ### Initialize and enter the virtual environment Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-contributing.mdx Sets up the development environment, installs dependencies like pytest, and enables editable mode for semgrep within the virtual environment. ```bash pipenv shell ``` -------------------------------- ### Install Packages in Semgrep Docker CI Job Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/snippets/semgrep-ci/packages-in-semgrep-docker.mdx Example of how to install bash, jq, and curl in a CI job using the Semgrep Docker image. This is recommended if your workflow depends on these packages. ```yaml job: container: semgrep/semgrep:latest runs-on: ubuntu-latest-16-core steps: - uses: actions/checkout@v6 - name: Install dependencies run: apk add bash jq curl - run: semgrep scan --json ... | jq ... ``` -------------------------------- ### Setup Development Environment and Build Semgrep Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Commands to set up the OCaml development environment and build Semgrep from source using VS Code. Ensure dune and ocamlmerlin are in your PATH. ```bash cd /path/to/semgrep eval $(opam env) dune --version # just checking dune is in your PATH ocamlmerlin -version # just checking ocamlmerlin is in your PATH code . ``` -------------------------------- ### Setup Semgrep Plugin in Claude Code Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/guardian.mdx Set up the Semgrep plugin in Claude Code after installation. This command finalizes the configuration for local Semgrep Guardian usage. ```bash /setup-semgrep-plugin ``` -------------------------------- ### Start Semgrep Network Broker with Configuration Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/semgrep-ci/network-broker.mdx Run this Docker command to start the Semgrep Network Broker, mounting your configuration file. Replace VERSION_NUMBER with the desired broker version. ```bash sudo docker run -d -it --rm -v $(pwd):/emt ghcr.io/semgrep/semgrep-network-broker:VERSION_NUMBER -c /emt/config.yaml ``` -------------------------------- ### Example GitLab CI Variables with Sample Values Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/kb/semgrep-ci/mr-comments-through-gitlab-runner.mdx This example demonstrates how to populate the required GitLab CI variables with sample values for testing or demonstration purposes. ```shell export GITLAB_CI='true' export CI_PROJECT_PATH="gitlab-org/gitlab-foss" export CI_MERGE_REQUEST_PROJECT_URL="https://example.com/gitlab-org/gitlab-foss" export CI_PROJECT_URL="$CI_MERGE_REQUEST_PROJECT_URL" export CI_COMMIT_SHA="1ecfd275763eff1d6b4844ea3168962458c9f27a" export CI_COMMIT_REF_NAME="main" export CI_MERGE_REQUEST_TARGET_BRANCH_NAME="main" export CI_JOB_URL="https://gitlab.com/gitlab-examples/ci-debug-trace/-/jobs/379424655" export CI_PIPELINE_SOURCE='merge_request_event' export CI_MERGE_REQUEST_IID="1" export CI_MERGE_REQUEST_DIFF_BASE_SHA="1ecfd275763eff1d6b4844ea6874447h694gh23d" export CI_MERGE_REQUEST_TITLE="Testing branches" ``` -------------------------------- ### Example .semgrepconfig.yml for Project Tags Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/semgrep-appsec-platform/tags.mdx Add tags to your repository by defining them in the `.semgrepconfig.yml` file. This method is recommended for consistent tag management. ```yaml tags: - favourite - awesomeproject ``` -------------------------------- ### Install pre-commit hooks Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/contributing-code.mdx Installs the pre-commit hooks for the Semgrep project. Ensure Docker is installed and running before executing this command. ```bash pre-commit install ``` -------------------------------- ### Mitigation Example for Open3.pipeline Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/cheat-sheets/ruby-command-injection.mdx Shows a safer way to construct commands for Open3.pipeline by passing arguments as an array instead of a single string. ```ruby Open3.pipeline(["bash", "-c", "myCommand myArg1 " + input_value]) # Use: Open3.pipeline(["/path/to/myCommand", "myArg1", input_value]) ``` -------------------------------- ### Install Semgrep Local Plugin in Claude Code Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/guardian.mdx Install the Semgrep local plugin from the marketplace in Claude Code. This command installs the plugin after the marketplace has been added. ```bash /plugin install semgrep@semgrep-marketplace ``` -------------------------------- ### Install Specific Semgrep Version with uv Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/kb/semgrep-code/run-specific-version.mdx Utilize uv tool install to pin and install a specific Semgrep version. This method is efficient for managing project dependencies. ```bash uv tool install semgrep==x.y.z ``` -------------------------------- ### Install Semgrep CLI with Homebrew, Pipx, or Uv Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/guardian.mdx Install the Semgrep CLI using your preferred package manager. Ensure you have Python 3.10 or later installed, as it's a runtime requirement. ```bash # install using Homebrew brew install semgrep ``` ```bash # or, install using pipx (https://pipx.pypa.io/stable/how-to-install-pipx/) pipx install semgrep ``` ```bash # or, install using uv (https://docs.astral.sh/uv/) uv tool install semgrep ``` -------------------------------- ### Install Semgrep CLI using pipx Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/for-developers/signin.mdx Install the Semgrep CLI tool using pipx, a recommended method for macOS, Linux, and Windows users. Ensure pipx is installed first. ```bash # macOS, Linux, Windows users - using pipx (recommended) # See https://pipx.pypa.io/stable/how-to-install-pipx/ to install pipx pipx install semgrep ``` -------------------------------- ### Install Semgrep CLI Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/extensions/semgrep-intellij.mdx Install the Semgrep command-line interface (CLI) which is required for the IntelliJ extension to function. Choose the installation method that best suits your operating system and package manager. ```bash # For macOS $ brew install semgrep ``` ```bash # For Ubuntu/Windows/Linux/macOS, using pipx (https://pipx.pypa.io/stable/how-to/install-pipx/) $ pipx install semgrep ``` ```bash # Or, using uv (https://docs.astral.sh/uv/) $ uv tool install semgrep ``` -------------------------------- ### Example Semgrep Rule Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/contributing-to-semgrep-rules-repository.mdx A basic example of a Semgrep rule definition in YAML format. ```yaml rules: - id: my-rule pattern: var $X = "..."; … ``` -------------------------------- ### Install Semgrep CLI with uv Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/getting-started/quickstart-ce.mdx Use this command to install the Semgrep CLI using uv, a fast Python package installer and virtual environment manager. This method is available on macOS, Linux, and Windows. ```bash uv tool install semgrep ``` -------------------------------- ### Go Code Example for Zip Reader Open Calls Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/writing-rules/pattern-syntax.mdx Illustrates matching `reader.Open(filename)` when `reader` is a `*zip.Reader`, and not matching `dir.Open(c.Param("file"))` where `dir` is `http.Dir`. ```go func read_file(reader *zip.Reader, filename) { // Matched reader.Open(filename) dir := http.Dir("/") // Not matched f, err := dir.Open(c.Param("file")) } ``` -------------------------------- ### Enter Semgrep CLI Virtual Environment Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Navigates into the CLI directory and activates the Python virtual environment using pipenv. Returns to the repository root afterwards. ```bash cd cli; pipenv shell cd .. # from within the virtual environment, return to the repo root ``` -------------------------------- ### Run Micro Benchmarks Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Navigate to the 'perf/perf-matching/' directory and run './run-perf-suite' to execute micro benchmarks. ```bash ./run-perf-suite ``` -------------------------------- ### Confirm Semgrep CLI installation Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/for-developers/signin.mdx Verify that the Semgrep CLI tool has been installed successfully by checking its version. ```bash semgrep --version ``` -------------------------------- ### Install semgrep globally Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-contributing.mdx Installs the semgrep binary globally using pipenv, allowing it to be run from any directory. ```bash pipenv install --dev ``` -------------------------------- ### Run Real Benchmarks Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx Navigate to the 'perf' directory and run './run-benchmarks' to execute real benchmarks against a suite of repositories. ```bash ./run-benchmarks ``` -------------------------------- ### GitLab CI Script Example Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/semgrep-ci/configuring-blocking-and-errors-in-ci.mdx Example of how to configure Semgrep CI command within a GitLab CI script. ```yaml script: semgrep ci --suppress-errors ``` -------------------------------- ### Running Semgrep Core with a Specific Rule Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/contributing-code.mdx This command demonstrates how to run the semgrep-core binary directly with a specific configuration file and target file. It shows the raw output from semgrep-core. ```bash semgrep-core -config unsafe-exec.yaml unsafe-exec.py -lang python ``` -------------------------------- ### Buildkite: Sample CI Configuration Snippet Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/snippets/semgrep-ci/sample-ci-configs.mdx This is a sample Buildkite configuration snippet for running Semgrep scans. It includes environment variables and outlines steps for both full scans and diff-aware scans for pull requests. ```yaml # This is a placeholder for the actual Buildkite YAML configuration. # The full configuration would typically include steps for different scan types # (e.g., full scan on push, diff-aware scan on PR) and necessary environment variables. # Example structure: # steps: # - label: ":semgrep: Semgrep Scan (Push)" # key: semgrep-push # commands: # - export SEMGREP_APP_TOKEN=$SEMGREP_APP_TOKEN # - semgrep ci # - label: ":semgrep: Semgrep Scan (PR)" # key: semgrep-pr # commands: # - export SEMGREP_APP_TOKEN=$SEMGREP_APP_TOKEN # - export SEMGREP_BASELINE_REF="origin/main" # - git fetch origin "+refs/heads/*:refs/remotes/origin/*" # - semgrep ci --diff ``` -------------------------------- ### Run semgrep CLI help command Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-contributing.mdx Executes the help command for the semgrep CLI from within the 'cli/' directory to verify the installation. ```bash pipenv run semgrep --help ``` -------------------------------- ### Run First Semgrep Scan Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/getting-started/quickstart.mdx Navigate to your project's root directory in the CLI and run the 'semgrep ci' command to perform your first scan. ```bash semgrep ci ``` -------------------------------- ### Docker Scan Command (Old) Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/release-notes/march-2023.mdx Example of how to run a Semgrep scan using the semgrep/semgrep Docker image with the previous custom entry point. ```bash docker run -v $(pwd):/src semgrep/semgrep scan ... ``` -------------------------------- ### Display Semgrep CLI Help Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/cli-reference.mdx Run this command to see the main help output for the Semgrep CLI, listing available commands. ```bash semgrep --help ``` -------------------------------- ### Example Usage: Match Dots Stmts Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/contributing/semgrep-core-contributing.mdx An example command demonstrating matching a specific Semgrep pattern against a Python file. ```bash sc -f tests/python/dots_stmts.sgrep tests/python/dots_stmts.py -lang python ``` -------------------------------- ### Install uv Package Manager Source: https://github.com/semgrep/semgrep-docs/blob/main/docs/kb/semgrep-ci/azure-self-hosted-ubuntu.mdx Installs the uv package manager using a curl script. Ensure this is run on your self-hosted runner. ```bash $ curl -LsSf https://astral.sh/uv/install.sh | sh ```