Communitygithub.com

lguidolin/Postgres-PostGraphile-RLS-與-SQL

用於撰寫 PostgreSQL、PostGraphile 設定、列級安全策略、SQL schema 檔案,或處理瀏覽器→應用程式→PostGraphile→Postgres 資料路徑時使用。關鍵字包含 RLS、SECURITY DEFINER、search_path、pgSettings、gRANTS、角色、GraphQL 深度限制、查詢成本、statement_timeout、SQL 檔案組織。

Postgres-PostGraphile-RLS-與-SQL 是什麼?

此技能為使用 PostGraphile 作為 GraphQL 層的 PostgreSQL 資料庫堆疊提供全面的準則。它強制執行「縱深防禦」安全模型,其中列級安全策略 (RLS) 是防止資料外洩的最終防線,確保即使上層發生錯誤也無法跨越租戶邊界。該技能定義了嚴格的資料路徑(瀏覽器 → 應用伺服器 → PostGraphile → PostgreSQL),並禁止任何旁路通道。它涵蓋安全規則,例如使用有意義的角色來建模真實的會話狀態、在 SECURITY DEFINER 函數中強制內聯設定 search_path,以及透過深度限制和查詢成本分析來限制 GraphQL 的攻擊面。此外,它還規定了 SQL 風格指南,例如每個檔案一個物件、基於目錄的組織以及優先使用 NULL 而非空字串,這使其對於在 PostGraphile 生態系統中維護安全、可擴展且可維護的資料庫專案的開發人員至關重要。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/lguidolin/agent-skills/tree/main/skills/postgres-postgraphile-rls-and-sql

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

Postgres、PostGraphile、RLS 與 SQL

總覽

此堆疊的資料層機制。實作縱深防禦(RLS 作為最終防線)與唯一合法資料路徑。特定於此堆疊——不適用於沒有 PostGraphile/Postgres 的專案。

資料路徑

瀏覽器 → 應用伺服器 → PostGraphile → PostgreSQL。 唯一合法的路徑。

  • 瀏覽器從不直接與 GraphQL/資料庫通訊;應用程式從不直接查詢資料庫以取得應用程式資料。
  • 應用程式僅以標頭形式傳遞工作階段上下文(使用者 ID、組織提示)——應用程式中無授權邏輯;上下文在下游驗證,而非信任。
  • 單一路徑 = 單一強制執行驗證上下文、驗證與 RLS 的位置。側通道是繞過強制執行的漏洞。

安全規則

  • RLS 是最終的強制執行層。 應用層的檢查是 UX;資料庫才是防線。即使上層發生錯誤,也無法洩漏另一個租戶的資料列。
  • 角色建模真實的工作階段狀態——未登入、已登入但無組織、已登入且有組織、僅連線角色、僅遷移角色。角色具有實際意義。
  • 特權連線角色永不執行應用程式查詢。 應用查詢在受限且受 RLS 約束的角色下執行。
  • SECURITY DEFINER 函數在其定義中內嵌設定 search_path——每次皆然,無一例外。
  • 缺席值為 NULL,絕非空字串。 禁止使用空字串作為哨兵;當值不存在時省略該值。
  • GraphQL 是 DoS 攻擊面——加以限制。 強制執行查詢深度限制與成本/複雜度分析;拒絕超額查詢。限制分頁大小。為應用角色設定資料庫的 statement_timeout。除非刻意需要,否則在生產環境中停用自省查詢。

SQL 風格

  • 每個物件一個檔案——每個資料表、函數、策略、權限授予集各自獨立一個檔案。
  • 按 schema/object_type/name 組織——目錄結構反映資料庫的組織方式。
  • 包含順序即為依賴順序; 包含清單即為目錄。
  • 內嵌文件。 COMMENT ON 與物件放在同一個檔案中——說明是什麼與為什麼。
  • 使用描述性別名(資料表名稱的單數形式,而非單一字元);遵循 Postgres 慣用法(current_* 取得器)。
  • 型別與面向使用者的角色是資料,而非硬編碼的約束——使用具有鍵/標籤/描述的查詢表,以外鍵驗證,無需 schema 變更即可擴充。

快速參考

規則檢查事項
新的 SECURITY DEFINER 函數search_path 是否已內嵌設定?
包含租戶資料的新資料表RLS 策略與拒絕測試(參見 graphql-contract-testing)?
缺席的值是 NULL,而非 ''?
新的 GraphQL 表面是否設有深度/成本限制與分頁上限?
新的物件是否在獨立檔案、依賴順序包含、內嵌 COMMENT ON?

完整原理:參閱守則第 XIII、XIV、XVIII 條,收錄於 engineering-constitution/references/engineering-constitution.md。遷移:zero-downtime-migrations。合約測試:graphql-contract-testing。

Individual skills in this repo

This repo contains 18 individual skills — each has its own dedicated page.

lguidolin/change-hygiene-and-code-craft

