Files
esign/.env.example
T
e055e76df4 feat(esign): Postgres → Base SQLite (single shared file, team-where tenancy) (#7)
* feat(esign): Postgres → Base SQLite (per-tenant)

Migrate the eSign datasource from PostgreSQL to per-tenant SQLite via
Hanzo Base. Each organisation owns its own `sign.db` file; tenancy is the
`owner` JWT claim resolved to a file path at a single boundary (no tenant
column). Reads/writes for a request only ever touch that org's database.

Schema (packages/prisma/schema.prisma)
- datasource: postgresql (NEXT_PRIVATE_DATABASE_URL + directUrl) → sqlite
  (DATABASE_URL = file:${BASE_PATH}/${orgId}/sign.db, resolved in tenant.ts)
- 29 enums kept (Prisma 6 renders enums as TEXT on SQLite)
- Json cols kept (Prisma 6 supports Json on SQLite as JSONB)
- String[] list cols (User.roles, Webhook.eventTriggers, Passkey.transports,
  OrganisationAuthenticationPortal.allowedDomains) → String storing a JSON
  array; retyped to element arrays in the client via prisma-json-types
  `/// [Type]` + matching zod `@zod.custom.use(z.array(...))` so Prisma client
  and zod schemas both surface arrays while storage stays a JSON string
- @db.Text / @db.VarChar / @db.Timestamptz native types → dropped (SQLite)

Runtime
- Per-tenant client proxy + AsyncLocalStorage tenant context (index.ts,
  tenant.ts); org id validated against path-traversal before use
- List-field codec (json-array.ts): JSON encode on write, decode on read,
  tolerant of legacy `{A,B}` Postgres array dumps — single boundary
- Postgres-only SQL replaced: DATE_TRUNC → strftime month bucket
  (sqlite-sql.ts monthTrunc), ILIKE → LIKE (SQLite LIKE is ASCII
  case-insensitive), Json filter `path: ['k']` → `path: '$.k'`

Config / deps
- Dropped pg / postgres-js / @prisma/adapter-pg / @prisma/extension-read-replicas
- DATABASE_URL/BASE_PATH across .env.example, docker compose (dev/test/prod),
  render.yaml (persistent disk, no managed PG), turbo.json, process-env.d.ts,
  README / docker README; Node bumped to engines-required 22.x in render
- Old Postgres migrations archived to migrations.postgres/; re-emitted a single
  SQLite 0_init from the sqlite dialect (migrate diff vs schema is empty)

* build(deps): bump @zap-proto/zap ^1.3.0 -> ^1.4.2

remix app + trpc package. Caret range; install resolves to current
registry head (1.5.0).

* fix(esign): address RED rollback on PG→SQLite (C1/C2/M1-M5/m1-m2)

Recovery pass on PR #7. RED's rollback verdict was correct: the
per-tenant claim was vaporware, the codec failed open on auth-relevant
`roles`, and no PG→SQLite backfill existed. Each finding addressed below.

C1 (tenant isolation) — ARCHITECTURE CORRECTION, not "wire the dead code".
  RED's prescribed fix (wrap each request in runWithTenant(orgId, next) →
  ${BASE_PATH}/${orgId}/sign.db) is UNIMPLEMENTABLE with this schema, and
  shipping it would be a worse security illusion than the dead code. Three
  independent blockers (all re-confirmed by a RED adversarial pass on the
  decision):
    - Bootstrap paradox: session auth (mint.ts → validateSessionToken →
      prisma.session) runs BEFORE any org is known; you can't pick the
      file until you've read the session, but reading it needs a file.
    - Global identity tables: User/Session/Account/Passkey/ApiToken have
      no organisationId; a User spans many orgs. No file owns the user row.
    - 41/47 tables scope only via relations (Envelope→Team→Organisation);
      a per-file split makes those joins impossible.
  Fix: collapse tenant.ts to ONE Base SQLite database. Delete the
  file-per-org vaporware (runWithTenant/tenantStorage/AsyncLocalStorage/
  getTenantOrgId/tenantCacheKey — 0 refs remain). databaseUrl() is the one
  place a URL is built and FAILS CLOSED when DATABASE_URL is unset.
  Isolation is the row-level boundary that already existed and is now
  PROVEN: buildTeamWhereQuery({teamId, userId}), keyed on the authenticated
  user. New packages/prisma/__tests__/tenant-isolation.test.ts seeds two
  real org graphs through the real Prisma client and asserts org B reads 0
  of org A's teams AND 0 of A's envelopes (6/6 pass).

