clivly 0.17.0 → 0.18.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/README.md +36 -33
- package/{adapter-BpAfha3z.d.mts → adapter-BXykDFyG.d.mts} +1 -1
- package/{adapter-BvSigBPG.d.cts → adapter-BiUN_I3u.d.cts} +1 -1
- package/{auth-adapter-CsZE4ugg.d.cts → auth-adapter-BNvri2gX.d.mts} +1 -1
- package/{auth-adapter-Q6CL2VHr.d.mts → auth-adapter-D2MUT1Hk.d.cts} +1 -1
- package/auth-better-auth.d.cts +1 -1
- package/auth-better-auth.d.mts +1 -1
- package/auth-clerk.d.cts +1 -1
- package/auth-clerk.d.mts +1 -1
- package/auth-workos.d.cts +1 -1
- package/auth-workos.d.mts +1 -1
- package/core-adapter.d.cts +1 -1
- package/core-adapter.d.mts +1 -1
- package/core-auth-adapter.d.cts +1 -1
- package/core-auth-adapter.d.mts +1 -1
- package/core-drizzle.d.cts +1 -1
- package/core-drizzle.d.mts +1 -1
- package/core-entity-config.cjs +1 -1
- package/core-entity-config.d.cts +1 -1
- package/core-entity-config.d.mts +1 -1
- package/core-entity-config.mjs +1 -1
- package/core-entity-heuristics.cjs +16 -15
- package/core-entity-heuristics.d.cts +3 -3
- package/core-entity-heuristics.d.mts +3 -3
- package/core-entity-heuristics.mjs +1 -1
- package/core-mapping-form.d.cts +1 -1
- package/core-mapping-form.d.mts +1 -1
- package/core-mappings-config.cjs +1 -1
- package/core-mappings-config.d.cts +1 -1
- package/core-mappings-config.d.mts +1 -1
- package/core-mappings-config.mjs +1 -1
- package/core-sync-engine.cjs +1 -1
- package/core-sync-engine.d.cts +1 -1
- package/core-sync-engine.d.mts +1 -1
- package/core-sync-engine.mjs +1 -1
- package/core-types.d.cts +2 -2
- package/core-types.d.mts +2 -2
- package/core-view-compiler.cjs +1 -1
- package/core-view-compiler.d.cts +1 -1
- package/core-view-compiler.d.mts +1 -1
- package/core-view-compiler.mjs +1 -1
- package/core.cjs +7 -7
- package/core.d.cts +10 -10
- package/core.d.mts +10 -10
- package/core.mjs +7 -7
- package/{drizzle-MUNZgh1l.d.cts → drizzle-B9cjBavj.d.cts} +1 -1
- package/{drizzle-UJbPCzPJ.d.mts → drizzle-L5FOGe_-.d.mts} +1 -1
- package/drizzle.cjs +2 -2
- package/drizzle.d.cts +3 -3
- package/drizzle.d.mts +3 -3
- package/drizzle.mjs +2 -2
- package/{entity-config-nWWgJXLw.cjs → entity-config-DiCJ6mhI.cjs} +1 -1
- package/{entity-config-BF7pcXFB.d.cts → entity-config-X-KlnYXX.d.mts} +1 -1
- package/{entity-config-pzamFSKw.d.mts → entity-config-dNclLyBM.d.cts} +1 -1
- package/{entity-config-DQVSprlG.mjs → entity-config-g_oMPXlN.mjs} +1 -1
- package/entity-heuristics-CIParuQe.cjs +655 -0
- package/entity-heuristics-DE5ujoHw.mjs +566 -0
- package/index.cjs +69 -20
- package/index.mjs +57 -8
- package/{internal-DtLZyjf8.mjs → internal-9oDwwbNg.mjs} +1 -1
- package/{internal-CjJy-cy1.cjs → internal-BKYt5j0l.cjs} +1 -1
- package/{internal-Gie7hCpK.d.mts → internal-COALpgov.d.cts} +1 -1
- package/{internal-C-uvosSL.d.cts → internal-D3q4FPcA.d.mts} +1 -1
- package/internal-adapter.d.cts +1 -1
- package/internal-adapter.d.mts +1 -1
- package/internal-auth-adapter.d.cts +1 -1
- package/internal-auth-adapter.d.mts +1 -1
- package/internal-drizzle.d.cts +1 -1
- package/internal-drizzle.d.mts +1 -1
- package/internal-entity-config.cjs +1 -1
- package/internal-entity-config.d.cts +1 -1
- package/internal-entity-config.d.mts +1 -1
- package/internal-entity-config.mjs +1 -1
- package/internal-entity-heuristics.cjs +16 -575
- package/internal-entity-heuristics.d.cts +3 -1
- package/internal-entity-heuristics.d.mts +3 -1
- package/internal-entity-heuristics.mjs +1 -562
- package/internal-mapping-form.d.cts +1 -1
- package/internal-mapping-form.d.mts +1 -1
- package/internal-mappings-config.cjs +1 -1
- package/internal-mappings-config.d.cts +1 -1
- package/internal-mappings-config.d.mts +1 -1
- package/internal-mappings-config.mjs +1 -1
- package/internal-sync-engine.cjs +1 -1
- package/internal-sync-engine.d.cts +1 -1
- package/internal-sync-engine.d.mts +1 -1
- package/internal-sync-engine.mjs +1 -1
- package/internal-types.d.cts +2 -2
- package/internal-types.d.mts +2 -2
- package/internal-view-compiler.cjs +1 -1
- package/internal-view-compiler.d.cts +1 -1
- package/internal-view-compiler.d.mts +1 -1
- package/internal-view-compiler.mjs +1 -1
- package/internal.cjs +7 -7
- package/internal.d.cts +10 -10
- package/internal.d.mts +10 -10
- package/internal.mjs +7 -7
- package/{mapping-form-BymksK7h.d.mts → mapping-form-Bpvgf5b8.d.mts} +1 -1
- package/{mapping-form-Cmf8y-1m.d.cts → mapping-form-CV7y6qox.d.cts} +1 -1
- package/{mappings-config-Dsz6rcJb.d.cts → mappings-config-BBppW9fp.d.cts} +1 -1
- package/{mappings-config-DBG-KnYl.cjs → mappings-config-CNxBrbt9.cjs} +1 -1
- package/{mappings-config-C7FPwrJT.mjs → mappings-config-D_g20PyS.mjs} +1 -1
- package/{mappings-config-ktJhxmHm.d.mts → mappings-config-Dmm0H6p_.d.mts} +1 -1
- package/{namespace-probe-By8SCnym.d.mts → namespace-probe-BAXprXQh.d.mts} +49 -5
- package/{namespace-probe-CTkb35Ro.d.cts → namespace-probe-qgjiudCo.d.cts} +49 -5
- package/package.json +1 -7
- package/{projection-DNi_2dVa.mjs → projection-BkYXvjTr.mjs} +2434 -1147
- package/{projection-B8RtxuEq.cjs → projection-By63gMC-.cjs} +2434 -1147
- package/{projection-sql-BYv40Hen.mjs → projection-sql-C3gZE3GS.mjs} +1 -1
- package/{projection-sql-SuhzhsMe.cjs → projection-sql-CHz7KzNS.cjs} +1 -1
- package/{schemas-DySv963y.d.cts → schemas-BSuo_eZQ.d.cts} +97 -35
- package/{schemas-DySv963y.d.mts → schemas-BSuo_eZQ.d.mts} +97 -35
- package/{sdk-BJ3OkhCq.cjs → sdk-D53fH4uu.cjs} +164 -28
- package/{sdk-CXrarwvs.mjs → sdk-D5J7kRQm.mjs} +164 -28
- package/sdk.cjs +2 -2
- package/sdk.d.cts +4 -4
- package/sdk.d.mts +4 -4
- package/sdk.mjs +2 -2
- package/{sync-engine-D9ltfqkD.d.cts → sync-engine-C7W0Gkq0.d.cts} +2 -2
- package/{sync-engine-C_SwnFgF.d.mts → sync-engine-C9Q2FkR9.d.mts} +2 -2
- package/{sync-engine-BTUoH82k.mjs → sync-engine-CwdLkmch.mjs} +1 -1
- package/{sync-engine-DBlSP9r2.cjs → sync-engine-Di-vF26b.cjs} +1 -1
- package/{types-BtQCi_qP.d.cts → types-C08UluBS.d.cts} +23 -2
- package/{types-CTOl3pAX.d.mts → types-CZsSYbeC.d.mts} +23 -2
- package/{view-compiler-B8tJ5IGw.mjs → view-compiler-CLTi1VL-.mjs} +2 -2
- package/{view-compiler-Ce_QE3dh.cjs → view-compiler-DaOz5QVl.cjs} +2 -2
- package/{view-compiler-BZgZisTG.d.mts → view-compiler-M23WiVGG.d.mts} +1 -1
- package/{view-compiler-D7GLf1n8.d.cts → view-compiler-cXqt4X-c.d.cts} +1 -1
- package/vite.d.cts +1 -1
- package/vite.d.mts +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
const require_projection = require("./projection-
|
|
2
|
-
const require_entity_config = require("./entity-config-
|
|
3
|
-
const
|
|
1
|
+
const require_projection = require("./projection-By63gMC-.cjs");
|
|
2
|
+
const require_entity_config = require("./entity-config-DiCJ6mhI.cjs");
|
|
3
|
+
const require_entity_heuristics = require("./entity-heuristics-CIParuQe.cjs");
|
|
4
|
+
const require_mappings_config = require("./mappings-config-CNxBrbt9.cjs");
|
|
4
5
|
const require_drizzle_meta = require("./drizzle-meta-BPnRLpsB.cjs");
|
|
5
6
|
// ../sdk/src/deployment-identity.ts
|
|
6
7
|
const MAX_DEPLOYMENT_ID_LENGTH = 128;
|
|
@@ -1010,6 +1011,78 @@ function resolvePresenceTransport(host, configured) {
|
|
|
1010
1011
|
transport: DEFAULT_TRANSPORT
|
|
1011
1012
|
};
|
|
1012
1013
|
}
|
|
1014
|
+
// ../sdk/src/projection-guard.ts
|
|
1015
|
+
/**
|
|
1016
|
+
* Tables auth libraries keep live credentials in. Whole-table: their secrets
|
|
1017
|
+
* sit in columns with generic names (`token`, `value`) that a column rule
|
|
1018
|
+
* cannot single out. `user`, `member` and `organization` are not here — they
|
|
1019
|
+
* are legitimate contact and company sources.
|
|
1020
|
+
*/
|
|
1021
|
+
const CREDENTIAL_TABLES = /* @__PURE__ */ new Set([
|
|
1022
|
+
"session",
|
|
1023
|
+
"verification",
|
|
1024
|
+
"two_factor",
|
|
1025
|
+
"twofactor",
|
|
1026
|
+
"passkey",
|
|
1027
|
+
"apikey",
|
|
1028
|
+
"api_key",
|
|
1029
|
+
"jwks"
|
|
1030
|
+
]);
|
|
1031
|
+
const BASE_ALIAS = "base";
|
|
1032
|
+
function normalise(name) {
|
|
1033
|
+
return name.toLowerCase().replaceAll("-", "_");
|
|
1034
|
+
}
|
|
1035
|
+
/** Every (table, column) a projection reads, with join aliases resolved. */
|
|
1036
|
+
function readColumns(projection) {
|
|
1037
|
+
const tableOf = /* @__PURE__ */ new Map([[BASE_ALIAS, projection.baseTable]]);
|
|
1038
|
+
for (const join of projection.joins) tableOf.set(join.key, join.table);
|
|
1039
|
+
const refs = [];
|
|
1040
|
+
for (const join of projection.joins) for (const condition of join.on) refs.push(condition.left, condition.right);
|
|
1041
|
+
for (const binding of Object.values(projection.fieldBindings)) refs.push(binding.from);
|
|
1042
|
+
const { identity } = projection;
|
|
1043
|
+
if (identity.kind === "column") refs.push(identity.from);
|
|
1044
|
+
else if (identity.kind === "composite") refs.push(...identity.parts);
|
|
1045
|
+
else if (identity.column) refs.push({
|
|
1046
|
+
table: BASE_ALIAS,
|
|
1047
|
+
column: identity.column
|
|
1048
|
+
});
|
|
1049
|
+
return refs.map((ref) => ({
|
|
1050
|
+
table: tableOf.get(ref.table) ?? ref.table,
|
|
1051
|
+
column: ref.column
|
|
1052
|
+
}));
|
|
1053
|
+
}
|
|
1054
|
+
function referencedTables(projection) {
|
|
1055
|
+
return [projection.baseTable, ...projection.joins.map((join) => join.table)];
|
|
1056
|
+
}
|
|
1057
|
+
/** Why this projection may not run here; empty when it may. */
|
|
1058
|
+
function projectionExposureErrors(projection, policy = {}) {
|
|
1059
|
+
const allowed = new Set((policy.allowSensitive ?? []).map(normalise));
|
|
1060
|
+
const errors = [];
|
|
1061
|
+
for (const table of referencedTables(projection)) {
|
|
1062
|
+
if (CREDENTIAL_TABLES.has(normalise(table)) && !allowed.has(normalise(`${table}.*`))) errors.push(`Table "${table}" holds credentials and is never synced.`);
|
|
1063
|
+
if (policy.tables && !(table in policy.tables)) errors.push(`Table "${table}" is not in this app's exposure.tables allowlist.`);
|
|
1064
|
+
}
|
|
1065
|
+
for (const { table, column } of readColumns(projection)) {
|
|
1066
|
+
const ref = `${table}.${column}`;
|
|
1067
|
+
if (require_entity_heuristics.isSensitiveColumn(column) && !allowed.has(normalise(ref))) errors.push(`Column "${ref}" looks like a credential and is never synced. List it in exposure.allowSensitive if it is safe.`);
|
|
1068
|
+
const columns = policy.tables?.[table];
|
|
1069
|
+
if (Array.isArray(columns) && !columns.includes(column)) errors.push(`Column "${ref}" is not in this app's exposure.tables allowlist.`);
|
|
1070
|
+
}
|
|
1071
|
+
return [...new Set(errors)];
|
|
1072
|
+
}
|
|
1073
|
+
/** Drop credential columns, and columns outside the allowlist, from a raw-row preview. */
|
|
1074
|
+
function exposableColumns(table, columns, policy = {}) {
|
|
1075
|
+
const allowed = new Set((policy.allowSensitive ?? []).map(normalise));
|
|
1076
|
+
const result = /* @__PURE__ */ new Set();
|
|
1077
|
+
if (CREDENTIAL_TABLES.has(normalise(table)) && !allowed.has(normalise(`${table}.*`)) || policy.tables !== void 0 && !(table in policy.tables)) return result;
|
|
1078
|
+
const listed = policy.tables?.[table];
|
|
1079
|
+
for (const column of columns) {
|
|
1080
|
+
if (require_entity_heuristics.isSensitiveColumn(column) && !allowed.has(normalise(`${table}.${column}`))) continue;
|
|
1081
|
+
if (Array.isArray(listed) && !listed.includes(column)) continue;
|
|
1082
|
+
result.add(column);
|
|
1083
|
+
}
|
|
1084
|
+
return result;
|
|
1085
|
+
}
|
|
1013
1086
|
// ../sdk/src/push-outcome.ts
|
|
1014
1087
|
function isCount(value) {
|
|
1015
1088
|
return typeof value === "number" && Number.isFinite(value);
|
|
@@ -1049,8 +1122,8 @@ const MISSING_INTROSPECTOR = "this app cannot run dashboard-authored derived obj
|
|
|
1049
1122
|
* `sql.raw` at execution time.
|
|
1050
1123
|
*/
|
|
1051
1124
|
function resolveRemoteProjections(args) {
|
|
1052
|
-
const { entries, introspector, schema } = args;
|
|
1053
|
-
return entries.map((entry) => resolveOne(entry, schema, introspector));
|
|
1125
|
+
const { entries, exposure, introspector, schema } = args;
|
|
1126
|
+
return entries.map((entry) => resolveOne(entry, schema, introspector, exposure));
|
|
1054
1127
|
}
|
|
1055
1128
|
/**
|
|
1056
1129
|
* The base table's PK, resolved from the schema THIS app discovered — never
|
|
@@ -1071,7 +1144,7 @@ function resolveLocalBasePk(projection, schema) {
|
|
|
1071
1144
|
if (!baseTable) return;
|
|
1072
1145
|
return require_entity_config.primaryKeyColumns(baseTable)[0];
|
|
1073
1146
|
}
|
|
1074
|
-
function resolveOne(entry, schema, introspector) {
|
|
1147
|
+
function resolveOne(entry, schema, introspector, exposure) {
|
|
1075
1148
|
const parsed = require_projection.projectionSchema.safeParse(entry.projection);
|
|
1076
1149
|
if (!parsed.success) return {
|
|
1077
1150
|
id: entry.id,
|
|
@@ -1079,6 +1152,12 @@ function resolveOne(entry, schema, introspector) {
|
|
|
1079
1152
|
reason: `projection did not parse: ${parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`
|
|
1080
1153
|
};
|
|
1081
1154
|
const projection = parsed.data;
|
|
1155
|
+
const refused = projectionExposureErrors(projection, exposure);
|
|
1156
|
+
if (refused.length > 0) return {
|
|
1157
|
+
id: entry.id,
|
|
1158
|
+
status: "invalid",
|
|
1159
|
+
reason: refused.join("; ")
|
|
1160
|
+
};
|
|
1082
1161
|
const errors = require_entity_config.validateProjection(projection, schema, entry.entityKey);
|
|
1083
1162
|
if (errors.length > 0) return {
|
|
1084
1163
|
id: entry.id,
|
|
@@ -1268,7 +1347,7 @@ function selfCheck(input) {
|
|
|
1268
1347
|
const issues = [];
|
|
1269
1348
|
if (input.reportsTriggerUrl && !input.hasTriggerSecret) issues.push({
|
|
1270
1349
|
code: "trigger_route_unauthenticated",
|
|
1271
|
-
message: "Your app's sync route
|
|
1350
|
+
message: "Your app's sync route has no signing secret, so it refuses every sync Clivly starts.",
|
|
1272
1351
|
fix: "Set CLIVLY_SYNC_TRIGGER_SECRET in the environment your app runs in, then restart it."
|
|
1273
1352
|
});
|
|
1274
1353
|
if (input.lastPushFailed) issues.push({
|
|
@@ -1376,7 +1455,7 @@ async function fullSyncSource(args) {
|
|
|
1376
1455
|
// ../sdk/src/trigger-method.ts
|
|
1377
1456
|
/** Host-side override, mirroring `CLIVLY_INSTANT_PUSH_DISABLED`. */
|
|
1378
1457
|
const REQUIRE_POST_ENV_VAR = "CLIVLY_TRIGGER_REQUIRE_POST";
|
|
1379
|
-
const NON_POST_DEPRECATION_WARNING = `Clivly: this sync trigger was called with GET
|
|
1458
|
+
const NON_POST_DEPRECATION_WARNING = `Clivly: this sync trigger was called with GET and ran only because ${REQUIRE_POST_ENV_VAR}=false. A GET can be started by a crawler or any web page, so change the caller — your cron job or uptime check — to POST, then remove ${REQUIRE_POST_ENV_VAR}.`;
|
|
1380
1459
|
const METHOD_NOT_ALLOWED = 405;
|
|
1381
1460
|
const NO_CONTENT = 204;
|
|
1382
1461
|
const OK = 200;
|
|
@@ -1539,6 +1618,16 @@ function reportedOrigin(req) {
|
|
|
1539
1618
|
function trustedOrigin(req) {
|
|
1540
1619
|
return req.headers.get("origin");
|
|
1541
1620
|
}
|
|
1621
|
+
/**
|
|
1622
|
+
* The visitor's address as the host's platform reported it. Clivly throttles
|
|
1623
|
+
* new chats per visitor on it; without it the only address Clivly sees is this
|
|
1624
|
+
* server's, so every visitor shared one bucket.
|
|
1625
|
+
*/
|
|
1626
|
+
function defaultVisitorIp(req) {
|
|
1627
|
+
const direct = req.headers.get("cf-connecting-ip") ?? req.headers.get("x-real-ip");
|
|
1628
|
+
if (direct) return direct.trim();
|
|
1629
|
+
return req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || null;
|
|
1630
|
+
}
|
|
1542
1631
|
function isChatSessionRequestBody(value) {
|
|
1543
1632
|
if (typeof value !== "object" || value === null) return false;
|
|
1544
1633
|
const body = value;
|
|
@@ -1565,7 +1654,23 @@ function originAllowed(origin, allowedOrigins) {
|
|
|
1565
1654
|
if (!origin) return false;
|
|
1566
1655
|
return allowedOrigins.includes(origin);
|
|
1567
1656
|
}
|
|
1568
|
-
function
|
|
1657
|
+
async function safeHostUser(resolveUser, request) {
|
|
1658
|
+
if (!resolveUser) return null;
|
|
1659
|
+
try {
|
|
1660
|
+
return await resolveUser(request);
|
|
1661
|
+
} catch {
|
|
1662
|
+
console.warn("[clivly] resolveUser threw; continuing without host identity");
|
|
1663
|
+
return null;
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
function safeVisitorIp(resolveVisitorIp, request) {
|
|
1667
|
+
try {
|
|
1668
|
+
return resolveVisitorIp(request);
|
|
1669
|
+
} catch {
|
|
1670
|
+
return null;
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API_URL$1, fetchImpl = fetch, resolveUser, resolveVisitorIp = defaultVisitorIp }) {
|
|
1569
1674
|
const sessionUrl = `${normalizeApiUrl(apiUrl)}/clivly/widget/session`;
|
|
1570
1675
|
if (!allowedOrigins || allowedOrigins.length === 0) console.warn("[clivly] createChatSessionHandler has no allowedOrigins; every origin can open a chat session against this workspace. Pass allowedOrigins to restrict it.");
|
|
1571
1676
|
return async (request) => {
|
|
@@ -1585,12 +1690,8 @@ function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API
|
|
|
1585
1690
|
return jsonResponse$1({ error: "invalid_json" }, 400);
|
|
1586
1691
|
}
|
|
1587
1692
|
if (!isChatSessionRequestBody(body)) return jsonResponse$1({ error: "invalid_request" }, 400);
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
hostUser = await resolveUser(request);
|
|
1591
|
-
} catch {
|
|
1592
|
-
console.warn("[clivly] resolveUser threw; continuing without host identity");
|
|
1593
|
-
}
|
|
1693
|
+
const hostUser = await safeHostUser(resolveUser, request);
|
|
1694
|
+
const visitorIp = safeVisitorIp(resolveVisitorIp, request);
|
|
1594
1695
|
try {
|
|
1595
1696
|
const upstream = await fetchImpl(sessionUrl, {
|
|
1596
1697
|
method: "POST",
|
|
@@ -1604,7 +1705,8 @@ function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API
|
|
|
1604
1705
|
visitor: body.visitor,
|
|
1605
1706
|
visitorToken: body.visitorToken,
|
|
1606
1707
|
origin,
|
|
1607
|
-
...hostUser ? { hostUser } : {}
|
|
1708
|
+
...hostUser ? { hostUser } : {},
|
|
1709
|
+
...visitorIp ? { visitorIp } : {}
|
|
1608
1710
|
})
|
|
1609
1711
|
});
|
|
1610
1712
|
const responseBody = await upstream.text();
|
|
@@ -1966,11 +2068,11 @@ async function handlePreviewRequest(config, preview) {
|
|
|
1966
2068
|
if (!source) return jsonResponse({ error: `No configured source for table "${preview.sourceTable}"` }, 400);
|
|
1967
2069
|
const idField = source.idField ?? "id";
|
|
1968
2070
|
const filterColumns = preview.filter ? Object.keys(preview.filter).filter((k) => k !== "$raw") : [];
|
|
1969
|
-
const needed =
|
|
2071
|
+
const needed = exposableColumns(preview.sourceTable, [
|
|
1970
2072
|
idField,
|
|
1971
2073
|
...Object.values(preview.fieldMap),
|
|
1972
2074
|
...filterColumns
|
|
1973
|
-
]);
|
|
2075
|
+
], config.exposure);
|
|
1974
2076
|
const rows = (await source.fetchPage({
|
|
1975
2077
|
since: null,
|
|
1976
2078
|
limit: PREVIEW_LIMIT
|
|
@@ -1983,7 +2085,14 @@ async function handlePreviewRequest(config, preview) {
|
|
|
1983
2085
|
async function handleProjectionPreviewRequest(config, request) {
|
|
1984
2086
|
const run = config.introspector?.runProjection;
|
|
1985
2087
|
if (!run) return jsonResponse({ error: "This app cannot preview derived objects. Set `introspector: drizzleIntrospector(db)` in clivly.config.ts, on a clivly version that supports runProjection." }, 400);
|
|
1986
|
-
const
|
|
2088
|
+
const parsed = require_projection.projectionSchema.safeParse(request.projection);
|
|
2089
|
+
if (!parsed.success) return jsonResponse({ error: "The projection did not parse." }, 400);
|
|
2090
|
+
const refused = [...projectionExposureErrors(parsed.data, config.exposure), ...require_entity_config.validateProjection(parsed.data, buildSchema(config), parsed.data.entityKey).map((e) => `${e.path}: ${e.message}`)];
|
|
2091
|
+
if (refused.length > 0) return jsonResponse({
|
|
2092
|
+
error: "projection_refused",
|
|
2093
|
+
reasons: refused
|
|
2094
|
+
}, FORBIDDEN_STATUS);
|
|
2095
|
+
const rows = await run.call(config.introspector, parsed.data, { limit: request.limit });
|
|
1987
2096
|
return jsonResponse({
|
|
1988
2097
|
rows,
|
|
1989
2098
|
sampledCount: rows.length
|
|
@@ -2018,6 +2127,7 @@ function createClivlySDK(config) {
|
|
|
2018
2127
|
const fetcher = createFetcher(config.retry);
|
|
2019
2128
|
let warnedUnsignedTrigger = false;
|
|
2020
2129
|
let warnedNonPostTrigger = false;
|
|
2130
|
+
const acceptedSignatures = /* @__PURE__ */ new Map();
|
|
2021
2131
|
const remoteProjections = createRemoteProjectionFetcher({
|
|
2022
2132
|
apiKey: config.apiKey,
|
|
2023
2133
|
apiUrl,
|
|
@@ -2144,6 +2254,7 @@ function createClivlySDK(config) {
|
|
|
2144
2254
|
const runSync = async (options) => {
|
|
2145
2255
|
const remoteOutcomes = resolveRemoteProjections({
|
|
2146
2256
|
entries: await remoteProjections.fetch(),
|
|
2257
|
+
exposure: config.exposure,
|
|
2147
2258
|
introspector: config.introspector,
|
|
2148
2259
|
schema: buildSchema(config)
|
|
2149
2260
|
});
|
|
@@ -2202,7 +2313,9 @@ function createClivlySDK(config) {
|
|
|
2202
2313
|
}
|
|
2203
2314
|
};
|
|
2204
2315
|
const describeSyncTrigger = () => ({
|
|
2316
|
+
allowUnsigned: config.syncTrigger?.allowUnsigned === true,
|
|
2205
2317
|
configured: config.syncTrigger?.url,
|
|
2318
|
+
hasSecret: Boolean(config.syncTrigger?.secret),
|
|
2206
2319
|
resolved: resolveTriggerUrl({
|
|
2207
2320
|
configuredUrl: config.syncTrigger?.url,
|
|
2208
2321
|
configuredPath: config.syncTrigger?.path,
|
|
@@ -2424,13 +2537,25 @@ function createClivlySDK(config) {
|
|
|
2424
2537
|
await heartbeat();
|
|
2425
2538
|
if (config.source || config.introspector) await runSync(options);
|
|
2426
2539
|
};
|
|
2427
|
-
const
|
|
2540
|
+
const triggerRequestAuth = async (req, rawBody) => {
|
|
2428
2541
|
const secret = config.syncTrigger?.secret;
|
|
2429
|
-
if (secret)
|
|
2542
|
+
if (secret) {
|
|
2543
|
+
const header = req.headers.get(SIGNATURE_HEADER);
|
|
2544
|
+
if (!(await verifyTriggerSignature(header, rawBody, secret) && header)) return "refused";
|
|
2545
|
+
return claimSignature(header) ? "signed" : "refused";
|
|
2546
|
+
}
|
|
2547
|
+
const allowUnsigned = config.syncTrigger?.allowUnsigned === true;
|
|
2430
2548
|
if (!warnedUnsignedTrigger) {
|
|
2431
2549
|
warnedUnsignedTrigger = true;
|
|
2432
|
-
console.
|
|
2550
|
+
console.error(allowUnsigned ? "Clivly: the sync trigger route is mounted without `syncTrigger.secret` and with `allowUnsigned`, so anyone who can reach the URL can start a sync. Set CLIVLY_SYNC_TRIGGER_SECRET; requests from Clivly are refused until you do." : "Clivly: the sync trigger route is mounted without `syncTrigger.secret`, so it refuses every request, including Clivly's. Set CLIVLY_SYNC_TRIGGER_SECRET in this app's environment.");
|
|
2433
2551
|
}
|
|
2552
|
+
return allowUnsigned && rawBody === "" ? "unsigned" : "unsigned_refused";
|
|
2553
|
+
};
|
|
2554
|
+
const claimSignature = (header) => {
|
|
2555
|
+
const now = Date.now();
|
|
2556
|
+
for (const [seen, expiresAt] of acceptedSignatures) if (expiresAt <= now) acceptedSignatures.delete(seen);
|
|
2557
|
+
if (acceptedSignatures.has(header)) return false;
|
|
2558
|
+
acceptedSignatures.set(header, now + SIGNATURE_TOLERANCE_SECONDS * SECONDS_PER_MS * 2);
|
|
2434
2559
|
return true;
|
|
2435
2560
|
};
|
|
2436
2561
|
const parseTriggerBody = (rawBody) => {
|
|
@@ -2448,11 +2573,11 @@ function createClivlySDK(config) {
|
|
|
2448
2573
|
*
|
|
2449
2574
|
* Split out of the handler so the method decision reads as one thing rather
|
|
2450
2575
|
* than three branches ahead of the request plumbing — the same shape as
|
|
2451
|
-
* `
|
|
2576
|
+
* `triggerRequestAuth` above.
|
|
2452
2577
|
*/
|
|
2453
2578
|
const refuseDisallowedMethod = (req) => {
|
|
2454
2579
|
const decision = resolveTriggerMethod({
|
|
2455
|
-
enforceDefault:
|
|
2580
|
+
enforceDefault: true,
|
|
2456
2581
|
env: config.env ?? process.env,
|
|
2457
2582
|
method: req.method
|
|
2458
2583
|
});
|
|
@@ -2478,16 +2603,27 @@ function createClivlySDK(config) {
|
|
|
2478
2603
|
if (payload.previewProjection) return await handleProjectionPreviewRequest(config, payload.previewProjection);
|
|
2479
2604
|
return null;
|
|
2480
2605
|
};
|
|
2606
|
+
const refuseUnauthorized = (auth) => {
|
|
2607
|
+
if (auth === "refused") return jsonResponse({
|
|
2608
|
+
ok: false,
|
|
2609
|
+
error: "unauthorized"
|
|
2610
|
+
}, UNAUTHORIZED_STATUS);
|
|
2611
|
+
if (auth === "unsigned_refused") return jsonResponse({
|
|
2612
|
+
ok: false,
|
|
2613
|
+
error: "signature_required",
|
|
2614
|
+
message: "This route needs a signed request. Set CLIVLY_SYNC_TRIGGER_SECRET in this app's environment."
|
|
2615
|
+
}, FORBIDDEN_STATUS);
|
|
2616
|
+
return null;
|
|
2617
|
+
};
|
|
2481
2618
|
const createClivlyHandler = () => async (req) => {
|
|
2482
2619
|
observedUrl = observedTriggerUrl(req) ?? observedUrl;
|
|
2483
2620
|
const refusal = refuseDisallowedMethod(req);
|
|
2484
2621
|
if (refusal) return refusal;
|
|
2485
2622
|
const rawBody = await req.text().catch(() => "");
|
|
2486
|
-
|
|
2487
|
-
ok: false,
|
|
2488
|
-
error: "unauthorized"
|
|
2489
|
-
}, UNAUTHORIZED_STATUS);
|
|
2623
|
+
const auth = await triggerRequestAuth(req, rawBody);
|
|
2490
2624
|
const payload = parseTriggerBody(rawBody);
|
|
2625
|
+
const denied = refuseUnauthorized(auth);
|
|
2626
|
+
if (denied) return denied;
|
|
2491
2627
|
const runId = payload.runId ?? null;
|
|
2492
2628
|
const startedAt = Date.now();
|
|
2493
2629
|
try {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { i as projectionSchema } from "./projection-
|
|
2
|
-
import { _ as validateProjection, s as customObjectDeclarations, w as primaryKeyColumns } from "./entity-config-
|
|
3
|
-
import {
|
|
1
|
+
import { i as projectionSchema } from "./projection-BkYXvjTr.mjs";
|
|
2
|
+
import { _ as validateProjection, s as customObjectDeclarations, w as primaryKeyColumns } from "./entity-config-g_oMPXlN.mjs";
|
|
3
|
+
import { a as isSensitiveColumn } from "./entity-heuristics-DE5ujoHw.mjs";
|
|
4
|
+
import { t as entitiesToMappings } from "./mappings-config-D_g20PyS.mjs";
|
|
4
5
|
import { t as DRIZZLE_META } from "./drizzle-meta-D00lFvAk.mjs";
|
|
5
6
|
// ../sdk/src/deployment-identity.ts
|
|
6
7
|
const MAX_DEPLOYMENT_ID_LENGTH = 128;
|
|
@@ -1010,6 +1011,78 @@ function resolvePresenceTransport(host, configured) {
|
|
|
1010
1011
|
transport: DEFAULT_TRANSPORT
|
|
1011
1012
|
};
|
|
1012
1013
|
}
|
|
1014
|
+
// ../sdk/src/projection-guard.ts
|
|
1015
|
+
/**
|
|
1016
|
+
* Tables auth libraries keep live credentials in. Whole-table: their secrets
|
|
1017
|
+
* sit in columns with generic names (`token`, `value`) that a column rule
|
|
1018
|
+
* cannot single out. `user`, `member` and `organization` are not here — they
|
|
1019
|
+
* are legitimate contact and company sources.
|
|
1020
|
+
*/
|
|
1021
|
+
const CREDENTIAL_TABLES = /* @__PURE__ */ new Set([
|
|
1022
|
+
"session",
|
|
1023
|
+
"verification",
|
|
1024
|
+
"two_factor",
|
|
1025
|
+
"twofactor",
|
|
1026
|
+
"passkey",
|
|
1027
|
+
"apikey",
|
|
1028
|
+
"api_key",
|
|
1029
|
+
"jwks"
|
|
1030
|
+
]);
|
|
1031
|
+
const BASE_ALIAS = "base";
|
|
1032
|
+
function normalise(name) {
|
|
1033
|
+
return name.toLowerCase().replaceAll("-", "_");
|
|
1034
|
+
}
|
|
1035
|
+
/** Every (table, column) a projection reads, with join aliases resolved. */
|
|
1036
|
+
function readColumns(projection) {
|
|
1037
|
+
const tableOf = /* @__PURE__ */ new Map([[BASE_ALIAS, projection.baseTable]]);
|
|
1038
|
+
for (const join of projection.joins) tableOf.set(join.key, join.table);
|
|
1039
|
+
const refs = [];
|
|
1040
|
+
for (const join of projection.joins) for (const condition of join.on) refs.push(condition.left, condition.right);
|
|
1041
|
+
for (const binding of Object.values(projection.fieldBindings)) refs.push(binding.from);
|
|
1042
|
+
const { identity } = projection;
|
|
1043
|
+
if (identity.kind === "column") refs.push(identity.from);
|
|
1044
|
+
else if (identity.kind === "composite") refs.push(...identity.parts);
|
|
1045
|
+
else if (identity.column) refs.push({
|
|
1046
|
+
table: BASE_ALIAS,
|
|
1047
|
+
column: identity.column
|
|
1048
|
+
});
|
|
1049
|
+
return refs.map((ref) => ({
|
|
1050
|
+
table: tableOf.get(ref.table) ?? ref.table,
|
|
1051
|
+
column: ref.column
|
|
1052
|
+
}));
|
|
1053
|
+
}
|
|
1054
|
+
function referencedTables(projection) {
|
|
1055
|
+
return [projection.baseTable, ...projection.joins.map((join) => join.table)];
|
|
1056
|
+
}
|
|
1057
|
+
/** Why this projection may not run here; empty when it may. */
|
|
1058
|
+
function projectionExposureErrors(projection, policy = {}) {
|
|
1059
|
+
const allowed = new Set((policy.allowSensitive ?? []).map(normalise));
|
|
1060
|
+
const errors = [];
|
|
1061
|
+
for (const table of referencedTables(projection)) {
|
|
1062
|
+
if (CREDENTIAL_TABLES.has(normalise(table)) && !allowed.has(normalise(`${table}.*`))) errors.push(`Table "${table}" holds credentials and is never synced.`);
|
|
1063
|
+
if (policy.tables && !(table in policy.tables)) errors.push(`Table "${table}" is not in this app's exposure.tables allowlist.`);
|
|
1064
|
+
}
|
|
1065
|
+
for (const { table, column } of readColumns(projection)) {
|
|
1066
|
+
const ref = `${table}.${column}`;
|
|
1067
|
+
if (isSensitiveColumn(column) && !allowed.has(normalise(ref))) errors.push(`Column "${ref}" looks like a credential and is never synced. List it in exposure.allowSensitive if it is safe.`);
|
|
1068
|
+
const columns = policy.tables?.[table];
|
|
1069
|
+
if (Array.isArray(columns) && !columns.includes(column)) errors.push(`Column "${ref}" is not in this app's exposure.tables allowlist.`);
|
|
1070
|
+
}
|
|
1071
|
+
return [...new Set(errors)];
|
|
1072
|
+
}
|
|
1073
|
+
/** Drop credential columns, and columns outside the allowlist, from a raw-row preview. */
|
|
1074
|
+
function exposableColumns(table, columns, policy = {}) {
|
|
1075
|
+
const allowed = new Set((policy.allowSensitive ?? []).map(normalise));
|
|
1076
|
+
const result = /* @__PURE__ */ new Set();
|
|
1077
|
+
if (CREDENTIAL_TABLES.has(normalise(table)) && !allowed.has(normalise(`${table}.*`)) || policy.tables !== void 0 && !(table in policy.tables)) return result;
|
|
1078
|
+
const listed = policy.tables?.[table];
|
|
1079
|
+
for (const column of columns) {
|
|
1080
|
+
if (isSensitiveColumn(column) && !allowed.has(normalise(`${table}.${column}`))) continue;
|
|
1081
|
+
if (Array.isArray(listed) && !listed.includes(column)) continue;
|
|
1082
|
+
result.add(column);
|
|
1083
|
+
}
|
|
1084
|
+
return result;
|
|
1085
|
+
}
|
|
1013
1086
|
// ../sdk/src/push-outcome.ts
|
|
1014
1087
|
function isCount(value) {
|
|
1015
1088
|
return typeof value === "number" && Number.isFinite(value);
|
|
@@ -1049,8 +1122,8 @@ const MISSING_INTROSPECTOR = "this app cannot run dashboard-authored derived obj
|
|
|
1049
1122
|
* `sql.raw` at execution time.
|
|
1050
1123
|
*/
|
|
1051
1124
|
function resolveRemoteProjections(args) {
|
|
1052
|
-
const { entries, introspector, schema } = args;
|
|
1053
|
-
return entries.map((entry) => resolveOne(entry, schema, introspector));
|
|
1125
|
+
const { entries, exposure, introspector, schema } = args;
|
|
1126
|
+
return entries.map((entry) => resolveOne(entry, schema, introspector, exposure));
|
|
1054
1127
|
}
|
|
1055
1128
|
/**
|
|
1056
1129
|
* The base table's PK, resolved from the schema THIS app discovered — never
|
|
@@ -1071,7 +1144,7 @@ function resolveLocalBasePk(projection, schema) {
|
|
|
1071
1144
|
if (!baseTable) return;
|
|
1072
1145
|
return primaryKeyColumns(baseTable)[0];
|
|
1073
1146
|
}
|
|
1074
|
-
function resolveOne(entry, schema, introspector) {
|
|
1147
|
+
function resolveOne(entry, schema, introspector, exposure) {
|
|
1075
1148
|
const parsed = projectionSchema.safeParse(entry.projection);
|
|
1076
1149
|
if (!parsed.success) return {
|
|
1077
1150
|
id: entry.id,
|
|
@@ -1079,6 +1152,12 @@ function resolveOne(entry, schema, introspector) {
|
|
|
1079
1152
|
reason: `projection did not parse: ${parsed.error.issues.map((i) => `${i.path.join(".")}: ${i.message}`).join("; ")}`
|
|
1080
1153
|
};
|
|
1081
1154
|
const projection = parsed.data;
|
|
1155
|
+
const refused = projectionExposureErrors(projection, exposure);
|
|
1156
|
+
if (refused.length > 0) return {
|
|
1157
|
+
id: entry.id,
|
|
1158
|
+
status: "invalid",
|
|
1159
|
+
reason: refused.join("; ")
|
|
1160
|
+
};
|
|
1082
1161
|
const errors = validateProjection(projection, schema, entry.entityKey);
|
|
1083
1162
|
if (errors.length > 0) return {
|
|
1084
1163
|
id: entry.id,
|
|
@@ -1268,7 +1347,7 @@ function selfCheck(input) {
|
|
|
1268
1347
|
const issues = [];
|
|
1269
1348
|
if (input.reportsTriggerUrl && !input.hasTriggerSecret) issues.push({
|
|
1270
1349
|
code: "trigger_route_unauthenticated",
|
|
1271
|
-
message: "Your app's sync route
|
|
1350
|
+
message: "Your app's sync route has no signing secret, so it refuses every sync Clivly starts.",
|
|
1272
1351
|
fix: "Set CLIVLY_SYNC_TRIGGER_SECRET in the environment your app runs in, then restart it."
|
|
1273
1352
|
});
|
|
1274
1353
|
if (input.lastPushFailed) issues.push({
|
|
@@ -1376,7 +1455,7 @@ async function fullSyncSource(args) {
|
|
|
1376
1455
|
// ../sdk/src/trigger-method.ts
|
|
1377
1456
|
/** Host-side override, mirroring `CLIVLY_INSTANT_PUSH_DISABLED`. */
|
|
1378
1457
|
const REQUIRE_POST_ENV_VAR = "CLIVLY_TRIGGER_REQUIRE_POST";
|
|
1379
|
-
const NON_POST_DEPRECATION_WARNING = `Clivly: this sync trigger was called with GET
|
|
1458
|
+
const NON_POST_DEPRECATION_WARNING = `Clivly: this sync trigger was called with GET and ran only because ${REQUIRE_POST_ENV_VAR}=false. A GET can be started by a crawler or any web page, so change the caller — your cron job or uptime check — to POST, then remove ${REQUIRE_POST_ENV_VAR}.`;
|
|
1380
1459
|
const METHOD_NOT_ALLOWED = 405;
|
|
1381
1460
|
const NO_CONTENT = 204;
|
|
1382
1461
|
const OK = 200;
|
|
@@ -1539,6 +1618,16 @@ function reportedOrigin(req) {
|
|
|
1539
1618
|
function trustedOrigin(req) {
|
|
1540
1619
|
return req.headers.get("origin");
|
|
1541
1620
|
}
|
|
1621
|
+
/**
|
|
1622
|
+
* The visitor's address as the host's platform reported it. Clivly throttles
|
|
1623
|
+
* new chats per visitor on it; without it the only address Clivly sees is this
|
|
1624
|
+
* server's, so every visitor shared one bucket.
|
|
1625
|
+
*/
|
|
1626
|
+
function defaultVisitorIp(req) {
|
|
1627
|
+
const direct = req.headers.get("cf-connecting-ip") ?? req.headers.get("x-real-ip");
|
|
1628
|
+
if (direct) return direct.trim();
|
|
1629
|
+
return req.headers.get("x-forwarded-for")?.split(",")[0]?.trim() || null;
|
|
1630
|
+
}
|
|
1542
1631
|
function isChatSessionRequestBody(value) {
|
|
1543
1632
|
if (typeof value !== "object" || value === null) return false;
|
|
1544
1633
|
const body = value;
|
|
@@ -1565,7 +1654,23 @@ function originAllowed(origin, allowedOrigins) {
|
|
|
1565
1654
|
if (!origin) return false;
|
|
1566
1655
|
return allowedOrigins.includes(origin);
|
|
1567
1656
|
}
|
|
1568
|
-
function
|
|
1657
|
+
async function safeHostUser(resolveUser, request) {
|
|
1658
|
+
if (!resolveUser) return null;
|
|
1659
|
+
try {
|
|
1660
|
+
return await resolveUser(request);
|
|
1661
|
+
} catch {
|
|
1662
|
+
console.warn("[clivly] resolveUser threw; continuing without host identity");
|
|
1663
|
+
return null;
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
function safeVisitorIp(resolveVisitorIp, request) {
|
|
1667
|
+
try {
|
|
1668
|
+
return resolveVisitorIp(request);
|
|
1669
|
+
} catch {
|
|
1670
|
+
return null;
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API_URL$1, fetchImpl = fetch, resolveUser, resolveVisitorIp = defaultVisitorIp }) {
|
|
1569
1674
|
const sessionUrl = `${normalizeApiUrl(apiUrl)}/clivly/widget/session`;
|
|
1570
1675
|
if (!allowedOrigins || allowedOrigins.length === 0) console.warn("[clivly] createChatSessionHandler has no allowedOrigins; every origin can open a chat session against this workspace. Pass allowedOrigins to restrict it.");
|
|
1571
1676
|
return async (request) => {
|
|
@@ -1585,12 +1690,8 @@ function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API
|
|
|
1585
1690
|
return jsonResponse$1({ error: "invalid_json" }, 400);
|
|
1586
1691
|
}
|
|
1587
1692
|
if (!isChatSessionRequestBody(body)) return jsonResponse$1({ error: "invalid_request" }, 400);
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
hostUser = await resolveUser(request);
|
|
1591
|
-
} catch {
|
|
1592
|
-
console.warn("[clivly] resolveUser threw; continuing without host identity");
|
|
1593
|
-
}
|
|
1693
|
+
const hostUser = await safeHostUser(resolveUser, request);
|
|
1694
|
+
const visitorIp = safeVisitorIp(resolveVisitorIp, request);
|
|
1594
1695
|
try {
|
|
1595
1696
|
const upstream = await fetchImpl(sessionUrl, {
|
|
1596
1697
|
method: "POST",
|
|
@@ -1604,7 +1705,8 @@ function createChatSessionHandler({ allowedOrigins, apiKey, apiUrl = DEFAULT_API
|
|
|
1604
1705
|
visitor: body.visitor,
|
|
1605
1706
|
visitorToken: body.visitorToken,
|
|
1606
1707
|
origin,
|
|
1607
|
-
...hostUser ? { hostUser } : {}
|
|
1708
|
+
...hostUser ? { hostUser } : {},
|
|
1709
|
+
...visitorIp ? { visitorIp } : {}
|
|
1608
1710
|
})
|
|
1609
1711
|
});
|
|
1610
1712
|
const responseBody = await upstream.text();
|
|
@@ -1966,11 +2068,11 @@ async function handlePreviewRequest(config, preview) {
|
|
|
1966
2068
|
if (!source) return jsonResponse({ error: `No configured source for table "${preview.sourceTable}"` }, 400);
|
|
1967
2069
|
const idField = source.idField ?? "id";
|
|
1968
2070
|
const filterColumns = preview.filter ? Object.keys(preview.filter).filter((k) => k !== "$raw") : [];
|
|
1969
|
-
const needed =
|
|
2071
|
+
const needed = exposableColumns(preview.sourceTable, [
|
|
1970
2072
|
idField,
|
|
1971
2073
|
...Object.values(preview.fieldMap),
|
|
1972
2074
|
...filterColumns
|
|
1973
|
-
]);
|
|
2075
|
+
], config.exposure);
|
|
1974
2076
|
const rows = (await source.fetchPage({
|
|
1975
2077
|
since: null,
|
|
1976
2078
|
limit: PREVIEW_LIMIT
|
|
@@ -1983,7 +2085,14 @@ async function handlePreviewRequest(config, preview) {
|
|
|
1983
2085
|
async function handleProjectionPreviewRequest(config, request) {
|
|
1984
2086
|
const run = config.introspector?.runProjection;
|
|
1985
2087
|
if (!run) return jsonResponse({ error: "This app cannot preview derived objects. Set `introspector: drizzleIntrospector(db)` in clivly.config.ts, on a clivly version that supports runProjection." }, 400);
|
|
1986
|
-
const
|
|
2088
|
+
const parsed = projectionSchema.safeParse(request.projection);
|
|
2089
|
+
if (!parsed.success) return jsonResponse({ error: "The projection did not parse." }, 400);
|
|
2090
|
+
const refused = [...projectionExposureErrors(parsed.data, config.exposure), ...validateProjection(parsed.data, buildSchema(config), parsed.data.entityKey).map((e) => `${e.path}: ${e.message}`)];
|
|
2091
|
+
if (refused.length > 0) return jsonResponse({
|
|
2092
|
+
error: "projection_refused",
|
|
2093
|
+
reasons: refused
|
|
2094
|
+
}, FORBIDDEN_STATUS);
|
|
2095
|
+
const rows = await run.call(config.introspector, parsed.data, { limit: request.limit });
|
|
1987
2096
|
return jsonResponse({
|
|
1988
2097
|
rows,
|
|
1989
2098
|
sampledCount: rows.length
|
|
@@ -2018,6 +2127,7 @@ function createClivlySDK(config) {
|
|
|
2018
2127
|
const fetcher = createFetcher(config.retry);
|
|
2019
2128
|
let warnedUnsignedTrigger = false;
|
|
2020
2129
|
let warnedNonPostTrigger = false;
|
|
2130
|
+
const acceptedSignatures = /* @__PURE__ */ new Map();
|
|
2021
2131
|
const remoteProjections = createRemoteProjectionFetcher({
|
|
2022
2132
|
apiKey: config.apiKey,
|
|
2023
2133
|
apiUrl,
|
|
@@ -2144,6 +2254,7 @@ function createClivlySDK(config) {
|
|
|
2144
2254
|
const runSync = async (options) => {
|
|
2145
2255
|
const remoteOutcomes = resolveRemoteProjections({
|
|
2146
2256
|
entries: await remoteProjections.fetch(),
|
|
2257
|
+
exposure: config.exposure,
|
|
2147
2258
|
introspector: config.introspector,
|
|
2148
2259
|
schema: buildSchema(config)
|
|
2149
2260
|
});
|
|
@@ -2202,7 +2313,9 @@ function createClivlySDK(config) {
|
|
|
2202
2313
|
}
|
|
2203
2314
|
};
|
|
2204
2315
|
const describeSyncTrigger = () => ({
|
|
2316
|
+
allowUnsigned: config.syncTrigger?.allowUnsigned === true,
|
|
2205
2317
|
configured: config.syncTrigger?.url,
|
|
2318
|
+
hasSecret: Boolean(config.syncTrigger?.secret),
|
|
2206
2319
|
resolved: resolveTriggerUrl({
|
|
2207
2320
|
configuredUrl: config.syncTrigger?.url,
|
|
2208
2321
|
configuredPath: config.syncTrigger?.path,
|
|
@@ -2424,13 +2537,25 @@ function createClivlySDK(config) {
|
|
|
2424
2537
|
await heartbeat();
|
|
2425
2538
|
if (config.source || config.introspector) await runSync(options);
|
|
2426
2539
|
};
|
|
2427
|
-
const
|
|
2540
|
+
const triggerRequestAuth = async (req, rawBody) => {
|
|
2428
2541
|
const secret = config.syncTrigger?.secret;
|
|
2429
|
-
if (secret)
|
|
2542
|
+
if (secret) {
|
|
2543
|
+
const header = req.headers.get(SIGNATURE_HEADER);
|
|
2544
|
+
if (!(await verifyTriggerSignature(header, rawBody, secret) && header)) return "refused";
|
|
2545
|
+
return claimSignature(header) ? "signed" : "refused";
|
|
2546
|
+
}
|
|
2547
|
+
const allowUnsigned = config.syncTrigger?.allowUnsigned === true;
|
|
2430
2548
|
if (!warnedUnsignedTrigger) {
|
|
2431
2549
|
warnedUnsignedTrigger = true;
|
|
2432
|
-
console.
|
|
2550
|
+
console.error(allowUnsigned ? "Clivly: the sync trigger route is mounted without `syncTrigger.secret` and with `allowUnsigned`, so anyone who can reach the URL can start a sync. Set CLIVLY_SYNC_TRIGGER_SECRET; requests from Clivly are refused until you do." : "Clivly: the sync trigger route is mounted without `syncTrigger.secret`, so it refuses every request, including Clivly's. Set CLIVLY_SYNC_TRIGGER_SECRET in this app's environment.");
|
|
2433
2551
|
}
|
|
2552
|
+
return allowUnsigned && rawBody === "" ? "unsigned" : "unsigned_refused";
|
|
2553
|
+
};
|
|
2554
|
+
const claimSignature = (header) => {
|
|
2555
|
+
const now = Date.now();
|
|
2556
|
+
for (const [seen, expiresAt] of acceptedSignatures) if (expiresAt <= now) acceptedSignatures.delete(seen);
|
|
2557
|
+
if (acceptedSignatures.has(header)) return false;
|
|
2558
|
+
acceptedSignatures.set(header, now + SIGNATURE_TOLERANCE_SECONDS * SECONDS_PER_MS * 2);
|
|
2434
2559
|
return true;
|
|
2435
2560
|
};
|
|
2436
2561
|
const parseTriggerBody = (rawBody) => {
|
|
@@ -2448,11 +2573,11 @@ function createClivlySDK(config) {
|
|
|
2448
2573
|
*
|
|
2449
2574
|
* Split out of the handler so the method decision reads as one thing rather
|
|
2450
2575
|
* than three branches ahead of the request plumbing — the same shape as
|
|
2451
|
-
* `
|
|
2576
|
+
* `triggerRequestAuth` above.
|
|
2452
2577
|
*/
|
|
2453
2578
|
const refuseDisallowedMethod = (req) => {
|
|
2454
2579
|
const decision = resolveTriggerMethod({
|
|
2455
|
-
enforceDefault:
|
|
2580
|
+
enforceDefault: true,
|
|
2456
2581
|
env: config.env ?? process.env,
|
|
2457
2582
|
method: req.method
|
|
2458
2583
|
});
|
|
@@ -2478,16 +2603,27 @@ function createClivlySDK(config) {
|
|
|
2478
2603
|
if (payload.previewProjection) return await handleProjectionPreviewRequest(config, payload.previewProjection);
|
|
2479
2604
|
return null;
|
|
2480
2605
|
};
|
|
2606
|
+
const refuseUnauthorized = (auth) => {
|
|
2607
|
+
if (auth === "refused") return jsonResponse({
|
|
2608
|
+
ok: false,
|
|
2609
|
+
error: "unauthorized"
|
|
2610
|
+
}, UNAUTHORIZED_STATUS);
|
|
2611
|
+
if (auth === "unsigned_refused") return jsonResponse({
|
|
2612
|
+
ok: false,
|
|
2613
|
+
error: "signature_required",
|
|
2614
|
+
message: "This route needs a signed request. Set CLIVLY_SYNC_TRIGGER_SECRET in this app's environment."
|
|
2615
|
+
}, FORBIDDEN_STATUS);
|
|
2616
|
+
return null;
|
|
2617
|
+
};
|
|
2481
2618
|
const createClivlyHandler = () => async (req) => {
|
|
2482
2619
|
observedUrl = observedTriggerUrl(req) ?? observedUrl;
|
|
2483
2620
|
const refusal = refuseDisallowedMethod(req);
|
|
2484
2621
|
if (refusal) return refusal;
|
|
2485
2622
|
const rawBody = await req.text().catch(() => "");
|
|
2486
|
-
|
|
2487
|
-
ok: false,
|
|
2488
|
-
error: "unauthorized"
|
|
2489
|
-
}, UNAUTHORIZED_STATUS);
|
|
2623
|
+
const auth = await triggerRequestAuth(req, rawBody);
|
|
2490
2624
|
const payload = parseTriggerBody(rawBody);
|
|
2625
|
+
const denied = refuseUnauthorized(auth);
|
|
2626
|
+
if (denied) return denied;
|
|
2491
2627
|
const runId = payload.runId ?? null;
|
|
2492
2628
|
const startedAt = Date.now();
|
|
2493
2629
|
try {
|