Skip to content
TypeScript

fix(server): bound Bitbucket API requests with 30s timeout (#14140)

Kevin Rajandf8df0d53b39
diff --git a/apps/server/src/sourceControl/BitbucketApi.test.ts b/apps/server/src/sourceControl/BitbucketApi.test.ts
index 22abb952990..cc23ae1c793 100644
--- a/apps/server/src/sourceControl/BitbucketApi.test.ts
+++ b/apps/server/src/sourceControl/BitbucketApi.test.ts
@@ -4,6 +4,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import * as ConfigProvider from "effect/ConfigProvider";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
+import * as Fiber from "effect/Fiber";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
@@ -65,13 +66,16 @@ function makeLayer(input: {
readonly requestFailure?: (
request: HttpClientRequest.HttpClientRequest,
) => HttpClientError.HttpClientError;
+ readonly stall?: boolean;
readonly git?: Partial<GitVcsDriver.GitVcsDriver["Service"]>;
readonly env?: Record<string, string>;
}) {
const execute = vi.fn((request: HttpClientRequest.HttpClientRequest) =>
- input.requestFailure
- ? Effect.fail(input.requestFailure(request))
- : Effect.succeed(HttpClientResponse.fromWeb(request, input.response(request))),
+ input.stall
+ ? Effect.never
+ : input.requestFailure
+ ? Effect.fail(input.requestFailure(request))
+ : Effect.succeed(HttpClientResponse.fromWeb(request, input.response(request))),
);
const gitMock = {
readConfigValue: vi.fn<GitVcsDriver.GitVcsDriver["Service"]["readConfigValue"]>(() =>
@@ -1099,3 +1103,80 @@ it.effect("cuts a response short rather than reading an unbounded diff into memo
),
),
);
+
+const stalledResponseBody = (status: number): Response =>
+ new Response(
+ new ReadableStream<Uint8Array>({
+ start(controller) {
+ controller.enqueue(new TextEncoder().encode("{"));
+ // Deliberately never close: headers arrive before the body stalls.
+ },
+ }),
+ { status, headers: { "content-type": "application/json" } },
+ );
+
+it.effect.each([200, 503])(
+ "times out a stalled Bitbucket JSON body after HTTP %s headers",
+ (status) => {
+ const { execute, layer } = makeLayer({ response: () => stalledResponseBody(status) });
+ return Effect.gen(function* () {
+ const api = yield* BitbucketApi.BitbucketApi;
+ const fiber = yield* Effect.forkChild(
+ Effect.flip(api.getPullRequest({ cwd: "/repo", reference: "42" })),
+ );
+ while (execute.mock.calls.length === 0) yield* Effect.yieldNow;
+ yield* TestClock.adjust("31 seconds");
+ const error = yield* Fiber.join(fiber);
+ assert.instanceOf(error, BitbucketApi.BitbucketRequestError);
+ assert.strictEqual(error.operation, "getPullRequest");
+ }).pipe(Effect.provide(layer));
+ },
+);
+
+it.effect.each([200, 503])(
+ "times out a stalled Bitbucket raw body after HTTP %s headers",
+ (status) => {
+ const { execute, layer } = makeLayer({ response: () => stalledResponseBody(status) });
+ return Effect.gen(function* () {
+ const api = yield* BitbucketApi.BitbucketApi;
+ const fiber = yield* Effect.forkChild(
+ Effect.flip(api.request({ method: "GET", url: "/user" })),
+ );
+ while (execute.mock.calls.length === 0) yield* Effect.yieldNow;
+ yield* TestClock.adjust("31 seconds");
+ const error = yield* Fiber.join(fiber);
+ assert.instanceOf(error, BitbucketApi.BitbucketRequestError);
+ assert.strictEqual(error.operation, "request");
+ }).pipe(Effect.provide(layer));
+ },
+);
+
+it.effect("times out before JSON headers arrive", () => {
+ const { execute, layer } = makeLayer({ response: () => Response.json({}), stall: true });
+ return Effect.gen(function* () {
+ const api = yield* BitbucketApi.BitbucketApi;
+ const fiber = yield* Effect.forkChild(
+ Effect.flip(api.getPullRequest({ cwd: "/repo", reference: "42" })),
+ );
+ while (execute.mock.calls.length === 0) yield* Effect.yieldNow;
+ yield* TestClock.adjust("31 seconds");
+ const error = yield* Fiber.join(fiber);
+ assert.instanceOf(error, BitbucketApi.BitbucketRequestError);
+ assert.strictEqual(error.operation, "getPullRequest");
+ }).pipe(Effect.provide(layer));
+});
+
+it.effect("times out before raw response headers arrive", () => {
+ const { execute, layer } = makeLayer({ response: () => Response.json({}), stall: true });
+ return Effect.gen(function* () {
+ const api = yield* BitbucketApi.BitbucketApi;
+ const fiber = yield* Effect.forkChild(
+ Effect.flip(api.request({ method: "GET", url: "/user" })),
+ );
+ while (execute.mock.calls.length === 0) yield* Effect.yieldNow;
+ yield* TestClock.adjust("31 seconds");
+ const error = yield* Fiber.join(fiber);
+ assert.instanceOf(error, BitbucketApi.BitbucketRequestError);
+ assert.strictEqual(error.operation, "request");
+ }).pipe(Effect.provide(layer));
+});
diff --git a/packages/source-control-bitbucket/src/server/BitbucketApi.ts b/packages/source-control-bitbucket/src/server/BitbucketApi.ts
index d9d68144f1f..741dcbeb08a 100644
--- a/packages/source-control-bitbucket/src/server/BitbucketApi.ts
+++ b/packages/source-control-bitbucket/src/server/BitbucketApi.ts
@@ -39,6 +39,8 @@ const DEFAULT_API_BASE_URL = "https://api.bitbucket.org/2.0";
const DEFAULT_MAX_RESPONSE_BYTES = 8 * 1024 * 1024;
/** Bitbucket redirects a diff once; this leaves room without following a chain forever. */
const MAX_REDIRECTS = 3;
+/** A single API operation must bound both header delivery and body consumption. */
+const BITBUCKET_API_REQUEST_TIMEOUT = "30 seconds";
const BitbucketApiEnvConfig = Config.all({
baseUrl: Config.String("T3CODE_BITBUCKET_API_BASE_URL").pipe(
@@ -732,6 +734,10 @@ export const make = Effect.gen(function* () {
}),
),
Effect.flatMap((response) => decodeResponse(operation, schema, response)),
+ Effect.timeout(BITBUCKET_API_REQUEST_TIMEOUT),
+ Effect.catchTags({
+ TimeoutError: (cause) => Effect.fail(new BitbucketRequestError({ operation, cause })),
+ }),
);
const resolveRepository = Effect.fn("BitbucketApi.resolveRepository")(function* (input: {
@@ -968,6 +974,12 @@ export const make = Effect.gen(function* () {
orElse: (failed) => responseError("request", failed),
})(response),
),
+ // This also caps redirect hops: no request can finish headers then stall its body forever.
+ Effect.timeout(BITBUCKET_API_REQUEST_TIMEOUT),
+ Effect.catchTags({
+ TimeoutError: (cause) =>
+ Effect.fail(new BitbucketRequestError({ operation: "request", cause })),
+ }),
);
return BitbucketApi.of({