Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "../../utils/defaultModels/enrichmentEntitlement.js";

const KEY = "test-only-entitlement-key-with-at-least-32-bytes";
const BILLING_CONNECTION_ID = "11111111-1111-4111-8111-111111111111";
const NOW = new Date("2026-08-30T12:00:00.000Z");

const signedLease = (overrides = {}) => {
Expand All @@ -20,6 +21,7 @@ const signedLease = (overrides = {}) => {
issued_at: "2026-08-30T11:55:00.000Z",
valid_until: "2026-08-30T12:25:00.000Z",
products: ["ctx:day-archetype", "ctx:weather-archetype"],
billing_connection_id: BILLING_CONNECTION_ID,
...overrides,
};
return {
Expand Down Expand Up @@ -48,13 +50,15 @@ describe("enrichment entitlement reconciliation", () => {
assert.equal(result.valid, true);
assert.equal(result.enabled, true);
assert.equal(result.lease.entitlement_revision, "7");
assert.equal(result.lease.billing_connection_id, BILLING_CONNECTION_ID);
});

it("fails closed on signature tampering, expiry, and account mismatch", async () => {
for (const lease of [
{ ...signedLease(), signature: "tampered" },
signedLease({ valid_until: "2026-08-30T11:59:59.000Z" }),
signedLease({ account_partition: "other.is" }),
signedLease({ billing_connection_id: null }),
]) {
const result = await resolveEnrichmentEntitlement("customer.is", config, {
now: () => NOW,
Expand Down Expand Up @@ -94,6 +98,7 @@ describe("enrichment entitlement reconciliation", () => {
signature_version: "hmac-sha256-v1",
signature: "opaque",
products: ["ctx:day-archetype", "ctx:weather-archetype"],
billing_connection_id: BILLING_CONNECTION_ID,
},
});
assert.deepEqual(merged.default_models, settings.default_models);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const canonicalPayload = (payload) =>
issued_at: payload?.issued_at,
valid_until: payload?.valid_until,
products: payload?.products,
billing_connection_id: payload?.billing_connection_id,
});

const signaturesMatch = (left, right) => {
Expand Down Expand Up @@ -64,7 +65,12 @@ export const resolveEnrichmentEntitlement = async (
typeof payload?.enabled !== "boolean" ||
typeof payload?.entitlement_revision !== "string" ||
!Array.isArray(payload?.products) ||
payload.products.join(",") !== PRODUCTS.join(",")
payload.products.join(",") !== PRODUCTS.join(",") ||
(payload.enabled
? !/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(
payload.billing_connection_id || "",
)
: payload.billing_connection_id !== null)
) {
return disabled("lease_malformed");
}
Expand Down Expand Up @@ -96,6 +102,7 @@ export const resolveEnrichmentEntitlement = async (
signature_version: envelope.signature_version,
signature: envelope.signature,
products: [...payload.products],
billing_connection_id: payload.billing_connection_id,
},
};
} catch {
Expand All @@ -115,6 +122,7 @@ export const mergeEnrichmentEntitlement = (settings = {}, resolution) => {
signature_version: null,
signature: null,
products: PRODUCTS,
billing_connection_id: null,
reason: resolution.reason || "invalid_lease",
};
return {
Expand Down
Loading
Loading