@rebasepro/server-postgres 0.14.1 → 0.14.2-canary.g991e9df

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/dist/index.es.js CHANGED
@@ -3155,6 +3155,37 @@ function isRoleSwitchingPermissionError(error) {
3155
3155
  return msg.includes("set role") || msg.includes("member of role");
3156
3156
  }
3157
3157
  /**
3158
+ * Was this `42501` the *caller* being refused by a policy, rather than the
3159
+ * server lacking a privilege?
3160
+ *
3161
+ * Both arrive as `insufficient_privilege`, and they are opposite kinds of
3162
+ * problem. A row-level-security refusal is a working access-control system
3163
+ * doing its job: the caller asked for something their policies do not permit,
3164
+ * which is a 403 and nobody's bug. A missing `GRANT` is the deployment being
3165
+ * wrong — the connection role cannot touch the table at all, no policy is
3166
+ * involved, and nothing the caller changes about the request will help.
3167
+ *
3168
+ * Postgres distinguishes them in the message, so this does too:
3169
+ *
3170
+ * new row violates row-level security policy for table "notes" → the caller
3171
+ * permission denied for table notes → the server
3172
+ *
3173
+ * Only writes reach this. A read that RLS excludes is not an error — the rows
3174
+ * are filtered and the caller gets an empty page — so the erroring case is
3175
+ * specifically an `INSERT`/`UPDATE` whose row fails a policy's `WITH CHECK`.
3176
+ *
3177
+ * Matched on the message because that is the only thing carrying the
3178
+ * distinction; the SQLSTATE is identical either way. Narrow by design: anything
3179
+ * not naming row-level security stays the server's problem, since reporting a
3180
+ * genuine privilege misconfiguration as "forbidden" would send an operator
3181
+ * hunting for a policy bug that does not exist.
3182
+ */
3183
+ function isRowLevelSecurityDenial(error) {
3184
+ const pgError = extractPgError(error);
3185
+ if (!pgError || pgError.code !== "42501") return false;
3186
+ return pgError.message.toLowerCase().includes("row-level security policy");
3187
+ }
3188
+ /**
3158
3189
  * Translate a raw PostgreSQL error into a user-friendly message.
3159
3190
  *
3160
3191
  * @param pgError - The extracted PostgreSQL error (from {@link extractPgError})
@@ -3209,8 +3240,11 @@ function pgErrorToFriendlyMessage(pgError, context) {
3209
3240
  message: `Table not found for "${context}": ${pgMessage}. Check if your schema is up to date (run migrations).${suffix}`,
3210
3241
  code
3211
3242
  };
3212
- case "42501": return {
3213
- message: `Permission denied on "${tableRef}". Check your database credentials and RLS policies.${suffix}`,
3243
+ case "42501": return pgMessage.toLowerCase().includes("row-level security policy") ? {
3244
+ message: `Not permitted to write this row in "${tableRef}": it does not satisfy the row-level security policy.${suffix}`,
3245
+ code
3246
+ } : {
3247
+ message: `Permission denied on "${tableRef}": the database role this server connects as lacks privileges on it.${suffix}`,
3214
3248
  code
3215
3249
  };
3216
3250
  case "28000": return {
@@ -4766,6 +4800,7 @@ var PersistService = class {
4766
4800
  if (pgError) {
4767
4801
  const { message, code } = pgErrorToFriendlyMessage(pgError, collectionSlug);
4768
4802
  if (/^2[23]/.test(code)) return code === "23505" ? ApiError.conflict(message, `PG_${code}`) : ApiError.badRequest(message, `PG_${code}`);
4803
+ if (isRowLevelSecurityDenial(error)) return ApiError.forbidden(message, "WRITE_DENIED");
4769
4804
  return new Error(message);
4770
4805
  }
4771
4806
  const causeMessage = extractCauseMessage(error);