Communitygithub.com

lguidolin/postgres-postgraphile-rls-and-sql

PostgreSQL、PostGraphile構成、行レベルセキュリティポリシー、SQLスキーマファイルの作成、およびブラウザ→アプリ→PostGraphile→Postgresのデータパスに関する作業に使用します。キーワード — RLS、SECURITY DEFINER、search_path、pgSettings、GRANT、ロール、GraphQL深さ制限、クエリコスト、statement_timeout、SQLファイル構成。

postgres-postgraphile-rls-and-sql とは?

このスキルは、PostgreSQL、PostGraphile、行レベルセキュリティ(RLS)、およびSQLに関する専門知識を提供します。「多層防御」アーキテクチャを実装し、RLSを最終防御壁として、ただ一つの正規データパス(ブラウザ→アプリサーバー→PostGraphile→PostgreSQL)を強制します。アプリサーバーは認可ロジックを持たず、セッションコンテキストをヘッダとしてのみ渡し、データベース側で検証・適用します。具体的なルールとして、RLSを最終適用層とすること、ロールは実際のセッション状態(未ログイン、組織なしログイン、組織ありログイン、接続専用ロール、マイグレーション専用ロール)をモデル化すること、SECURITY DEFINER関数ではsearch_pathをインラインで設定すること、欠損値には空文字ではなくNULLを使用すること、GraphQLのDoS対策としてクエリ深さ制限・コスト分析・ページネーション上限・statement_timeoutを設定することなどを含みます。SQLスタイルとしては、1ファイル1オブジェクト、schema/object_type/name構成、依存順インクルード、インラインコメント(COMMENT ON)を推奨します。対応プラットフォームは、PostgreSQL、PostGraphile、および関連するSQLツール全般です。

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

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

Postgres、PostGraphile、RLSおよびSQL

概要

このスタックのデータ層メカニズムです。多層防御(RLSを最終壁とする)および唯一の正規データパスを実装します。スタック固有の内容であり、PostGraphile/Postgresを使用しないプロジェクトには適用されません。

データパス

ブラウザ → アプリサーバー → PostGraphile → PostgreSQL。 唯一の正規パスです。

  • ブラウザはGraphQL/DBと直接通信しません。アプリはアプリケーションデータのためにDBに直接問い合わせしません。
  • アプリはセッションコンテキストをヘッダとしてのみ(ユーザーID、組織ヒント)渡します。アプリ内に認可ロジックはありません。コンテキストは下流で検証され、信頼されません。
  • 単一パス = 認証コンテキスト、検証、およびRLSを強制する単一の場所です。サイドチャネルは強制を回避する穴です。

セキュリティルール

  • RLSは最終適用層です。 アプリ層のチェックはUXであり、データベースが壁です。上位層のバグがあっても、他のテナントの行が漏洩することはありません。
  • ロールは実際のセッション状態をモデル化します — 未ログイン、組織なしログイン、組織ありログイン、接続専用ロール、マイグレーション専用ロール。ロールには意味があります。
  • 特権接続ロールはアプリケーションクエリを実行しません。 アプリクエリは制約されたRLS対象ロールの下で実行されます。
  • SECURITY DEFINER関数は定義内でsearch_pathをインライン設定します — 毎回、例外なく。
  • 欠損はNULLであり、空文字列ではありません。 空文字列センチネルは禁止されています。欠損時は値を省略します。
  • GraphQLはDoS面です — 制限をかけます。 クエリ深さ制限およびコスト/複雑性分析を強制し、予算超過クエリを拒否します。ページネーションサイズに上限を設けます。アプリロールにDBの**statement_timeout**を設定します。意図的に必要としない限り、プロダクションでイントロスペクションを無効化します。

SQLスタイル

  • 1ファイル1オブジェクト — 各テーブル、関数、ポリシー、GRANTはそれぞれ独自のファイルに。
  • schema/object_type/nameで構成 — ディレクトリはデータベースの構成を反映します。
  • インクルード順序は依存順序です。 インクルードマニフェストが目次です。
  • インラインで文書化します。 COMMENT ONはオブジェクトと同じファイルにあります — 何をそしてなぜ。
  • 説明的なエイリアス(テーブル名の単数形、単一文字ではない)。Postgresの慣用に従います(current_*ゲッター)。
  • 型およびユーザー向けロールはデータであり、ハードコードされた制約ではありません — キー/ラベル/説明を持つルックアップテーブル、FKで検証、スキーマ変更なしで拡張可能。

クイックリファレンス

ルールチェック内容
新しい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.

関連スキル