Code Monkey home page Code Monkey logo

filter-sarif's Introduction

filter-sarif

Takes a SARIF file and a list of inclusion and exclusion patterns as input and removes alerts from the SARIF file according to those patterns.

Example

The following example removes all alerts from all Java test files:

name: "Filter SARIF"
on:
  push:
    branches: [master]

jobs:
  analyze:
    name: Analyze
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        language: [ 'java' ]

    steps:
    - name: Checkout repository
      uses: actions/checkout@v4

    - name: Initialize CodeQL
      uses: github/codeql-action/init@v3
      with:
        languages: ${{ matrix.language }}

    - name: Autobuild
      uses: github/codeql-action/autobuild@v3

    - name: Perform CodeQL Analysis
      uses: github/codeql-action/analyze@v3
      with:
        category: "/language:${{matrix.language}}"
        output: sarif-results
        upload: failure-only

    - name: filter-sarif
      uses: advanced-security/filter-sarif@v1
      with:
        patterns: |
          +**/*.java
          -**/*Test*.java
        input: sarif-results/java.sarif
        output: sarif-results/java.sarif

    - name: Upload SARIF
      uses: github/codeql-action/upload-sarif@v3
      with:
        sarif_file: sarif-results/java.sarif

    - name: Upload loc as a Build Artifact
      uses: actions/upload-artifact@v4
      with:
        name: sarif-results
        path: sarif-results
        retention-days: 1

Note how we provided upload: failure-only and output: sarif-results to the analyze action. That way we can filter the SARIF with the filter-sarif action before uploading it via upload-sarif. Diagnostic output is still uploaded and visible on the tool status page if the run fails. Finally, we also attach the resulting SARIF file to the build, which is convenient for later inspection.

Patterns

Each pattern line is of the form:

[+/-]<file pattern>[:<rule pattern>]

for example:

-**/*Test*.java:**               # exclusion pattern: remove all alerts from all Java test files
-**/*Test*.java                  # ditto, short form of the line above
+**/*.java:java/sql-injection    # inclusion pattern: This line has precedence over the first two
                                 # and thus allows alerts of type "java/sql-injection"
**/*.java:java/sql-injection     # ditto, the "+" in inclusion patterns is optional
**                               # allow all alerts in all files (reverses all previous lines)

A minimal config to allow only files in the path myproject/ is:

-**/*                            # exclusion pattern: DENY ALL
myproject/**/*                   # inclusion pattern: allows alerts in the path 'myproject/'
  • The path separator character in patterns is always /, independent of the platform the code is running on and independent of the paths in the SARIF file.
  • * matches any character, except a path separator
  • ** matches any character and is only allowed between path separators, e.g. /**/file.txt, **/file.txt or **. NOT allowed: **.txt, /etc**
  • The rule pattern is optional. If omitted, it will apply to alerts of all types.
  • Subsequent lines override earlier ones. By default all alerts are included.
  • If you need to use the literals +, -, \ or : in your pattern, you can escape them with \, e.g. \-this/is/an/inclusion/file/pattern\:with-a-semicolon:and/a/rule/pattern/with/a/\\/backslash. For + and -, this is only necessary if they appear at the beginning of the pattern line.

filter-sarif's People

Contributors

aegilops avatar aibaars avatar felickz avatar geekmasher avatar henrymercer avatar way-dave avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

filter-sarif's Issues

The minimal example is invalid

Hi,

Using the minimal example in our workflow:

-**/*                            # exclusion pattern: DENY ALL
myproject/**                     # inclusion pattern: allows alerts in the path 'myproject/'

Produces the following error:

Run advanced-security/filter-sarif@v1
Run unset LD_PRELOAD
Given patterns:
files: **/*    # exclusion    rules:  DENY ALL (negative)
files: src/**   # inclusion    rules: ** (positive)
Traceback (most recent call last):
  File "/home/runner/work/_actions/advanced-security/filter-sarif/v1/filter_sarif.py", line 147, in <module>
    main()
  File "/home/runner/work/_actions/advanced-security/filter-sarif/v1/filter_sarif.py", line 144, in main
    filter_sarif(args)
  File "/home/runner/work/_actions/advanced-security/filter-sarif/v1/filter_sarif.py", line 99, in filter_sarif
    if uri is None or match_path_and_rule(uri, ruleId, args.patterns):
  File "/home/runner/work/_actions/advanced-security/filter-sarif/v1/filter_sarif.py", line 16, in match_path_and_rule
    if match(rp, rule) and match(fp, path):
  File "/home/runner/work/_actions/advanced-security/filter-sarif/v1/globber.py", line 79, in match
    raise ValueError('** in {} not alone between path separators'.format(pattern))
ValueError: ** in src/**   # inclusion not alone between path separators
Error: Process completed with exit code 1.

Should we fix this example and what is a valid way of including a single directory?

I am trying this for now:

-**/*
myproject/**/*

Thanks!

Tag HEAD of 'develop' with '1.0-filter-message'

Please can we tag the HEAD of the develop branch with 1.0-filter-message, or something similar?

I'd like to point users at this feature, before it's merged to main, but don't want to just use develop since that might change to include a different new feature (in theory!).

@zbazztian

v1 Tag Usage Not Working

When I attempt to use filter-sarif in my workflow with the v1 tag it fails with an error: TypeError: 'type' object is not subscriptable.

I did some investigation and this is due to the globber.py -> _match_components function due to the list[str] usage. I see in the develop branch this has been fixed, here, with the addition of the typing import and code change as well.

Are there any plans to create a new tag that works? For now I am just referencing the main branch's latest commit SHA.

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.