D6:GitHub 安全套件管理
D6-O1 大規模推出與管理安全功能
兩個層次:security configurations 與 global settings
- Security configurations: 一組安全功能的啟用設定,套用到組織內的任何 repository;控制 repository 層級的功能。
- Global settings: 控制組織層級的安全設定,由所有 repository 繼承;用來客製化各功能如何分析你的組織。
兩者都是官方建議的做法。有權操作的人:組織擁有者、security manager,以及具 admin 角色的組織成員。套用設定的位置:組織 Settings → Security → Advanced Security → Configurations;Global settings 在同一個選單下。
套用、狀態與卸除
套用 configuration 後,每個 repository 與它進入「受管理的關係」,這個關係會隨時間改變。狀態如下:
| 狀態 | 意義 |
|---|---|
| attached | 已套用,repository 繼承所有設定 |
| attaching/updating | 過渡狀態:正在套用或更新 |
| enforced | 已套用且強制;repository 管理員不能改變該設定控制的功能啟用狀態 |
| removed | repository 管理員改了與未強制的設定衝突的項目;仍關聯但不再繼承全部設定,也不計入涵蓋指標;重新套用即可恢復 |
| removed_by_enterprise | 企業層級的設定變更與已套用的組織設定衝突 |
| failed | repository 現有設定與 configuration 衝突,無法套用 |
| detached | 沒有套用任何設定;顯示為「No configuration」 |
- removed 與 detached 不同: detached 是完全斷開,repository 現有的安全設定不變,但沒有 configuration 管理;removed 仍與 configuration 關聯。要避免 removed,對 configuration 啟用強制。
- 狀態變更會記錄在稽核日誌的
repository_security_configuration事件。 - 套用到多個 repository:在 Repositories 分頁可先篩選,再逐一選取、選本頁全部,或「Select all」選取符合篩選的全部;套用前會顯示對授權用量的影響。套用會涵蓋現用與封存的 repository(有些功能,如 secret scanning,會在封存 repository 執行)。
- 對已附加其他 configuration 的 repository,直接套用新的即可,不需要先卸除。卸除用於 repository 的安全需求太特殊時,改為個別管理;卸除不會改變現有的啟用設定。
強制(enforcement)
強制的 configuration 讓 repository 擁有者不能改變該 configuration 已啟用或已停用的功能(未設定的功能不強制)。有些情況會破壞強制,例如 code scanning 的啟用在 repository 的 GitHub Actions 先啟用後又停用、所需的 Actions 不可用,或 default setup 不分析的語言定義被改變時不會套用。透過 REST API 嘗試變更強制 configuration 的功能,呼叫看似成功,但啟用狀態不會改變。
判斷練習
情境: 管理員發現某 repository 的狀態是 removed,想確認它是否還受 configuration 管理。
解析: removed 表示 repository 管理員改了與未強制 configuration 衝突的設定;configuration 仍關聯,但 repository 不再繼承全部設定,也不計入涵蓋指標。重新套用可恢復,啟用強制可預防。這不同於 detached。
來源:Enabling security features at scale(GH500-DOC-48)、Applying a custom security configuration(GH500-DOC-51)、Detaching repositories from their security configurations(GH500-DOC-52)、Security configuration statuses(GH500-DOC-56)、Security configuration enforcement(GH500-DOC-57);2026-11-02 已對照抓下的頁面段落;其餘為整理,備考前請讀來源原文。
本節沒有涵蓋學習指南提到的「GitHub Enterprise Cloud 與 Enterprise Server 的功能差異」:我讀到的頁面只在零星處標註版本(例如 Enterprise Server 3.17、3.19、3.21),沒有完整的對照表,所以不補寫。
D6-O2 設定安全功能與預設值
建立 custom security configuration
建立路徑:Settings → Security → Advanced Security → Configurations → New configuration → Custom configuration。可為不同組織或群組建立不同的 configuration。每個功能都可選擇「啟用」「停用」或「保持現有設定」。納入 Secret Protection 或 Code Security 功能時,套用到私有與內部 repository 會產生使用成本(或需要授權)。
| 區塊 | 可設定的項目 |
|---|---|
| Secret Protection | 啟用 secret scanning 警示;validity checks;extended metadata(只能在 validity checks 啟用時啟用);generic patterns;AI 偵測的 secret;push protection;bypass privileges(含豁免與審查流程);防止直接關閉警示(委派警示關閉) |
| Code Security | Default setup;runner 類型(可用自訂標籤);防止直接關閉警示 |
| Dependency scanning | Dependency graph;automatic dependency submission;Dependabot alerts;reachability analysis;security updates;防止直接關閉警示;malware alerts |
| 其他 | Private vulnerability reporting |
- 同時啟用 Code Security 與 Dependency graph 會啟用 dependency review。
- Default setup 的「Enabled with advanced setup allowed」: 讓你建立能套用到所有 repository 的 configuration,無論目前的 code scanning 設定;只在 CodeQL 分析目前沒有執行的 repository 啟用 default setup(此選項自 GitHub Enterprise Server 3.19 起提供)。
- Policy 區塊: 「Use as default for newly created repositories」可選 Public、Private and internal 或 All repositories;「Enforce configuration」啟用強制。預設 configuration 只自動套用到新建立的 repository;轉移進組織的 repository 需要手動套用。
停用付費功能
要關閉 Secret Protection 或 Code Security,最簡單的方式是建立一個把該產品在最上層停用的 configuration,再套用到要關閉的 repository。要讓 Code Security 保持停用,需把 Code scanning 區塊的「Default setup」與「Prevent direct alert dismissals」設為 Disabled;Dependency graph 與 Dependabot alerts 包含在所有方案中,可保持啟用。警告: 把 Code scanning 區塊的「Prevent direct alert dismissals」改為 Not set,會自動把 Code Security 切換為 Enabled;為避免意外啟用付費功能,儲存與套用前要重新確認最上層的 Code Security 設定。官方建議把 configuration 取名得非常清楚,例如「No Code Security」。授權用量顯示在 Repositories 分頁的 Apply configurations 區塊。
Global settings
| 類別 | 可設定項目 |
|---|---|
| Dependabot | Auto-triage 規則;grouped security updates;在 Actions runner 上做相依性更新;runner 類型(預設標準 GitHub-hosted runner,可改用標籤 runner);授予 Dependabot 存取私有 repository |
| Code scanning | 為 default setup 推薦 Extended query suite;為所有使用 CodeQL 的 repository 啟用 Copilot Autofix;AI Scan(pull request、repository);AI applications 掃描;以 CodeQL model pack 擴充涵蓋範圍;讓不活躍 repository 每 30 天繼續掃描 |
| Secret scanning | 為被阻擋的 commit 加上資源連結;自訂樣式;指定納入 push protection 的樣式 |
- Dependabot runner: 標籤 runner 預設標籤為
dependabot,也可用自訂標籤與 runner 群組;基於安全理由,公開 repository 一律用 GitHub-hosted runner,標籤 runner 對公開 repository 無效。 - AI Scan 與 AI applications 掃描是各自獨立的設定: 啟用 AI Scan 不會啟用 AI applications 掃描。Repository 繼承組織設定,但 repository 管理員可以個別選擇退出。
- 「不活躍 repository 繼續掃描」的週期固定 30 天,不可設定。
- 另可讓 code scanning 與 Dependabot 安全存取私有 registry;若使用 Microsoft Defender for Cloud、JFrog Artifactory 或 CI/CD 將產物推向正式環境,可整合「生產環境脈絡」來協助排定 code scanning 與 Dependabot 警示的優先順序。
判斷練習
情境: 某組織建立了一個預設 configuration,三天後一個 repository 從別的組織轉移進來,管理員以為它已自動套用。
解析: 預設 configuration 只自動套用到「新建立」的 repository;轉移進來的需要手動套用。
來源:Creating a custom security configuration(GH500-DOC-50)、Configuring global security settings for your organization(GH500-DOC-49)、Managing your paid use of Advanced Security(GH500-DOC-55)、Enabling security features at scale(GH500-DOC-48);2026-11-02 已對照抓下的頁面段落;其餘為整理,備考前請讀來源原文。
D6-O3 定義治理、存取與 Code Security 工作流程
角色與權限
| 角色/身分 | 在本領域能做什麼 |
|---|---|
| 組織擁有者、security manager、具 admin 角色的成員 | 建立、套用與卸除 security configuration;設定 global settings;以 script 大規模啟用 advanced setup;管理授權用量 |
| Repository 管理員 | 在未強制的 configuration 下可改動設定(使狀態變成 removed);可對 AI Scan 等繼承的設定選擇退出 |
| 自訂角色 | 可讓非擁有者、非 security manager 的成員審查並略過警示關閉請求(見 D5) |
強制邊界、繞過與例外
- 強制: 啟用強制的 configuration 阻擋 repository 擁有者改變其控制的功能(見 O1)。
- 繞過與豁免: 在 configuration 的 Secret Protection 區塊設定 bypass privileges,指定的對象可繞過或略過 push protection,其他貢獻者走審查流程(見 D5)。
- 防止直接關閉警示: 在 configuration 中啟用委派警示關閉,可分別用於 secret scanning、code scanning 與 Dependabot(見 D5)。
大規模啟用 code scanning
- Default setup 大規模啟用: repository 需滿足全部條件才符合資格:尚未啟用 advanced setup;GitHub Actions 已啟用;公開,或已啟用 Code Security。可為組織所有符合資格的 repository 啟用,或篩選出子集。可透過組織安全設定頁客製化(例如用 model pack 擴充涵蓋範圍);也可要求組織的
github-codeql-config-filerepository property 並設定預設值,套用整個組織的設定檔而不必維護 workflow 檔。private registry 的程式碼需要讓 default setup 有存取權。 - Advanced setup 大規模啟用: 對不適用 default setup 的 repository,用批次設定 script 啟用,組織需啟用 GitHub Actions。步驟:找出可用同一設定分析的 repository 群組(例如都用 production 環境建置 Java);建立並測試呼叫 CodeQL action 的 workflow;用範例 script(GitHub CLI 擴充
advanced-security/gh-add-files,或 Python、NodeJS、PowerShell 範例)把 workflow 加到每個 repository。可在 script 指定已發布的 CodeQL model pack 擴充涵蓋範圍。CodeQL model pack 目前是公開預覽,支援 C/C++、C#、Java/Kotlin、Python、Ruby 與 Rust;VS Code 的 CodeQL model editor 支援為 C#、Java/Kotlin、Python 與 Ruby 的相依性建模。
判斷練習
情境: 組織有 200 個 repository,其中一部分已經用 advanced setup 跑 CodeQL。管理員想建立一個能套用到全部 repository 的 configuration,又不想搞壞現有的 advanced setup。
解析: 在 configuration 的 Default setup 選「Enabled with advanced setup allowed」:它只在 CodeQL 分析目前沒有執行的 repository 啟用 default setup。
來源:Configuring default setup for code scanning at scale(GH500-DOC-53)、Configuring advanced setup for code scanning with CodeQL at scale(GH500-DOC-54)、Creating a custom security configuration(GH500-DOC-50)、Delegated alert dismissal(GH500-DOC-11);2026-11-02 已對照抓下的頁面段落;其餘為整理,備考前請讀來源原文。
D6-O4 管理 CodeQL 與安全自動化
管理 CodeQL 的方式
- 核准的 CodeQL 設定: default setup(自動設定,可客製化語言、query suite、threat model、model pack)或 advanced setup 的自訂 workflow;組織可用 repository properties(
github-codeql-config-file、github-codeql-extra-queries)統一施行,套用到所有 setup 類型(見 D4、D5)。 - 推薦 query suite: 組織擁有者與 security manager 可在 global settings 推薦 Extended query suite;預設執行的是 Default suite。
- Copilot Autofix: 可在 global settings 為所有使用 CodeQL 的 repository 啟用;管理員也可在企業、組織、repository 層級停用,設定層層向下生效(見 D4)。
API 與自動化
REST API 的「Configurations」端點可建立與管理 security configuration,涵蓋組織與企業層級:
| 動作 | 端點(名稱) |
|---|---|
| 查詢 | 取得組織或企業的 configuration;取得單一 configuration;取得預設 configuration;取得與某 configuration 關聯的 repository;取得某 repository 的 configuration |
| 建立、更新、刪除 | Create/Update/Delete a code security configuration(組織與企業各有對應端點) |
| 套用與卸除 | Attach a configuration to repositories(企業 configuration 另有對應端點);Detach configurations from repositories |
| 設為預設 | Set a code security configuration as a default(組織與企業各有對應端點) |
- 「取得企業 configuration」端點要求呼叫者是企業管理員,OAuth app 與 classic personal access token 需要
read:enterprise範圍;該端點不適用 GitHub App user access token、installation access token 或 fine-grained personal access token。 - 強制的 configuration 透過 API 變更功能時,呼叫看似成功但不會改變狀態(見 O1)。
- 其他自動化:dependency submission API、code scanning 與 Dependabot 的 webhook 與 REST API(見 D3、D4)、批次 script 啟用 advanced setup(見 O3)、稽核日誌追蹤 configuration 狀態變更。
判斷練習
情境: 團隊用腳本呼叫 API 想修改一個已強制的 configuration 所控制的功能,呼叫回應成功,隔天發現功能沒變。
解析: 對強制的 configuration,透過 REST API 變更功能的呼叫看似成功,但啟用狀態不會改變;要真正調整,需要修改 configuration 本身或取消強制。
來源:Configurations(REST API)(GH500-DOC-58)、Security configuration enforcement(GH500-DOC-57)、Configuring global security settings for your organization(GH500-DOC-49)、Repository properties for code scanning(GH500-DOC-36);2026-11-02 已對照抓下的頁面段落;其餘為整理,備考前請讀來源原文。