GitHub ActionsSecuritySARIFCI/CD··5 min read

Securing AI-Generated Code in GitHub Actions with SARIF and ESLint

Vaishnavi Sawant
Vaishnavi Sawant

Co-Creator, AI Guard

As more pull requests are authored or assisted by AI agents (such as Claude Code, GitHub Copilot Workspace, and Cursor), software teams face an unprecedented volume of code reviews. Human reviewers cannot catch every missing await or overlooked authorization check on a 500-line diff.

To prevent AI-introduced security risks from entering your main branch, your CI/CD pipeline must act as an automated gate.

In this guide, we will walk through setting up AI Guard in GitHub Actions with full SARIF 2.1.0 integration, posting inline PR annotations directly to developer reviews.


The Architecture: SARIF 2.1.0 + GitHub Code Scanning

SARIF (Static Analysis Results Interchange Format) is an OASIS standard for static analysis tools. GitHub has native, first-class support for SARIF through GitHub Code Scanning.

When your CI pipeline produces a SARIF file: 1. GitHub parses each rule result, line number, column, and severity level. 2. An inline annotation is placed directly on the PR diff, showing the error and the exact fix. 3. If an issue is critical, GitHub can block merge until the finding is resolved. 4. Historical findings are tracked across commits in the repository's Security tab.


Step-by-Step GitHub Actions Setup

Create a new file at .github/workflows/ai-guard.yml:

yaml
name: AI Guard Code Safety

on:
  pull_request:
    branches: [main, develop]
  push:
    branches: [main]

permissions:
  contents: read
  security-events: write # Required for SARIF upload

jobs:
  ai-guard-scan:
    name: AI Guard Verification
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # Required for differential commit checking

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: 'npm'

      - name: Install Dependencies
        run: npm ci

      - name: Run Differential PR Scan
        run: |
          npx ai-guard changed \
            --pr \
            --strict \
            --sarif \
            --sarif-output ai-guard-results.sarif \
            --fail-on errors

      - name: Upload Results to GitHub Code Scanning
        uses: github/codeql-action/upload-sarif@v3
        if: always() # Ensure results upload even if the previous step failed
        with:
          sarif_file: ai-guard-results.sarif
          category: ai-guard

Why Differential Scanning (`--pr`) is Critical

Running full static analysis across millions of lines of code on every pull request slows down CI and creates developer friction.

With ai-guard changed --pr, AI Guard inspects only the files modified between the pull request HEAD and the target base branch (e.g., origin/main). - A 200-file project scan completes in under 1.5 seconds. - Developers get instant feedback on their changes without waiting for heavy container builds.


Handling Legacy Codebases: The Baseline Strategy

If you are introducing AI Guard into an established codebase that already has pre-existing issues, you don't want your first PR scan to fail on hundreds of legacy warnings.

AI Guard solves this with baselines:

bash
# Run locally once to record existing findings:
npx ai-guard baseline --create

This creates .ai-guard-baseline.json. In CI, point AI Guard to the baseline:

bash
npx ai-guard run --baseline .ai-guard-baseline.json --fail-on errors

AI Guard will ignore pre-existing debt and fail only if a new PR introduces *new* security or async issues.


Summary

Automating deterministic checks in CI ensures your team can adopt AI tools with complete confidence. You gain the velocity of AI pair programming with the safety of continuous automated guardrails.

Protect your codebase from AI anti-patterns

Explore all 18 deterministic AST rules or install the ESLint plugin today.