Skip to content

Commit 9f8c476

Browse files
authored
Updates to csharp
1 parent b500e49 commit 9f8c476

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

troubleshooting/codeql-builds/compiled-languages-csharp.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,52 @@ Start here: [CodeQL Docs - The build takes too long](https://docs.github.com/en
238238

239239

240240
## Optimization - Removing Code From Scans
241-
CodeQL will extract and analyze any code that is passed through the compiler. Consider excluding any code you do not wish to include in a security scan to speed up and remove noise from this process. This is commonly employed for unit tests, demo code, or code that would not benefit from being scanned (ex: DacPacs).
241+
Consider excluding any code you do not wish to include in a security scan to speed up and remove noise from this process. This is commonly employed for unit tests, demo code, or code that would not benefit from being scanned (ex: DacPacs).
242242

243-
With .NET we can employ a few mechanisms to remove code from CodeQL scans (e.g. you would want to run your unit test in another workflow ):
243+
### `build-mode: none`
244+
245+
[Build-mode none](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#codeql-build-modes) has added support for CodeQL [configuration paths filters](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning#specifying-directories-to-scan) for this compiled language. Since this mode only will recursively look for `.cs` files throughout the codebase to scan, we can be a bit more prescriptive in our config:
246+
247+
```yaml
248+
- uses: github/codeql-action/init@v3
249+
with:
250+
languages: ${{ matrix.language }}
251+
build-mode: none
252+
config: |
253+
paths-ignore:
254+
- **/*.tests/**
255+
- '**/*.test.cs'
256+
- '**/*.tests.cs'
257+
- '**/examples/**'
258+
- '**/samples/**'
259+
- '**/demo/**'
260+
- '**/docs/**'
261+
```
262+
263+
Tip: ensure credentials to your private registries listed in your `nuget.config` are available/injected so that `none` mode does not attempt to hit a registry that will fail for every dependency.
264+
265+
Alternatively, you might consider breaking up code into smaller chunks to scan. In a monorepo with many microservices, it might make sense to only scan dependent code together. CodeQL has natural boundaries at the network layer - if a direct method call is not invoked then there is little value in scanning the code together. Consider specifying a folder to scan (vs ignore)
266+
267+
```yaml
268+
- uses: github/codeql-action/init@v3
269+
with:
270+
languages: ${{ matrix.language }}
271+
build-mode: none
272+
config: |
273+
paths-ignore:
274+
- '**/MicroserviceA/**'
275+
- '**/Framework/**'
276+
277+
# If scanning more than one analysis per repo - ensure you upload results with a unique category
278+
- name: Perform CodeQL Analysis
279+
uses: github/codeql-action/analyze@v3
280+
with:
281+
category: "/language:${{matrix.language}}/MicroserviceA"
282+
```
283+
284+
### `build-mode: autobuild` or `build-mode: manual`
285+
286+
CodeQL will extract and analyze any code that is passed through the compiler. With .NET builds, we can employ a few mechanisms to exclude code from being captured by the CodeQL Csharp tracer/extractor (e.g. you would want to run your unit test in another workflow ):
244287
- A [solution filter](https://docs.microsoft.com/en-us/visualstudio/msbuild/solution-filters?view=vs-2019) to only build required projects
245288
- An explicit [solution file that excludes projects](https://docs.microsoft.com/en-us/visualstudio/ide/how-to-exclude-projects-from-a-build?view=vs-2022)
246289
- example from the Open Source project: [Identity Server](https://github.com/DuendeSoftware/IdentityServer/)
@@ -250,6 +293,7 @@ With .NET we can employ a few mechanisms to remove code from CodeQL scans (e.g.
250293
- Build in release mode - exclude test projects from that [build configuration](https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/ide/how-to-create-and-edit-configurations?view=vs-2015&redirectedfrom=MSDN#to-modify-a-solution-wide-build-configuration)
251294

252295
## Optimizations - CodeQL Engine
296+
- NOTE: [as of CodeQL 2.15.3 - this is now disabled by default.](https://codeql.github.com/docs/codeql-overview/codeql-changelog/codeql-cli-2.15.3/#c)
253297
- CodeQL will (by default) pull in source code from your dependencies using CIL extraction to assist in mapping out your data flows. While this can improve the precision of the results, this can also lead to a large increase in database size. You might consider disabling this feature for a quick scan but running a cron based scan with the option enabled.
254298

255299
### GitHub Actions

0 commit comments

Comments
 (0)