KB-414A

§2.4-2.6 K2 — 4 Bypass L3 + Lỗ hổng khác

6 min read Revision 1
s176pg-reconfigk2bypassinvestigate

§2.4-2.6 K2 — 4 BYPASS L3 + LỖ HỔNG KHÁC + DIRECTUS FLOWS SẠCH

Part of: PG Reform Tools — S176 mission Prev: 02-k0-k1-timeline-channels.md ← | → Next: 04-ks1-ddl-audit-gap.md Khối điều tra: K2 (L3 Bypass Writers)


§2.4 Bypass thực sự ngoài dot/bin/ — 4 BYPASS L3 CHỦ ĐÍCH

L3-1 — CI CAT-ALL UPDATE với session bypass

File: .github/workflows/deploy-vps.yml dòng 328

docker exec postgres psql -U directus -d directus -c "
  SELECT set_config('app.allow_meta_update', 'true', true);
  UPDATE meta_catalog SET record_count=... WHERE code='CAT-ALL';
  UPDATE meta_catalog SET record_count=... WHERE code='CAT-MOL';
  UPDATE meta_catalog SET record_count=... WHERE code='CAT-CMP';
  UPDATE meta_catalog SET record_count=... WHERE code='CAT-MAT';
  UPDATE meta_catalog SET record_count=... WHERE code='CAT-PRD';
  UPDATE meta_catalog SET record_count=... WHERE code='CAT-BLD';
"

Bypass có chủ đích qua session variable. PG có guard fn_guard_meta_catalog_update đọc current_setting('app.allow_meta_update') = backdoor. CI tận dụng để update CAT-ALL sau deploy. Tự tài liệu hoá trong web/server/api/registry/refresh-counts.post.ts ("CAT-ALL is updated by deploy step ... with app.allow_meta_update bypass") — known hack, ai cũng biết.

Fix S176: Thay bằng DOT-CAT-ALL-REFRESH (Điều 33 §15.2). Loại bỏ session bypass, dùng fn_refresh_cat_all() SECURITY DEFINER. Guard luôn ON.

L3-2 — CI auto-apply 36 SQL file

File: .github/workflows/deploy-vps.yml dòng 197-222

for sql_file in "$MIGRATION_DIR"/*.sql; do
  if [ ! -f "$MARKER_DIR/$base.done" ]; then
    docker exec -i postgres psql -U directus -d directus < "$sql_file"
    touch "$MARKER_DIR/$base.done"
  fi
done

36 file SQL nằm vĩnh viễn trong sql/, marker .done ngăn chạy lại nhưng file không xoá. Bao gồm:

  • s175_law_dedupe_p1.sql (UPDATE knowledge_documents với hardcoded ID list)
  • s175_source_id_current_unique_p2.sql
  • s161b_*, s162b_*, s164_*
  • s133_measurement_framework.sql, s134_pg_grants.sql
  • kb_protection_phase2_*.sql
  • vps_pg_schema_incomex_metadata_kb_20260405.sql — pg_dump snapshot có \restrict <token>, sensitive
  • 0048_link_policies_mysql.sql — H2 dead code dùng MySQL UUID() cú pháp, fail trên PG

Bonus KS.2: 6 file SQL CHƯA có marker .sql-applied/ (30/36 đã apply, 6 chưa):

  • s175_law_dedupe_p1.sql, s175_source_id_current_unique_p2.sql
  • 0048_link_policies_mysql.sql — xác nhận K2 hypothesis chưa từng apply thành công
  • kb_protection_phase2_*.sql (3 file)
  • vps_pg_schema_incomex_metadata_kb_20260405.sql
  • Marker system bắt đầu 2026-03-24, sau sự kiện 2026-03-10 ~2 tuần → các file SQL trước đó chạy mà không có marker

Fix S176: Thay bằng DOT-MIGRATION-APPLY (Điều 33 §15.1). File SQL phải có header -- DOT-MIGRATION-APPLY-OK. Apply xong UPDATE bảng migration_applied thay marker filesystem. Có DOT cặp dot-migration-verify. File retire phải DELETE hoặc move sql/archive/.

L3-3 — pg-client.js (Điều 31 runner)

File: scripts/integrity/pg-client.js

const { Client } = require('pg');
const connStr = process.env.DATABASE_URL;  // postgres://incomex:***@postgres:5432/directus
await client.query(
  `INSERT INTO measurement_log (run_id, measurement_id, result, source_value, target_value, delta)
   VALUES ($1, $2, $3, $4, $5, $6)`, [...]);

Bypass L3 thuần: Node.js pg lib, connect user incomex qua DATABASE_URL, INSERT thẳng bảng measurement_log trong DB directus. Toàn bộ Điều 31 integrity runner không có entry trong dot_tools. dedupe.js + main.js đi qua Directus API (L1 via Directus) nhưng orchestrator chính KHÔNG đăng ký.

Fix S176: Đăng ký vào dot_tools thành DOT chính thức cho Điều 31 runner. Hợp pháp hoá L1 yếu thành L1 chuẩn. Dùng password GSM thay DATABASE_URL riêng.

L3-4 — dot-script-lint, dot-cron-matrix-setup

Files: dot/bin/dot-script-lint, dot/bin/dot-cron-matrix-setup

  • Direct docker exec postgres psql
  • KS.3 verify: dot-script-lint CÓ metadata DOT_SCRIPT_LINT nhưng file_path=NULL; dot-cron-matrix-setup KHÔNG có metadata nào (installer one-shot không được định nghĩa là DOT)
  • dot-script-lint linting PG-related script nhưng chính nó là bypass (self-exempt — xem KS.3 WHY-A)

Fix S176: Đăng ký vào dot_tools, gắn paired_dot, dùng password GSM.


§2.5 Lỗ hổng nghiêm trọng khác (K2)

  1. Password leak: agent-data-test/scripts/import_firestore_to_pg.py có hardcoded Incomex2026PG_306ac539ad365fce rò rỉ git history. PHẢI ĐỔI password ngay khi triển khai phương án A.

  2. 5 cron PG function không có installer DOT: trong crontab/production.crontab chỉ 1/6 cron có installer (dot-cron-pivot-setup DOT-308). 5 còn lại không rõ ai cài.

  3. 2 crontab trong repo: crontab/production.crontab (live) vs infra/cron/incomex-crontab (dead — vẫn còn MySQL backup).

  4. 0048_link_policies_mysql.sql dead code — MySQL UUID() syntax fail trên PG.

  5. directus-schema.md KB doc đã lạc hậu từ S139.

  6. Birth registry trigger không chạy: count = 0.

  7. TD-375: v_registry_counts đang là TABLE writable thay vì VIEW.

  8. TD-376: system_issues thiếu authenticity guard → gốc bệnh 9 writer S175.


§2.6 Directus Flows — SẠCH (K2)

128 flows / 320 operations. 15 exec ops (toàn JS code generation cho assign_code, KHÔNG có raw SQL). 78 item-create + 14 item-update + 1 item-delete đi qua Directus item ops = L1 chuẩn. Flows KHÔNG phải nguồn bypass.


File 03 — §2.4-2.6 K2 | Index | S176 | Living doc