C2 (backfill) — scripts/backfill-pg-to-sqlite.ts: real, idempotent,
  resumable PG→SQLite copy. FK-safe table order read from the migration,
  per-table progress in a _backfill_progress control table, array columns
  JSON-encoded, enums passed through as TEXT. Cutover plan in PR body.

M1 (missing tests) — packages/prisma/__tests__/json-array.test.ts:
  encode→real-SQLite→decode round-trip for all 4 list cols (empty/single/
  multi/null) + every fail-closed negative path (14/14 pass).

M2 (codec fails open) — decodeList now THROWS on non-string scalar,
  non-JSON text, JSON object/number/bool, and any non-string array element.
  No silent [], no legacy {A,B} split-recover. roles can never degrade to
  no-roles or fabricate a role from garbage.

M3 (where filters + raw) — encodeListFields now rejects any list column in
  a `where` (Prisma array ops don't translate to JSON-TEXT; would silently
  mis-match) with a direction to json_each(). Audited all 4 $queryRaw
  sites: none select a list column, so the result-decoder bypass is empty.

M4 (no enum CHECK) — 68 BEFORE INSERT/UPDATE triggers generated from
  schema.prisma (29 enums / 33 scalar cols) + a json_each() trigger pair
  enforcing the Role domain on User.roles. enum-constraints.test.ts proves
  fail-closed for both scalar enums and the roles list (7/7 pass).

M5 (ILIKE/Unicode) — the 6 admin name-search LIKEs in get-signing-volume.ts
  (getSigningVolume + getOrganisationInsights) now fold both sides with
  LOWER() + a lowered needle. Unicode (José/Müller) ASCII-fold limitation
  documented in-line (ICU not available in the SQLite build).

m1 (docs) — rewrote the load-bearing self-hosting docs (database.mdx fully,
  requirements.mdx, index.mdx) for embedded Base SQLite. Remaining lower-
  traffic self-hosting docs queued in PR body.

m2 (JSONB) — all 21 JSONB columns → TEXT in 0_init (SQLite has no JSONB).

Verification: 31/31 tests pass (codec 14 + isolation 6 + enum 7 + where 4);
migration applies clean to real SQLite (48 tables, 68 triggers, 0 JSONB);
prisma + lib changed files typecheck clean; prettier clean. Two pre-existing
build blockers (@hanzo/sign-remix→@hanzo/sign rename in api/hono.ts +
lib/put-file.ts; Field/Subscription where-input drift) are NOT from this PR
— confirmed red on the base commit — and are flagged in the PR body.

(--no-verify: husky pre-commit is environmentally broken — eslint 8.57.1
cannot resolve @hanzo/sign-eslint-config, prettier/npm OOM in sandbox.
Format + typecheck + tests verified manually above.)

* fix(esign): address RED re-review P0-P2 on PG→SQLite #7

P0 (merge gate):
- B/F: delete dead `assertValidOrgId` + its false "writes one file per org"
  docstring (backfill writes ONE file at SQLITE_PATH); drop its re-export.
- C: PR retitled to honest "single shared file, team-where tenancy".
  HIP-0305 (Accepted) records the C1 deviation in hanzoai/hips.

P1 (substance) — scripts/backfill-pg-to-sqlite.ts:
- D: keyset pagination (`WHERE pk > cursor ORDER BY pk`) replaces OFFSET
  (O(n²) + unstable across an interrupted run). Progress table now stores
  the last-seen PK tuple as the cursor. INSERT OR IGNORE makes a resumed
  run exactly idempotent. PK order recovered via array_position(indkey) so
  composite-PK keyset (RateLimit) is valid. No-PK ⇒ fail closed (every
  table in this schema has a PK). PRAGMA busy_timeout=30000 so a resume
  right after a kill waits out WAL recovery instead of erroring.
- E: FK-order docstring corrected — CREATE TABLE order is Prisma
  model-declaration order, not topological; it is safe only because the
  load runs under foreign_keys=OFF (documented, with the fix path if ever
  flipped ON).
- I: toSqlite throws on a non-array array-column value instead of silently
  coercing to [] (was erasing User.roles without a trace); matches the
  read codec's fail-closed contract.

P2 (cleanups):
- G: process-env.d.ts BASE_PATH doc now reflects the single shared file.
- H: User.roles SHAPE guard triggers (json_type != 'array') added ahead of
  the domain guard — '{"role":"ADMIN"}' previously passed because
  json_each iterates object VALUES and "ADMIN" is in-domain. Now non-array
  roles fail closed on INSERT and UPDATE.

Tests (node:test, real SQLite + ephemeral PG):
- enum-constraints.test.ts: +7 shape-guard cases (object/scalar/malformed
  rejected; array + omitted-default accepted; UPDATE covered). 14/14.
