Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(api): provider access after agreement reacceptance (#4709)

Developers Digest4cffaf2355c3
diff --git a/apps/api/src/services/alexandria/access.test.ts b/apps/api/src/services/alexandria/access.test.ts
index 1b8e2271b..8c847ed3c 100644
--- a/apps/api/src/services/alexandria/access.test.ts
+++ b/apps/api/src/services/alexandria/access.test.ts
@@ -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();
+});
diff --git a/apps/api/src/services/alexandria/access.ts b/apps/api/src/services/alexandria/access.ts
index 9b8ff71b6..3f75f2956 100644
--- a/apps/api/src/services/alexandria/access.ts
+++ b/apps/api/src/services/alexandria/access.ts
@@ -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[],
@@ -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 &&
@@ -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,
diff --git a/apps/api/src/services/alexandria/terms.ts b/apps/api/src/services/alexandria/terms.ts
index a203666a7..deff0a7e2 100644
--- a/apps/api/src/services/alexandria/terms.ts
+++ b/apps/api/src/services/alexandria/terms.ts
@@ -45,12 +45,17 @@ const statusSchema = z.object({
version: z.string().nullable(),
textHash: z.string().nullable(),
revoked: z.boolean(),
+ acceptedAt: z.unknown().optional(),
})
.passthrough(),
),
});
-export type LedgerAcceptance = { version: string; textHash: string | null };
+export type LedgerAcceptance = {
+ version: string;
+ textHash: string | null;
+ acceptedAt?: string | null;
+};
export async function acceptedProviders(
teamId: string,
@@ -72,6 +77,8 @@ export async function acceptedProviders(
accepted.set(item.provider, {
version: item.version,
textHash: item.textHash,
+ acceptedAt:
+ typeof item.acceptedAt === "string" ? item.acceptedAt : null,
});
}
return accepted;