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
14 changes: 12 additions & 2 deletions workers/events/src/readiness.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import worker, {
pruneExpiredAnalyticsEvents,
} from "./index";
import { testDatabase } from "./sqlite.testSupport";
import { posthogEventUuid, readBoundedJson } from "./transport";
import { posthogEventUuid, posthogRequest, readBoundedJson } from "./transport";
import { withExportLease } from "./exportLease";

const installation = "installation:10000000-0000-4000-8000-000000000001";
Expand Down Expand Up @@ -73,6 +73,16 @@ afterEach(() => {
for (const db of databases.splice(0)) db.close();
});

it("does not follow PostHog redirects or turn them into network failures", async () => {
const fetcher = vi.fn().mockResolvedValue(new Response(null, { status: 302 }));
vi.stubGlobal("fetch", fetcher);

await expect(
posthogRequest("https://eu.i.posthog.com/batch", { method: "POST" }),
).rejects.toThrow("http");
expect(fetcher.mock.calls[0]![1].redirect).toBe("manual");
});

describe("inactive gateway readiness with real SQL", () => {
it("keeps diagnostic events queryable in D1 without exporting them to PostHog", async () => {
const f = fixture();
Expand Down Expand Up @@ -423,7 +433,7 @@ describe("inactive gateway readiness with real SQL", () => {
expect(first.uuid).toMatch(
/^[a-f0-9]{8}-[a-f0-9]{4}-8[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/,
);
expect(fetcher.mock.calls[0]![1].redirect).toBe("error");
expect(fetcher.mock.calls[0]![1].redirect).toBe("manual");
expect(fetcher.mock.calls[0]![1].signal).toBeInstanceOf(AbortSignal);
});

Expand Down
6 changes: 5 additions & 1 deletion workers/events/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export async function posthogRequest(url: string, init: RequestInit): Promise<Re
try {
const response = await fetch(url, {
...init,
redirect: "error",
// A manual redirect response is never followed, so credentials remain
// scoped to the configured PostHog origin. Cloudflare's runtime rejects
// `redirect: "error"` as a failed subrequest before a response is
// available, which prevents otherwise valid exports.
redirect: "manual",
signal: AbortSignal.timeout(5_000),
});
if (!response.ok) {
Expand Down