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
1 change: 1 addition & 0 deletions apps/api/src/controllers/v2/scrape-alexandria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export async function providerScrapeController(
teamId: req.auth.team_id,
orgId: orgIdFromAcuc(req.acuc),
apiKeyId: req.acuc.api_key_id ?? null,
apiKeyIdText: req.acuc.api_key_id_text ?? null,
flags: req.acuc.flags,
calls: body.alexandria,
resultAuthorization: req.get("authorization"),
Expand Down
262 changes: 262 additions & 0 deletions apps/api/src/services/alexandria/access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,265 @@ it("refuses a disabled provider and fails closed when agreements are unavailable
mocks.request.mockResolvedValue({ status: 503, body: "down" });
expect((await authorizeProviders("team", calls, {}))?.status).toBe(503);
});

it("allows a current agreement accepted after owner revocation, then blocks a later revocation", async () => {
const terms = { ...requirement(true) };
Object.assign(terms.body.providers[0].terms, { digest: "a".repeat(64) });
const access = {
status: "disabled",
disabledReason: "revoked_by_organization_admin",
disabledAt: "2026-09-20T12:00:00Z",
termsKey: "fred",
termsVersion: "2026-01",
};
const flags = { organizationDataSourceAccess: { fred: access } };
let acceptance = {
provider: "fred",
revoked: false,
version: "2026-01",
textHash: "a".repeat(64),
acceptedAt: "2026-09-20T11:00:00Z",
};
mocks.request.mockImplementation(async ({ path }) =>
path.includes("requirements")
? terms
: { status: 200, body: { providers: [acceptance] } },
);
expect(
(await authorizeProviders("team", calls, flags, "org"))?.body,
).toMatchObject({ code: "THIRD_PARTY_DATA_TERMS_REQUIRED" });
acceptance = {
...acceptance,
revoked: false,
acceptedAt: "2026-09-20T12:01:00Z",
};
expect(await authorizeProviders("team", calls, flags, "org")).toBeUndefined();
access.disabledAt = "2026-09-20T12:02:00Z";
expect((await authorizeProviders("team", calls, flags, "org"))?.status).toBe(
403,
);
});

it.each([
[
"disabled",
"revoked_by_staff",
"2026-09-20T12:00:00Z",
"2026-09-20T12:01:00Z",
"2026-01",
"a".repeat(64),
false,
],
[
"suspended",
"revoked_by_organization_admin",
"2026-09-20T12:00:00Z",
"2026-09-20T12:01:00Z",
"2026-01",
"a".repeat(64),
false,
],
[
"disabled",
"disabled_by_organization_admin",
"2026-09-20T12:00:00Z",
"2026-09-20T12:01:00Z",
"2026-01",
"a".repeat(64),
false,
],
[
"disabled",
"revoked_by_organization_admin",
"",
"2026-09-20T12:01:00Z",
"2026-01",
"a".repeat(64),
false,
],
[
"disabled",
"revoked_by_organization_admin",
"2026-09-20T12:00:00Z",
"",
"2026-01",
"a".repeat(64),
false,
],
[
"disabled",
"revoked_by_organization_admin",
"2026-09-20T12:00:00Z",
"2026-09-20T12:00:00Z",
"2026-01",
"a".repeat(64),
false,
],
[
"disabled",
"revoked_by_organization_admin",
"2026-09-20T12:00:00Z",
"2026-09-20T12:01:00Z",
"old",
"a".repeat(64),
false,
],
[
"disabled",
"revoked_by_organization_admin",
"2026-09-20T12:00:00Z",
"2026-09-20T12:01:00Z",
"2026-01",
"b".repeat(64),
false,
],
[
"disabled",
"revoked_by_organization_admin",
"2026-09-20T12:00:00Z",
"2026-09-20T12:01:00Z",
"2026-01",
"a".repeat(64),
true,
],
])(
"does not reopen access for %s / %s / %s / %s / %s / %s / revoked=%s",
async (
status,
disabledReason,
disabledAt,
acceptedAt,
version,
textHash,
revoked,
) => {
const terms = requirement(true);
Object.assign(terms.body.providers[0].terms, { digest: "a".repeat(64) });
mocks.request.mockImplementation(async ({ path }) =>
path.includes("requirements")
? terms
: {
status: 200,
body: {
providers: [
{ provider: "fred", revoked, version, textHash, acceptedAt },
],
},
},
);
const denied = await authorizeProviders(
"team",
calls,
{
organizationDataSourceAccess: {
fred: { status, disabledReason, disabledAt },
},
},
"org",
);
expect(denied?.status).toBe(403);
if (
status === "disabled" &&
disabledReason === "revoked_by_organization_admin"
)
expect(denied?.body).toMatchObject({
code: "THIRD_PARTY_DATA_TERMS_REQUIRED",
});
else {
expect(denied?.body).toMatchObject({
success: false,
error: "Access to fred is disabled for this organization.",
});
expect(denied?.body).not.toHaveProperty(
"code",
"THIRD_PARTY_DATA_TERMS_REQUIRED",
);
}
},
);

