@rebasepro/server-postgres 0.9.1-canary.16c42e9 → 0.9.1-canary.26fe4b2

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.
Files changed (35) hide show
  1. package/README.md +21 -0
  2. package/dist/PostgresBackendDriver.d.ts +18 -0
  3. package/dist/PostgresBootstrapper.d.ts +7 -1
  4. package/dist/auth/services.d.ts +53 -53
  5. package/dist/index.es.js +730 -194
  6. package/dist/index.es.js.map +1 -1
  7. package/dist/schema/auth-bootstrap-sql.d.ts +1 -1
  8. package/dist/schema/auth-schema.d.ts +24 -24
  9. package/dist/schema/doctor.d.ts +1 -1
  10. package/dist/schema/introspect-db-logic.d.ts +0 -5
  11. package/dist/schema/introspect-db-naming.d.ts +10 -0
  12. package/dist/security/policy-drift.d.ts +30 -0
  13. package/dist/security/rls-enforcement.d.ts +2 -2
  14. package/dist/services/channel-history.d.ts +118 -0
  15. package/dist/services/realtimeService.d.ts +69 -2
  16. package/package.json +8 -31
  17. package/src/PostgresBackendDriver.ts +56 -5
  18. package/src/PostgresBootstrapper.ts +18 -1
  19. package/src/auth/ensure-tables.ts +97 -17
  20. package/src/auth/services.ts +134 -133
  21. package/src/cli.ts +60 -0
  22. package/src/schema/auth-bootstrap-sql.ts +7 -1
  23. package/src/schema/auth-schema.ts +13 -13
  24. package/src/schema/doctor.ts +45 -20
  25. package/src/schema/introspect-db-inference.ts +1 -1
  26. package/src/schema/introspect-db-logic.ts +1 -10
  27. package/src/schema/introspect-db-naming.ts +15 -0
  28. package/src/schema/introspect-runtime.ts +1 -1
  29. package/src/security/policy-drift.test.ts +106 -1
  30. package/src/security/policy-drift.ts +56 -0
  31. package/src/security/rls-enforcement.ts +11 -5
  32. package/src/services/channel-history.ts +343 -0
  33. package/src/services/realtimeService.ts +198 -10
  34. package/src/services/row-pipeline.ts +27 -3
  35. package/src/websocket.ts +30 -11
@@ -91,6 +91,30 @@ function coerceDeclaredNumbers(
91
91
  }
92
92
 
93
93
  /** Render one target row in the requested style. */
94
+ /**
95
+ * Drop every column the collection marked `excludeFromApi`.
96
+ *
97
+ * Password hashes and verification tokens have to be readable server-side but
98
+ * must never reach a client — and "never" has to mean every exit from this
99
+ * pipeline, including relation targets, or a secret leaks through whichever
100
+ * path was overlooked. Keyed by both the property name and its column name,
101
+ * since a row can arrive keyed either way depending on the caller.
102
+ */
103
+ function stripExcluded(
104
+ row: Record<string, unknown>,
105
+ collection: CollectionConfig
106
+ ): Record<string, unknown> {
107
+ const properties = collection.properties as Record<string, Property> | undefined;
108
+ if (!properties) return row;
109
+
110
+ for (const [key, property] of Object.entries(properties)) {
111
+ if (!property?.excludeFromApi) continue;
112
+ delete row[key];
113
+ if (property.columnName) delete row[property.columnName];
114
+ }
115
+ return row;
116
+ }
117
+
94
118
  function renderTarget(
95
119
  targetRow: Record<string, unknown>,
96
120
  targetCollection: CollectionConfig,
@@ -100,7 +124,7 @@ function renderTarget(
100
124
  if (style === "inline") {
101
125
  // The target's columns, and only those: its address is the consumer's
102
126
  // to derive, and merging one in overwrites a real `id` column.
103
- return coerceDeclaredNumbers({ ...targetRow }, targetCollection);
127
+ return stripExcluded(coerceDeclaredNumbers({ ...targetRow }, targetCollection), targetCollection);
104
128
  }
105
129
 
106
130
  const address = relationTargetAddress(targetRow, targetCollection, registry);
@@ -168,7 +192,7 @@ export function toCmsRow(
168
192
  }
169
193
  }
170
194
 
171
- return normalized;
195
+ return stripExcluded(normalized, collection);
172
196
  }
173
197
 
174
198
  /**
@@ -211,5 +235,5 @@ export function toRestRow(
211
235
  }
212
236
  }
213
237
 
214
- return flat;
238
+ return stripExcluded(flat, collection);
215
239
  }
package/src/websocket.ts CHANGED
@@ -27,7 +27,7 @@ interface WsAuthConfig {
27
27
  * Normalized user identity for WebSocket sessions.
28
28
  */
29
29
  interface WsUserIdentity {
30
- userId: string;
30
+ uid: string;
31
31
  roles: string[];
32
32
  isAdmin: boolean;
33
33
  }
