github CodeQL 支援 PHP 嗎?

error message:

Error: Did not recognize the following languages: php
Warning: This run of the CodeQL Action does not have permission to access the CodeQL Action API endpoints. This could be because the Action is running on a pull request from a fork. If not, please ensure the workflow has at least the 'security-events: read' permission. Details: Code scanning is not enabled for this repository. Please enable code scanning in the repository settings. - https://docs.github.com/rest

這看起來像是 CodeQL 分析時遇到了兩個常見的障礙:一個是語言設定問題,另一個是 GitHub API 的權限或設定問題。

以下是針對這兩項錯誤的解決方案:


1. 語言錯誤:Did not recognize the following languages: php

問題點: 雖然 CodeQL 支援 PHP,但系統提示無法辨認。

解決方法:

  • 檢查大小寫:.github/workflows/codeql.yml 檔案中,請確保語言名稱是全小寫
  • 更新 Action 版本: 確保你使用的是較新的版本(建議使用 @v3)。
  • 縮排與語法: 如果有多種語言,請使用列表格式。

YAML

# 範例設定
- name: Initialize CodeQL
  uses: github/codeql-action/init@v3
  with:
    languages: php  # 確保這裡是小寫

2. 權限與 API 錯誤:does not have permission

問題點: 工作流(Workflow)沒有權限將掃描結果上傳到 GitHub 的安全後台。這通常發生在從 Fork(分叉)出來的 PR,或是權限設定過於嚴格時。

解決方法:

你需要在 YAML 檔案中明確加入 permissions 區塊,賦予工作流寫入安全事件的權力:

YAML

permissions:
  actions: read
  contents: read
  security-events: write  # 這是上傳結果最關鍵的權限

3. 警告:Code scanning is not enabled

問題點: 你的儲存庫(Repository)尚未正式啟用 Code Scanning 功能。

操作步驟:

  1. 進入 GitHub 儲存庫的 Settings(設定)
  2. 點擊左側選單的 Code security and analysis
  3. 找到 Code scanning 並點擊 Set up 或確保其為開啟狀態。

[!IMPORTANT]

如果這是私有儲存庫(Private Repo),你必須擁有 GitHub Advanced Security 授權才能使用 CodeQL 的自動掃描功能。


快速檢查清單

問題類型應對動作
來自 Fork 的 PRSettings > Actions > General 確認是否允許 Fork 的 PR 執行工作流並存取秘密(Secrets)。
多語言設定若有其他語言,請寫成:languages: [ 'javascript', 'php' ]
GitHub Enterprise如果你在企業版環境,請確認 Runner 是否可以連線到 GitHub 的 API 端點。

請問你這個專案是公開的還是組織內的私有專案呢?這會影響到權限設定的細節。


討論串: Why doesn’t CodeQL support auditing PHP
https://github.com/github/codeql/issues/12376

這是一個很常見的誤解。簡單來說:CodeQL 絕對支援 PHP,但在 GitHub Action 的某些特定版本或環境下,可能會因為「配置方式」導致它找不到 PHP。

關於你提到的 issue (#12376) 以及相關的討論,這裡有幾個核心重點幫助你理解:

1. CodeQL 確實支援 PHP

PHP 是 CodeQL 官方支援的語言之一(包括分析 SQL 注入、跨站腳本 XSS 等漏洞)。你可以從官方文件中看到 PHP 始終在支援清單中。

2. 為什麼會出現 “Did not recognize php” 的錯誤?

在該 issue 和類似案例中,通常是因為以下兩個原因:

  • 版本相容性 (Action Version): 如果你使用的 github/codeql-action 版本太舊,或者內含的 CodeQL CLI 版本不匹配,可能會導致語言包加載失敗。
  • 預設套件包 (Standard Bundles):CodeQL 在執行時會下載一個包含所有語言規則的「套件包 (Bundle)」。如果下載過程中出錯,或是你指定的分析器路徑不對,就會報出找不到語言的錯誤。
  • 大小寫問題:CodeQL 對設定檔非常敏感,必須確保是全小寫的 php

3. 如何解決這個問題?

根據目前的最佳實踐,請檢查並修正你的 .github/workflows/codeql.yml

A. 更新 Action 版本(最重要)

確保你使用的是 v3(目前的主流版本):

YAML

- name: Initialize CodeQL
  uses: github/codeql-action/init@v3
  with:
    languages: php

B. 手動指定 CodeQL 套件來源(如果問題持續)

有時候 GitHub 的託管執行器(Runner)快取了壞掉的 CodeQL 版本。你可以嘗試強制它使用最新的版本:

YAML

- name: Initialize CodeQL
  uses: github/codeql-action/init@v3
  with:
    languages: php
    tools: latest # 強制下載最新版工具

4. 針對你提到的權限錯誤(補充)

你之前提到的第二個錯誤 Warning: This run of the CodeQL Action does not have permission... 其實才是主因。

當 CodeQL 因為權限不足無法從 GitHub API 獲取配置或下載必要的語言組件時,它可能會退回到一個「空狀態」,然後報錯說它「不認識 php」。

請務必檢查:

  • 你的 Repository 設置中:Settings > Code security and analysis > Code scanning 是否已啟動?
  • 如果這是 Fork 過來的專案,預設是不能執行 CodeQL 並回傳結果的(為了安全)。你必須在 PR 頁面手動核准執行,或者在該 Fork 倉庫的設定中開啟 Actions 權限。

總結

CodeQL 支援 PHP。 你看到的錯誤通常不是因為「不支援」,而是因為 權限被阻擋Action 版本過舊 導致 CodeQL 無法正確加載 PHP 的分析模組。請嘗試將 Action 更新至 v3 並在 YAML 中加入 permissions: security-events: write

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *