@hogsend/engine 0.35.0 → 0.36.0
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/package.json +7 -7
- package/src/app.ts +22 -1
- package/src/index.ts +48 -0
- package/src/lib/api-key-hash.ts +7 -2
- package/src/lib/banner.ts +71 -0
- package/src/lib/contacts.ts +57 -0
- package/src/lib/feed.ts +233 -0
- package/src/lib/ingestion.ts +10 -0
- package/src/lib/user-token.ts +100 -0
- package/src/middleware/api-key.ts +4 -0
- package/src/middleware/publishable-key.ts +119 -0
- package/src/middleware/require-admin.ts +18 -2
- package/src/routes/_shared.ts +61 -0
- package/src/routes/admin/api-keys.ts +46 -10
- package/src/routes/contacts/index.ts +38 -12
- package/src/routes/events/index.ts +70 -30
- package/src/routes/feed/index.ts +503 -0
- package/src/routes/feed/recipient.ts +183 -0
- package/src/routes/index.ts +65 -7
- package/src/routes/lists/index.ts +126 -6
- package/src/worker.ts +2 -0
- package/src/workflows/send-feed.ts +44 -0
|
@@ -8,6 +8,10 @@ export interface ApiKeyContext {
|
|
|
8
8
|
id: string;
|
|
9
9
|
name: string;
|
|
10
10
|
scopes: string[];
|
|
11
|
+
// Set ONLY by `requirePublishableOrIngest` for a publishable (pk_) key — the
|
|
12
|
+
// per-key browser Origin allowlist. `requireApiKey` (the secret path) never
|
|
13
|
+
// populates it, so its behavior is unchanged.
|
|
14
|
+
allowedOrigins?: string[] | null;
|
|
11
15
|
}
|
|
12
16
|
|
|
13
17
|
const SCOPE_HIERARCHY: Record<string, number> = {
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { apiKeys } from "@hogsend/db";
|
|
2
|
+
import { and, eq, isNull } from "drizzle-orm";
|
|
3
|
+
import { createMiddleware } from "hono/factory";
|
|
4
|
+
import type { AppEnv } from "../app.js";
|
|
5
|
+
import { hashApiKey } from "../lib/api-key-hash.js";
|
|
6
|
+
import { hasScope, requireApiKey } from "./api-key.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Data-plane guard for the BROWSER-REACHABLE subset of `/v1` (POST /events, the
|
|
10
|
+
* contacts upsert, GET /lists, list subscribe/unsubscribe, GET /lists/preferences).
|
|
11
|
+
*
|
|
12
|
+
* Accepts EITHER credential class:
|
|
13
|
+
*
|
|
14
|
+
* - SECRET key (anything NOT prefixed `pk_`): delegates to the UNCHANGED
|
|
15
|
+
* `requireApiKey` (legacy ADMIN_API_KEY + 60s cache + expiry), then applies
|
|
16
|
+
* the same `requireScope("ingest")` check inline. This path is
|
|
17
|
+
* byte-for-byte behaviorally identical to the existing
|
|
18
|
+
* `requireApiKey` → `requireScope("ingest")` stack.
|
|
19
|
+
*
|
|
20
|
+
* - PUBLISHABLE key (`pk_` bearer): its own UNCACHED DB lookup (the secret
|
|
21
|
+
* cache does not carry `allowed_origins`). Requires scope `ingest-public`
|
|
22
|
+
* and REJECTS any pk_ token that somehow also carries `ingest`/`full-admin`
|
|
23
|
+
* (defence in depth — a prefix never implies privilege). Enforces the
|
|
24
|
+
* per-key Origin allowlist FAIL-CLOSED (no/empty allowlist ⇒ reject; Origin
|
|
25
|
+
* not in the allowlist ⇒ reject), then sets `publishable = true`.
|
|
26
|
+
*
|
|
27
|
+
* A pk_ key therefore cannot reach any route this guard is not mounted on, and
|
|
28
|
+
* cannot pass `requireScope("ingest")` anywhere else (its `["ingest-public"]`
|
|
29
|
+
* scope is neither `ingest` nor `full-admin`).
|
|
30
|
+
*/
|
|
31
|
+
export const requirePublishableOrIngest = createMiddleware<AppEnv>(
|
|
32
|
+
async (c, next) => {
|
|
33
|
+
const header = c.req.header("authorization");
|
|
34
|
+
const provided = header?.startsWith("Bearer ")
|
|
35
|
+
? header.slice(7)
|
|
36
|
+
: undefined;
|
|
37
|
+
|
|
38
|
+
if (!provided) {
|
|
39
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// SECRET path: NOT a pk_ token → behave EXACTLY like the existing data-plane
|
|
43
|
+
// guard. Delegate to the unchanged `requireApiKey` (so the legacy
|
|
44
|
+
// ADMIN_API_KEY + cache + expiry logic stays in ONE place), then apply the
|
|
45
|
+
// same `requireScope("ingest")` check inline. `requireApiKey` only calls its
|
|
46
|
+
// `next` on a successful auth — if it 401s it never runs the inner check, so
|
|
47
|
+
// `authed` stays false and we return whatever response it produced.
|
|
48
|
+
if (!provided.startsWith("pk_")) {
|
|
49
|
+
let authed = false;
|
|
50
|
+
const res = await requireApiKey(c, async () => {
|
|
51
|
+
authed = true;
|
|
52
|
+
});
|
|
53
|
+
if (!authed) {
|
|
54
|
+
// requireApiKey short-circuited (401/expired) — return its response.
|
|
55
|
+
return res;
|
|
56
|
+
}
|
|
57
|
+
const apiKey = c.get("apiKey");
|
|
58
|
+
if (!apiKey || !hasScope(apiKey.scopes, "ingest")) {
|
|
59
|
+
return c.json({ error: "Forbidden: insufficient scope" }, 403);
|
|
60
|
+
}
|
|
61
|
+
return next();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// PUBLISHABLE path: pk_ token. Own DB lookup — `requireApiKey`'s cache does
|
|
65
|
+
// not carry `allowed_origins`, and pk_ traffic is browser-Origin-gated and
|
|
66
|
+
// lower-volume, so correctness over the 60s cache.
|
|
67
|
+
const { db } = c.get("container");
|
|
68
|
+
const keyHash = hashApiKey(provided);
|
|
69
|
+
const rows = await db
|
|
70
|
+
.select()
|
|
71
|
+
.from(apiKeys)
|
|
72
|
+
.where(and(eq(apiKeys.keyHash, keyHash), isNull(apiKeys.revokedAt)))
|
|
73
|
+
.limit(1);
|
|
74
|
+
const key = rows[0];
|
|
75
|
+
if (!key) {
|
|
76
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
77
|
+
}
|
|
78
|
+
if (key.expiresAt && key.expiresAt < new Date()) {
|
|
79
|
+
return c.json({ error: "API key expired" }, 401);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const scopes = key.scopes as string[];
|
|
83
|
+
// A pk_ key MUST be exactly a publishable key. Reject a pk_-prefixed token
|
|
84
|
+
// that somehow carries `ingest`/`full-admin` — the mint route forces
|
|
85
|
+
// `["ingest-public"]`, but never trust the prefix alone to imply privilege.
|
|
86
|
+
if (
|
|
87
|
+
!scopes.includes("ingest-public") ||
|
|
88
|
+
scopes.includes("ingest") ||
|
|
89
|
+
scopes.includes("full-admin")
|
|
90
|
+
) {
|
|
91
|
+
return c.json({ error: "Forbidden: insufficient scope" }, 403);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Per-key Origin allowlist — FAIL-CLOSED. No allowlist (null/empty), no
|
|
95
|
+
// Origin header, or an Origin not in the allowlist ⇒ reject.
|
|
96
|
+
const allowed = key.allowedOrigins ?? [];
|
|
97
|
+
const origin = c.req.header("origin");
|
|
98
|
+
if (allowed.length === 0 || !origin || !allowed.includes(origin)) {
|
|
99
|
+
return c.json({ error: "Forbidden: origin not allowed" }, 403);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
c.set("apiKey", {
|
|
103
|
+
id: key.id,
|
|
104
|
+
name: key.name,
|
|
105
|
+
scopes,
|
|
106
|
+
allowedOrigins: allowed,
|
|
107
|
+
});
|
|
108
|
+
c.set("publishable", true);
|
|
109
|
+
|
|
110
|
+
// Best-effort last-used bump (mirror requireApiKey's fire-and-forget).
|
|
111
|
+
db.update(apiKeys)
|
|
112
|
+
.set({ lastUsedAt: new Date() })
|
|
113
|
+
.where(eq(apiKeys.id, key.id))
|
|
114
|
+
.then(() => {})
|
|
115
|
+
.catch(() => {});
|
|
116
|
+
|
|
117
|
+
return next();
|
|
118
|
+
},
|
|
119
|
+
);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createMiddleware } from "hono/factory";
|
|
2
2
|
import type { AppEnv } from "../app.js";
|
|
3
|
-
import { requireApiKey } from "./api-key.js";
|
|
3
|
+
import { hasScope, requireApiKey } from "./api-key.js";
|
|
4
4
|
|
|
5
5
|
// Authorizes admin routes via EITHER an API key (Bearer header, the
|
|
6
6
|
// programmatic/CLI path) OR a Better-Auth session (cookie, the Studio path).
|
|
@@ -11,7 +11,23 @@ import { requireApiKey } from "./api-key.js";
|
|
|
11
11
|
export const requireAdmin = createMiddleware<AppEnv>(async (c, next) => {
|
|
12
12
|
const header = c.req.header("authorization");
|
|
13
13
|
if (header?.startsWith("Bearer ")) {
|
|
14
|
-
|
|
14
|
+
// Authenticate the key, then REQUIRE `full-admin` scope. Without this,
|
|
15
|
+
// ANY valid non-revoked key reaches the entire admin surface — including a
|
|
16
|
+
// PUBLISHABLE (pk_/ingest-public) key embedded in browser JS, which could
|
|
17
|
+
// then mint a full-admin secret. `requireApiKey` only runs its `next` on a
|
|
18
|
+
// successful auth (else it returns 401/expired), so use a flag to surface
|
|
19
|
+
// its short-circuit response; otherwise apply the scope gate.
|
|
20
|
+
let authed = false;
|
|
21
|
+
const res = await requireApiKey(c, async () => {
|
|
22
|
+
authed = true;
|
|
23
|
+
});
|
|
24
|
+
if (!authed) return res;
|
|
25
|
+
|
|
26
|
+
const apiKey = c.get("apiKey");
|
|
27
|
+
if (!apiKey || !hasScope(apiKey.scopes, "full-admin")) {
|
|
28
|
+
return c.json({ error: "Forbidden: insufficient scope" }, 403);
|
|
29
|
+
}
|
|
30
|
+
return next();
|
|
15
31
|
}
|
|
16
32
|
|
|
17
33
|
const { auth } = c.get("container");
|
package/src/routes/_shared.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Context } from "hono";
|
|
2
|
+
import { InvalidUserTokenError, verifyUserToken } from "../lib/user-token.js";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* The data-plane identity guard shared by `/v1/contacts`, `/v1/events`,
|
|
@@ -21,6 +22,66 @@ export function requireIdentity(
|
|
|
21
22
|
return null;
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
/**
|
|
26
|
+
* The publishable-key identity gate, shared by every browser-reachable handler
|
|
27
|
+
* (events ingest, contacts upsert, list subscribe/unsubscribe).
|
|
28
|
+
*
|
|
29
|
+
* - SECRET key (`!c.get("publishable")`): defers to the UNCHANGED
|
|
30
|
+
* `requireIdentity` — identical 400 wording, identical behavior.
|
|
31
|
+
*
|
|
32
|
+
* - PUBLISHABLE key: a token-less pk_ key may act ONLY on its own anon id.
|
|
33
|
+
* - No claimed identity (`email`/`userId` both absent) → allowed (the
|
|
34
|
+
* secure anon-only default). Returns `null`.
|
|
35
|
+
* - A claimed identity REQUIRES a verified `userToken` proving the
|
|
36
|
+
* `userId`. Reject with 403 when: no `userToken`; `email` is asserted
|
|
37
|
+
* (no email arm exists in v1 — a token only binds a userId); or the
|
|
38
|
+
* token's `userId` does not match the claimed `userId`.
|
|
39
|
+
*
|
|
40
|
+
* Returns a `Response` to `return`, or `null` to proceed.
|
|
41
|
+
*/
|
|
42
|
+
export function gatePublishableIdentity(
|
|
43
|
+
c: Context,
|
|
44
|
+
body: { email?: string; userId?: string; userToken?: string },
|
|
45
|
+
secret: string,
|
|
46
|
+
opts?: { field?: "email" | "to" },
|
|
47
|
+
) {
|
|
48
|
+
if (!c.get("publishable")) {
|
|
49
|
+
return requireIdentity(c, body, opts);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Publishable. No claimed identity → anon-only, allowed (the secure default).
|
|
53
|
+
if (!body.email && !body.userId) return null;
|
|
54
|
+
|
|
55
|
+
// Claimed identity REQUIRES a verified userToken binding the userId.
|
|
56
|
+
if (!body.userToken) {
|
|
57
|
+
return c.json(
|
|
58
|
+
{
|
|
59
|
+
error:
|
|
60
|
+
"publishable key cannot act on another identity without a userToken",
|
|
61
|
+
},
|
|
62
|
+
403,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const payload = verifyUserToken({ token: body.userToken, secret });
|
|
68
|
+
// A token binds ONLY a userId (v1 has no email arm). Reject any asserted
|
|
69
|
+
// email, and require the claimed userId to match the token's userId.
|
|
70
|
+
if (body.email || (body.userId && body.userId !== payload.userId)) {
|
|
71
|
+
return c.json(
|
|
72
|
+
{ error: "userToken does not authorize this identity" },
|
|
73
|
+
403,
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
if (err instanceof InvalidUserTokenError) {
|
|
79
|
+
return c.json({ error: "Invalid userToken" }, 403);
|
|
80
|
+
}
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
24
85
|
/**
|
|
25
86
|
* Extract a human-readable message from an `applyListMembership` failure,
|
|
26
87
|
* matching the wording every data-plane route used inline.
|
|
@@ -10,6 +10,9 @@ const apiKeySchema = z.object({
|
|
|
10
10
|
name: z.string(),
|
|
11
11
|
keyPrefix: z.string(),
|
|
12
12
|
scopes: z.array(z.string()),
|
|
13
|
+
// Per-key browser Origin allowlist for publishable (pk_) keys; null for
|
|
14
|
+
// secret keys.
|
|
15
|
+
allowedOrigins: z.array(z.string()).nullable(),
|
|
13
16
|
createdBy: z.string().nullable(),
|
|
14
17
|
lastUsedAt: z.string().nullable(),
|
|
15
18
|
revokedAt: z.string().nullable(),
|
|
@@ -55,14 +58,35 @@ const createKeyRoute = createRoute({
|
|
|
55
58
|
body: {
|
|
56
59
|
content: {
|
|
57
60
|
"application/json": {
|
|
58
|
-
schema: z
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
schema: z
|
|
62
|
+
.object({
|
|
63
|
+
name: z.string().min(1).max(100),
|
|
64
|
+
scopes: z
|
|
65
|
+
.array(
|
|
66
|
+
z.enum([
|
|
67
|
+
"read",
|
|
68
|
+
"journey-admin",
|
|
69
|
+
"full-admin",
|
|
70
|
+
"ingest",
|
|
71
|
+
"ingest-public",
|
|
72
|
+
]),
|
|
73
|
+
)
|
|
74
|
+
.min(1)
|
|
75
|
+
.default(["read"]),
|
|
76
|
+
expiresAt: z.string().datetime().optional(),
|
|
77
|
+
// Mint a PUBLISHABLE (pk_) browser key: forces scope
|
|
78
|
+
// `["ingest-public"]` and requires a non-empty `allowedOrigins`.
|
|
79
|
+
publishable: z.boolean().optional().default(false),
|
|
80
|
+
allowedOrigins: z.array(z.string().url()).optional(),
|
|
81
|
+
})
|
|
82
|
+
.refine(
|
|
83
|
+
(b) => !b.publishable || (b.allowedOrigins?.length ?? 0) > 0,
|
|
84
|
+
{
|
|
85
|
+
message:
|
|
86
|
+
"publishable keys require at least one allowedOrigins entry",
|
|
87
|
+
path: ["allowedOrigins"],
|
|
88
|
+
},
|
|
89
|
+
),
|
|
66
90
|
},
|
|
67
91
|
},
|
|
68
92
|
},
|
|
@@ -77,6 +101,7 @@ const createKeyRoute = createRoute({
|
|
|
77
101
|
key: z.string(),
|
|
78
102
|
keyPrefix: z.string(),
|
|
79
103
|
scopes: z.array(z.string()),
|
|
104
|
+
allowedOrigins: z.array(z.string()).nullable(),
|
|
80
105
|
expiresAt: z.string().nullable(),
|
|
81
106
|
createdAt: z.string(),
|
|
82
107
|
}),
|
|
@@ -117,6 +142,7 @@ function serializeKey(row: typeof apiKeys.$inferSelect) {
|
|
|
117
142
|
name: row.name,
|
|
118
143
|
keyPrefix: row.keyPrefix,
|
|
119
144
|
scopes: row.scopes as string[],
|
|
145
|
+
allowedOrigins: row.allowedOrigins ?? null,
|
|
120
146
|
createdBy: row.createdBy,
|
|
121
147
|
lastUsedAt: row.lastUsedAt?.toISOString() ?? null,
|
|
122
148
|
revokedAt: row.revokedAt?.toISOString() ?? null,
|
|
@@ -159,7 +185,15 @@ export const apiKeysRouter = new OpenAPIHono<AppEnv>()
|
|
|
159
185
|
const body = c.req.valid("json");
|
|
160
186
|
const actor = c.get("apiKey");
|
|
161
187
|
|
|
162
|
-
const { key, prefix, hash } = generateApiKey(
|
|
188
|
+
const { key, prefix, hash } = generateApiKey({
|
|
189
|
+
publishable: body.publishable,
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// A publishable (pk_) key is FORCED to exactly `["ingest-public"]` —
|
|
193
|
+
// regardless of any submitted `scopes` — so it can never carry
|
|
194
|
+
// `ingest`/`full-admin` (non-negotiable: no escalation). Secret keys keep
|
|
195
|
+
// their submitted scopes and a null allowlist (path untouched).
|
|
196
|
+
const scopes = body.publishable ? ["ingest-public"] : body.scopes;
|
|
163
197
|
|
|
164
198
|
const [created] = await db
|
|
165
199
|
.insert(apiKeys)
|
|
@@ -167,7 +201,8 @@ export const apiKeysRouter = new OpenAPIHono<AppEnv>()
|
|
|
167
201
|
name: body.name,
|
|
168
202
|
keyPrefix: prefix,
|
|
169
203
|
keyHash: hash,
|
|
170
|
-
scopes
|
|
204
|
+
scopes,
|
|
205
|
+
allowedOrigins: body.publishable ? (body.allowedOrigins ?? null) : null,
|
|
171
206
|
createdBy: actor?.name ?? null,
|
|
172
207
|
expiresAt: body.expiresAt ? new Date(body.expiresAt) : null,
|
|
173
208
|
})
|
|
@@ -182,6 +217,7 @@ export const apiKeysRouter = new OpenAPIHono<AppEnv>()
|
|
|
182
217
|
key,
|
|
183
218
|
keyPrefix: prefix,
|
|
184
219
|
scopes: created.scopes as string[],
|
|
220
|
+
allowedOrigins: created.allowedOrigins ?? null,
|
|
185
221
|
expiresAt: created.expiresAt?.toISOString() ?? null,
|
|
186
222
|
createdAt: created.createdAt.toISOString(),
|
|
187
223
|
},
|
|
@@ -2,6 +2,7 @@ import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
|
2
2
|
import type { AppEnv } from "../../app.js";
|
|
3
3
|
import {
|
|
4
4
|
findContacts,
|
|
5
|
+
PublishableAnonymousMergeError,
|
|
5
6
|
resolveContact,
|
|
6
7
|
resolveOrCreateContact,
|
|
7
8
|
serializeContact,
|
|
@@ -10,7 +11,11 @@ import {
|
|
|
10
11
|
import { emitOutbound } from "../../lib/outbound.js";
|
|
11
12
|
import { applyListMembership } from "../../lib/preferences.js";
|
|
12
13
|
import { errorSchema } from "../../lib/schemas.js";
|
|
13
|
-
import {
|
|
14
|
+
import {
|
|
15
|
+
gatePublishableIdentity,
|
|
16
|
+
listMembershipError,
|
|
17
|
+
requireIdentity,
|
|
18
|
+
} from "../_shared.js";
|
|
14
19
|
|
|
15
20
|
// The public, serialized contact shape (§2.5). `externalId` is nullable (D1 —
|
|
16
21
|
// email-only / anonymous contacts) and timestamps are ISO strings.
|
|
@@ -45,6 +50,9 @@ const upsertRoute = createRoute({
|
|
|
45
50
|
anonymousId: z.string().min(1).max(200).optional(),
|
|
46
51
|
properties: z.record(z.string(), z.unknown()).optional(),
|
|
47
52
|
lists: z.record(z.string(), z.boolean()).optional(),
|
|
53
|
+
// Publishable-key identity assertion (§Phase 1). Ignored on the
|
|
54
|
+
// secret-key path.
|
|
55
|
+
userToken: z.string().optional(),
|
|
48
56
|
}),
|
|
49
57
|
},
|
|
50
58
|
},
|
|
@@ -67,6 +75,11 @@ const upsertRoute = createRoute({
|
|
|
67
75
|
content: { "application/json": { schema: errorSchema } },
|
|
68
76
|
description: "Missing recipient or unmanageable list membership",
|
|
69
77
|
},
|
|
78
|
+
403: {
|
|
79
|
+
content: { "application/json": { schema: errorSchema } },
|
|
80
|
+
description:
|
|
81
|
+
"Publishable key attempted to act on another identity without a verified userToken",
|
|
82
|
+
},
|
|
70
83
|
},
|
|
71
84
|
});
|
|
72
85
|
|
|
@@ -136,21 +149,34 @@ const deleteRoute = createRoute({
|
|
|
136
149
|
|
|
137
150
|
export const contactsRouter = new OpenAPIHono<AppEnv>()
|
|
138
151
|
.openapi(upsertRoute, async (c) => {
|
|
139
|
-
const { db, hatchet, logger } = c.get("container");
|
|
152
|
+
const { db, hatchet, logger, env } = c.get("container");
|
|
140
153
|
const body = c.req.valid("json");
|
|
141
154
|
|
|
142
|
-
const guard =
|
|
155
|
+
const guard = gatePublishableIdentity(c, body, env.BETTER_AUTH_SECRET);
|
|
143
156
|
if (guard) return guard;
|
|
144
157
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
158
|
+
let resolved: Awaited<ReturnType<typeof resolveOrCreateContact>>;
|
|
159
|
+
try {
|
|
160
|
+
resolved = await resolveOrCreateContact({
|
|
161
|
+
db,
|
|
162
|
+
userId: body.userId,
|
|
163
|
+
email: body.email,
|
|
164
|
+
// §4: 2nd-precedence resolver key (zero-merge stitch).
|
|
165
|
+
anonymousId: body.anonymousId,
|
|
166
|
+
contactProperties: body.properties,
|
|
167
|
+
// §Phase 1 GAP-1: a publishable (pk_) browser upsert is anon-clamped —
|
|
168
|
+
// its browser-readable `anonymousId` may NOT attach to / merge / poison
|
|
169
|
+
// an already-identified victim contact. Secret-key upserts are never
|
|
170
|
+
// clamped.
|
|
171
|
+
restrictToAnonymous: c.get("publishable") === true,
|
|
172
|
+
});
|
|
173
|
+
} catch (err) {
|
|
174
|
+
if (err instanceof PublishableAnonymousMergeError) {
|
|
175
|
+
return c.json({ error: err.message }, 403);
|
|
176
|
+
}
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
const { id, created, linked, merged } = resolved;
|
|
154
180
|
|
|
155
181
|
// INTENT-LAYER outbound emit (decision #3): fire `contact.created` on a real
|
|
156
182
|
// creation, `contact.updated` only when an existing contact was linked/merged
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
2
2
|
import type { AppEnv } from "../../app.js";
|
|
3
|
+
import { PublishableAnonymousMergeError } from "../../lib/contacts.js";
|
|
3
4
|
import { ingestEvent } from "../../lib/ingestion.js";
|
|
4
5
|
import { applyListMembership } from "../../lib/preferences.js";
|
|
5
6
|
import { errorSchema } from "../../lib/schemas.js";
|
|
6
|
-
import {
|
|
7
|
+
import { gatePublishableIdentity, listMembershipError } from "../_shared.js";
|
|
7
8
|
|
|
8
9
|
const eventRequestSchema = z.object({
|
|
9
10
|
name: z.string().min(1),
|
|
@@ -22,6 +23,10 @@ const eventRequestSchema = z.object({
|
|
|
22
23
|
lists: z.record(z.string(), z.boolean()).optional(),
|
|
23
24
|
idempotencyKey: z.string().optional(),
|
|
24
25
|
timestamp: z.string().datetime().optional(),
|
|
26
|
+
// Publishable-key identity assertion (§Phase 1). A pk_ key is anon-only
|
|
27
|
+
// unless it presents a server-minted `userToken` proving the claimed `userId`.
|
|
28
|
+
// Ignored on the secret-key path (which uses `requireIdentity`).
|
|
29
|
+
userToken: z.string().optional(),
|
|
25
30
|
});
|
|
26
31
|
|
|
27
32
|
const eventResponseSchema = z.object({
|
|
@@ -70,48 +75,72 @@ const eventRoute = createRoute({
|
|
|
70
75
|
content: { "application/json": { schema: errorSchema } },
|
|
71
76
|
description: "Missing recipient or unmanageable list membership",
|
|
72
77
|
},
|
|
78
|
+
403: {
|
|
79
|
+
content: { "application/json": { schema: errorSchema } },
|
|
80
|
+
description:
|
|
81
|
+
"Publishable key attempted to act on another identity without a verified userToken",
|
|
82
|
+
},
|
|
73
83
|
},
|
|
74
84
|
});
|
|
75
85
|
|
|
76
86
|
export const eventsRouter = new OpenAPIHono<AppEnv>().openapi(
|
|
77
87
|
eventRoute,
|
|
78
88
|
async (c) => {
|
|
79
|
-
const { db, registry, hatchet, logger, analytics } =
|
|
89
|
+
const { db, registry, hatchet, logger, analytics, env } =
|
|
90
|
+
c.get("container");
|
|
80
91
|
const body = c.req.valid("json");
|
|
81
92
|
|
|
82
|
-
const guard =
|
|
93
|
+
const guard = gatePublishableIdentity(c, body, env.BETTER_AUTH_SECRET);
|
|
83
94
|
if (guard) return guard;
|
|
84
95
|
|
|
85
96
|
// The `Idempotency-Key` header wins over the body field (§2.5).
|
|
86
97
|
const headerKey = c.req.header("idempotency-key");
|
|
87
98
|
const idempotencyKey = headerKey ?? body.idempotencyKey;
|
|
88
99
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
//
|
|
102
|
-
//
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
100
|
+
let result: Awaited<ReturnType<typeof ingestEvent>>;
|
|
101
|
+
try {
|
|
102
|
+
result = await ingestEvent({
|
|
103
|
+
db,
|
|
104
|
+
registry,
|
|
105
|
+
hatchet,
|
|
106
|
+
logger,
|
|
107
|
+
// §5.3: thread the active analytics provider so a collide-MERGE /
|
|
108
|
+
// key-flip fires the provider-neutral `mergeIdentities` stitch.
|
|
109
|
+
analytics,
|
|
110
|
+
// §Phase 1 GAP-1: a publishable (pk_) browser write is anon-clamped —
|
|
111
|
+
// its browser-readable `anonymousId` may NOT attach to / merge / poison
|
|
112
|
+
// an already-identified victim contact. Secret-key ingest is never
|
|
113
|
+
// clamped.
|
|
114
|
+
restrictToAnonymous: c.get("publishable") === true,
|
|
115
|
+
event: {
|
|
116
|
+
event: body.name,
|
|
117
|
+
userId: body.userId,
|
|
118
|
+
userEmail: body.email,
|
|
119
|
+
// §4: 2nd-precedence resolver key — lets the contact's canonical key
|
|
120
|
+
// equal the browser anon id (zero-merge stitch).
|
|
121
|
+
anonymousId: body.anonymousId,
|
|
122
|
+
eventProperties: body.eventProperties ?? {},
|
|
123
|
+
contactProperties: body.contactProperties,
|
|
124
|
+
idempotencyKey,
|
|
125
|
+
// §2.5: caller-supplied event time (backfill/replay). The validated
|
|
126
|
+
// ISO string is coerced to a Date inside ingestEvent.
|
|
127
|
+
occurredAt: body.timestamp,
|
|
128
|
+
// Provenance derived from the authenticated key CLASS, never the
|
|
129
|
+
// request body (so it can't be spoofed): a publishable (pk_) key is
|
|
130
|
+
// always an in-app surface interaction from @hogsend/js; a secret key
|
|
131
|
+
// is the server-side data plane. "inapp" still fans out to PostHog
|
|
132
|
+
// (the loop-guard only special-cases "posthog").
|
|
133
|
+
source: c.get("publishable") === true ? "inapp" : "api",
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
} catch (err) {
|
|
137
|
+
// A publishable anon write that resolved to an identified victim contact
|
|
138
|
+
// (or would drive a merge) is rejected at the resolver — surface as 403.
|
|
139
|
+
if (err instanceof PublishableAnonymousMergeError) {
|
|
140
|
+
return c.json({ error: err.message }, 403);
|
|
141
|
+
}
|
|
142
|
+
throw err;
|
|
143
|
+
}
|
|
115
144
|
|
|
116
145
|
// Lists applied AFTER ingest so the contact exists (§2.5 lists ordering).
|
|
117
146
|
// `applyListMembership` writes `email_preferences` independently of the
|
|
@@ -123,7 +152,18 @@ export const eventsRouter = new OpenAPIHono<AppEnv>().openapi(
|
|
|
123
152
|
// status and (b) tempt a client to retry, re-ingesting the event. Surface it
|
|
124
153
|
// as a `listsError` warning on the 202 instead.
|
|
125
154
|
let listsError: string | undefined;
|
|
126
|
-
|
|
155
|
+
// §Phase 1 GAP-2 (defense in depth): a publishable caller reaching here has
|
|
156
|
+
// no token-less email/userId (the gate 403'd it) — but assert it explicitly
|
|
157
|
+
// so a future change that lets `anonymousId` carry a list write can't
|
|
158
|
+
// silently write `email_preferences` for a victim. `applyListMembership`
|
|
159
|
+
// needs a resolvable email anyway; a publishable list write is a no-op.
|
|
160
|
+
const publishableListWrite =
|
|
161
|
+
c.get("publishable") === true && (body.email || body.userId);
|
|
162
|
+
if (
|
|
163
|
+
!publishableListWrite &&
|
|
164
|
+
body.lists &&
|
|
165
|
+
Object.keys(body.lists).length > 0
|
|
166
|
+
) {
|
|
127
167
|
try {
|
|
128
168
|
await applyListMembership({
|
|
129
169
|
db,
|