opencode-supabase 0.1.1 → 0.1.2-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -0
- package/package.json +3 -1
- package/skills/.upstream.json +13 -0
- package/skills/supabase/SKILL.md +112 -0
- package/skills/supabase/assets/feedback-issue-template.md +17 -0
- package/skills/supabase/references/skill-feedback.md +17 -0
- package/skills/supabase-postgres-best-practices/SKILL.md +64 -0
- package/skills/supabase-postgres-best-practices/references/_contributing.md +170 -0
- package/skills/supabase-postgres-best-practices/references/_sections.md +39 -0
- package/skills/supabase-postgres-best-practices/references/_template.md +34 -0
- package/skills/supabase-postgres-best-practices/references/advanced-full-text-search.md +55 -0
- package/skills/supabase-postgres-best-practices/references/advanced-jsonb-indexing.md +49 -0
- package/skills/supabase-postgres-best-practices/references/conn-idle-timeout.md +46 -0
- package/skills/supabase-postgres-best-practices/references/conn-limits.md +44 -0
- package/skills/supabase-postgres-best-practices/references/conn-pooling.md +41 -0
- package/skills/supabase-postgres-best-practices/references/conn-prepared-statements.md +46 -0
- package/skills/supabase-postgres-best-practices/references/data-batch-inserts.md +54 -0
- package/skills/supabase-postgres-best-practices/references/data-n-plus-one.md +53 -0
- package/skills/supabase-postgres-best-practices/references/data-pagination.md +50 -0
- package/skills/supabase-postgres-best-practices/references/data-upsert.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-advisory.md +56 -0
- package/skills/supabase-postgres-best-practices/references/lock-deadlock-prevention.md +68 -0
- package/skills/supabase-postgres-best-practices/references/lock-short-transactions.md +50 -0
- package/skills/supabase-postgres-best-practices/references/lock-skip-locked.md +54 -0
- package/skills/supabase-postgres-best-practices/references/monitor-explain-analyze.md +45 -0
- package/skills/supabase-postgres-best-practices/references/monitor-pg-stat-statements.md +55 -0
- package/skills/supabase-postgres-best-practices/references/monitor-vacuum-analyze.md +55 -0
- package/skills/supabase-postgres-best-practices/references/query-composite-indexes.md +44 -0
- package/skills/supabase-postgres-best-practices/references/query-covering-indexes.md +40 -0
- package/skills/supabase-postgres-best-practices/references/query-index-types.md +48 -0
- package/skills/supabase-postgres-best-practices/references/query-missing-indexes.md +43 -0
- package/skills/supabase-postgres-best-practices/references/query-partial-indexes.md +45 -0
- package/skills/supabase-postgres-best-practices/references/schema-constraints.md +80 -0
- package/skills/supabase-postgres-best-practices/references/schema-data-types.md +46 -0
- package/skills/supabase-postgres-best-practices/references/schema-foreign-key-indexes.md +59 -0
- package/skills/supabase-postgres-best-practices/references/schema-lowercase-identifiers.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-partitioning.md +55 -0
- package/skills/supabase-postgres-best-practices/references/schema-primary-keys.md +61 -0
- package/skills/supabase-postgres-best-practices/references/security-privileges.md +54 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-basics.md +50 -0
- package/skills/supabase-postgres-best-practices/references/security-rls-performance.md +57 -0
- package/src/server/index.ts +6 -0
- package/src/server/skills.ts +111 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Apply Principle of Least Privilege
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Reduced attack surface, better audit trail
|
|
5
|
+
tags: privileges, security, roles, permissions
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Apply Principle of Least Privilege
|
|
9
|
+
|
|
10
|
+
Grant only the minimum permissions required. Never use superuser for application queries.
|
|
11
|
+
|
|
12
|
+
**Incorrect (overly broad permissions):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Application uses superuser connection
|
|
16
|
+
-- Or grants ALL to application role
|
|
17
|
+
grant all privileges on all tables in schema public to app_user;
|
|
18
|
+
grant all privileges on all sequences in schema public to app_user;
|
|
19
|
+
|
|
20
|
+
-- Any SQL injection becomes catastrophic
|
|
21
|
+
-- drop table users; cascades to everything
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Correct (minimal, specific grants):**
|
|
25
|
+
|
|
26
|
+
```sql
|
|
27
|
+
-- Create role with no default privileges
|
|
28
|
+
create role app_readonly nologin;
|
|
29
|
+
|
|
30
|
+
-- Grant only SELECT on specific tables
|
|
31
|
+
grant usage on schema public to app_readonly;
|
|
32
|
+
grant select on public.products, public.categories to app_readonly;
|
|
33
|
+
|
|
34
|
+
-- Create role for writes with limited scope
|
|
35
|
+
create role app_writer nologin;
|
|
36
|
+
grant usage on schema public to app_writer;
|
|
37
|
+
grant select, insert, update on public.orders to app_writer;
|
|
38
|
+
grant usage on sequence orders_id_seq to app_writer;
|
|
39
|
+
-- No DELETE permission
|
|
40
|
+
|
|
41
|
+
-- Login role inherits from these
|
|
42
|
+
create role app_user login password 'xxx';
|
|
43
|
+
grant app_writer to app_user;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Revoke public defaults:
|
|
47
|
+
|
|
48
|
+
```sql
|
|
49
|
+
-- Revoke default public access
|
|
50
|
+
revoke all on schema public from public;
|
|
51
|
+
revoke all on all tables in schema public from public;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Reference: [Roles and Privileges](https://supabase.com/blog/postgres-roles-and-privileges)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Enable Row Level Security for Multi-Tenant Data
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: Database-enforced tenant isolation, prevent data leaks
|
|
5
|
+
tags: rls, row-level-security, multi-tenant, security
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Enable Row Level Security for Multi-Tenant Data
|
|
9
|
+
|
|
10
|
+
Row Level Security (RLS) enforces data access at the database level, ensuring users only see their own data.
|
|
11
|
+
|
|
12
|
+
**Incorrect (application-level filtering only):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Relying only on application to filter
|
|
16
|
+
select * from orders where user_id = $current_user_id;
|
|
17
|
+
|
|
18
|
+
-- Bug or bypass means all data is exposed!
|
|
19
|
+
select * from orders; -- Returns ALL orders
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Correct (database-enforced RLS):**
|
|
23
|
+
|
|
24
|
+
```sql
|
|
25
|
+
-- Enable RLS on the table
|
|
26
|
+
alter table orders enable row level security;
|
|
27
|
+
|
|
28
|
+
-- Create policy for users to see only their orders
|
|
29
|
+
create policy orders_user_policy on orders
|
|
30
|
+
for all
|
|
31
|
+
using (user_id = current_setting('app.current_user_id')::bigint);
|
|
32
|
+
|
|
33
|
+
-- Force RLS even for table owners
|
|
34
|
+
alter table orders force row level security;
|
|
35
|
+
|
|
36
|
+
-- Set user context and query
|
|
37
|
+
set app.current_user_id = '123';
|
|
38
|
+
select * from orders; -- Only returns orders for user 123
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Policy for authenticated role:
|
|
42
|
+
|
|
43
|
+
```sql
|
|
44
|
+
create policy orders_user_policy on orders
|
|
45
|
+
for all
|
|
46
|
+
to authenticated
|
|
47
|
+
using (user_id = auth.uid());
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reference: [Row Level Security](https://supabase.com/docs/guides/database/postgres/row-level-security)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Optimize RLS Policies for Performance
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 5-10x faster RLS queries with proper patterns
|
|
5
|
+
tags: rls, performance, security, optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Optimize RLS Policies for Performance
|
|
9
|
+
|
|
10
|
+
Poorly written RLS policies can cause severe performance issues. Use subqueries and indexes strategically.
|
|
11
|
+
|
|
12
|
+
**Incorrect (function called for every row):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create policy orders_policy on orders
|
|
16
|
+
using (auth.uid() = user_id); -- auth.uid() called per row!
|
|
17
|
+
|
|
18
|
+
-- With 1M rows, auth.uid() is called 1M times
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (wrap functions in SELECT):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
create policy orders_policy on orders
|
|
25
|
+
using ((select auth.uid()) = user_id); -- Called once, cached
|
|
26
|
+
|
|
27
|
+
-- 100x+ faster on large tables
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Use security definer functions for complex checks:
|
|
31
|
+
|
|
32
|
+
```sql
|
|
33
|
+
-- Create helper function (runs as definer, bypasses RLS)
|
|
34
|
+
create or replace function is_team_member(team_id bigint)
|
|
35
|
+
returns boolean
|
|
36
|
+
language sql
|
|
37
|
+
security definer
|
|
38
|
+
set search_path = ''
|
|
39
|
+
as $$
|
|
40
|
+
select exists (
|
|
41
|
+
select 1 from public.team_members
|
|
42
|
+
where team_id = $1 and user_id = (select auth.uid())
|
|
43
|
+
);
|
|
44
|
+
$$;
|
|
45
|
+
|
|
46
|
+
-- Use in policy (indexed lookup, not per-row check)
|
|
47
|
+
create policy team_orders_policy on orders
|
|
48
|
+
using ((select is_team_member(team_id)));
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Always add indexes on columns used in RLS policies:
|
|
52
|
+
|
|
53
|
+
```sql
|
|
54
|
+
create index orders_user_id_idx on orders (user_id);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Reference: [RLS Performance](https://supabase.com/docs/guides/database/postgres/row-level-security#rls-performance-recommendations)
|
package/src/server/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Plugin } from "@opencode-ai/plugin";
|
|
|
2
2
|
|
|
3
3
|
import { createServerLogWriter, createSupabaseLogger } from "../shared/log.ts";
|
|
4
4
|
import { createSupabaseAuth } from "./auth.ts";
|
|
5
|
+
import { registerSupabaseSkillPaths } from "./skills.ts";
|
|
5
6
|
import { createSupabaseTools } from "./tools.ts";
|
|
6
7
|
|
|
7
8
|
const server: Plugin = async (input, options) => {
|
|
@@ -10,6 +11,11 @@ const server: Plugin = async (input, options) => {
|
|
|
10
11
|
});
|
|
11
12
|
|
|
12
13
|
return {
|
|
14
|
+
config: async (config) => {
|
|
15
|
+
registerSupabaseSkillPaths(config, options, {
|
|
16
|
+
warn: logger.warn,
|
|
17
|
+
});
|
|
18
|
+
},
|
|
13
19
|
auth: createSupabaseAuth(input, options, { logger }),
|
|
14
20
|
tool: createSupabaseTools(input, options, { logger }),
|
|
15
21
|
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const BUNDLED_SUPABASE_SKILLS = [
|
|
5
|
+
"supabase",
|
|
6
|
+
"supabase-postgres-best-practices",
|
|
7
|
+
] as const;
|
|
8
|
+
|
|
9
|
+
export type BundledSupabaseSkill = (typeof BUNDLED_SUPABASE_SKILLS)[number];
|
|
10
|
+
|
|
11
|
+
type Warn = (message: string, data?: Record<string, unknown>) => unknown;
|
|
12
|
+
|
|
13
|
+
type ResolverDeps = {
|
|
14
|
+
warn?: Warn;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type RegisterDeps = ResolverDeps & {
|
|
18
|
+
skillsRoot?: string;
|
|
19
|
+
exists?: (path: string) => boolean;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
type ConfigWithSkills = object & {
|
|
23
|
+
skills?: {
|
|
24
|
+
paths?: unknown;
|
|
25
|
+
} | false;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type SkillsConfig = {
|
|
29
|
+
paths?: unknown;
|
|
30
|
+
} & Record<string, unknown>;
|
|
31
|
+
|
|
32
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
33
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function pluginSkillsOption(options: unknown) {
|
|
37
|
+
if (!isRecord(options) || !("skills" in options)) return true;
|
|
38
|
+
return (options as { skills?: unknown }).skills;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function resolveEnabledSupabaseSkills(options: unknown, deps: ResolverDeps = {}) {
|
|
42
|
+
const value = pluginSkillsOption(options);
|
|
43
|
+
if (value === false) return [];
|
|
44
|
+
if (value === true || value === undefined) return [...BUNDLED_SUPABASE_SKILLS];
|
|
45
|
+
|
|
46
|
+
if (!isRecord(value)) {
|
|
47
|
+
deps.warn?.("invalid Supabase skills option; loading bundled skills", { value });
|
|
48
|
+
return [...BUNDLED_SUPABASE_SKILLS];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const known = new Set<string>(BUNDLED_SUPABASE_SKILLS);
|
|
52
|
+
for (const key of Object.keys(value)) {
|
|
53
|
+
if (!known.has(key)) {
|
|
54
|
+
deps.warn?.("unknown Supabase bundled skill option ignored", { skill: key });
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (value[key] !== undefined && typeof value[key] !== "boolean") {
|
|
58
|
+
deps.warn?.("invalid Supabase bundled skill option value", {
|
|
59
|
+
skill: key,
|
|
60
|
+
value: value[key] as unknown,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return BUNDLED_SUPABASE_SKILLS.filter((skill) => value[skill] !== false);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function defaultSkillsRoot() {
|
|
69
|
+
return path.resolve(path.dirname(new URL(import.meta.url).pathname), "../../skills");
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function registerSupabaseSkillPaths(
|
|
73
|
+
config: object,
|
|
74
|
+
options: unknown,
|
|
75
|
+
deps: RegisterDeps = {},
|
|
76
|
+
) {
|
|
77
|
+
const configWithSkills = config as ConfigWithSkills;
|
|
78
|
+
const skillsRoot = deps.skillsRoot ?? defaultSkillsRoot();
|
|
79
|
+
const exists = deps.exists ?? fs.existsSync;
|
|
80
|
+
const enabled = resolveEnabledSupabaseSkills(options, deps);
|
|
81
|
+
const skillsConfig: SkillsConfig = isRecord(configWithSkills.skills)
|
|
82
|
+
? (configWithSkills.skills as SkillsConfig)
|
|
83
|
+
: {};
|
|
84
|
+
|
|
85
|
+
if (!isRecord(configWithSkills.skills)) {
|
|
86
|
+
configWithSkills.skills = skillsConfig;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (!Array.isArray(skillsConfig.paths)) {
|
|
90
|
+
if (skillsConfig.paths !== undefined) {
|
|
91
|
+
deps.warn?.("invalid Supabase skills.paths value; resetting to array", {
|
|
92
|
+
value: skillsConfig.paths as unknown,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
skillsConfig.paths = [];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const paths = skillsConfig.paths as string[];
|
|
99
|
+
|
|
100
|
+
for (const skill of enabled) {
|
|
101
|
+
const skillPath = path.join(skillsRoot, skill);
|
|
102
|
+
if (!exists(skillPath)) {
|
|
103
|
+
deps.warn?.("bundled Supabase skill directory not found", { skill, path: skillPath });
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!paths.includes(skillPath)) {
|
|
108
|
+
paths.push(skillPath);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|