@everystack/server 0.4.31 → 0.4.32

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/CHANGELOG.md CHANGED
@@ -4,6 +4,12 @@ All notable changes to `@everystack/server` are documented here.
4
4
 
5
5
  Format based on [Keep a Changelog](https://keepachangelog.com/).
6
6
 
7
+ ## [0.4.32] - 2026-09-21
8
+
9
+ ### Fixed
10
+
11
+ - db:provision detects PUBLIC's CREATE on schema public, revokes it under --confirm
12
+
7
13
  ## [0.4.31] - 2026-09-16
8
14
 
9
15
  ### Added
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@everystack/server",
3
- "version": "0.4.31",
3
+ "version": "0.4.32",
4
4
  "description": "Server runtime primitives for Lambda — event adapters, routing, SSR, image processing",
5
5
  "license": "AGPL-3.0-only",
6
6
  "author": "Scalable Technology, Inc. <licensing@scalable.technology>",
@@ -187,8 +187,8 @@
187
187
  "sst": "4.13.1",
188
188
  "ts-jest": "29.4.9",
189
189
  "typescript": "5.9.3",
190
- "@everystack/cli": "0.4.85",
191
- "@everystack/auth": "0.4.5"
190
+ "@everystack/auth": "0.4.5",
191
+ "@everystack/cli": "0.4.87"
192
192
  },
193
193
  "scripts": {
194
194
  "test": "jest",
package/src/provision.ts CHANGED
@@ -196,9 +196,25 @@ export interface ProvisionResult {
196
196
  * anything and never throw — a reader that cannot read is loud, recoverable, and reported.
197
197
  */
198
198
  readerGrants?: { granted: number; refusals: string[] };
199
+ /**
200
+ * The PUBLIC-on-schema-`public` posture, and what (if anything) was done about it. Always present
201
+ * once convergence has run — `publicCanCreate: false` is the already-clean answer, and the CLI
202
+ * says nothing about it. Same non-fatal contract as ENACT: never throws, never aborts.
203
+ */
204
+ publicSchemaPosture?: PublicSchemaPosture;
199
205
  error?: string;
200
206
  }
201
207
 
208
+ /** The result of the PUBLIC-on-`public` posture step. See `convergePublicSchemaPosture`. */
209
+ export interface PublicSchemaPosture {
210
+ /** Does the PUBLIC pseudo-role hold CREATE on schema `public`? (The pre-PG15 default.) */
211
+ publicCanCreate: boolean;
212
+ /** Was the REVOKE applied AND verified? Only ever true when `publicCanCreate` was true. */
213
+ revoked: boolean;
214
+ /** Why it was not applied — the missing `--confirm`, a connection that cannot, a server refusal. */
215
+ refusals: string[];
216
+ }
217
+
202
218
  const ALNUM = /^[A-Za-z0-9]+$/;
203
219
 
204
220
  export async function runProvision(
@@ -384,8 +400,8 @@ export async function runProvision(
384
400
  export async function convergeOwnership(
385
401
  payload: Pick<ProvisionPayload, 'declaredOwners' | 'declaredObjects' | 'confirm' | 'adminRole' | 'readerRole' | 'searchPath'>,
386
402
  deps: ProvisionDeps,
387
- ): Promise<Pick<ProvisionResult, 'ownerEnactment' | 'ownerAdoption' | 'readerGrants'>> {
388
- const out: Pick<ProvisionResult, 'ownerEnactment' | 'ownerAdoption' | 'readerGrants'> = {};
403
+ ): Promise<Pick<ProvisionResult, 'ownerEnactment' | 'ownerAdoption' | 'readerGrants' | 'publicSchemaPosture'>> {
404
+ const out: Pick<ProvisionResult, 'ownerEnactment' | 'ownerAdoption' | 'readerGrants' | 'publicSchemaPosture'> = {};
389
405
  if (!deps.query) return out;
390
406
  const operator = payload.adminRole ?? 'migrator';
391
407
 
@@ -416,11 +432,143 @@ export async function convergeOwnership(
416
432
  out.readerGrants = { granted: 0, refusals: [`reader grants failed (credentials are already saved; nothing is stranded): ${err?.message ?? String(err)}`] };
417
433
  }
418
434
  }
435
+
436
+ // The PUBLIC-on-`public` posture — LAST, and UNCONDITIONAL. It is a property of the DATABASE, not
437
+ // of what the Models declare, so it must not hide behind the declared-ownership guards above: the
438
+ // brownfield app with nothing declared is precisely the one most likely to carry the pre-PG15
439
+ // grant. One catalog read on a converged database, one statement when the operator has confirmed.
440
+ try {
441
+ out.publicSchemaPosture = await convergePublicSchemaPosture(deps, !!payload.confirm);
442
+ } catch (err: any) {
443
+ out.publicSchemaPosture = { publicCanCreate: false, revoked: false, refusals: [
444
+ `the schema "public" posture check failed (credentials are already saved; nothing is stranded): ${err?.message ?? String(err)}`,
445
+ ] };
446
+ }
419
447
  return out;
420
448
  }
421
449
 
422
450
  const truthy = (v: unknown): boolean => v === true || v === 't' || v === 'true';
423
451
 
452
+ /** The one statement this step ever sends. Also the fix SQL every refusal below hands back. */
453
+ export const REVOKE_PUBLIC_CREATE_SQL = 'REVOKE CREATE ON SCHEMA public FROM PUBLIC';
454
+
455
+ /**
456
+ * The posture probe: does the PUBLIC pseudo-role hold CREATE on schema `public`, and can THIS
457
+ * connection take it away?
458
+ *
459
+ * `public_can_create` reads the ACL directly rather than asking `has_schema_privilege('public', …)`.
460
+ * Both answer correctly on a live server (measured, PG16), but the ACL read is the idiom the rest of
461
+ * this file already uses (`toFacts`/`toCaps` over `aclexplode`) and it is unambiguous: grantee 0 IS
462
+ * the PUBLIC pseudo-role, whereas the literal `'public'` in `has_schema_privilege` is a special-cased
463
+ * NAME that resolves to no row in `pg_roles`. A NULL `nspacl` (owner-only defaults, and what a
464
+ * hand-recreated `public` schema carries on every version) explodes to zero rows — correctly, no grant.
465
+ *
466
+ * This is deliberately NOT `server_version_num`. PG15 removed the grant as a DEFAULT; it says nothing
467
+ * about the database in front of us. A PG16 database restored from a pre-15 dump still carries it, and
468
+ * an operator can re-add it on any version with one legal statement. The catalog is the only truth.
469
+ *
470
+ * `can_revoke` is the capability half, and it is load-bearing for a reason PostgreSQL makes easy to
471
+ * miss: a REVOKE run by a role that does not administer the schema DOES NOT FAIL. It emits
472
+ * `WARNING: no privileges could be revoked for "public"` and returns success with the grant still in
473
+ * place. `pg_has_role(current_user, nspowner, 'USAGE')` is true for the schema's owner, for a member
474
+ * of it, and for a superuser — exactly the set that can actually revoke.
475
+ */
476
+ export const PUBLIC_SCHEMA_POSTURE_SQL = `SELECT
477
+ EXISTS (
478
+ SELECT 1 FROM pg_namespace n, aclexplode(n.nspacl) a
479
+ WHERE n.nspname = 'public' AND a.grantee = 0 AND a.privilege_type = 'CREATE'
480
+ ) AS public_can_create,
481
+ EXISTS (
482
+ SELECT 1 FROM pg_namespace n
483
+ WHERE n.nspname = 'public' AND pg_has_role(current_user, n.nspowner, 'USAGE')
484
+ ) AS can_revoke`;
485
+
486
+ /**
487
+ * Converge the PUBLIC-on-schema-`public` posture — detect always, act only under `confirm`.
488
+ *
489
+ * THE GAP. Before PG15, `CREATE ON SCHEMA public` was granted to the PUBLIC pseudo-role by default.
490
+ * PG15 removed that default. Where it is still in place, every login this file provisions —
491
+ * authenticator, migrator, reader — can create objects in `public` through an ambient grant none of
492
+ * them was given, whatever its own privileges say. A schema anyone can write is also a search_path
493
+ * hijack surface: plant a shadow object there and a privileged session resolving through `public`
494
+ * runs it (the same chain `security-audit` reds on).
495
+ *
496
+ * WHY THIS IS GATED, when db:build revokes the same grant unconditionally. db:build refuses a
497
+ * non-empty target, so there is nothing in `public` to strand and no app to surprise. db:provision
498
+ * has no such guarantee: it runs against a LIVE, often long-running database, and some legacy apps
499
+ * genuinely create objects in `public` at runtime. Taking that away silently, mid-deploy, is a
500
+ * behaviour change no one asked for. So this reports by default and acts only when the operator has
501
+ * seen the report and re-run with `--confirm` — the same shape ADOPT uses for its runtime-changing
502
+ * reassignments.
503
+ *
504
+ * ROUTED like ENACT's grants: the operator's own connection when it administers `public`, the master
505
+ * when it does not, and a refusal carrying the runnable SQL when neither can. VERIFIED after the
506
+ * fact, because — see PUBLIC_SCHEMA_POSTURE_SQL — an under-privileged REVOKE reports success while
507
+ * changing nothing, so "the statement ran" would have printed `revoked` over a wide-open database.
508
+ *
509
+ * Non-fatal by construction: it never throws. It runs strictly after the credential is in the secret
510
+ * store, so anything it fails to do is recoverable by a re-run and strands nothing.
511
+ */
512
+ export async function convergePublicSchemaPosture(
513
+ deps: ProvisionDeps,
514
+ confirm: boolean,
515
+ ): Promise<PublicSchemaPosture> {
516
+ const query = deps.query;
517
+ if (!query) return { publicCanCreate: false, revoked: false, refusals: [] };
518
+
519
+ const [row] = await query(PUBLIC_SCHEMA_POSTURE_SQL);
520
+ if (!truthy(row?.public_can_create)) return { publicCanCreate: false, revoked: false, refusals: [] };
521
+
522
+ if (!confirm) {
523
+ return { publicCanCreate: true, revoked: false, refusals: [
524
+ 'the PUBLIC pseudo-role holds CREATE on schema "public" — the pre-PG15 default, which PG15 ' +
525
+ 'removed. Every login this provision created can place objects there through that ambient ' +
526
+ 'grant, and a writable schema on a search path is a hijack surface for every privileged ' +
527
+ 'session resolving through it. Taking it away can break an app that creates objects in ' +
528
+ '"public" at runtime, so it is NOT done silently: review, then re-run with --confirm. ' +
529
+ `The statement it would send: ${REVOKE_PUBLIC_CREATE_SQL};`,
530
+ ] };
531
+ }
532
+
533
+ // Route to a connection that can actually revoke. The operator usually does not own `public`
534
+ // (postgres or pg_database_owner does), so the master is the normal venue.
535
+ let execute: ((sql: string) => Promise<unknown>) | null = null;
536
+ if (truthy(row?.can_revoke)) {
537
+ execute = deps.execute;
538
+ } else if (deps.master) {
539
+ const [masterRow] = await deps.master.query(PUBLIC_SCHEMA_POSTURE_SQL);
540
+ if (truthy(masterRow?.can_revoke)) execute = (s: string) => deps.master!.execute(s);
541
+ }
542
+ if (!execute) {
543
+ return { publicCanCreate: true, revoked: false, refusals: [
544
+ 'cannot revoke PUBLIC\'s CREATE on schema "public": neither the operator connection nor the ' +
545
+ 'master is the owner of schema "public" (nor a member of its owner). PostgreSQL would accept ' +
546
+ 'the statement from them and change NOTHING — it warns "no privileges could be revoked" and ' +
547
+ `returns success. Run it as the schema's owner: ${REVOKE_PUBLIC_CREATE_SQL};`,
548
+ ] };
549
+ }
550
+
551
+ try {
552
+ await execute(`${REVOKE_PUBLIC_CREATE_SQL};`);
553
+ } catch (err: any) {
554
+ return { publicCanCreate: true, revoked: false, refusals: [
555
+ `revoking PUBLIC's CREATE on schema "public" failed: ${err?.message ?? String(err)}. ` +
556
+ `Nothing else is affected — credentials are already saved. Fix: ${REVOKE_PUBLIC_CREATE_SQL};`,
557
+ ] };
558
+ }
559
+
560
+ // VERIFY. The statement completing is not evidence (see above), so re-read the catalog.
561
+ const [after] = await query(PUBLIC_SCHEMA_POSTURE_SQL);
562
+ if (truthy(after?.public_can_create)) {
563
+ return { publicCanCreate: true, revoked: false, refusals: [
564
+ 'the REVOKE was accepted but PUBLIC still holds CREATE on schema "public". PostgreSQL does ' +
565
+ 'not error when a role that does not administer the schema revokes — it warns and changes ' +
566
+ `nothing. Run it as the schema's owner: ${REVOKE_PUBLIC_CREATE_SQL};`,
567
+ ] };
568
+ }
569
+ return { publicCanCreate: true, revoked: true, refusals: [] };
570
+ }
571
+
424
572
  /**
425
573
  * Run `statements` as ONE transaction on `execute`, and leave the SESSION usable whatever happens.
426
574
  *