กัน Secret และข้อมูล PDPA/PCI ก่อนหลุดเข้า LLM หรือขึ้น Git ด้วย AI Privacy Gateway + Git Guard

ทำไมแค่ .gitignore ถึงไม่พอในยุค AI Coding Agent#

ทุกวันนี้เวลาใช้ AI Coding Agent ช่วยเขียนโค้ดหรือดีบัก ข้อมูลสำคัญมีทางรั่วออกนอกเครื่องได้ 2 ทางหลัก คือ (1) วิ่งออกไปใน Prompt (เช่น Agent อ่านไฟล์ .env แล้วส่งเข้า LLM ภายนอก หรือเราเผลอก๊อปข้อมูลลูกค้าที่มีเลขบัตรประชาชน/เบอร์โทรไปวางในแชท) และ (2) หลุดไปใน Git commit / push ขึ้น GitHub

ลำพังไฟล์ .gitignore หรือการจำด้วยตัวเองว่า “อย่าเผลอวางคีย์” มักเอาไม่อยู่ในหน้างานจริง ผมเลยสร้างระบบด่านตรวจ 2 ชั้นแยกตาม ประตูทางออก ของข้อมูล:

  1. ด่านก่อนออกสู่ LLM (ai-privacy-gateway): Middleware คั่นหน้า Local AI Router (พอร์ต 20128) ตรวจทั้ง 16 กฎ Secrets (OWASP LLM02) และ 5 กฎ PDPA/PCI-DSS ใน ~0.3 ms พร้อมตัวจำแนกความหมายระดับธุรกิจ (Jev System One) ก่อนยิงออกนอกเครื่อง
  2. ด่านก่อนลง Git และขึ้น Remote (~/.githooks): จับคู่ gitleaks (สแกน Pattern แบบเร็ว) กับ jev-git-guard (ไบนารี Rust ที่พอร์ตจาก Python) ทำงานทั้งตอน pre-commit และ pre-push

ปัญหาหน้างานที่แก้ไม่ได้ด้วย Regex อย่างเดียว#

  1. Regex ธรรมดามักยิงมั่ว (False Positive): ตัวเลข 13 หลักในโค้ดอาจเป็น Unix Timestamp ไม่ใช่เลขบัตรประชาชนไทย และอีเมลตัวอย่างอย่าง user@example.com ในไฟล์เทสต์ก็ไม่ใช่ข้อมูลลูกค้าจริง
  2. การเซ็นเซอร์ tool output ทำ Agent แก้โค้ดพัง: ถ้าสั่งลบอีเมลและเบอร์โทรในผลลัพธ์ของ read_file ทิ้งหมด เวลา Agent จะเข้าไปแก้ไฟล์ mock data หรือไฟล์เทสต์มันจะเห็นโค้ดเพี้ยนและ patch พลาด
  3. ความลับทางธุรกิจไม่มี Pattern ตายตัว: ข้อมูลอย่าง “สรุปยอดกำไรโรงงานไตรมาสนี้” หรือ Prompt Injection ไม่มี prefix แบบ sk-... ให้จับด้วย Regex
  4. pre-commit อย่างเดียวกัน --no-verify ไม่ได้: ถ้ามี commit เก่าที่หลุดรอดจาก pre-commit ไปแล้ว ด่าน pre-commit รอบถัดไปจะมองไม่เห็น ต้องมี pre-push ตรวจ diff ก่อนออกจากเครื่องซ้ำอีกชั้น

สถาปัตยกรรมด่านตรวจ 2 ประตู#

ประตูที่ 1: Outbound LLM Traffic (Port 20128)
[ Hermes / CLI / IDE ]
│ (/v1/chat/completions, /v1/messages, /v1/responses)
▼
[ ai-privacy-gateway (wrapHttpHandler บน 9router) ]
├── Stage 1 (~0.3 ms): Deterministic Regex + Checksums
│ ├── Role = ALL (user, system, tool) ──► 16 Secret Rules (OWASP LLM02)
│ └── Role = USER only ──► 5 PDPA/PCI Rules (Modulo-11 + Luhn)
├── Stage 2 (~250–320 ms, toggleable): Jev System-One Semantic Classifier
└── Zero-Leak Audit Log (privacy-audit.jsonl: 5MB x 3, 64KB tail read)
│
▼
[ Upstream LLM Providers ]
ประตูที่ 2: Outbound Git Traffic (~/.githooks)
[ git commit ] ──► gitleaks protect --staged ──► jev-git-guard.exe (pre-commit)
[ git push ] ──► jev-git-guard.exe --mode=pre-push (diff @{u}..HEAD) ──► GitHub

