Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add the check-sarif action
Allows us to analyze and then check that certain queries were included
in the analysis and others were not.
  • Loading branch information
aeisenberg committed Jun 14, 2022
commit bcb7fad5b36d248e14d8a4dc349801927ad78ca9
20 changes: 20 additions & 0 deletions .github/check-sarif/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Check SARIF

Check failure

Code scanning / CodeQL

Inconsistent action input

Action [.github/check-sarif](1) and action [.github/query-filter-test](2) both declare input sarif-file, however their definitions are not identical. This may be confusing to users.
description: Checks a SARIF file to see if certain queries were run and others were not run.
inputs:
sarif-file:
required: true
description: The sarif file to check

queries-run:
required: true
description: |
Comma separated list of query ids that should be included in this SARIF file.

queries-not-run:
Comment thread
henrymercer marked this conversation as resolved.
required: true
description: |
Comma separated list of query ids that should NOT be included in this SARIF file.

runs:
using: node12
Comment thread
henrymercer marked this conversation as resolved.
main: index.js
52 changes: 52 additions & 0 deletions .github/check-sarif/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'
Comment thread
henrymercer marked this conversation as resolved.

const core = require('@actions/core');
const fs = require('fs')

const sarif = JSON.parse(fs.readFileSync(core.getInput('sarif-file'), 'utf8'))
const rules = sarif.runs[0].tool.extensions.flatMap(ext => ext.rules || [])

// Expected Queries
Comment thread
henrymercer marked this conversation as resolved.
Outdated
const expectedQueriesRun = getInput('queries-run')
const queriesThatShouldHaveRunButDidnt = expectedQueriesRun.reduce((acc, queryId) => {
if (!rules.some(rule => rule.id === queryId)) {
acc.push(queryId)
}
return acc
}, []);
Comment thread
henrymercer marked this conversation as resolved.
Outdated

if (queriesThatShouldHaveRunButDidnt.length > 0) {
core.setFailed(`The following queries were expected to run but did not: ${queriesThatShouldHaveRunButDidnt.join(', ')}`)
}

// Unexpected Queries
Comment thread
henrymercer marked this conversation as resolved.
Outdated
const expectedQueriesNotRun = getInput('queries-not-run')

const queriesThatShouldNotHaveRunButDid = expectedQueriesNotRun.reduce((acc, queryId) => {
if (rules.some(rule => rule.id === queryId)) {
acc.push(queryId)
}
return acc
}, []);
Comment thread
henrymercer marked this conversation as resolved.
Outdated

if (queriesThatShouldNotHaveRunButDid.length > 0) {
core.setFailed(`The following queries were NOT expected to have run but did: ${queriesThatShouldNotHaveRunButDid.join(', ')}`)
}


core.startGroup('All queries run')
rules.forEach(rule => {
core.info(`${rule.id}: ${(rule.properties && rule.properties.name) || rule.name}`)
})
core.endGroup()

core.startGroup('Full SARIF')
core.info(JSON.stringify(sarif, null, 2))
core.endGroup()

function getInput(name) {
Comment thread
henrymercer marked this conversation as resolved.
Outdated
return core.getInput(name)
.split(',')
.map(q => q.trim())
.filter(q => q.length > 0)
}
48 changes: 48 additions & 0 deletions .github/workflows/expected-queries-runs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Expected queries runs
Comment thread
aeisenberg marked this conversation as resolved.
Outdated
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

on:
push:
branches:
- main
- releases/v1
- releases/v2
pull_request:
types:
- opened
- synchronize
- reopened
- ready_for_review
workflow_dispatch: {}

jobs:
expected-queries:
timeout-minutes: 45
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
- name: Prepare test
id: prepare-test
uses: ./.github/prepare-test
with:
version: latest
- uses: ./../action/init
with:
languages: javascript
tools: ${{ steps.prepare-test.outputs.tools-url }}
- uses: ./../action/analyze
with:
output: ${{ runner.temp }}/results
upload-database: false
upload: false
env:
TEST_MODE: true

- name: Check Sarif
uses: ./../action/.github/check-sarif
with:
sarif-file: ${{ runner.temp }}/results/javascript.sarif
Comment thread
aeisenberg marked this conversation as resolved.
Outdated
queries-run: js/incomplete-hostname-regexp,js/path-injection
queries-not-run: foo,bar