Git security
git จำทุกอย่าง รวมถึง token ที่คุณลบไปแล้ว มาดูห้าเรื่องที่ทำ repo พัง ด่านที่กันแต่ละเรื่อง และต้องทำอะไรเมื่อ secret หลุดไปแล้ว
สิ่งที่จะได้
- อธิบายได้ว่าลบ .env ทีหลังไม่ช่วย และกันไว้ตั้งแต่ต้นด้วย hook สแกนก่อน commit ได้
- ยกเลิก token ที่หลุดแล้วออกอันใหม่ก่อน แล้วค่อยล้าง history ด้วย
git filter-repo - ตั้ง branch protection ให้ main ได้: ห้าม force-push ต้องมีคน review รับแต่ commit ที่เซ็น
- เลือกใช้ fine-grained token ได้: repo เดียว สิทธิ์เท่าที่ใช้ และมีวันหมดอายุ
สรุป 1 นาที
กดข้อไหน ภาพข้างล่างจะเล่นขั้นนั้นให้ดู
ตอนนี้กันได้ 0 จาก 5 เรื่อง hook ก่อน commit อยู่ในเครื่องคุณ ข้ามได้ด้วย --no-verify ที่เหลืออยู่ที่ host ซึ่งไม่มีใครข้ามได้ง่าย ๆ
เผลอ commit ไฟล์ .env ที่มี token แล้ว push ขึ้น repo public commit ถัดไปลบไฟล์ทิ้งแล้ว พอหรือยัง
ในภาพ: commit ในเครื่องคุณเรียงเป็นบ้านแถวหนึ่งทางซ้าย บนหลังคามีสิ่งที่ commit นั้นเปลี่ยน (สีแดงคือมี token อยู่ข้างใน) และตอนที่เปิดการสแกนไว้ จะมีด่านก่อน commit ขวางทางคุณอยู่ ถนน push พาไปทางขวาถึงลานของ host ที่มีกำแพงล้อม ข้างในคือ origin/main มีประตูเดียวในกำแพง กติกาของ host ในขั้นนั้นยืนอยู่ตรงประตู PR #12 รออยู่ข้างประตู production อยู่ถัดไป ข้างล่างคือถนนของคนร้าย ปลายหนึ่งมี clone เก่า อีกปลายคือบริการที่ token ที่หลุดเปิดได้ repo กับ token ในภาพเป็นของเล่นทั้งหมด
เทียบทุกแบบ: Git security
ห้าเรื่องเดิมเกิดกับ repo สี่แบบ แต่ละแบบเพิ่มด่านจากแบบก่อน ดูว่าเรื่องไหนยังหลุดอยู่
ใช้เมื่อ ของเล่นในเครื่องที่ไม่มีใครอื่นแตะ
ไม่ควรใช้เมื่อ อะไรที่มีคนอื่นหรือมี secret
แบบจำลอง: จับคู่หนึ่งเรื่องกับด่านหลักหนึ่งด่านให้ดูง่าย ชื่อ setting จริงต่างกันไปตาม host (GitHub, GitLab, Bitbucket) แต่มีครบทุกเจ้า · กดช่องไหนก็ได้ ภาพใหญ่ด้านบนจะเปลี่ยนเป็นแบบนั้น พร้อมบอกว่าเหมาะกับงานแบบไหน
ห้าเรื่อง ห้าด่าน
| เรื่องที่พลาด | ด่าน | อยู่ที่ |
|---|---|---|
| token หลุดใน commit | .gitignore + pre-commit scan | เครื่องคุณ |
| force-push ทับ main | ห้าม force-push main | host |
| merge โดยไม่มีใครดู | ต้องมีคน review | host |
| commit ปลอมชื่อคนอื่น | ต้องเซ็น commit | host |
| token ในเครื่องถูกขโมย | token แคบ + 2FA | host |
หลุดแล้ว rotate ก่อน
secret ที่ push ขึ้นไปแล้ว ให้คิดไว้เลยว่าโดนเอาไปแล้ว bot คอยอ่านทุก push ที่เป็น public ยกเลิกแล้วออกอันใหม่ก่อนทำอย่างอื่น ล้าง history ทีหลัง และมันไปไม่ถึง clone กับ fork ที่มีของเก่าอยู่แล้ว
ด่านในเครื่องช่วยได้ ด่านที่ host เป็นตัวตัดสิน
hook ก่อน commit จับความพลาดส่วนใหญ่ได้ตั้งแต่ยังไม่เกิด แต่มันอยู่แค่ในเครื่องเดียว และ --no-verify ข้ามได้ branch protection, การบังคับ review, push protection และกติกาเรื่องลายเซ็น อยู่ที่ host ซึ่งทุกคนต้องผ่าน
ในโค้ดจริง
#!/bin/sh
# A token committed, then "deleted": still in history. Then the same mistake stopped twice (.gitignore, a pre-commit
# hook), what --no-verify does to the hook, and what rewriting history can't reach (a clone made before).
# The token is made up at run time (random letters behind GitHub's classic-token prefix): no real secret is in here.
# History is rewritten with git filter-branch only because it ships with git; git's own docs point to git filter-repo.
# Run: sh secrets.sh (needs git; works in a throwaway folder, then deletes it)
set -eu
export GIT_AUTHOR_NAME=Mai GIT_AUTHOR_EMAIL=mai@example.com GIT_COMMITTER_NAME=Mai GIT_COMMITTER_EMAIL=mai@example.com
export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 FILTER_BRANCH_SQUELCH_WARNING=1
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
token="ghp""_$(LC_ALL=C tr -dc 'A-Za-z0-9' < /dev/urandom | head -c 36)"
in_history() { git log --all -S "$token" --format=%s | wc -l | tr -d ' '; }
git init -q -b main "$tmp/shop" && cd "$tmp/shop"
echo "# shop" > README.md && git add . && git commit -qm "readme"
echo "console.log('hi')" > app.js && git add . && git commit -qm "app"
echo "GITHUB_TOKEN=$token" > .env && git add . && git commit -qm "add .env"
git rm -q .env && git commit -qm "remove .env"
[ ! -e .env ] && [ "$(git show HEAD~1:.env)" = "GITHUB_TOKEN=$token" ]
echo "deleted file, still in history: $(in_history) commits"
git clone -q "$tmp/shop" "$tmp/old-clone"
# 1. .gitignore first: git won't stage the file at all
echo ".env" > .gitignore && git add .gitignore && git commit -qm "ignore .env"
echo "GITHUB_TOKEN=$token" > .env
if git add .env 2>/dev/null; then echo "unexpected: .env staged"; exit 1; fi
printf '.gitignore: git add refused · '
# 2. a pre-commit hook that refuses token-shaped text (real scanners such as gitleaks know hundreds of shapes)
cat > .git/hooks/pre-commit <<'HOOK'
#!/bin/sh
if git diff --cached -U0 | grep '^+' | grep -Eq '(ghp|gho|ghs|ghu)_[A-Za-z0-9]{36}|AKIA[0-9A-Z]{16}'; then
echo "pre-commit: refused, the staged changes contain something that looks like a token" >&2
exit 1
fi
HOOK
chmod +x .git/hooks/pre-commit
echo "const token = '$token'" > config.js && git add config.js
if git commit -qm "config" 2>/dev/null; then echo "unexpected: commit went through"; exit 1; fi
printf 'pre-commit hook: commit refused · '
git commit -q --no-verify -m "config" && printf '%s' "--no-verify: went through · "
git reset -q --hard HEAD~1
# 3. rewrite history to take .env out of every commit, and drop what still points at the old ones
git filter-branch --index-filter 'git rm -q --cached --ignore-unmatch .env' -- --all > /dev/null 2>&1
git for-each-ref --format='%(refname)' refs/original | xargs -n 1 git update-ref -d
git reflog expire --expire=now --all && git gc -q --prune=now
here=$(in_history); there=$(cd "$tmp/old-clone" && in_history)
[ "$here" = 0 ] && [ "$there" = 2 ]
echo "after rewriting history: $here here, an old clone still has $there, so revoke the token first"
sh secrets.sh (ต้องมี git รันจริงใน repo ชั่วคราว แล้วลบทิ้ง)ผลที่พิมพ์ออกมา:deleted file, still in history: 2 commits .gitignore: git add refused · pre-commit hook: commit refused · --no-verify: went through · after rewriting history: 0 here, an old clone still has 2, so revoke the token first
git จริงใน repo ชั่วคราว: token ปลอมถูก commit แล้วลบ (ยังอยู่ใน 2 commit) .gitignore ไม่ให้ add ไฟล์ hook ก่อน commit ไม่ให้ commit ผ่าน --no-verify เดินผ่านไปได้ และการแก้ history ที่ clone เก่ายังเก็บ token ไว้ รันจริง ข้างล่างคือที่มันพิมพ์ออกมา
คำถามที่เจอบ่อย
ลบไฟล์ .env ใน commit ถัดไปแล้ว ปลอดภัยหรือยัง
ยัง git เก็บทุก commit ไว้ทั้งก้อน token เลยยังอยู่ใน history และทุก clone ก็มีติดไป ถ้า repo เป็น public มี bot คอยอ่านทุก push อยู่แล้ว
เผลอ push secret ขึ้นไปแล้ว ต้องทำอะไรก่อน
ยกเลิก secret นั้นแล้วออกอันใหม่ก่อนทำอย่างอื่น คิดไว้เลยว่าโดนเอาไปแล้ว จากนั้นค่อยล้าง history ด้วย git filter-repo แล้วให้ทุกคน clone ใหม่ การแก้ history ไปไม่ถึง clone กับ fork ที่มีของเก่าอยู่แล้ว
กันไม่ให้ secret หลุดเข้า commit ตั้งแต่แรกยังไง
ใส่ .env ไว้ใน .gitignore ตั้งแต่ commit แรก แล้วมี hook ก่อน commit ที่สแกนหาข้อความหน้าตาเหมือน token แต่ hook อยู่แค่ในเครื่องเดียว และ --no-verify ข้ามได้ เลยต้องเปิด push protection ที่ host ไว้ด้วย
กัน force-push ทับ main ยังไง
ตั้ง branch protection ที่ host ให้ main ไม่รับ force-push และเปลี่ยนได้ผ่าน PR เท่านั้น ถ้าต้อง force-push branch ของตัวเองจริง ๆ ใช้ --force-with-lease มันจะไม่ยอมถ้า branch บน remote ขยับไปหลังจากที่เรา fetch ครั้งล่าสุด
ทำไมต้องบังคับ review ก่อน merge เข้า main
จะได้ไม่มีอะไรขึ้น main แล้ว deploy ไปโดยไม่มีใครเห็นเลย main รับแค่ PR ที่อีกคน approve แล้วและ CI ผ่าน ของที่มี bug หรือแปลก ๆ ต้องผ่านตาอย่างน้อยอีกหนึ่งคู่
มีคน commit ในชื่อเราได้ไหม
ได้ ชื่อกับอีเมลของ author ใน git เป็นแค่ข้อความ ใครก็ตั้ง user.name เป็นชื่ออะไรก็ได้ เซ็น commit ด้วย key SSH หรือ GPG แล้ว host จะขึ้น Verified ให้ตัวที่ลายเซ็นถูก และตั้งให้ main ไม่รับตัวที่ไม่ได้เซ็นได้
ควรใช้ access token แบบไหน
token แบบ fine-grained ให้แค่ repo เดียว สิทธิ์เท่าที่ต้องใช้ และมีวันหมดอายุ หลุดไปก็ถึงแค่ repo นั้น token แบบ classic ที่ให้สิทธิ์ repo ทั้งหมด หลุดทีเดียวไปถึงทุก repo ที่เราเข้าได้ แล้วเปิด 2FA ให้บัญชีด้วย
บทต่อไป
Supply chain attackระดับ: กลาง
repo ของคุณมีด่านครบแล้ว บทต่อไปดูโค้ดที่ dependency รันเองตอน install ใน CI ซึ่ง token ของคุณอาจรออยู่