Skip to content

Commit b90814a

Browse files
author
Brian
committed
Add CodeQL Workflow for Code Security Analysis
Add CodeQL Workflow for Code Security Analysis This pull request introduces a CodeQL workflow to enhance the security analysis of our repository. CodeQL is a powerful static analysis tool that helps identify and mitigate security vulnerabilities in our codebase. By integrating this workflow into our GitHub Actions, we can proactively identify and address potential issues before they become security threats. We added a new CodeQL workflow file (.github/workflows/codeql.yml) that - Runs on every pull request (functionality to run on every push to main branches is included as a comment for convenience). - Runs daily. - Excludes queries with a high false positive rate or low-severity findings. - Does not display results for git submodules, focusing only on our own codebase. Testing: To validate the functionality of this workflow, we have run several test scans on the codebase and reviewed the results. The workflow successfully compiles the project, identifies issues, and provides actionable insights while reducing noise by excluding certain queries and third-party code. Deployment: Once this pull request is merged, the CodeQL workflow will be active and automatically run on every push and pull request to the main branch. To view the results of these code scans, please follow these steps: 1. Under the repository name, click on the Security tab. 2. In the left sidebar, click Code scanning alerts. Additional Information: - You can further customize the workflow to adapt to your specific needs by modifying the workflow file. - For more information on CodeQL and how to interpret its results, refer to the GitHub documentation and the CodeQL documentation (https://codeql.github.com/ and https://codeql.github.com/docs/). Signed-off-by: Brian <[email protected]>
1 parent e76dd6b commit b90814a

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
sudo apt-get update
4+
sudo apt-get install -y git cmake build-essential curl libcurl4-openssl-dev libssl-dev uuid-dev
5+
6+
mkdir cmake
7+
cd cmake
8+
cmake ..
9+
cmake --build .

.github/workflows/codeql.yml

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
# push:
16+
# branches: [ "main", "master" ]
17+
schedule:
18+
- cron: '0 0 * * *'
19+
pull_request:
20+
branches: '*'
21+
22+
jobs:
23+
analyze:
24+
name: Analyze
25+
# Runner size impacts CodeQL analysis time. To learn more, please see:
26+
# - https://gh.io/recommended-hardware-resources-for-running-codeql
27+
# - https://gh.io/supported-runners-and-hardware-resources
28+
# - https://gh.io/using-larger-runners
29+
# Consider using larger runners for possible analysis time improvements.
30+
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-20.04' }}
31+
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
32+
permissions:
33+
actions: read
34+
contents: read
35+
security-events: write
36+
37+
strategy:
38+
fail-fast: false
39+
matrix:
40+
language: [ 'cpp' ]
41+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift' ]
42+
# Use only 'java' to analyze code written in Java, Kotlin or both
43+
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
44+
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
45+
46+
steps:
47+
- name: Checkout repository
48+
uses: actions/checkout@v3
49+
with:
50+
submodules: recursive
51+
52+
# Initializes the CodeQL tools for scanning.
53+
- name: Initialize CodeQL
54+
uses: github/codeql-action/init@v2
55+
with:
56+
languages: ${{ matrix.language }}
57+
# If you wish to specify custom queries, you can do so here or in a config file.
58+
# By default, queries listed here will override any specified in a config file.
59+
# Prefix the list here with "+" to use these queries and those in the config file.
60+
61+
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
62+
# queries: security-extended,security-and-quality
63+
queries: security-and-quality
64+
65+
66+
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
67+
# If this step fails, then you should remove it and run the build manually (see below)
68+
#- name: Autobuild
69+
# uses: github/codeql-action/autobuild@v2
70+
71+
# ℹ️ Command-line programs to run using the OS shell.
72+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
73+
74+
# If the Autobuild fails above, remove it and uncomment the following three lines.
75+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
76+
77+
- run: |
78+
./.github/workflows/codeql-buildscript.sh
79+
80+
- name: Perform CodeQL Analysis
81+
uses: github/codeql-action/analyze@v2
82+
with:
83+
category: "/language:${{matrix.language}}"
84+
upload: false
85+
id: step1
86+
87+
# Filter out rules with low severity or high false positve rate
88+
# Also filter out warnings in third-party code
89+
- name: Filter out unwanted errors and warnings
90+
uses: advanced-security/filter-sarif@v1
91+
with:
92+
patterns: |
93+
-**:cpp/path-injection
94+
-**:cpp/world-writable-file-creation
95+
-**:cpp/poorly-documented-function
96+
-**:cpp/potentially-dangerous-function
97+
-**:cpp/use-of-goto
98+
-**:cpp/integer-multiplication-cast-to-long
99+
-**:cpp/comparison-with-wider-type
100+
-**:cpp/leap-year/*
101+
-**:cpp/ambiguously-signed-bit-field
102+
-**:cpp/suspicious-pointer-scaling
103+
-**:cpp/suspicious-pointer-scaling-void
104+
-**:cpp/unsigned-comparison-zero
105+
-**/cmake*/Modules/**
106+
input: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif
107+
output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif
108+
109+
- name: Upload CodeQL results to code scanning
110+
uses: github/codeql-action/upload-sarif@v2
111+
with:
112+
sarif_file: ${{ steps.step1.outputs.sarif-output }}
113+
category: "/language:${{matrix.language}}"
114+
115+
- name: Upload CodeQL results as an artifact
116+
if: success() || failure()
117+
uses: actions/upload-artifact@v3
118+
with:
119+
name: codeql-results
120+
path: ${{ steps.step1.outputs.sarif-output }}
121+
retention-days: 5
122+
123+
- name: Fail if an error is found
124+
run: |
125+
./.github/workflows/fail_on_error.py \
126+
${{ steps.step1.outputs.sarif-output }}/cpp.sarif

.github/workflows/fail_on_error.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
3+
import json
4+
import sys
5+
6+
# Return whether SARIF file contains error-level results
7+
def codeql_sarif_contain_error(filename):
8+
with open(filename, 'r') as f:
9+
s = json.load(f)
10+
11+
for run in s.get('runs', []):
12+
rules_metadata = run['tool']['driver']['rules']
13+
if not rules_metadata:
14+
rules_metadata = run['tool']['extensions'][0]['rules']
15+
16+
for res in run.get('results', []):
17+
if 'ruleIndex' in res:
18+
rule_index = res['ruleIndex']
19+
elif 'rule' in res and 'index' in res['rule']:
20+
rule_index = res['rule']['index']
21+
else:
22+
continue
23+
try:
24+
rule_level = rules_metadata[rule_index]['defaultConfiguration']['level']
25+
except IndexError as e:
26+
print(e, rule_index, len(rules_metadata))
27+
else:
28+
if rule_level == 'error':
29+
return True
30+
return False
31+
32+
if __name__ == "__main__":
33+
if codeql_sarif_contain_error(sys.argv[1]):
34+
sys.exit(1)

0 commit comments

Comments
 (0)