@lovable.dev/custom-view-server
v0.2.1
Published
Verify Lovable operator assertions on custom-view operator endpoints in published Lovable apps
Readme
@lovable.dev/custom-view-server
Verify Lovable operator assertions on custom-view operator endpoints inside published Lovable apps.
Why
A Lovable custom view (an editor panel for the project's operator) calls the
app's own backend endpoints through lovable.data(). That call is proxied
server-side by the Lovable API, which checks workspace membership — but the
endpoints it targets (e.g. /api/posts) ship as plain routes on the published
app, world-callable on public apps.
This package closes that gap. The Lovable API mints a short-lived RS256
operator assertion on the trusted data hop and forwards it via the
x-lovable-operator-assertion header. The app verifies the assertion against
the project's JWKS and rejects everything else with a 401.
The verification runs on Web Crypto (via jose) and works in Cloudflare Workers (workerd), Node 18+, Bun, and Deno.
Usage
Guard an operator route:
import { requireOperator } from "@lovable.dev/custom-view-server";
export async function handler(request: Request): Promise<Response> {
const auth = await requireOperator(request);
if (!auth.ok) return auth.response; // 401, JSON { error, code }
// auth.claims.sub is the operator's user identity (when present)
return Response.json({ posts: await listPosts() });
}The app's own project ID is resolved from the environment Lovable injects
(__LOVABLE_PROJECT_ID in process.env for sandbox dev and Node/Bun, the
Cloudflare Workers env binding on published apps) — never hardcode it, since
remixing copies source verbatim while assertions are minted for the new
project. Pass projectId explicitly only to override the environment.
Lower-level building blocks:
import {
verifyOperatorAssertion, // token string -> claims (throws OperatorAssertionError)
verifyOperatorRequest, // Request -> claims (throws OperatorAssertionError)
OPERATOR_ASSERTION_HEADER, // "x-lovable-operator-assertion"
} from "@lovable.dev/custom-view-server";This package is wired into apps by the Lovable custom-views integration (like the MCP and Lovable Cloud integrations) — apps normally don't hand-configure it.
What is verified
| Check | Detail |
| --------- | ------------------------------------------------------------------ |
| Signature | RS256 only, against the per-project JWKS |
| Issuer | https://api.lovable.dev/oidc/projects/<project_id> |
| Audience | lovable-app |
| Expiry | exp required (tokens without it are rejected), nbf when present, small clock tolerance (default 5s) |
| Marker | principal: "operator" — identity tokens are rejected |
| Project | project_id claim must equal the verifying app's own project ID |
The JWKS is fetched from
<issuer>/.well-known/jwks.json and cached in-process; pass jwks in the
options to verify against an inline key set instead (tests, offline).
Options
interface VerifyOperatorAssertionOptions {
projectId?: string; // override; resolved from __LOVABLE_PROJECT_ID when omitted
apiOrigin?: string; // default "https://api.lovable.dev"
issuer?: string; // issuer override; JWKS follows it (<issuer>/.well-known/jwks.json)
jwks?: JSONWebKeySet; // inline key set instead of remote fetch
clockToleranceSec?: number; // default 5
fetch?: typeof fetch; // custom fetch for the JWKS request
}Errors
All verification failures throw (or, via requireOperator, return) an
OperatorAssertionError with a stable code:
missing_assertion, malformed_assertion, invalid_signature,
expired_assertion, wrong_issuer, wrong_audience, wrong_project,
not_operator, jwks_unavailable, invalid_assertion.
The 401 response body carries only { error: "unauthorized", code } — never
token contents or verification internals.
Security notes
- Assertions are minted only by the Lovable API after an operator's workspace membership (edit access) is checked; the signing key never leaves Lovable.
- Request binding (method + path) and assertion TTL are subject to the security review in CA-2032 and may tighten in a future release.
