Communitygithub.com

PostgreSQL / PostGraphile / RLS 与 SQL

在编写 PostgreSQL、PostGraphile 配置、行级安全策略、SQL 模式文件,或处理浏览器→应用→PostGraphile→Postgres 数据路径时使用。关键词 — RLS、SECURITY DEFINER、search_path、pgSettings、授权、角色、GraphQL 深度限制、查询成本、statement_timeout、SQL 文件组织。

PostGraphile / RLS 与 SQL 是什么?

该技能定义了基于 PostGraphile 和 PostgreSQL 的堆栈中的数据层机制,强调“纵深防御”原则,以行级安全(RLS)作为最终防线,并强制执行“双刀流——唯一合法数据路径”。它规定了浏览器必须通过应用服务器、PostGraphile 再到 PostgreSQL 的严格单向路径,应用仅通过标头传递会话上下文,不执行任何授权逻辑。技能详细说明了安全规则:RLS 作为最终强制执行层,角色映射真实会话状态,特权连接角色不执行应用查询,所有 SECURITY DEFINER 函数必须在定义中内联设置 search_path,缺失值使用 NULL 而非空字符串,以及通过深度限制、成本分析、分页大小限制和 stabase_timeout 来限制 GraphQL 的 DoS 攻击面。SQL 风格方面,要求每个对象一个文件,按 schema/object_type/name 组织,包含顺序依赖,内联 COMMENT ON 文档,使用描述性别名,并将类型和面向用户的角色视为通过外键验证的可扩展查找表数据。该技能适用于使用 PostGraphile 和 PostgreSQL 的特定堆栈项目,不适用于没有这些组件的项目。

兼容平台~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 是最终强制执行层。 应用层的检查只是用户体验;数据库才是壁垒。即使上层存在错误,也不会泄露其他租户的行。
  • 角色模拟真实会话状态——未登录、已登录无组织、已登录有组织、仅连接角色、仅迁移角色。角色具有实际意义。
  • 特权连接角色从不执行应用查询。 应用查询在受限、受 RLS 约束的角色下运行。
  • SECURITY DEFINER 函数在其定义中内联设置 search_path ——每次,无一例外。
  • 缺失值为 NULL,绝不能是空字符串。 禁止使用空字符串哨兵;缺失时应省略该值。
  • GraphQL 是 DoS 攻击面——必须加以约束。 强制查询深度限制和成本/复杂度分析;拒绝超出预算的查询。限制分页大小。为应用角色设置数据库**statement_timeout**。在生产环境中禁用内省,除非故意需要。

SQL 风格

  • 每个对象一个文件——每个表、函数、策略、授权集各存于其自身文件中。
  • 按 schema/object_type/name 组织——目录镜像数据库的组织方式。
  • 包含顺序遵循依赖顺序; 包含清单就是目录。
  • 内联文档。 COMMENT ON 与对象位于同一文件中——说明是什么和为什么。
  • 描述性别名(表名的单数形式,而非单个字母);遵循 Postgres 习惯用法(current_* getter)。
  • 类型和面向用户的角色是数据,而非硬编码的约束——使用键/标签/描述构成的查找表,通过外键验证,可在不更改模式的情况下扩展。

快速参考

规则检查项
新的 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.

相关技能