Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Semgrep and CodeQL sections on testing in CI #57

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 30 additions & 0 deletions content/docs/static-analysis/codeql/10-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,36 @@ to ensure that it is correct and if it is, rename `MemcpyCall.actual` to
For more information about testing CodeQL queries, see the
[official documentation](https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/testing-custom-queries).

## Testing custom queries in CI

### GitHub Actions

The following workflow can be used to test custom CodeQL queries in GitHub Actions:

```yml
name: Test CodeQL queries

on: [push, pull_request]

jobs:
codeql-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: init
uses: github/codeql-action/init@v3
- uses: actions/cache@v4
with:
path: ~/.codeql
key: ${{ runner.os }}-${{ runner.arch }}-${{ steps.init.outputs.codeql-version }}
- name: Run tests
run: |
${{ steps.init.outputs.codeql-path }} test run ./path/to/query/tests/
```

This workflow also speeds up subsequent runs by caching query extraction and
compilation, and pack dependency installation.

## Editor support for CodeQL

The CodeQL CLI includes a server for the language-server protocol (LSP)
Expand Down
29 changes: 29 additions & 0 deletions content/docs/static-analysis/semgrep/10-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,35 @@ test file:
4. **Evaluate the rule against real-world code**: Test the rule against actual code from your projects,
open-source repositories, or other codebases to assess its effectiveness in real-life scenarios.

## Testing custom rules in CI

### GitHub Actions

The following workflow can be used to test custom Semgrep rules in GitHub Actions:

```yml
name: Test Semgrep rules

on: [push, pull_request]

jobs:
semgrep-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: "3.11"
cache: "pip"
- run: python -m pip install -r requirements.txt
- run: semgrep --test --test-ignore-todo ./path/to/rules/
```

Make sure to include `semgrep` in your `requirements.txt` (or [`poetry` or `pipenv` equivalents](https://github.com/actions/setup-python/blob/main/docs/advanced-usage.md#caching-packages))
file to speed up workflow runs by caching the dependency. Note, we include
`--test-ignore-todo` here so we do not fail CI runs on [TODO tests](https://semgrep.dev/docs/writing-rules/testing-rules),
which are a valuable form of documentation for future rule improvements.

## Autofix feature

The autofix feature can automatically correct identified vulnerabilities, potential errors, or coding standard violations.
Expand Down
Loading