it.each([
"0",
"2026-02-30T12:00:00Z",
"2026-09-20",
"2026-09-20T12:00:00",
1789905600000,
null,
])(
"fails closed for invalid revocation or acceptance timestamp %s",
async bad => {
const terms = requirement(true);
Object.assign(terms.body.providers[0].terms, { digest: "a".repeat(64) });
for (const side of ["revocation", "acceptance"]) {
mocks.request.mockImplementation(async ({ path }) =>
path.includes("requirements")
? terms
: {
status: 200,
body: {
providers: [
{
provider: "fred",
revoked: false,
version: "2026-01",
textHash: "a".repeat(64),
acceptedAt:
side === "acceptance" ? bad : "2026-09-20T12:01:00Z",
},
],
},
},
);
const result = await authorizeProviders(
"team",
calls,
{
organizationDataSourceAccess: {
fred: {
status: "disabled",
disabledReason: "revoked_by_organization_admin",
disabledAt: (side === "revocation"
? bad
: "2026-09-20T12:00:00Z") as string,
},
},
},
"org",
);
expect(result?.body, side).toMatchObject({
code: "THIRD_PARTY_DATA_TERMS_REQUIRED",
});
}
},
);

it("does not discard other acceptances when a timestamp has an unexpected shape", async () => {
const terms = requirement(true);
Object.assign(terms.body.providers[0].terms, { digest: "a".repeat(64) });
mocks.request.mockImplementation(async ({ path }) =>
path.includes("requirements")
? terms
: {
status: 200,
body: {
providers: [
{
provider: "other",
revoked: false,
version: "v1",
textHash: "b".repeat(64),
acceptedAt: { invalid: true },
},
{
provider: "fred",
revoked: false,
version: "2026-01",
textHash: "a".repeat(64),
acceptedAt: "2026-09-20T12:01:00Z",
},
],
},
},
);
expect(await authorizeProviders("team", calls, {}, "org")).toBeUndefined();
});
48 changes: 41 additions & 7 deletions apps/api/src/services/alexandria/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ const requirementsSchema = z.object({
),
});

const timestampSchema = z.iso.datetime({ offset: true });

function timestamp(value: unknown): number {
const parsed = timestampSchema.safeParse(value);
return parsed.success ? Date.parse(parsed.data) : NaN;
}

function matchesAcceptance(
accepted: LedgerAcceptance | undefined,
terms: { version: string; digest?: string },
): boolean {
return (
terms.digest !== undefined &&
accepted?.version === terms.version &&
accepted.textHash === terms.digest
);
}

export async function authorizeProviders(
teamId: string,
calls: ProviderCall[],
Expand Down Expand Up @@ -61,11 +79,32 @@ export async function authorizeProviders(
let ledger: Map<string, LedgerAcceptance> | undefined;
for (const item of parsed.data.providers) {
const access = flags?.organizationDataSourceAccess?.[item.provider];
if (access && access.status !== "enabled")
if (access && access.status !== "enabled") {
const revokedByOwner =
access.status === "disabled" &&
access.disabledReason === "revoked_by_organization_admin";
if (revokedByOwner && item.required && item.terms && orgId !== null) {
ledger ??= await acceptedProviders(teamId, orgId);
const accepted = ledger.get(item.provider);
const acceptedAt = timestamp(accepted?.acceptedAt);
const disabledAt = timestamp(access.disabledAt);
if (
matchesAcceptance(accepted, item.terms) &&
Number.isFinite(acceptedAt) &&
Number.isFinite(disabledAt) &&
acceptedAt > disabledAt
)
continue;
return {
status: 403,
body: getThirdPartyDataTermsRequiredResponse(item.terms),
};
}
return refusal(
403,
`Access to ${item.provider} is disabled for this organization.`,
);
}
if (!item.required || !item.terms) continue;
if (
access?.termsKey === item.terms.key &&
Expand All @@ -75,12 +114,7 @@ export async function authorizeProviders(
if (orgId !== null) {
ledger ??= await acceptedProviders(teamId, orgId);
const accepted = ledger.get(item.provider);
if (
item.terms.digest !== undefined &&
accepted?.version === item.terms.version &&
accepted.textHash === item.terms.digest
)
continue;
if (matchesAcceptance(accepted, item.terms)) continue;
}
return {
status: 403,
Expand Down
7 changes: 7 additions & 0 deletions apps/api/src/services/alexandria/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function exchangeRequest(input: {
requestId?: string;
maximumCredits?: number;
resultAuthorization?: string;
termsIdentity?: { organizationId: string; apiKeyId: string };
}): Promise<ExchangeResponse> {
if (!config.FIRE_EXCHANGE_URL) throw new Error("Exchange is not configured");
const base = config.FIRE_EXCHANGE_URL.replace(/\/+$/, "");
Expand All @@ -30,6 +31,12 @@ export async function exchangeRequest(input: {
? { authorization: input.resultAuthorization }
: {}),
"x-exchange-team-id": input.teamId,
...(input.termsIdentity
? {
"x-exchange-organization-id": input.termsIdentity.organizationId,
"x-exchange-api-key-id": input.termsIdentity.apiKeyId,
}
: {}),
"x-exchange-extended-catalog-access": "true",
...(input.requestId ? { "x-request-id": input.requestId } : {}),
...(input.maximumCredits === undefined
Expand Down
Loading
Loading