จุดสำคัญในประตูที่ 1: ai-privacy-gateway (Pure Node.js, Zero Dependencies)#

  • Role-Aware Scrubbing (แยกตามบทบาทข้อความ):
    • SECRETS_PRESET (16 กฎ) รันทุก Role (user, system, assistant, tool): ไม่ว่าจะเป็นคีย์ Anthropic (sk-ant-), OpenAI (sk-), MaxPlus (ccsk/ccmk), GitHub PAT (ghp_/github_pat_), AWS (AKIA/ASIA), Google API (AIza), Slack, Telegram Bot Token, JWT, Private Key Block, DB Connection String (postgres://, mysql://, mongodb://, redis://), Bearer Token หรือค่าใน .env — ต่อให้ Agent อ่านติดมาจาก read_file ก็ถูกแทนที่ด้วย [REDACTED:...] ก่อนออกนอกเครื่องทันที
    • PDPA_PCI_PRESET (5 กฎ) รันเฉพาะ role: "user": ตรวจเลขบัตรประชาชนไทย 13 หลัก, บัตรเครดิต/เดบิต, เบอร์โทรไทย (06x/08x/09x และ +66), อีเมล, และเลขบัญชีธนาคารแบบมีคำนำหน้า — ช่วยกันผู้ใช้เผลอวางข้อมูลลูกค้าจริงลงแชท โดยไม่ไปทำลายโค้ด mock/test ที่ Agent อ่านผ่าน role: "tool"
  • Checksum Validators กันยิงมั่ว:
    • เลขบัตรประชาชนไทยต้องผ่านสูตร Modulo-11 Checksum (Timestamp 13 หลักจะไม่โดนจับ)
    • เลขบัตรเครดิต/เดบิต (13–19 หลัก) ต้องผ่าน Luhn Algorithm (PCI-DSS)
    • อีเมลยกเว้นโดเมนตัวอย่าง (@example.com, @example.org, @example.net, @localhost, @users.noreply.github.com)
  • Stage 2 Semantic Classifier (src/classifier.js): เชื่อม TypeSafe System One (jev-latest / jev-1.14.0) เพื่อประเมินความลับที่ไม่มีรูปแบบตายตัว 4 ระดับ (public, internal, confidential, restricted), ความน่าจะเป็นของ contains_real_pii, และ is_prompt_injection โดยสลับโหมด off | selective | always และแอ็กชัน tag | block สด ๆ ผ่าน POST /v1/privacy/config ได้โดยไม่ต้องรีสตาร์ทเซิร์ฟเวอร์
  • Zero-Leak Audit Log (logs/privacy-audit.jsonl): เก็บเฉพาะชื่อกฎและข้อความที่เซ็นเซอร์แล้ว ไม่เคยเก็บค่าดิบลงดิสก์ มี SHA-256 Deduplication (maxSeenHashes = 2000), Auto Rotation 5 MB เก็บย้อนหลัง 3 ไฟล์ และอ่านย้อนหลังแบบ bounded 64 KB (GET /v1/privacy/audit)

จุดสำคัญในประตูที่ 2: Global Git Hooks (gitleaks + jev-git-guard)#

  • กำหนด core.hooksPath = ~/.githooks ใช้ร่วมกันทุก Repository ในเครื่อง (พร้อมต่อท้ายเรียก .git/hooks/pre-commit.local ถ้าโปรเจกต์นั้นมี hook เฉพาะ)
  • pre-commit (2 ชั้น):
    1. รัน gitleaks protect --staged --verbose สแกน secret ใน staged diff
    2. รัน jev-git-guard.exe (เขียนด้วย Rust เพื่อความเร็ว และทดสอบ parity 7 กรณีเทียบกับตัว Python jev-git-guard.py แล้ว 100%)
  • pre-push (--mode=pre-push): ตรวจ diff ของ commit ที่ยังไม่ขึ้น remote (@{u}..HEAD) เพื่อดักกรณีที่เคย commit ด้วย --no-verify
  • เกณฑ์การตัดสินใจ (Decision Gate Thresholds):
    • has_secrets.noul >= 0.60 -> BLOCK (exit 1)
    • is_destructive.noul >= 0.75 (เช่น ลบ config สำคัญ, schema, lockfile) -> BLOCK (exit 1)
    • verdict == "block" ที่ confidence >= 0.70 -> BLOCK (exit 1)
    • ถ้าไม่มีคีย์หรือ API timeout จะ fail-open (exit 0) เพื่อไม่ให้ทำงานออฟไลน์ไม่ได้ และรองรับ JEV_GUARD_SKIP=1 หรือ JEV_GUARD_ONLY

วิธีเอาแนวคิดนี้ไปตั้งบนเครื่องของคุณ (Step-by-Step + Master Prompt)#

สเต็ปที่ 1: เปิด Global Git Hooks พื้นฐานภายใน 2 นาที#

  1. ติดตั้ง gitleaks ในเครื่อง และสร้างโฟลเดอร์ ~/.githooks
  2. ตั้งค่า Git ส่วนกลางด้วย git config --global core.hooksPath ~/.githooks
  3. วางสคริปต์ pre-commit เรียก gitleaks protect --staged --verbose (และต่อด้วยตัวตรวจ diff เชิงความหมาย) พร้อมเพิ่ม pre-push เช็ก @{u}..HEAD

สเต็ปที่ 2: Master Prompt สำหรับสั่ง AI Agent สร้าง Outbound Privacy Middleware#

ช่วยสร้าง Outbound AI Privacy Middleware ด้วย Node.js (zero external dependencies) สำหรับคั่นหน้า Local LLM Proxy พร้อมระบบ Role-Aware Scrubbing ตามเงื่อนไขนี้:
1. Stage 1 Deterministic Scrubber (~<1ms):
- สร้าง SECRETS_PRESET (OpenAI, Anthropic, GitHub PAT, AWS, Google API Key, JWT, Private Key, DB URI, Bearer, .env secrets) และบังคับรันกับทุก role (user, system, assistant, tool)
- สร้าง PDPA_PCI_PRESET (บัตรประชาชนไทย 13 หลักตรวจ Modulo-11, บัตรเครดิตตรวจ Luhn, เบอร์โทรไทย, อีเมลโดยยกเว้น @example.com/@localhost) และรันเฉพาะ role: "user" เพื่อไม่ให้กระทบโค้ดที่ agent อ่านผ่าน tool
2. Stage 2 Semantic Classifier (Toggleable):
- เพิ่ม endpoint POST /v1/privacy/config สำหรับเปิด/ปิดและสลับโหมด off | selective | always แบบ runtime
3. Zero-Leak Audit Logger:
- บันทึกเฉพาะ rule name และข้อความหลัง redact แล้วลง privacy-audit.jsonl พร้อม rotation และ tail read แบบจำกัดขนาด (เช่น 64 KB)
4. Unit Tests:
- เขียน test ยืนยันว่า timestamp 13 หลักไม่โดนจับเป็นเลขบัตร ปชช., test email ไม่โดน redact ใน role tool, และ secret ใน .env ถูก redact ในทุก role

จุดที่ติดจริงหน้างานและวิธีแก้#

ปัญหาที่เจอจริงผลกระทบวิธีแก้ที่ทำจริง
Git Hooks ไม่สืบทอด Env ของ Agentเวลาสั่ง commit/push จาก Git Hook ตัวตรวจหา TYPESAFE_API_KEY ใน os.environ ไม่เจอเพิ่มตัวอ่าน fallback จาก %LOCALAPPDATA%/hermes/.env แบบไม่รันสคริปต์ (ให้ process env ชนะ .env เสมอ)
อัปเดต 9router แล้ว Middleware หลุดการรัน npm i -g 9router@latest ทับไฟล์ custom-server.js ทำให้ Privacy Hook หายเขียนสคริปต์ npm run patch-9router (scripts/patch-9router.js) สำรองไฟล์เดิมและผูก wrapHttpHandler กลับให้อัตโนมัติ
พอร์ต Python ช้าเวลาคอมมิตบ่อยสตาร์ต Python interpreter ทุกรอบที่ commitพอร์ตเป็นไบนารี Rust (jev-git-guard-rs) พร้อม Parity Harness (mock_jev.py + run_parity.sh) เทียบ stdout/exit code ให้ตรงกัน 100%

ผลการทดสอบและตัวเลขวัดผลจริง#

  • Stage 1 Latency: ~0.3 ms ต่อ Request (ผ่าน unit & E2E tests 7/7 ใน ai-privacy-gateway)
  • Stage 2 Jev Semantic Evaluation (TypeSafe jev-1.14.0):
    • คำถามโค้ดทั่วไป (Merge Sort) -> level: "public", sensitivityScore: 0, flagged: false
    • ข้อความสรุปยอดขาย/กำไรโรงงาน + ข้อมูลลูกค้า -> Stage 1 เซ็นเซอร์เลขบัตร ปชช. + เบอร์โทรทันที และ Stage 2 ประเมิน level: "confidential", sensitivityScore: 0.7933, containsRealPii: 0.78, flagged: true ในเวลา 322.7 ms
  • Git Hooks (gitleaks + jev-git-guard): สแกน staged diff + unpushed commits จริงในเครื่อง (~180 ms สำหรับ gitleaks และ ~300–450 ms สำหรับ jev-git-guard Rust binary); โดย jev-git-guard-rs ผ่าน 8/8 unit tests และ 7/7 parity scenarios
กัน Secret และข้อมูล PDPA/PCI ก่อนหลุดเข้า LLM หรือขึ้น Git ด้วย AI Privacy Gateway + Git Guard
https://www.chinnakrit.dev/th/posts/ai-privacy-gateway-and-git-guard/
ผู้เขียน
chinnakrit
เผยแพร่
2026-11-13
สัญญาอนุญาต
CC BY-NC-SA 4.0
© 2026 chinnakrit
RSS / Sitemap
Powered by Astro & Fuwari
© 2026 chinnakrit
RSS / Sitemap
Powered by Astro & Fuwari