處理 AI review findings 並改寫 Node.js entrypoint #1
@@ -9,6 +9,7 @@ die() {
|
|||||||
|
|
||||||
cleanup() {
|
cleanup() {
|
||||||
rm -f "${auth_file:-}" "${auth_path:-}" "${codex_output:-}"
|
rm -f "${auth_file:-}" "${auth_path:-}" "${codex_output:-}"
|
||||||
|
rmdir "${auth_lock:-}" 2>/dev/null || true
|
||||||
}
|
}
|
||||||
|
|
||||||
trap cleanup EXIT
|
trap cleanup EXIT
|
||||||
@@ -24,13 +25,13 @@ fi
|
|||||||
CODEX_HOME="${CODEX_HOME:-/root/.codex}"
|
CODEX_HOME="${CODEX_HOME:-/root/.codex}"
|
||||||
PROMPT="${PROMPT:-請自我介紹}"
|
PROMPT="${PROMPT:-請自我介紹}"
|
||||||
mkdir -p "$CODEX_HOME" || die "Unable to create CODEX_HOME."
|
mkdir -p "$CODEX_HOME" || die "Unable to create CODEX_HOME."
|
||||||
|
umask 077
|
||||||
|
|
||||||
auth_file="$(mktemp "$CODEX_HOME/auth.XXXXXX")"
|
auth_file="$(mktemp "$CODEX_HOME/auth.XXXXXX")"
|
||||||
auth_path="$CODEX_HOME/auth.json"
|
auth_path="$CODEX_HOME/auth.json"
|
||||||
|
auth_lock="$CODEX_HOME/auth.lock"
|
||||||
|
|
||||||
if [[ -e "$auth_path" ]]; then
|
mkdir "$auth_lock" || die "Unable to lock Codex auth.json."
|
||||||
die "Refusing to overwrite existing Codex auth.json."
|
|
||||||
fi
|
|
||||||
|
|
||||||
|
Ghost marked this conversation as resolved
|
|||||||
if ! printf '%s\n' "$OAUTH" | base64 -d > "$auth_file"; then
|
if ! printf '%s\n' "$OAUTH" | base64 -d > "$auth_file"; then
|
||||||
die "OAUTH must be valid base64 encoded Codex auth.json."
|
die "OAUTH must be valid base64 encoded Codex auth.json."
|
||||||
@@ -40,26 +41,41 @@ if ! jq -e 'type == "object"' "$auth_file" >/dev/null; then
|
|||||||
die "Decoded OAUTH must be a JSON object."
|
die "Decoded OAUTH must be a JSON object."
|
||||||
fi
|
fi
|
||||||
|
Ghost marked this conversation as resolved
gitea-actions
commented
嚴重等級:🟡 警告 **嚴重等級**:🟡 警告
**審查員**:Mage
**問題**:使用 `install -m 600` 將 `auth_file` 移至 `auth_path`。若 `mktemp` 產生的 `auth_file` 與 `auth_path` 不在同一個檔案系統分區(Filesystem),`install` 指令(底層通常是 copy + chmod/chown)可能會有短暫時間檔案權限為預設值,存在權限外洩風險。
**建議**:建議在確認檔案權限無誤後,於同一分區內使用 `mv` 進行原子性移轉,或在寫入前先明確設定 `umask`。
|
|||||||
|
|
||||||
|
if [[ -e "$auth_path" ]]; then
|
||||||
|
die "Refusing to overwrite existing Codex auth.json."
|
||||||
|
Ghost marked this conversation as resolved
gitea-actions
commented
嚴重等級:🟡 警告 **嚴重等級**:🟡 警告
**審查員**:Bard
**問題**:使用靜態路徑檢查 `auth.json` 是否存在,若前次執行中斷導致檔案未清除,會導致後續執行失敗(Self-inflicted DoS)。
**建議**:移除對既有檔案的檢查,改為在執行前確保該檔案為最新且受控狀態,或使用唯一的隨機臨時檔。
|
|||||||
|
fi
|
||||||
|
|
||||||
install -m 600 "$auth_file" "$auth_path"
|
install -m 600 "$auth_file" "$auth_path"
|
||||||
rm -f "$auth_file"
|
rm -f "$auth_file"
|
||||||
|
Ghost marked this conversation as resolved
gitea-actions
commented
嚴重等級:🔴 嚴重 **嚴重等級**:🔴 嚴重
**審查員**:Mage
**問題**:在執行 install -m 600 時,若 codex 進程已經在嘗試讀取 auth.json,會發生檔案存取競態(Race Condition)。雖然使用了 flock,但這僅在同一個 shell 腳本實例中有效,無法保護跨容器或跨執行環境的檔案存取一致性。
**建議**:建議將 auth.json 放置於唯讀且受限的目錄中,並通過環境變數直接傳遞路徑給 codex,而非在執行時進行檔案寫入與複製。
|
|||||||
|
|
||||||
codex_output="$(mktemp)"
|
codex_output="$(mktemp)"
|
||||||
|
|
||||||
set +e
|
run_codex() {
|
||||||
codex exec \
|
set +e
|
||||||
--dangerously-bypass-approvals-and-sandbox \
|
codex exec \
|
||||||
--skip-git-repo-check \
|
--dangerously-bypass-approvals-and-sandbox \
|
||||||
--model "$MODEL" \
|
--skip-git-repo-check \
|
||||||
"$PROMPT" 2>&1 | tee "$codex_output"
|
--model "$MODEL" \
|
||||||
|
Ghost marked this conversation as resolved
gitea-actions
commented
嚴重等級:🟡 警告 **嚴重等級**:🟡 警告
**審查員**:Mage
**問題**:在容器化環境(通常是 ephemeral 的)中,auth.json 寫入後立刻被刪除,這會導致 codex 在後續執行中因找不到驗證檔案而無法運作。另外,trap 的清理機制會導致該檔案在 codex 完成工作前被刪除,這對於長效執行或需要多次存取的應用場景是錯誤的設計。
**建議**:評估 codex 是否需要該檔案在執行期間持續存在。若需要,請調整清理時機,或考慮使用記憶體中的臨時檔案系統(tmpfs)來提升安全性,而非直接移除檔案。
|
|||||||
codex_status="${PIPESTATUS[0]}"
|
"$PROMPT" 2>&1 | tee "$codex_output"
|
||||||
set -e
|
local status="${PIPESTATUS[0]}"
|
||||||
|
set -e
|
||||||
|
return "$status"
|
||||||
|
}
|
||||||
|
|
||||||
|
if run_codex; then
|
||||||
|
codex_status=0
|
||||||
|
else
|
||||||
|
codex_status="$?"
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
|
if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
|
||||||
if [[ -r /proc/sys/kernel/random/uuid ]]; then
|
while :; do
|
||||||
output_delimiter="CODEX_OUTPUT_$(cat /proc/sys/kernel/random/uuid)"
|
|
||||||
else
|
|
||||||
output_delimiter="CODEX_OUTPUT_$(mktemp -u XXXXXXXXXXXXXXXX)"
|
output_delimiter="CODEX_OUTPUT_$(mktemp -u XXXXXXXXXXXXXXXX)"
|
||||||
fi
|
|
||||||
|
if ! grep -qxF "$output_delimiter" "$codex_output"; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
if [[ "$codex_status" -eq 0 ]]; then
|
if [[ "$codex_status" -eq 0 ]]; then
|
||||||
echo "status=completed" >> "$GITHUB_OUTPUT"
|
echo "status=completed" >> "$GITHUB_OUTPUT"
|
||||||
|
|||||||
嚴重等級:🟡 警告
審查員:Mage
問題:使用
mkdir作為鎖定機制若容器意外崩潰可能殘留鎖檔。建議:考慮使用更穩健的
flock機制來管理檔案鎖並確保自動清理。