Code Monkey home page Code Monkey logo

sample-semgrep-ci's Introduction

Github Action Example with Semgrep SAST

This repository contains an example workflow showcasing the integration of Semgrep, a powerful static analysis tool, within a GitHub Actions workflow for performing Static Application Security Testing (SAST).

Workflow Overview

The provided GitHub Actions workflow demonstrates how to:

  • Run Semgrep scan on your codebase.
  • Save the scan results in SARIF format.
  • Upload the SARIF file as an artifact.
  • Utilize the GitHub upload-sarif action to display scan findings in the GitHub Security tab.

Contents

  • .github/workflows/: Contains the workflow YAML file.
  • vulnerable-source-code/: Placeholder directory representing the codebase for scanning.
  • README.md: Instructions and overview.

Feel free to use this as a reference for integrating Semgrep scans into your CI/CD pipelines and enhancing your code security.

Manual Usage

semgrep scan -j 100 --config p/default --config ./custom-semgrep-rules/ src > out.txts

# with sarif format
semgrep scan -j 100 -q --sarif --config p/default --config ./custom-semgrep-rules/ src > semgrep-result.sarif

semgrep scan -j 100 -q --sarif --config p/default --config ./custom-semgrep-rules/ src > semgrep-result.sarif

Tips: Using SARIF Viewer in VSCode or sarif-tools to beautify the sarif format file

Github Action File

# Name of this GitHub Actions workflow.
name: Semgrep

on:
  # Scan changed files in PRs (diff-aware scanning):
  pull_request: {}
  # Scan on-demand through GitHub Actions interface:
  workflow_dispatch: {}
  # Scan mainline branches and report all findings:
  push:
    branches: ["master", "main"]

jobs:
  semgrep_scan:
    # User definable name of this GitHub Actions job.
    name: semgrep/ci
    # If you are self-hosting, change the following `runs-on` value:
    runs-on: ubuntu-latest
    container:
      # A Docker image with Semgrep installed. Do not change this.
      image: returntocorp/semgrep
    # Skip any PR created by dependabot to avoid permission issues:
    if: (github.actor != 'dependabot[bot]')
    permissions:
      # required for all workflows
      security-events: write
      # only required for workflows in private repositories
      actions: read
      contents: read

    steps:
      # Fetch project source with GitHub Actions Checkout.
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Perform Semgrep Analysis
      # @NOTE: This is the actual semgrep command to scan your code.
      # Modify the --config option to 'r/all' to scan using all rules,
      # or use multiple flags to specify particular rules, such as
      # --config r/all --config custom/rules
        run: semgrep scan -q --sarif --config auto ./vulnerable-source-code > semgrep-results.sarif

      # upload the results for the CodeQL GitHub app to annotate the code
      - name: Save SARIF results as artifact
        uses: actions/upload-artifact@v3
        with:
          name: semgrep-scan-results
          path: semgrep-results.sarif

      # Upload SARIF file generated in previous step
      - name: Upload SARIF result to the GitHub Security Dashboard
        uses: github/codeql-action/upload-sarif@v2
        with:
          sarif_file: semgrep-results.sarif
        if: always()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.