postgresai 0.15.0-dev.1 → 0.15.0-dev.10

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/lib/util.ts CHANGED
@@ -70,15 +70,18 @@ export function maskSecret(secret: string): string {
70
70
  export interface RootOptsLike {
71
71
  apiBaseUrl?: string;
72
72
  uiBaseUrl?: string;
73
+ storageBaseUrl?: string;
73
74
  }
74
75
 
75
76
  export interface ConfigLike {
76
77
  baseUrl?: string | null;
78
+ storageBaseUrl?: string | null;
77
79
  }
78
80
 
79
81
  export interface ResolvedBaseUrls {
80
82
  apiBaseUrl: string;
81
83
  uiBaseUrl: string;
84
+ storageBaseUrl: string;
82
85
  }
83
86
 
84
87
  /**
@@ -105,17 +108,20 @@ export function normalizeBaseUrl(value: string): string {
105
108
  export function resolveBaseUrls(
106
109
  opts?: RootOptsLike,
107
110
  cfg?: ConfigLike,
108
- defaults: { apiBaseUrl?: string; uiBaseUrl?: string } = {}
111
+ defaults: { apiBaseUrl?: string; uiBaseUrl?: string; storageBaseUrl?: string } = {}
109
112
  ): ResolvedBaseUrls {
110
113
  const defApi = defaults.apiBaseUrl || "https://postgres.ai/api/general/";
111
114
  const defUi = defaults.uiBaseUrl || "https://console.postgres.ai";
115
+ const defStorage = defaults.storageBaseUrl || "https://postgres.ai/storage";
112
116
 
113
117
  const apiCandidate = (opts?.apiBaseUrl || process.env.PGAI_API_BASE_URL || cfg?.baseUrl || defApi) as string;
114
118
  const uiCandidate = (opts?.uiBaseUrl || process.env.PGAI_UI_BASE_URL || defUi) as string;
119
+ const storageCandidate = (opts?.storageBaseUrl || process.env.PGAI_STORAGE_BASE_URL || cfg?.storageBaseUrl || defStorage) as string;
115
120
 
116
121
  return {
117
122
  apiBaseUrl: normalizeBaseUrl(apiCandidate),
118
123
  uiBaseUrl: normalizeBaseUrl(uiCandidate),
124
+ storageBaseUrl: normalizeBaseUrl(storageCandidate),
119
125
  };
120
126
  }
121
127
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresai",
3
- "version": "0.15.0-dev.1",
3
+ "version": "0.15.0-dev.10",
4
4
  "description": "postgres_ai CLI",
5
5
  "license": "Apache-2.0",
6
6
  "private": false,
@@ -28,7 +28,7 @@
28
28
  "embed-metrics": "bun run scripts/embed-metrics.ts",
29
29
  "embed-checkup-dictionary": "bun run scripts/embed-checkup-dictionary.ts",
30
30
  "embed-all": "bun run embed-metrics && bun run embed-checkup-dictionary",
31
- "build": "bun run embed-all && bun build ./bin/postgres-ai.ts --outdir ./dist/bin --target node && node -e \"const fs=require('fs');const f='./dist/bin/postgres-ai.js';fs.writeFileSync(f,fs.readFileSync(f,'utf8').replace('#!/usr/bin/env bun','#!/usr/bin/env node'))\" && cp -r ./sql ./dist/sql",
31
+ "build": "bun run embed-all && bun build ./bin/postgres-ai.ts --outdir ./dist/bin --target node && node -e \"const fs=require('fs');const f='./dist/bin/postgres-ai.js';fs.writeFileSync(f,fs.readFileSync(f,'utf8').replace('#!/usr/bin/env bun','#!/usr/bin/env node'))\" && cp -r ./sql ./dist/sql && cp ../instances.demo.yml ./instances.demo.yml",
32
32
  "prepublishOnly": "npm run build",
33
33
  "start": "bun ./bin/postgres-ai.ts --help",
34
34
  "start:node": "node ./dist/bin/postgres-ai.js --help",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@modelcontextprotocol/sdk": "^1.20.2",
44
- "commander": "^12.1.0",
44
+ "commander": "^14.0.3",
45
45
  "js-yaml": "^4.1.0",
46
46
  "pg": "^8.16.3"
47
47
  },
@@ -51,7 +51,7 @@
51
51
  "@types/pg": "^8.15.6",
52
52
  "ajv": "^8.17.1",
53
53
  "ajv-formats": "^3.0.1",
54
- "typescript": "^5.9.3"
54
+ "typescript": "^6.0.2"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public"
@@ -51,7 +51,16 @@ function generateTypeScript(data: CheckupDictionaryEntry[], sourceUrl: string):
51
51
  return lines.join("\n");
52
52
  }
53
53
 
54
+ // Allowed hosts for fetch requests to prevent SSRF
55
+ const ALLOWED_HOSTS = ["postgres.ai"];
56
+
54
57
  async function fetchWithTimeout(url: string, timeoutMs: number): Promise<Response> {
58
+ // Validate URL against allowlist to prevent SSRF
59
+ const parsed = new URL(url);
60
+ if (!ALLOWED_HOSTS.includes(parsed.hostname)) {
61
+ throw new Error(`Fetch blocked: host "${parsed.hostname}" is not in the allowlist`);
62
+ }
63
+
55
64
  const controller = new AbortController();
56
65
  const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
57
66
 
@@ -49,6 +49,8 @@ const REQUIRED_METRICS = [
49
49
  // Bloat estimation (F004, F005)
50
50
  "pg_table_bloat",
51
51
  "pg_btree_bloat",
52
+ // I/O statistics (I001)
53
+ "pg_stat_io",
52
54
  ];
53
55
 
54
56
  function main() {
@@ -0,0 +1,139 @@
1
+ # Permission Check Test Summary
2
+
3
+ ## Changes Made
4
+
5
+ Changed all references from `public.pg_statistic` to `postgres_ai.pg_statistic` in:
6
+ - `cli/lib/init.ts` - Permission check SQL query
7
+ - `cli/test/init.test.ts` - All test expectations (28 occurrences)
8
+
9
+ ## Key Fix: Safe Schema Checking
10
+
11
+ **Before (883fa95):**
12
+ ```sql
13
+ exists (
14
+ select from pg_views
15
+ where schemaname = 'public' and viewname = 'pg_statistic'
16
+ ) as granted
17
+ ```
18
+
19
+ **After (6db79f6) - INCORRECT, caused crashes:**
20
+ ```sql
21
+ to_regclass('postgres_ai.pg_statistic') is not null as granted
22
+ ```
23
+
24
+ **Current (this fix):**
25
+ ```sql
26
+ case
27
+ when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
28
+ else to_regclass('postgres_ai.pg_statistic') is not null
29
+ end as granted
30
+ ```
31
+
32
+ ### Why this fix matters
33
+
34
+ **Issue with bare `to_regclass()`:**
35
+ - Returns NULL when the schema doesn't exist ✓
36
+ - Returns NULL when the view doesn't exist ✓
37
+ - **Throws error** when the schema exists but user lacks USAGE privilege ✗
38
+
39
+ **Fix:**
40
+ - Check `has_schema_privilege()` first to avoid the permission error
41
+ - Returns NULL safely in all cases where we can't check the view
42
+ - Prevents crashes when postgres_ai schema exists but user lacks USAGE
43
+
44
+ ## Test Results
45
+
46
+ ### Unit Tests ✅
47
+ ```
48
+ ✓ 95 tests passed across 3 files
49
+ - 84 tests in init.test.ts (including 9 checkCurrentUserPermissions tests)
50
+ - 2 tests in config-consistency.test.ts
51
+ - 9 tests in permission-check-sql.test.ts
52
+ ```
53
+
54
+ ### Expected Behavior by Scenario
55
+
56
+ | Scenario | User Permissions | postgres_ai Schema | Expected Result |
57
+ |----------|-----------------|-------------------|-----------------|
58
+ | 1. Superuser | superuser + postgres_ai.pg_statistic | ✓ Exists | ✅ PASS (clean) |
59
+ | 2. pg_monitor, no schema access | pg_monitor only | ✗ No USAGE | ✅ PASS (warning) |
60
+ | 3. No pg_monitor | minimal permissions | ✗ Doesn't exist | ✅ PASS (error + fix SQL) |
61
+ | 8. After prepare-db | pg_monitor + postgres_ai grants | ✓ Exists + SELECT | ✅ PASS (clean) |
62
+
63
+ ### SQL Behavior Verification
64
+
65
+ **Scenario 2 & 3: Schema doesn't exist or no USAGE**
66
+ ```sql
67
+ -- Check privilege first, then to_regclass (no crash)
68
+ case
69
+ when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
70
+ else to_regclass('postgres_ai.pg_statistic') is not null
71
+ end → NULL
72
+
73
+ -- SELECT check is skipped (returns NULL, not treated as missing optional)
74
+ case
75
+ when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
76
+ when to_regclass('postgres_ai.pg_statistic') is null then null
77
+ else has_table_privilege(current_user, 'postgres_ai.pg_statistic', 'select')
78
+ end → NULL
79
+ ```
80
+
81
+ **Scenario 1 & 8: Schema exists with proper grants**
82
+ ```sql
83
+ -- User has USAGE, to_regclass returns OID (view is visible)
84
+ case
85
+ when not has_schema_privilege(current_user, 'postgres_ai', 'USAGE') then null
86
+ else to_regclass('postgres_ai.pg_statistic') is not null
87
+ end → TRUE
88
+
89
+ -- SELECT check is performed
90
+ has_table_privilege(current_user, 'postgres_ai.pg_statistic', 'select') → TRUE/FALSE
91
+ ```
92
+
93
+ ## Integration Test Limitations
94
+
95
+ Integration tests cannot run due to locale configuration issues with `initdb`:
96
+ ```
97
+ error: initdb: error: invalid locale settings; check LANG and LC_* environment variables
98
+ ```
99
+
100
+ However, unit tests provide comprehensive coverage of the permission check logic, including:
101
+ - All permission scenarios (granted, denied, skipped)
102
+ - Multiple missing permissions
103
+ - Error propagation
104
+ - Fix command generation
105
+ - Message formatting
106
+
107
+ ## Schema Consistency
108
+
109
+ The change ensures consistency across the codebase:
110
+ - ✅ `cli/lib/init.ts` - now checks postgres_ai.pg_statistic
111
+ - ✅ `cli/lib/supabase.ts` - already checks postgres_ai.pg_statistic
112
+ - ✅ `cli/sql/03.permissions.sql` - creates postgres_ai.pg_statistic
113
+ - ✅ `config/target-db/init.sql` - creates postgres_ai.pg_statistic
114
+ - ✅ `config/pgwatch-prometheus/metrics.yml` - references postgres_ai.pg_statistic
115
+
116
+ ## Commits
117
+
118
+ 1. **955cff2** - `fix: change public.pg_statistic to postgres_ai.pg_statistic`
119
+ - Updated permission check queries
120
+ - Updated all test expectations
121
+
122
+ 2. **6db79f6** - `fix: use to_regclass() for safe postgres_ai.pg_statistic check`
123
+ - Replaced pg_views query with to_regclass()
124
+ - ⚠️ This introduced a bug: crashes when schema exists but user lacks USAGE
125
+
126
+ 3. **[current]** - `fix: wrap to_regclass() with has_schema_privilege() check`
127
+ - Fixed crash when postgres_ai schema exists but user lacks USAGE privilege
128
+ - Added privilege check before calling to_regclass() in all locations
129
+ - Updated in: init.ts (3 places) and supabase.ts (1 place)
130
+
131
+ ## Verification Command
132
+
133
+ ```bash
134
+ # Run all permission-related tests
135
+ bun test test/init.test.ts test/config-consistency.test.ts test/permission-check-sql.test.ts
136
+
137
+ # Verify no public.pg_statistic references remain (except in comments)
138
+ git grep -n 'public\.pg_statistic' cli/
139
+ ```