From df5994c3ed66c3defcebb556310e1c0760e1c580 Mon Sep 17 00:00:00 2001 From: Caetano Melone Date: Fri, 22 Aug 2025 10:04:50 -0700 Subject: [PATCH 1/6] Static security CI check The Bandit Python security tool will be run for each PR, and any results will be uploaded to GitHub's security dashboard for this repository. --- .github/workflows/ci.yml | 12 +++++++ .github/workflows/requirements/security.txt | 12 +++++++ .github/workflows/security.yml | 37 +++++++++++++++++++++ src/hubcast/clients/github/auth.py | 2 +- 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/requirements/security.txt create mode 100644 .github/workflows/security.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e207f2ce..18e4640c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,7 @@ jobs: pull-requests: read outputs: style: ${{ steps.filter.outputs.style }} + security: ${{ steps.filter.outputs.security }} unit-tests: ${{ steps.filter.outputs.unit-tests }} steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # @v2 @@ -34,6 +35,11 @@ jobs: - '.github/**/*' - 'src/**/*' - 'pyproject.toml' + security: + - '.github/**/*' + - 'src/**/*' + - 'pyproject.toml' + - '.bandit' # unit-tests: # - '.github/**/*' # - 'src/**/*' @@ -45,6 +51,11 @@ jobs: needs: changes uses: ./.github/workflows/style.yml + security: + if: ${{ needs.changes.outputs.security == 'true' }} + needs: [changes, style] + uses: ./.github/workflows/security.yml + # unit-tests: # if: ${{ needs.changes.outputs.unit-tests == 'true' }} # needs: [changes, style] @@ -59,6 +70,7 @@ jobs: needs: - changes - style + - security # - unit-tests # - coverage if: always() diff --git a/.github/workflows/requirements/security.txt b/.github/workflows/requirements/security.txt new file mode 100644 index 00000000..76f512ac --- /dev/null +++ b/.github/workflows/requirements/security.txt @@ -0,0 +1,12 @@ +bandit==1.8.6 +jschema-to-python==1.2.3 +jsonpickle==4.1.1 +markdown-it-py==4.0.0 +mdurl==0.1.2 +pbr==7.0.1 +Pygments==2.19.2 +PyYAML==6.0.2 +rich==14.1.0 +sarif-om==1.0.4 +setuptools==80.9.0 +stevedore==5.4.1 diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml new file mode 100644 index 00000000..991438a7 --- /dev/null +++ b/.github/workflows/security.yml @@ -0,0 +1,37 @@ +name: Security Checks +on: + # This Workflow can be triggered manually + workflow_dispatch: + workflow_call: + + +jobs: + security: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 + + # https://bandit.readthedocs.io/en/latest/faq.html#under-which-version-of-python-should-i-install-bandit + - name: Set up Python 3.10 + uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 + with: + python-version: '3.10' + cache: 'pip' + cache-dependency-path: '.github/workflows/requirements/security.txt' + + - name: Install Python dependencies + run: | + pip install -r .github/workflows/requirements/security.txt + + - name: Run Bandit + run: | + # bandit should not exit before the results are uploaded to github + bandit -r src -f sarif -o results.sarif || true + + # upload security results to github; a bot will add inline comments to the PR if any issues are found + # https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/uploading-a-sarif-file-to-github + - name: Upload SARIF file + uses: github/codeql-action/upload-sarif@v3 + with: + sarif_file: results.sarif + category: bandit diff --git a/src/hubcast/clients/github/auth.py b/src/hubcast/clients/github/auth.py index 3643885e..dab59c71 100644 --- a/src/hubcast/clients/github/auth.py +++ b/src/hubcast/clients/github/auth.py @@ -6,7 +6,7 @@ from gidgethub import aiohttp as gh_aiohttp #: location for authenticated app to get a token for one of its installations -INSTALLATION_TOKEN_URL = "app/installations/{installation_id}/access_tokens" +INSTALLATION_TOKEN_URL = "app/installations/{installation_id}/access_tokens" # nosec B105 class TokenCache: From e9e53e06782ace07152684861e66114b9696c3db Mon Sep 17 00:00:00 2001 From: Caetano Melone Date: Tue, 26 Aug 2025 14:07:42 -0700 Subject: [PATCH 2/6] Address Alec's comments - minimize requirements.txt for the security check - use always() instead of || true to ensure the sarif file is uploaded - add explanation to ignore check line Co-authored-by: Caetano Melone Co-authored-by: Alec Scott --- .github/workflows/requirements/security.txt | 13 +------------ .github/workflows/security.yml | 4 ++-- src/hubcast/clients/github/auth.py | 3 ++- 3 files changed, 5 insertions(+), 15 deletions(-) diff --git a/.github/workflows/requirements/security.txt b/.github/workflows/requirements/security.txt index 76f512ac..47765662 100644 --- a/.github/workflows/requirements/security.txt +++ b/.github/workflows/requirements/security.txt @@ -1,12 +1 @@ -bandit==1.8.6 -jschema-to-python==1.2.3 -jsonpickle==4.1.1 -markdown-it-py==4.0.0 -mdurl==0.1.2 -pbr==7.0.1 -Pygments==2.19.2 -PyYAML==6.0.2 -rich==14.1.0 -sarif-om==1.0.4 -setuptools==80.9.0 -stevedore==5.4.1 +bandit[sarif]==1.8.6 diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 991438a7..59374165 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -25,12 +25,12 @@ jobs: - name: Run Bandit run: | - # bandit should not exit before the results are uploaded to github - bandit -r src -f sarif -o results.sarif || true + bandit -r src -f sarif -o results.sarif # upload security results to github; a bot will add inline comments to the PR if any issues are found # https://docs.github.com/en/code-security/code-scanning/integrating-with-code-scanning/uploading-a-sarif-file-to-github - name: Upload SARIF file + if: always() uses: github/codeql-action/upload-sarif@v3 with: sarif_file: results.sarif diff --git a/src/hubcast/clients/github/auth.py b/src/hubcast/clients/github/auth.py index dab59c71..4157fc13 100644 --- a/src/hubcast/clients/github/auth.py +++ b/src/hubcast/clients/github/auth.py @@ -5,7 +5,8 @@ import gidgethub.apps as gha from gidgethub import aiohttp as gh_aiohttp -#: location for authenticated app to get a token for one of its installations +# location for authenticated app to get a token for one of its installations +# bandit thinks this is a hardcoded password, we ignore security checks on this line INSTALLATION_TOKEN_URL = "app/installations/{installation_id}/access_tokens" # nosec B105 From 383e8c6c0c5cb6d58fdb0a7a84d456465b8164bb Mon Sep 17 00:00:00 2001 From: Caetano Melone Date: Tue, 26 Aug 2025 15:22:11 -0700 Subject: [PATCH 3/6] add advanced CodeQL config to only scan PRs when certain paths are changed --- .github/workflows/codeql.yml | 105 +++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 00000000..aa943c5c --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,105 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL Advanced" + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + # trigger codeql on a PR if these files are changed + paths: + - '.github/**/*' + - 'src/**/*' + - 'pyproject.toml' + schedule: + - cron: '42 10 * * 5' + +jobs: + analyze: + name: Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} + permissions: + # required for all workflows + security-events: write + + # required to fetch internal or private CodeQL packs + packages: read + + # only required for workflows in private repositories + actions: read + contents: read + + strategy: + fail-fast: false + matrix: + include: + - language: actions + build-mode: none + - language: python + build-mode: none + # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' + # Use `c-cpp` to analyze code written in C, C++ or both + # Use 'java-kotlin' to analyze code written in Java, Kotlin or both + # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both + # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, + # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. + # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how + # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Add any setup steps before running the `github/codeql-action/init` action. + # This includes steps like installing compilers or runtimes (`actions/setup-node` + # or others). This is typically only required for manual builds. + # - name: Setup runtime (example) + # uses: actions/setup-example@v1 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # 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 + # queries: security-extended,security-and-quality + + # If the analyze step fails for one of the languages you are analyzing with + # "We were unable to automatically build your code", modify the matrix above + # to set the build mode to "manual" for that language. Then modify this step + # to build your code. + # â„šī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + - if: matrix.build-mode == 'manual' + shell: bash + run: | + echo 'If you are using a "manual" build mode for one or more of the' \ + 'languages you are analyzing, replace this with the commands to build' \ + 'your code, for example:' + echo ' make bootstrap' + echo ' make release' + exit 1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 + with: + category: "/language:${{matrix.language}}" From 4806640cd3a125a62914cb4efeef99b8a2e1993d Mon Sep 17 00:00:00 2001 From: Caetano Melone Date: Tue, 26 Aug 2025 15:26:59 -0700 Subject: [PATCH 4/6] add scoped permissions to security workflow and rm workflow_dispatch: trigger Co-authored-by: Caetano Melone Co-authored-by: Alec Scott --- .github/workflows/security.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 59374165..22db0a98 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -1,9 +1,9 @@ name: Security Checks on: - # This Workflow can be triggered manually - workflow_dispatch: workflow_call: +permissions: + contents: read jobs: security: From 24852a99efefa729f1bcda14c2aeaf75504b2e41 Mon Sep 17 00:00:00 2001 From: Caetano Melone Date: Tue, 26 Aug 2025 16:08:27 -0700 Subject: [PATCH 5/6] consolidate codeql.yml into existing security workflow --- .github/workflows/ci.yml | 3 + .github/workflows/codeql.yml | 105 --------------------------------- .github/workflows/security.yml | 44 ++++++++++++-- 3 files changed, 43 insertions(+), 109 deletions(-) delete mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 18e4640c..70809785 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,6 +55,9 @@ jobs: if: ${{ needs.changes.outputs.security == 'true' }} needs: [changes, style] uses: ./.github/workflows/security.yml + permissions: + contents: read + security-events: write # unit-tests: # if: ${{ needs.changes.outputs.unit-tests == 'true' }} diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index aa943c5c..00000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,105 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL Advanced" - -on: - push: - branches: [ "main" ] - pull_request: - branches: [ "main" ] - # trigger codeql on a PR if these files are changed - paths: - - '.github/**/*' - - 'src/**/*' - - 'pyproject.toml' - schedule: - - cron: '42 10 * * 5' - -jobs: - analyze: - name: Analyze (${{ matrix.language }}) - # Runner size impacts CodeQL analysis time. To learn more, please see: - # - https://gh.io/recommended-hardware-resources-for-running-codeql - # - https://gh.io/supported-runners-and-hardware-resources - # - https://gh.io/using-larger-runners (GitHub.com only) - # Consider using larger runners or machines with greater resources for possible analysis time improvements. - runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} - permissions: - # required for all workflows - security-events: write - - # required to fetch internal or private CodeQL packs - packages: read - - # only required for workflows in private repositories - actions: read - contents: read - - strategy: - fail-fast: false - matrix: - include: - - language: actions - build-mode: none - - language: python - build-mode: none - # CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'rust', 'swift' - # Use `c-cpp` to analyze code written in C, C++ or both - # Use 'java-kotlin' to analyze code written in Java, Kotlin or both - # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both - # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, - # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. - # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how - # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - # Add any setup steps before running the `github/codeql-action/init` action. - # This includes steps like installing compilers or runtimes (`actions/setup-node` - # or others). This is typically only required for manual builds. - # - name: Setup runtime (example) - # uses: actions/setup-example@v1 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v3 - with: - languages: ${{ matrix.language }} - build-mode: ${{ matrix.build-mode }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - - # 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 - # queries: security-extended,security-and-quality - - # If the analyze step fails for one of the languages you are analyzing with - # "We were unable to automatically build your code", modify the matrix above - # to set the build mode to "manual" for that language. Then modify this step - # to build your code. - # â„šī¸ Command-line programs to run using the OS shell. - # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - if: matrix.build-mode == 'manual' - shell: bash - run: | - echo 'If you are using a "manual" build mode for one or more of the' \ - 'languages you are analyzing, replace this with the commands to build' \ - 'your code, for example:' - echo ' make bootstrap' - echo ' make release' - exit 1 - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3 - with: - category: "/language:${{matrix.language}}" diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 22db0a98..9381e72e 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -2,11 +2,8 @@ name: Security Checks on: workflow_call: -permissions: - contents: read - jobs: - security: + bandit-scan: runs-on: ubuntu-latest steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 @@ -35,3 +32,42 @@ jobs: with: sarif_file: results.sarif category: bandit + + codeql-analyze: + name: CodeQL Analyze (${{ matrix.language }}) + # Runner size impacts CodeQL analysis time. To learn more, please see: + # - https://gh.io/recommended-hardware-resources-for-running-codeql + # - https://gh.io/supported-runners-and-hardware-resources + # - https://gh.io/using-larger-runners (GitHub.com only) + # Consider using larger runners or machines with greater resources for possible analysis time improvements. + runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} + + strategy: + fail-fast: false + matrix: + include: + - language: actions + build-mode: none + - language: python + build-mode: none + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v3 + with: + languages: ${{ matrix.language }} + build-mode: ${{ matrix.build-mode }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # 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 + # queries: security-extended,security-and-quality + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v3 + with: + category: "/language:${{matrix.language}}" From 801f358da8f69650fc5ec0eceeb08312a1754f52 Mon Sep 17 00:00:00 2001 From: Caetano Melone Date: Tue, 26 Aug 2025 16:24:56 -0700 Subject: [PATCH 6/6] explicitly define permissions within job --- .github/workflows/security.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index 9381e72e..132e6bcd 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -2,9 +2,12 @@ name: Security Checks on: workflow_call: + jobs: bandit-scan: runs-on: ubuntu-latest + permissions: + contents: read steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 @@ -41,6 +44,9 @@ jobs: # - https://gh.io/using-larger-runners (GitHub.com only) # Consider using larger runners or machines with greater resources for possible analysis time improvements. runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} + permissions: + contents: read + security-events: write strategy: fail-fast: false