Skip to content

Commit 338ff2d

Browse files
authored
Merge pull request #2351 from IVOES/master
Add CodeQL Workflow for Code Security Analysis
2 parents 0877a48 + 66b9bd5 commit 338ff2d

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
FAMILY=stm32l4
4+
python3 tools/get_deps.py $FAMILY
5+
python3 tools/build_make.py $FAMILY

.github/workflows/codeql.yml

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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: [ 'master' ]
17+
paths:
18+
- 'src/**'
19+
- 'examples/**'
20+
- 'lib/**'
21+
- 'hw/**'
22+
- '.github/workflows/codeql.yml'
23+
pull_request:
24+
branches: [ 'master' ]
25+
paths:
26+
- 'src/**'
27+
- 'examples/**'
28+
- 'lib/**'
29+
- 'hw/**'
30+
- '.github/workflows/codeql.yml'
31+
schedule:
32+
- cron: '0 0 * * *'
33+
34+
jobs:
35+
analyze:
36+
name: Analyze
37+
# Runner size impacts CodeQL analysis time. To learn more, please see:
38+
# - https://gh.io/recommended-hardware-resources-for-running-codeql
39+
# - https://gh.io/supported-runners-and-hardware-resources
40+
# - https://gh.io/using-larger-runners
41+
# Consider using larger runners for possible analysis time improvements.
42+
runs-on: ubuntu-latest
43+
timeout-minutes: 360
44+
permissions:
45+
actions: read
46+
contents: read
47+
security-events: write
48+
49+
strategy:
50+
fail-fast: false
51+
matrix:
52+
language: [ 'c-cpp' ]
53+
# CodeQL supports [ 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' ]
54+
# Use only 'java-kotlin' to analyze code written in Java, Kotlin or both
55+
# Use only 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
56+
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
57+
58+
steps:
59+
- name: Checkout repository
60+
uses: actions/checkout@v3
61+
62+
- name: Install ARM GCC
63+
uses: carlosperate/arm-none-eabi-gcc-action@v1
64+
with:
65+
release: '11.2-2022.02'
66+
67+
# Initializes the CodeQL tools for scanning.
68+
- name: Initialize CodeQL
69+
uses: github/codeql-action/init@v2
70+
with:
71+
languages: ${{ matrix.language }}
72+
# If you wish to specify custom queries, you can do so here or in a config file.
73+
# By default, queries listed here will override any specified in a config file.
74+
# Prefix the list here with "+" to use these queries and those in the config file.
75+
76+
# 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
77+
# queries: security-extended,security-and-quality
78+
queries: security-and-quality
79+
80+
81+
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
82+
# If this step fails, then you should remove it and run the build manually (see below)
83+
#- name: Autobuild
84+
# uses: github/codeql-action/autobuild@v2
85+
86+
# ℹ️ Command-line programs to run using the OS shell.
87+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
88+
89+
# If the Autobuild fails above, remove it and uncomment the following three lines.
90+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
91+
92+
- run: |
93+
./.github/workflows/codeql-buildscript.sh
94+
95+
- name: Perform CodeQL Analysis
96+
uses: github/codeql-action/analyze@v2
97+
with:
98+
category: "/language:${{matrix.language}}"
99+
upload: false
100+
id: step1
101+
102+
# Filter out rules with low severity or high false positive rate
103+
# Also filter out warnings in third-party code
104+
- name: Filter out unwanted errors and warnings
105+
uses: advanced-security/filter-sarif@v1
106+
with:
107+
patterns: |
108+
-**:cpp/path-injection
109+
-**:cpp/world-writable-file-creation
110+
-**:cpp/poorly-documented-function
111+
-**:cpp/potentially-dangerous-function
112+
-**:cpp/use-of-goto
113+
-**:cpp/integer-multiplication-cast-to-long
114+
-**:cpp/comparison-with-wider-type
115+
-**:cpp/leap-year/*
116+
-**:cpp/ambiguously-signed-bit-field
117+
-**:cpp/suspicious-pointer-scaling
118+
-**:cpp/suspicious-pointer-scaling-void
119+
-**:cpp/unsigned-comparison-zero
120+
-**/third*party/**
121+
-**/3rd*party/**
122+
-**/external/**
123+
input: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif
124+
output: ${{ steps.step1.outputs.sarif-output }}/cpp.sarif
125+
126+
- name: Upload SARIF
127+
uses: github/codeql-action/upload-sarif@v2
128+
with:
129+
sarif_file: ${{ steps.step1.outputs.sarif-output }}
130+
category: "/language:${{matrix.language}}"
131+
132+
- name: Archive CodeQL results
133+
uses: actions/upload-artifact@v3
134+
with:
135+
name: codeql-results
136+
path: ${{ steps.step1.outputs.sarif-output }}
137+
retention-days: 5

.github/workflows/fail_on_error.py

Lines changed: 34 additions & 0 deletions
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)