Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/quiet-access-caches.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cloudflare/workers-auth": patch
---

Use the current Cloudflare Access service-token credentials after environment variables change. Interactive Access cookie caching is unchanged.
4 changes: 1 addition & 3 deletions packages/workers-auth/src/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ export async function getAccessHeaders(

if (clientId && clientSecret) {
logger.debug("Using Access Service Token headers for domain:", domain);
const headers = {
return {
"CF-Access-Client-Id": clientId,
"CF-Access-Client-Secret": clientSecret,
};
headersCache[domain] = headers;
return headers;
}

// Warn if only one of the two env vars is set
Expand Down
102 changes: 102 additions & 0 deletions packages/workers-auth/tests/access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const msw = setupServer();

beforeAll(() => msw.listen({ onUnhandledRequest: "error" }));
afterEach(() => {
vi.unstubAllEnvs();
msw.restoreHandlers();
msw.resetHandlers();
});
Expand All @@ -84,6 +85,7 @@ const isNonInteractiveOrCI = () => true;

describe("access", () => {
beforeEach(() => {
vi.unstubAllEnvs();
clearAccessCaches();
silentLogger.warn = vi.fn();
msw.use(...mswAccessHandlers);
Expand Down Expand Up @@ -170,6 +172,77 @@ describe("access", () => {
expect(silentLogger.warn).not.toHaveBeenCalled();
});

it("should not reuse service token headers after the env vars are unset", async ({
expect,
}) => {
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "first-client-id.access");
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "first-client-secret");
await getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI,
});

vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", undefined);
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", undefined);

await expect(
getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI,
})
).rejects.toThrow("no Access Service Token credentials were found");
});

it("should not reuse service token headers when only CLOUDFLARE_ACCESS_CLIENT_ID remains", async ({
expect,
}) => {
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "first-client-id.access");
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "first-client-secret");
await getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI,
});

vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "second-client-id.access");
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", undefined);

await expect(
getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI,
})
).rejects.toThrow("no Access Service Token credentials were found");
expect(silentLogger.warn).toHaveBeenCalledWith(
expect.stringContaining("Only CLOUDFLARE_ACCESS_CLIENT_ID was found")
);
});

it("should not reuse service token headers when only CLOUDFLARE_ACCESS_CLIENT_SECRET remains", async ({
expect,
}) => {
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "first-client-id.access");
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "first-client-secret");
await getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI,
});

vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", undefined);
vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "second-client-secret");

await expect(
getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI,
})
).rejects.toThrow("no Access Service Token credentials were found");
expect(silentLogger.warn).toHaveBeenCalledWith(
expect.stringContaining(
"Only CLOUDFLARE_ACCESS_CLIENT_SECRET was found"
)
);
});

it("should warn when only CLOUDFLARE_ACCESS_CLIENT_ID is set", async ({
expect,
}) => {
Expand Down Expand Up @@ -297,6 +370,35 @@ See https://developers.cloudflare.com/cloudflare-one/access-controls/service-cre
);
});

it("should reuse the cached CF_Authorization cookie header", async ({
expect,
}) => {
const fake = createFakeProcess();
vi.mocked(spawn).mockReturnValueOnce(fake.child);

const firstHeaders = getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI: () => false,
});
await vi.waitFor(() => {
expect(spawn).toHaveBeenCalledOnce();
});
fake.complete("fetched your token:\n\ntest-access-token\n");

await expect(firstHeaders).resolves.toEqual({
Cookie: "CF_Authorization=test-access-token",
});
await expect(
getAccessHeaders("access-protected.com", {
logger: silentLogger,
isNonInteractiveOrCI: () => false,
})
).resolves.toEqual({
Cookie: "CF_Authorization=test-access-token",
});
expect(spawn).toHaveBeenCalledOnce();
});

it("should kill a still-pending cloudflared when the process exits", async ({
expect,
}) => {
Expand Down
Loading