|
| 1 | +--- |
| 2 | +title: DevOps with .NET and GitHub Actions - Secure code with CodeQL |
| 3 | +description: Add security scanning to your .NET code with GitHub Actions and CodeQL |
| 4 | +author: colindembovsky |
| 5 | +ms.date: 03/04/2021 |
| 6 | +--- |
| 7 | + |
| 8 | +# Secure .NET Code with CodeQL and GitHub Actions |
| 9 | + |
| 10 | +[CodeQL](https://codeql.github.com/docs/codeql-overview/about-codeql/) is a static code analysis engine that can automate security and quality checks. With CodeQL, you can perform *variant analysis*, which uses known vulnerabilities as seeds to find similar issues. CodeQL is part of [GitHub Advanced Security](https://docs.github.com/github/getting-started-with-github/about-github-advanced-security) that includes: |
| 11 | + |
| 12 | +> [!div class="checklist"] |
| 13 | +> |
| 14 | +> * Code scanning—find potential security vulnerabilities in your code. |
| 15 | +> * Secret scanning—detect secrets and tokens that are committed. |
| 16 | +> * Dependency scanning—detect vulnerabilities in packages that you consume. |
| 17 | +
|
| 18 | +CodeQL [supports some of the most popular programming languages and compilers](https://codeql.github.com/docs/codeql-overview/supported-languages-and-frameworks/): |
| 19 | + |
| 20 | +- C/C++ |
| 21 | +- Java |
| 22 | +- C# |
| 23 | +- Python |
| 24 | +- Go |
| 25 | +- JavaScript |
| 26 | +- TypeScript |
| 27 | + |
| 28 | +CodeQL is a powerful language and security professionals can create custom queries using CodeQL. However, teams can benefit immensely from the large open-source collection of queries that the security community has created without having to write any custom CodeQL. |
| 29 | + |
| 30 | +In this article, you'll set up a GitHub workflow that will scan code in your repository using CodeQL. You will: |
| 31 | + |
| 32 | +> [!div class="checklist"] |
| 33 | +> |
| 34 | +> * Create a code scanning action. |
| 35 | +> * Edit the workflow file to include custom scan settings. |
| 36 | +> * See scanning results. |
| 37 | +
|
| 38 | +> [!NOTE] |
| 39 | +> To see security alerts for your repository, you must be a repository owner. |
| 40 | +
|
| 41 | +## Create the code scanning workflow |
| 42 | + |
| 43 | +You can use a starter workflow for code scanning by navigating to the **Security** tab of your repository. |
| 44 | + |
| 45 | +1. Navigate to your GitHub repository and select the **Security** > **Code Scanning Alerts**. The top recommended workflow should be CodeQL Analysis. Select **Set up this workflow**. |
| 46 | + |
| 47 | +  |
| 48 | + |
| 49 | + **Figure 1:** Create a new code scanning workflow. |
| 50 | + |
| 51 | +1. A new workflow file is created in your *.github/workflows* folder. |
| 52 | +1. Select **Start Commit** on the upper right to save the default workflow. You can commit to the `main` branch. |
| 53 | + |
| 54 | +  |
| 55 | + |
| 56 | + **Figure 2:** Commit the file. |
| 57 | + |
| 58 | +1. Select the **Actions** tab. In the left-hand tree, you'll see a **CodeQL** node. Select this node to filter for CodeQL workflow runs. |
| 59 | + |
| 60 | +  |
| 61 | + |
| 62 | + **Figure 3:** View the CodeQL workflow runs. |
| 63 | + |
| 64 | +Take a look at the workflow file while it runs. If you remove the comments from the file, you'll see the following YAML: |
| 65 | + |
| 66 | +```yml |
| 67 | +name: "CodeQL" |
| 68 | + |
| 69 | +on: |
| 70 | + push: |
| 71 | + branches: [ main ] |
| 72 | + pull_request: |
| 73 | + branches: [ main ] |
| 74 | + schedule: |
| 75 | + - cron: '40 14 * * 6' |
| 76 | + |
| 77 | +jobs: |
| 78 | + analyze: |
| 79 | + name: Analyze |
| 80 | + runs-on: ubuntu-latest |
| 81 | + |
| 82 | + strategy: |
| 83 | + fail-fast: false |
| 84 | + matrix: |
| 85 | + language: [ 'csharp' ] |
| 86 | + |
| 87 | + steps: |
| 88 | + - name: Checkout repository |
| 89 | + uses: actions/checkout@v2 |
| 90 | + |
| 91 | + - name: Initialize CodeQL |
| 92 | + uses: github/codeql-action/init@v1 |
| 93 | + with: |
| 94 | + languages: ${{ matrix.language }} |
| 95 | + |
| 96 | + - name: Autobuild |
| 97 | + uses: github/codeql-action/autobuild@v1 |
| 98 | + |
| 99 | + - name: Perform CodeQL Analysis |
| 100 | + uses: github/codeql-action/analyze@v1 |
| 101 | +``` |
| 102 | +
|
| 103 | +Notice the following things: |
| 104 | +
|
| 105 | +1. The workflow `name` is `CodeQL`. |
| 106 | +1. This workflow triggers on `push` and `pull_request` events to the `main` branch. There's also a `cron` trigger. The `cron` trigger lets you define a schedule for triggering this workflow and is randomly generated for you. In this case, this workflow will run at 14:40 UTC every Saturday. |
| 107 | + |
| 108 | + > [!TIP] |
| 109 | + > If you edit the workflow file and hover over the cron expression, a tooltip will show you the English text for the cron expression. |
| 110 | + |
| 111 | +1. There's a single job called `analyze` that runs on the `ubuntu-latest` hosted agent. |
| 112 | +1. This workflow defines a `strategy` with a `matrix` on the array of `language`. In this case, there's only `csharp`. If the repository contained other languages, you could add them to this array. This causes the job to "fan out" and create an instance per value of the matrix. |
| 113 | +1. There are four steps, starting with `checkout`. |
| 114 | +1. The second step initializes the CodeQL scanner for the `language` this job is going to scan. CodeQL intercepts calls to the compiler to build a database of the code while the code is being built. |
| 115 | +1. The `Autobuild` step will attempt to automatically build the source code using common conventions. If this step fails, you can replace it with your own custom build steps. |
| 116 | +1. After building, the CodeQL analysis is performed, where suites of queries are run against the code database. |
| 117 | +1. The run should complete successfully. However, there appear to be no issues. |
| 118 | + |
| 119 | +  |
| 120 | + |
| 121 | + **Figure 4:** No results to the initial scan. |
| 122 | + |
| 123 | +## Customize CodeQL settings |
| 124 | + |
| 125 | +The CodeQL scan isn't reporting any security issues. That's expected with this basic sample. CodeQL can also scan for *quality* issues. The current workflow is using the default `security-extended` suite. You can add quality scanning in by adding a configuration file to customize the scanning suites. In this step, you'll configure CodeQL to use the `security-and-quality` suites. |
| 126 | + |
| 127 | +> [!INFORMATION] |
| 128 | +> For other CodeQL configuration options, see [Configuring CodeQL code scanning in your CI system](https://docs.github.com/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-codeql-code-scanning-in-your-ci-system). |
| 129 | + |
| 130 | +1. Navigate to the *.github* folder in the **Code** tab and select **Add File**: |
| 131 | + |
| 132 | +  |
| 133 | + |
| 134 | + **Figure 5:** Create a new file. |
| 135 | + |
| 136 | +1. Enter *codeql/codeql-config.yml* as the name. This creates the file in a folder. Paste in the following code: |
| 137 | + |
| 138 | + ```yml |
| 139 | + name: "Security and Quality" |
| 140 | +
|
| 141 | + queries: |
| 142 | + - uses: security-and-quality |
| 143 | + ``` |
| 144 | + |
| 145 | +  |
| 146 | + |
| 147 | + **Figure 6:** Create the CodeQL configuration file. |
| 148 | + |
| 149 | +1. Select **Commit to main** at bottom of the editor to commit the file. |
| 150 | +1. Edit the CodeQL workflow to use the new configuration file. Navigate to *.github/workflows/codeql-analysis.yml* and select the pencil icon. Add a new property to the `with` section as shown below: |
| 151 | + |
| 152 | + ```yml |
| 153 | + - name: Initialize CodeQL |
| 154 | + uses: github/codeql-action/init@v1 |
| 155 | + with: |
| 156 | + languages: ${{ matrix.language }} |
| 157 | + config-file: ./.github/codeql/codeql-config.yml # <-- add this line |
| 158 | + ``` |
| 159 | + |
| 160 | +1. Select **Start Commit** and commit to the `main` branch. |
| 161 | + |
| 162 | +## Review the security alerts |
| 163 | + |
| 164 | +> [!IMPORTANT] |
| 165 | +> You must be a repository owner to view security alerts. |
| 166 | +> |
| 167 | +> This sample repository is small. As such, it doesn't contain any major security or quality issues. However, "real world" repositories will likely have some issues. |
| 168 | + |
| 169 | +When the last CodeQL workflow run completes, you should see two issues in the **Security** tab: |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +**Figure 7:** View security alerts. |
| 174 | + |
| 175 | +1. Select the first alert to open it. |
| 176 | +1. In this case, the alert is for a generated file that isn't committed to the repository. For that reason, the preview is unavailable. |
| 177 | +1. Notice the tags that are applied. These tags can be used for filtering issues. |
| 178 | +1. Select **Show more** under the rule information to show help and recommendations. |
| 179 | + |
| 180 | +  |
| 181 | + |
| 182 | + **Figure 8:** Open an alert. |
| 183 | + |
| 184 | +1. Selecting **Dismiss** will open options for dismissing this issue: |
| 185 | + |
| 186 | +  |
| 187 | + |
| 188 | + **Figure 9:** Dismiss an alert. |
| 189 | + |
| 190 | +>[!div class="step-by-step"] |
| 191 | +>[Previous](actions-deploy.md) |
| 192 | +>[Next](monitoring.md) |
0 commit comments