我如何更新我的 GitHub Actions CI 管道,這樣,如果特洛伊木馬代碼白皮書中展示的攻擊的任何變體作為 PR 提交到我的 GitHub 存盤庫,PR 要么自動拒絕提交,要么將評論添加到 PR 警告中關于漏洞。
背景:在 2021 年 10 月 30 日,Nicholas Boucher 和 Ross Anderson 發表了一篇名為Trojan Source: Invisible Vulnerabilities 的論文,其中概述了在代碼提交中惡意使用 unicode 的幾種方式,這些代碼提交(逐像素)與非惡意代碼,但實際上是惡意的。除了用于定義和呼叫不同函式的更明顯的“歧義字符”外,它們還專門描述了聰明的攻擊者如何利用 unicode 雙向控制字符來做一些非常討厭的事情。
更多背景:我管理一個托管在 GitHub 上的開源 Python 專案。拋開這篇論文發表后,GitHub在查看包含潛在惡意 unicode 字符的代碼時添加了警告,合并 PR 時無法在 GitHub WUI 中直觀地檢測 PR 中的這些問題。
我的問題是:如何保護自己免受尚未發現的惡意 unicode 提交的影響?還有其他字面上看不到的漏洞?
我可以向我的 GitHub Actions CI 管道添加什么來警告我用戶貢獻的 Python 代碼中的隱形危險?
編輯:應該捕獲的示例包括以下 python 片段:
- https://github.com/nickboucher/trojan-source/tree/main/Python
uj5u.com熱心網友回復:
您可以向 GitHub 操作管道添加作業流,以檢測非 ASCII 字符并自動對 PR 注釋警告。
將此添加到.github/workflows/unicode_warn.yml您的回購的根目錄中:
################################################################################
# File: .github/workflows/unicode_warn.yml
# Version: 0.1
# Purpose: Detects Unicode in PRs and comments the results of findings in PR
# * https://tech.michaelaltfield.net/bidi-unicode-github-defense/
# Authors: Michael Altfield <[email protected]>
# Created: 2021-11-20
# Updated: 2021-11-20
################################################################################
name: malicious_sanity_checks
# execute this workflow automatically on all PRs
on: [pull_request]
jobs:
unicode_warn:
runs-on: ubuntu-latest
container: debian:bullseye-slim
steps:
- name: Prereqs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
apt-get update
apt-get install -y git bsdmainutils
git clone "https://token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" .
shell: bash
- name: Check diff for unicode
id: unicode_diff
run: |
set -x
diff=`git diff --unified=0 ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep -E "^[ ]" | grep -Ev '^(--- a/|\ \ \ b/)'`
unicode_diff=`echo -n "${diff}" | grep -oP "[^\x00-\x7F]*"`
unicode_grep_exit_code=$?
echo "${unicode_diff}"
unicode_diff_hexdump=`echo -n "${unicode_diff}" | hd`
echo "${unicode_diff_hexdump}"
# did we select any unicode characters?
if [[ "${unicode_diff_hexdump}" == "" ]]; then
# we didn't find any unicode characters
human_result="INFO: No unicode characters found in PR's commits"
echo "${human_result}"
else
# we found at least 1 unicode character
human_result="^^ WARNING: Unicode characters found in diff!"
echo "${human_result}"
echo "${diff}"
fi
echo "UNICODE_HUMAN_RESULT=${human_result}" >> $GITHUB_ENV
shell: bash {0}
# leave a comment on the PR. See also
# * https://stackoverflow.com/a/64126737
# make sure this doesn't open command injection risks
# * https://github.com/victoriadrake/github-guestbook/issues/1#issuecomment-657121754
- name: Leave comment on PR
uses: actions/github-script@v5
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "${{ env.UNICODE_HUMAN_RESULT }}"
})
上述檔案定義了一個名為 evil_sanity_checks 的作業流,其作業名為 unicode_warn。此作業包含每次在您的 repo 中創建新 PR 時執行的多個步驟:
- 先決條件- 首先,安裝了 git 和 hd 等基本依賴
- 檢查 unicode 的差異- 一個簡單的 BASH 腳本使用 grep 來檢測跨 PR 提交合并的差異中的非 ascii 字符
- 在 PR 上發表評論 - 在 PR 上添加評論,指示提交是否包含 unicode 字符
有關此內容以及可以保護您免受的木馬源漏洞的更多資訊,請參閱來源:
- https://tech.michaelaltfield.net/2021/11/22/bidi-unicode-github-defense/
也可以看看
- https://stackoverflow.com/a/64126737
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/368058.html