@@ -183,7 +183,7 @@ code } }
183
183
 
184
184
  if (adapterUser) {
185
185
  verifiedUser = {
186
- userId: adapterUser.uid,
186
+ uid: adapterUser.uid,
187
187
  roles: adapterUser.roles,
188
188
  isAdmin: adapterUser.isAdmin
189
189
  };
@@ -195,13 +195,13 @@ code } }
195
195
  // Service key: a static secret, not a JWT. Checked
196
196
  // before verification, mirroring the HTTP middleware —
197
197
  // verifying it as a JWT can only ever fail.
198
- verifiedUser = { userId: "service", roles: ["admin"], isAdmin: true };
198
+ verifiedUser = { uid: "service", roles: ["admin"], isAdmin: true };
199
199
  } else {
200
200
  // Standard JWT path
201
201
  const jwtPayload = extractUserFromToken(token);
202
202
  if (jwtPayload) {
203
203
  verifiedUser = {
204
- userId: jwtPayload.userId,
204
+ uid: jwtPayload.uid,
205
205
  roles: jwtPayload.roles ?? [],
206
206
  isAdmin: (jwtPayload.roles ?? []).some((r: string) => r === "admin")
207
207
  };
@@ -218,10 +218,10 @@ code } }
218
218
  ws.send(JSON.stringify({
219
219
  type: "AUTH_SUCCESS",
220
220
  requestId,
221
- payload: { userId: verifiedUser.userId,
221
+ payload: { uid: verifiedUser.uid,
222
222
  roles: verifiedUser.roles }
223
223
  }));
224
- wsDebug(`🔐 [WebSocket Server] Client ${clientId} authenticated as ${verifiedUser.userId}`);
224
+ wsDebug(`🔐 [WebSocket Server] Client ${clientId} authenticated as ${verifiedUser.uid}`);
225
225
  } else {
226
226
  wsDebug(`[WS] replying AUTH_ERROR for requestId ${requestId} (invalid token)`);
227
227
  sendError("AUTH_ERROR", "INVALID_TOKEN", "Invalid or expired token");
@@ -272,7 +272,7 @@ roles: verifiedUser.roles }
272
272
  try {
273
273
  const userForAuth: User = session?.user
274
274
  ? {
275
- uid: session.user.userId,
275
+ uid: session.user.uid,
276
276
  displayName: null,
277
277
  email: null,
278
278
  photoURL: null,
@@ -421,7 +421,7 @@ colors: true }));
421
421
  sql: typeof sql === "string" ? sql.substring(0, 500) : sql,
422
422
  options,
423
423
  resultRows: Array.isArray(result) ? result.length : "unknown",
424
- userId: auditSession?.user?.userId ?? "unknown",
424
+ uid: auditSession?.user?.uid ?? "unknown",
425
425
  roles: auditSession?.user?.roles ?? [],
426
426
  isAdmin: auditSession?.user?.isAdmin ?? false,
427
427
  }));
@@ -476,6 +476,24 @@ colors: true }));
476
476
  }
477
477
  break;
478
478
 
479
+ case "FETCH_APPLICATION_ROLES": {
480
+ wsDebug("👤 [WebSocket Server] Processing FETCH_APPLICATION_ROLES request");
481
+ const delegate = await getScopedDelegate();
482
+ const admin = delegate.admin;
483
+ let roles: string[] = [];
484
+ if (isSQLAdmin(admin) && admin.fetchApplicationRoles) {
485
+ roles = await admin.fetchApplicationRoles();
486
+ }
487
+ wsDebug(`👤 [WebSocket Server] Fetched ${roles.length} application roles.`);
488
+ const response = {
489
+ type: "FETCH_APPLICATION_ROLES_SUCCESS",
490
+ payload: { roles },
491
+ requestId
492
+ };
493
+ ws.send(JSON.stringify(response));
494
+ }
495
+ break;
496
+
479
497
  case "FETCH_CURRENT_DATABASE": {
480
498
  wsDebug("📚 [WebSocket Server] Processing FETCH_CURRENT_DATABASE request");
481
499
  const delegate = await getScopedDelegate();
@@ -594,14 +612,15 @@ colors: true }));
594
612
  case "broadcast":
595
613
  case "presence_track":
596
614
  case "presence_untrack":
597
- case "presence_state": {
615
+ case "presence_state":
616
+ case "channel_history": {
598
617
  wsDebug("🔄 [WebSocket Server] Routing realtime message to RealtimeService:", type);
599
618
  // Attach auth context from the WS session so RLS-aware refetches work
600
619
  const session = clientSessions.get(clientId);
601
620
  const authContext = session?.user
602
- ? { userId: session.user.userId,
621
+ ? { uid: session.user.uid,
603
622
  roles: session.user.roles ?? [] }
604
- : { userId: "anon",
623
+ : { uid: "anon",
605
624
  roles: ["anon"] };
606
625
  // Let RealtimeService handle these messages
607
626
  await realtimeService.handleClientMessage(clientId, {