Use when writing or refactoring code, structuring a commit or PR, or deciding whether to abstract duplication. Symptoms — mixing reorg with logic changes, a PR doing several things at once, a file growing large, the second copy of similar code, or unsure whether to DRY something up.

lguidolin/cloud-delivery-aks

Use when deploying to Kubernetes or Azure Kubernetes Service (AKS), configuring cloud secrets, setting up progressive rollout/canary, per-PR ephemeral environments, or k8s health probes. Keywords — Kubernetes, AKS, Key Vault, Argo Rollouts, Flagger, canary, blue-green, liveness, readiness, PodDisruptionBudget, HPA, rollback, GHCR.

lguidolin/commit-history-rewrite

Use when an existing repository has messy commit history that needs to conform to conventional commits before adopting release-please, or when intermediate WIP/fixup/merge commits need to be cleaned up.

lguidolin/conventional-commits-and-releases

Use when committing, writing a commit message, opening a PR that will be squash-merged, or configuring automated versioning/changelogs. Keywords — conventional commits, release-please, semver, feat/fix/chore, breaking change, changelog.

lguidolin/defense-in-depth-security

Use when handling untrusted input, secrets, authentication/authorization, or dependencies — or threat-modeling a new surface. Keywords — STRIDE, threat model, least privilege, secrets management, supply chain, dependency scanning, input validation, audit log, defense in depth.

lguidolin/designing-before-building

Use when starting a feature, fixing a non-trivial bug, or about to write implementation code — before any code exists. Symptoms you need this: "this is simple, I'll just code it", reaching for the editor before a design is approved, or an idea that hasn't been turned into a spec and plan.

lguidolin/engineering-constitution

Use when starting work in a project that follows the engineering constitution, orienting to its rules, or deciding which engineering practice applies to a task — spec writing, commits, testing, security, deploys, database, or UI work.

lguidolin/graphql-contract-testing

Use when writing a GraphQL query/mutation that the UI and a test will share, or building route/schema contract or smoke tests. Symptoms — copying a query into a test, a test asserting on query text, schema change that didn't break the UI build, or RLS/permission drift. Keywords — graphql-codegen, typed document, contract test, route smoke test.

lguidolin/init-repo-CI

Use when setting up a new repository with conventional commits, release-please, and CI automation, or when retrofitting an existing repository that lacks automated versioning and PR validation workflows.

lguidolin/interface-craft-and-accessibility

Use when building or styling UI — components, layouts, forms, design tokens — or making accessibility decisions. Keywords — a11y, WCAG, keyboard navigation, focus state, contrast, design system, minimalist UI, component reuse, ARIA, semantic HTML.

lguidolin/merge-gates-and-automation

Use when setting up or changing CI, pre-push hooks, or a task runner, or deciding what must pass before merge. Symptoms — tempted to put authoritative checks only in a local hook, skip CI, bypass with --no-verify, or unsure what gates a merge vs. runs locally.

lguidolin/observability-and-slos

Use when adding logging, metrics, tracing, health checks, SLOs, or alerting — or when building a service surface that needs to be operable and debuggable. Keywords — structured logs, OpenTelemetry, correlation id, RED metrics, liveness, readiness, SLI, SLO, error budget, alerting.

lguidolin/performance-and-scale

Use when working on hot paths, list endpoints, pagination, data-access in loops, or public interfaces/schemas. Symptoms — unbounded queries, N+1 access, no latency budget, optimizing without measuring, or changing an interface many consumers depend on. Keywords — pagination, N+1, Hyrum's Law, performance budget, bundle size.

lguidolin/recording-decisions

Use when a design or architecture decision has been made and needs to be captured — writing a decision record or ADR, updating a decision index, noting a deferred idea, or superseding a past decision. Keywords — ADR, decision record, rationale, rejected alternatives, dependency index.

lguidolin/resilience-and-deploy-safety

Use when planning a deploy, designing a rollback, or responding to an incident or writing a postmortem. Keywords — deploy safety, rollback, immutable artifact, progressive delivery, canary, blast radius, incident response, blameless postmortem, error budget.

lguidolin/ship-it

Use when the user wants to ship work — push, PR, archive decision records, merge, and clean up. Handles the full lifecycle from committing final changes through post-merge cleanup including converting specs/plans to compact decision records.

lguidolin/tests-as-a-control

Use when writing or modifying tests, when a test breaks during a refactor, or when testing permission/role rules. Symptoms — tempted to edit a test to make it pass, testing only the happy path, a deny-test that started passing, flaky tests, or unsure what to assert.

lguidolin/zero-downtime-migrations

Use when changing a database schema where data must survive the change — adding/removing/renaming columns, constraints, indexes, or backfilling. Symptoms — a destructive migration bundled with a code deploy, a NOT NULL column with a backfill, a table-locking UPDATE, or a rename. Keywords — expand/contract, parallel change, backfill, NOT VALID, CREATE INDEX CONCURRENTLY, graphile-migrate.

相關技能