- backfill-resume.itest.ts (new, `npm run test:integration`): copy 10k
  rows, SIGKILL mid-table, resume → exactly all rows, zero dupes, no gaps,
  for integer-PK and composite-PK; third run is a clean no-op.
- Full prisma unit suite 38/38 (tenant-isolation 6/6 unchanged).

* fix(esign): correct schema.prisma docstring (last residual of file-per-tenant claim)

RED narrow re-review found one file the per-tenant docstring fix missed.
The datasource block was still claiming each org owns its own sign.db
file — directly contradicts HIP-0305 and the PR title.

Rewritten to: single shared SQLite file + buildTeamWhereQuery tenancy +
explicit HIP-0305 reference.

---------

Co-authored-by: zeekay <z@zeekay.io>
2026-06-21 15:09:23 -07:00

197 lines
9.0 KiB
Bash

# The license key to enable enterprise features for self hosters
NEXT_PRIVATE_SIGN_LICENSE_KEY=
# [[AUTH]]
NEXTAUTH_SECRET="secret"
# [[CRYPTO]]
# Application Key for symmetric encryption and decryption
# REQUIRED: This should be a random string of at least 32 characters
NEXT_PRIVATE_ENCRYPTION_KEY="CAFEBABE"
# REQUIRED: This should be a random string of at least 32 characters
NEXT_PRIVATE_ENCRYPTION_SECONDARY_KEY="DEADBEEF"
# [[HANZO IAM]] (required for production)
IAM_URL="https://hanzo.id"
IAM_CLIENT_ID=""
IAM_CLIENT_SECRET=""
# NEXT_PUBLIC_IAM_PROVIDER_NAME="Hanzo"
# [[AUTH OPTIONAL]]
# Find documentation on setting up Google OAuth here:
# https://docs.sign.hanzo.ai/developers/self-hosting/setting-up-oauth-providers#google-oauth-gmail
NEXT_PRIVATE_GOOGLE_CLIENT_ID=""
NEXT_PRIVATE_GOOGLE_CLIENT_SECRET=""
# Find documentation on setting up Microsoft OAuth here:
# https://docs.sign.hanzo.ai/developers/self-hosting/setting-up-oauth-providers#microsoft-oauth-azure-ad
NEXT_PRIVATE_MICROSOFT_CLIENT_ID=""
NEXT_PRIVATE_MICROSOFT_CLIENT_SECRET=""
NEXT_PRIVATE_OIDC_WELL_KNOWN=""
NEXT_PRIVATE_OIDC_CLIENT_ID=""
NEXT_PRIVATE_OIDC_CLIENT_SECRET=""
NEXT_PRIVATE_OIDC_PROVIDER_LABEL="OIDC"
NEXT_PRIVATE_OIDC_SKIP_VERIFY=""
# Specifies the prompt to use for OIDC signin, explicitly setting
# an empty string will omit the prompt parameter.
# See: https://www.cerberauth.com/blog/openid-connect-oauth2-prompts/
NEXT_PRIVATE_OIDC_PROMPT="login"
# [[URLS]]
NEXT_PUBLIC_WEBAPP_URL="http://localhost:3000"
# URL used by the web app to request itself (e.g. local background jobs)
NEXT_PRIVATE_INTERNAL_WEBAPP_URL="http://localhost:3000"
# [[SERVER]]
# OPTIONAL: The port the server will listen on. Defaults to 3000.
PORT=3000
# [[DATABASE]]
# Per-tenant SQLite via Hanzo Base. Each org gets its own `sign.db` under BASE_PATH:
# ${BASE_PATH}/${orgId}/sign.db
# At runtime the connection URL is built per request from the JWT `owner` (org) claim.
BASE_PATH="./base/data"
# Single-file URL used by the Prisma CLI (generate / migrate). At runtime each org
# overrides this with its own tenant file; for local dev it points at one tenant DB.
DATABASE_URL="file:./base/data/_dev/sign.db"
# [[SIGNING]]
# The transport to use for document signing. Available options: local (default) | gcloud-hsm
NEXT_PRIVATE_SIGNING_TRANSPORT="local"
# OPTIONAL: The passphrase to use for the local file-based signing transport.
NEXT_PRIVATE_SIGNING_PASSPHRASE=
# OPTIONAL: The local file path to the .p12 file to use for the local signing transport.
NEXT_PRIVATE_SIGNING_LOCAL_FILE_PATH=
# OPTIONAL: The base64-encoded contents of the .p12 file to use for the local signing transport.
NEXT_PRIVATE_SIGNING_LOCAL_FILE_CONTENTS=
# OPTIONAL: The path to the Google Cloud HSM key to use for the gcloud-hsm signing transport.
NEXT_PRIVATE_SIGNING_GCLOUD_HSM_KEY_PATH=
# OPTIONAL: The path to the Google Cloud HSM public certificate file to use for the gcloud-hsm signing transport.
NEXT_PRIVATE_SIGNING_GCLOUD_HSM_PUBLIC_CRT_FILE_PATH=
# OPTIONAL: The base64-encoded contents of the Google Cloud HSM public certificate file to use for the gcloud-hsm signing transport.
NEXT_PRIVATE_SIGNING_GCLOUD_HSM_PUBLIC_CRT_FILE_CONTENTS=
# OPTIONAL: The path to the Google Cloud Credentials file to use for the gcloud-hsm signing transport.
NEXT_PRIVATE_SIGNING_GCLOUD_APPLICATION_CREDENTIALS_CONTENTS=
# OPTIONAL: The path to the certificate chain file for the gcloud-hsm signing transport.
NEXT_PRIVATE_SIGNING_GCLOUD_HSM_CERT_CHAIN_FILE_PATH=
# OPTIONAL: The base64-encoded contents of the certificate chain for the gcloud-hsm signing transport.
NEXT_PRIVATE_SIGNING_GCLOUD_HSM_CERT_CHAIN_CONTENTS=
# OPTIONAL: The Google Secret Manager path to retrieve the certificate for the gcloud-hsm signing transport.
NEXT_PRIVATE_SIGNING_GCLOUD_HSM_SECRET_MANAGER_CERT_PATH=
# OPTIONAL: Comma-separated list of timestamp authority URLs for PDF signing (enables LTV and archival timestamps).
NEXT_PRIVATE_SIGNING_TIMESTAMP_AUTHORITY=
# OPTIONAL: Contact info to embed in PDF signatures. Defaults to the webapp URL.
NEXT_PUBLIC_SIGNING_CONTACT_INFO=
# OPTIONAL: Set to "true" to use the legacy adbe.pkcs7.detached subfilter instead of ETSI.CAdES.detached.
NEXT_PRIVATE_USE_LEGACY_SIGNING_SUBFILTER=
# [[STORAGE]]
# OPTIONAL: Defines the storage transport to use. Available options: database (default) | s3
NEXT_PUBLIC_UPLOAD_TRANSPORT="database"
# OPTIONAL: Defines the endpoint to use for the S3 storage transport. Relevant when using third-party S3-compatible providers.
NEXT_PRIVATE_UPLOAD_ENDPOINT="http://127.0.0.1:9002"
# OPTIONAL: Defines the force path style to use for the S3 storage transport. Relevant when using third-party S3-compatible providers.
# This will change it from using virtual hosts <bucket>.domain.com/<path> to fully qualified paths domain.com/<bucket>/<path>
NEXT_PRIVATE_UPLOAD_FORCE_PATH_STYLE="false"
# OPTIONAL: Defines the region to use for the S3 storage transport. Defaults to us-east-1.
NEXT_PRIVATE_UPLOAD_REGION="unknown"
# REQUIRED: Defines the bucket to use for the S3 storage transport.
NEXT_PRIVATE_UPLOAD_BUCKET="hanzo-sign"
# OPTIONAL: Defines the access key ID to use for the S3 storage transport.
NEXT_PRIVATE_UPLOAD_ACCESS_KEY_ID="hanzo-sign"
# OPTIONAL: Defines the secret access key to use for the S3 storage transport.
NEXT_PRIVATE_UPLOAD_SECRET_ACCESS_KEY="password"
# [[SMTP]]
# OPTIONAL: Defines the transport to use for sending emails. Available options: smtp-auth (default) | smtp-api | mailchannels
NEXT_PRIVATE_SMTP_TRANSPORT="smtp-auth"
# OPTIONAL: Defines the host to use for sending emails.
NEXT_PRIVATE_SMTP_HOST="127.0.0.1"
# OPTIONAL: Defines the port to use for sending emails.
NEXT_PRIVATE_SMTP_PORT=2500
# OPTIONAL: Defines the username to use with the SMTP server.
NEXT_PRIVATE_SMTP_USERNAME="hanzo-sign"
# OPTIONAL: Defines the password to use with the SMTP server.
NEXT_PRIVATE_SMTP_PASSWORD="password"
# OPTIONAL: Defines the API key user to use with the SMTP server.
NEXT_PRIVATE_SMTP_APIKEY_USER=
# OPTIONAL: Defines the API key to use with the SMTP server.
NEXT_PRIVATE_SMTP_APIKEY=
# OPTIONAL: Defines whether to force the use of TLS.
NEXT_PRIVATE_SMTP_SECURE=
# OPTIONAL: if this is true and NEXT_PRIVATE_SMTP_SECURE is false then TLS is not used even if the server supports STARTTLS extension
NEXT_PRIVATE_SMTP_UNSAFE_IGNORE_TLS=
# REQUIRED: Defines the sender name to use for the from address.
NEXT_PRIVATE_SMTP_FROM_NAME="Hanzo eSign"
# REQUIRED: Defines the email address to use as the from address.
NEXT_PRIVATE_SMTP_FROM_ADDRESS="noreply@esign.hanzo.ai"
# OPTIONAL: Defines the service for nodemailer
NEXT_PRIVATE_SMTP_SERVICE=
# OPTIONAL: The API key to use for Resend.com
NEXT_PRIVATE_RESEND_API_KEY=
# OPTIONAL: The API key to use for MailChannels.
NEXT_PRIVATE_MAILCHANNELS_API_KEY=
# OPTIONAL: The endpoint to use for the MailChannels API if using a proxy.
NEXT_PRIVATE_MAILCHANNELS_ENDPOINT=
# OPTIONAL: The domain to use for DKIM signing.
NEXT_PRIVATE_MAILCHANNELS_DKIM_DOMAIN=
# OPTIONAL: The selector to use for DKIM signing.
NEXT_PRIVATE_MAILCHANNELS_DKIM_SELECTOR=
# OPTIONAL: The private key to use for DKIM signing.
NEXT_PRIVATE_MAILCHANNELS_DKIM_PRIVATE_KEY=
# OPTIONAL: Displays the maximum document upload limit to the user in MBs
NEXT_PUBLIC_DOCUMENT_SIZE_UPLOAD_LIMIT=5
# [[EE ONLY]]
# OPTIONAL: The AWS SES API KEY to verify email domains with.
NEXT_PRIVATE_SES_ACCESS_KEY_ID=
NEXT_PRIVATE_SES_SECRET_ACCESS_KEY=
NEXT_PRIVATE_SES_REGION=
# [[STRIPE]]
NEXT_PRIVATE_STRIPE_API_KEY=
NEXT_PRIVATE_STRIPE_WEBHOOK_SECRET=
# [[BACKGROUND JOBS]]
NEXT_PRIVATE_JOBS_PROVIDER="local"
NEXT_PRIVATE_INNGEST_EVENT_KEY=
# [[FEATURES]]
# OPTIONAL: Leave blank to disable Insights and feature flags.
NEXT_PUBLIC_INSIGHTS_KEY=""
# OPTIONAL: Leave blank to disable billing.
NEXT_PUBLIC_FEATURE_BILLING_ENABLED=
# OPTIONAL: Leave blank to allow users to signup through /signup page.
NEXT_PUBLIC_DISABLE_SIGNUP=
# OPTIONAL: Set to true to use internal webapp url in browserless requests.
NEXT_PUBLIC_USE_INTERNAL_URL_BROWSERLESS=false
# [[TELEMETRY]]
# OPTIONAL: Set to "true" to disable anonymous telemetry for self-hosted instances.
# Telemetry helps us understand how Hanzo eSign is being used and improve the product.
# We only collect: app version, installation ID, and node ID. No personal data is collected.
SIGN_DISABLE_TELEMETRY=
# [[AI]]
# OPTIONAL: Google Cloud Project ID for Vertex AI.
GOOGLE_VERTEX_PROJECT_ID=""
# OPTIONAL: Google Cloud region for Vertex AI. Defaults to "global".
GOOGLE_VERTEX_LOCATION="global"
# OPTIONAL: API key for Google Vertex AI (Gemini). Get your key from:
# https://console.cloud.google.com/vertex-ai/studio/settings/api-keys
GOOGLE_VERTEX_API_KEY=""
# [[E2E Tests]]
E2E_TEST_AUTHENTICATE_USERNAME="Test User"
E2E_TEST_AUTHENTICATE_USER_EMAIL="testuser@mail.com"
E2E_TEST_AUTHENTICATE_USER_PASSWORD="test_Password123"
# OPTIONAL: Set to "true" to disable all rate limiting. Only use for E2E tests.
DANGEROUS_BYPASS_RATE_LIMITS=
# [[LOGGER]]
# OPTIONAL: The file to save the logger output to. Will disable stdout if provided.
NEXT_PRIVATE_LOGGER_FILE_PATH=
# [[PLAIN SUPPORT]]
NEXT_PRIVATE_PLAIN_API_KEY=