Skip to content

Updating a tenant with its current jwt_secret disconnects every client once GCM encryption is in use #2508

Open

Description

@hsusul

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I searched issues, PRs, discussions, branches, and local history for this behavior.

Describe the bug

A tenant update that sends the tenant's current jwt_secret disconnects every client of the tenant. Nothing about the tenant's auth changed. An update without extensions also shuts down the tenant's Connect process and stops its Postgres Changes processes, for the same reason.

It happens whenever the stored secret or the write cipher is AES-256-GCM. Only a secret stored with the legacy AES-128-ECB cipher and rewritten with it leaves clients connected. So a tenant whose secret is already stored as GCM keeps disconnecting after GCM writes are turned off.

Any PUT /api/tenants/:tenant_id or Api.update_tenant_by_external_id/2 call whose body carries the current secret triggers it, for example a full tenant config sent to change one limit. POST /api/tenants for an existing tenant reaches the same code, since TenantController.create/2 delegates to update/2.

GCM writes are off by default. DB_ENC_WRITE_GCM defaults to false (config/runtime.exs:25), and Crypto.write_gcm?/0 also requires DB_ENC_KEY_GCM. I have not checked how any hosted deployment is configured.

To reproduce

On upstream main 58ffae10, with the repository test databases up, add this test to the upsert with put describe block of test/realtime_web/controllers/tenant_controller_test.exs and run it. The test config sets db_enc_write_gcm: true, and default_tenant_attrs/1 sends the same jwt_secret the tenant was created with.

test "resending the jwt_secret", %{tenant: tenant, conn: conn} do
  external_id = tenant.external_id
  attrs = default_tenant_attrs(tenant_db_port(tenant))
  assert Crypto.decrypt!(tenant.jwt_secret) == attrs["jwt_secret"]
  :ok = Phoenix.PubSub.subscribe(Realtime.PubSub, "realtime:operations:" <> external_id)

  conn = put(conn, ~p"/api/tenants/#{external_id}", tenant: attrs)
  assert json_response(conn, 200)
  refute_receive %Phoenix.Socket.Broadcast{event: "system"}
end

It fails:

Unexpectedly received message %Phoenix.Socket.Broadcast{topic: nil, event: "system", payload: %{message: "Server requested disconnect", status: "ok", extension: "system"}} (which matched %Phoenix.Socket.Broadcast{event: "system"})

On the same commit, Tenant.changeset(tenant, %{"jwt_secret" => "s"}) on a tenant whose stored secret decrypts to "s" gives:

Stored cipher db_enc_write_gcm :jwt_secret in changes
GCM true yes
GCM false yes
ECB true yes
ECB false no

Expected behavior

An update that sends the tenant's current jwt_secret does not count as a jwt_secret change, whatever cipher stored it or writes it. Clients stay connected. For a body without extensions, the database connection is not restarted.

Actual behavior

Tenant.changeset/2 casts the sent plaintext, which never equals the stored ciphertext, so the cast always records a change. Tenant.encrypt_jwt_secret/1 then puts the new ciphertext. Ecto.Changeset.put_change/3 drops a change equal to the stored value. AES-128-ECB is deterministic, so an ECB secret rewritten with ECB drops out. AES-256-GCM uses a random IV, and the two ciphers never produce the same ciphertext, so every other case keeps the change. Api.update_tenant/2 then sees jwt_secret in the changes, and requires_disconnect and requires_restarting_db_connection (lib/realtime/api.ex:26-38) both match.

System information

  • macOS arm64.
  • Upstream main 58ffae10.
  • Elixir 1.20.4 and OTP 29 for the local run. CI pins Elixir 1.19.5 and OTP 28.
  • PostgreSQL 17.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions