|
| 1 | +name: "ML-powered queries" |
| 2 | +description: "Tests that ML-powered queries are run with the security-extended suite and that they produce alerts on a test DB" |
| 3 | +versions: [ |
| 4 | + # Latest release in 2.7.x series |
| 5 | + "stable-20220120", |
| 6 | + "cached", |
| 7 | + "latest", |
| 8 | + "nightly-latest", |
| 9 | + ] |
| 10 | +# Test on all three platforms since ML-powered queries use native code |
| 11 | +os: ["ubuntu-latest", "macos-latest", "windows-latest"] |
| 12 | +steps: |
| 13 | + - uses: ./../action/init |
| 14 | + with: |
| 15 | + languages: javascript |
| 16 | + queries: security-extended |
| 17 | + source-root: ./../action/tests/ml-powered-queries-repo |
| 18 | + tools: ${{ steps.prepare-test.outputs.tools-url }} |
| 19 | + |
| 20 | + - uses: ./../action/analyze |
| 21 | + with: |
| 22 | + output: "${{ runner.temp }}/results" |
| 23 | + upload-database: false |
| 24 | + env: |
| 25 | + TEST_MODE: true |
| 26 | + |
| 27 | + - name: Upload SARIF |
| 28 | + uses: actions/upload-artifact@v2 |
| 29 | + with: |
| 30 | + name: ml-powered-queries-${{ matrix.os }}-${{ matrix.version }}.sarif.json |
| 31 | + path: "${{ runner.temp }}/results/javascript.sarif" |
| 32 | + retention-days: 7 |
| 33 | + |
| 34 | + - name: Check results |
| 35 | + env: |
| 36 | + IS_WINDOWS: ${{ matrix.os == 'windows-latest' }} |
| 37 | + shell: bash |
| 38 | + run: | |
| 39 | + cd "$RUNNER_TEMP/results" |
| 40 | + # We should run at least the ML-powered queries in `expected_rules`. |
| 41 | + expected_rules="js/ml-powered/nosql-injection js/ml-powered/path-injection js/ml-powered/sql-injection js/ml-powered/xss" |
| 42 | +
|
| 43 | + for rule in ${expected_rules}; do |
| 44 | + found_rule=$(jq --arg rule "${rule}" '[.runs[0].tool.extensions[].rules | select(. != null) | |
| 45 | + flatten | .[].id] | any(. == $rule)' javascript.sarif) |
| 46 | + echo "Did find rule '${rule}': ${found_rule}" |
| 47 | + if [[ "${found_rule}" != "true" && "${IS_WINDOWS}" != "true" ]]; then |
| 48 | + echo "Expected SARIF output to contain rule '${rule}', but found no such rule." |
| 49 | + exit 1 |
| 50 | + elif [[ "${found_rule}" == "true" && "${IS_WINDOWS}" == "true" ]]; then |
| 51 | + echo "Found rule '${rule}' in the SARIF output which shouldn't have been part of the analysis." |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | + done |
| 55 | +
|
| 56 | + # We should have at least one alert from an ML-powered query. |
| 57 | + num_alerts=$(jq '[.runs[0].results[] | |
| 58 | + select(.properties.score != null and (.rule.id | startswith("js/ml-powered/")))] | length' \ |
| 59 | + javascript.sarif) |
| 60 | + echo "Found ${num_alerts} alerts from ML-powered queries."; |
| 61 | + if [[ "${num_alerts}" -eq 0 && "${IS_WINDOWS}" != "true" ]]; then |
| 62 | + echo "Expected to find at least one alert from an ML-powered query but found ${num_alerts}." |
| 63 | + exit 1 |
| 64 | + elif [[ "${num_alerts}" -ne 0 && "${IS_WINDOWS}" == "true" ]]; then |
| 65 | + echo "Expected not to find any alerts from an ML-powered query but found ${num_alerts}." |
| 66 | + exit 1 |
| 67 | + fi |
0 commit comments