Skip to content

Commit 1156d43

Browse files
committed
fix(server): reject invalid explicit Bitbucket repositories
1 parent a1d9d72 commit 1156d43

4 files changed

Lines changed: 248 additions & 23 deletions

File tree

‎apps/server/src/sourceControl/BitbucketApi.test.ts‎

Lines changed: 120 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import * as Effect from "effect/Effect";
66
import * as FileSystem from "effect/FileSystem";
77
import * as Layer from "effect/Layer";
88
import * as Option from "effect/Option";
9+
import * as Schema from "effect/Schema";
910
import * as TestClock from "effect/testing/TestClock";
1011
import {
1112
HttpClient,
@@ -21,6 +22,8 @@ import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
2122
import * as VcsDriverRegistry from "../vcs/VcsDriverRegistry.ts";
2223
import type * as VcsDriver from "../vcs/VcsDriver.ts";
2324

25+
const isBitbucketRepositoryLocatorError = Schema.is(BitbucketApi.BitbucketRepositoryLocatorError);
26+
2427
const bitbucketPullRequest = {
2528
id: 42,
2629
title: "Add Bitbucket provider",
@@ -104,7 +107,7 @@ function makeLayer(input: {
104107
} satisfies Partial<GitVcsDriver.GitVcsDriver["Service"]>;
105108

106109
const driver = {
107-
listRemotes: () =>
110+
listRemotes: vi.fn(() =>
108111
Effect.succeed({
109112
remotes: [
110113
{
@@ -120,8 +123,26 @@ function makeLayer(input: {
120123
expiresAt: Option.none(),
121124
},
122125
}),
126+
),
123127
} satisfies Partial<VcsDriver.VcsDriver["Service"]>;
124128

129+
const resolve = vi.fn(() =>
130+
Effect.succeed({
131+
kind: "git" as const,
132+
repository: {
133+
kind: "git" as const,
134+
rootPath: "/repo",
135+
metadataPath: null,
136+
freshness: {
137+
source: "live-local" as const,
138+
observedAt: DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"),
139+
expiresAt: Option.none(),
140+
},
141+
},
142+
driver: driver as unknown as VcsDriver.VcsDriver["Service"],
143+
}),
144+
);
145+
125146
const layer = BitbucketApi.layer.pipe(
126147
Layer.provide(
127148
Layer.succeed(
@@ -131,21 +152,7 @@ function makeLayer(input: {
131152
),
132153
Layer.provide(
133154
Layer.mock(VcsDriverRegistry.VcsDriverRegistry)({
134-
resolve: () =>
135-
Effect.succeed({
136-
kind: "git",
137-
repository: {
138-
kind: "git",
139-
rootPath: "/repo",
140-
metadataPath: null,
141-
freshness: {
142-
source: "live-local" as const,
143-
observedAt: DateTime.makeUnsafe("1970-01-01T00:00:00.000Z"),
144-
expiresAt: Option.none(),
145-
},
146-
},
147-
driver: driver as unknown as VcsDriver.VcsDriver["Service"],
148-
}),
155+
resolve,
149156
}),
150157
),
151158
Layer.provide(Layer.mock(GitVcsDriver.GitVcsDriver)(git)),
@@ -164,7 +171,7 @@ function makeLayer(input: {
164171
Layer.provideMerge(NodeServices.layer),
165172
);
166173

167-
return { execute, git: gitMock, layer };
174+
return { execute, git: gitMock, resolve, listRemotes: driver.listRemotes, layer };
168175
}
169176

170177
it.effect("parses pull request responses from the Bitbucket REST API", () => {
@@ -338,6 +345,102 @@ it.effect("reads repository clone URLs and default branch", () => {
338345
}).pipe(Effect.provide(layer));
339346
});
340347

348+
it.effect.each([false, true])(
349+
"rejects invalid explicit repositories before fallback, context=%s",
350+
(withContext) => {
351+
const { execute, resolve, listRemotes, layer } = makeLayer({
352+
response: () => Response.json(repositoryJson),
353+
});
354+
355+
return Effect.gen(function* () {
356+
const bitbucket = yield* BitbucketApi.BitbucketApi;
357+
for (const repository of ["t3code", "", " \t "]) {
358+
const error = yield* bitbucket
359+
.getRepositoryCloneUrls({
360+
cwd: "/repo",
361+
repository,
362+
...(withContext
363+
? {
364+
context: {
365+
provider: {
366+
kind: "bitbucket" as const,
367+
name: "Bitbucket",
368+
baseUrl: "https://bitbucket.org",
369+
},
370+
remoteName: "origin",
371+
remoteUrl: "[email protected]:pingdotgg/t3code.git",
372+
},
373+
}
374+
: {}),
375+
})
376+
.pipe(Effect.match({ onFailure: (error) => error, onSuccess: () => null }));
377+
378+
assert.instanceOf(error, BitbucketApi.BitbucketRepositoryLocatorError);
379+
assert.strictEqual(
380+
isBitbucketRepositoryLocatorError(error) ? error.repository : null,
381+
repository,
382+
);
383+
assert.strictEqual(resolve.mock.calls.length, 0);
384+
assert.strictEqual(listRemotes.mock.calls.length, 0);
385+
assert.strictEqual(execute.mock.calls.length, 0);
386+
}
387+
}).pipe(Effect.provide(layer));
388+
},
389+
);
390+
391+
it.effect("prefers an explicit repository and uses context when the repository is omitted", () => {
392+
const { execute, resolve, listRemotes, layer } = makeLayer({
393+
response: (request) =>
394+
Response.json(request.url.endsWith("/branching-model") ? {} : repositoryJson),
395+
});
396+
397+
return Effect.gen(function* () {
398+
const bitbucket = yield* BitbucketApi.BitbucketApi;
399+
const context = {
400+
provider: { kind: "bitbucket" as const, name: "Bitbucket", baseUrl: "https://bitbucket.org" },
401+
remoteName: "origin",
402+
remoteUrl: "[email protected]:another/context.git",
403+
};
404+
for (const repository of ["pingdotgg/t3code", "https://bitbucket.org/pingdotgg/t3code.git"]) {
405+
yield* bitbucket.getRepositoryCloneUrls({ cwd: "/repo", context, repository });
406+
}
407+
yield* bitbucket.getDefaultBranch({ cwd: "/repo", context });
408+
409+
assert.deepStrictEqual(
410+
execute.mock.calls.map(([request]) => request.url).toSorted(),
411+
[
412+
"https://api.test.local/2.0/repositories/pingdotgg/t3code",
413+
"https://api.test.local/2.0/repositories/pingdotgg/t3code",
414+
"https://api.test.local/2.0/repositories/another/context",
415+
"https://api.test.local/2.0/repositories/another/context/branching-model",
416+
].toSorted(),
417+
);
418+
assert.strictEqual(resolve.mock.calls.length, 0);
419+
assert.strictEqual(listRemotes.mock.calls.length, 0);
420+
}).pipe(Effect.provide(layer));
421+
});
422+
423+
it.effect("discovers the repository from cwd remotes when the repository is omitted", () => {
424+
const { execute, resolve, listRemotes, layer } = makeLayer({
425+
response: (request) =>
426+
Response.json(request.url.endsWith("/branching-model") ? {} : repositoryJson),
427+
});
428+
429+
return Effect.gen(function* () {
430+
const bitbucket = yield* BitbucketApi.BitbucketApi;
431+
const branch = yield* bitbucket.getDefaultBranch({ cwd: "/repo" });
432+
433+
assert.strictEqual(branch, "main");
434+
assert.deepStrictEqual(resolve.mock.calls, [[{ cwd: "/repo" }]]);
435+
assert.deepStrictEqual(listRemotes.mock.calls, [["/repo"]]);
436+
assert.isTrue(
437+
execute.mock.calls.every(([request]) =>
438+
request.url.startsWith("https://api.test.local/2.0/repositories/pingdotgg/t3code"),
439+
),
440+
);
441+
}).pipe(Effect.provide(layer));
442+
});
443+
341444
it.effect(
342445
"prefers the Bitbucket branching model development branch as the default PR target",
343446
() => {

‎apps/server/src/sourceControl/BitbucketApi.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,14 @@ export class BitbucketRepositoryLocatorError extends Schema.TaggedError<Bitbucke
7272
repository: Schema.String,
7373
},
7474
) {
75+
static readonly detail = "Bitbucket repositories must be specified as workspace/repository.";
76+
7577
get detail(): string {
76-
return "Bitbucket repositories must be specified as workspace/repository.";
78+
return BitbucketRepositoryLocatorError.detail;
7779
}
7880

7981
override get message(): string {
80-
return `Bitbucket API failed in createRepository: ${this.detail}`;
82+
return `Bitbucket API failed: ${this.detail}`;
8183
}
8284
}
8385

@@ -727,9 +729,9 @@ export const make = Effect.gen(function* () {
727729
readonly context?: SourceControlProvider.SourceControlProviderContext;
728730
readonly repository?: string;
729731
}) {
730-
const fromRepository =
731-
input.repository !== undefined ? parseBitbucketRepositorySlug(input.repository) : null;
732-
if (fromRepository) return fromRepository;
732+
if (input.repository !== undefined) {
733+
return yield* requireRepositoryLocator(input.repository);
734+
}
733735

734736
const fromContext =
735737
input.context?.provider.kind === "bitbucket"

‎apps/server/src/sourceControl/SourceControlRepositoryService.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ import * as FileSystem from "effect/FileSystem";
66
import * as Path from "effect/Path";
77
import * as Layer from "effect/Layer";
88
import * as PlatformError from "effect/PlatformError";
9+
import * as Schema from "effect/Schema";
910
import { ChildProcessSpawner } from "effect/unstable/process";
1011

1112
import { GitCommandError, SourceControlProviderError } from "@t3tools/contracts";
1213

1314
import * as ServerConfig from "../config.ts";
1415
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
16+
import * as BitbucketApi from "./BitbucketApi.ts";
17+
import * as BitbucketSourceControlProvider from "./BitbucketSourceControlProvider.ts";
1518
import type * as SourceControlProvider from "./SourceControlProvider.ts";
1619
import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
1720
import * as SourceControlRepositoryService from "./SourceControlRepositoryService.ts";
1821

22+
const isSourceControlProviderError = Schema.is(SourceControlProviderError);
23+
1924
const CLONE_URLS = {
2025
nameWithOwner: "octocat/t3code",
2126
url: "https://github.com/octocat/t3code",
@@ -152,6 +157,112 @@ it.effect("preserves provider failures without deriving the repository message f
152157
}).pipe(Effect.provide(makeLayer({ provider })));
153158
});
154159

160+
it.effect(
161+
"reports the safe Bitbucket locator detail through repository lookup and publishing",
162+
() =>
163+
Effect.gen(function* () {
164+
const locatorCause = new BitbucketApi.BitbucketRepositoryLocatorError({
165+
repository: "credential-token-abc123",
166+
});
167+
const provider = yield* BitbucketSourceControlProvider.make.pipe(
168+
Effect.provide(
169+
Layer.mock(BitbucketApi.BitbucketApi)({
170+
getRepositoryCloneUrls: () => Effect.fail(locatorCause),
171+
createRepository: () => Effect.fail(locatorCause),
172+
}),
173+
),
174+
);
175+
176+
yield* Effect.gen(function* () {
177+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
178+
const input = {
179+
provider: "bitbucket" as const,
180+
repository: locatorCause.repository,
181+
cwd: "/workspace",
182+
};
183+
for (const operation of [
184+
service.lookupRepository(input).pipe(Effect.asVoid),
185+
service.publishRepository({ ...input, visibility: "private" }).pipe(Effect.asVoid),
186+
]) {
187+
const error = yield* Effect.flip(operation);
188+
189+
assert.strictEqual(error.provider, "bitbucket");
190+
assert.strictEqual(
191+
error.detail,
192+
"Bitbucket repositories must be specified as workspace/repository.",
193+
);
194+
assert.notInclude(error.message, locatorCause.repository);
195+
assert.instanceOf(error.cause, SourceControlProviderError);
196+
assert.strictEqual(
197+
isSourceControlProviderError(error.cause) ? error.cause.cause : null,
198+
locatorCause,
199+
);
200+
}
201+
}).pipe(Effect.provide(makeLayer({ provider })));
202+
}),
203+
);
204+
205+
it.effect("keeps a plain Bitbucket locator lookalike out of the repository message", () => {
206+
const provider = makeProvider({
207+
kind: "bitbucket",
208+
getRepositoryCloneUrls: () =>
209+
Effect.fail(
210+
new SourceControlProviderError({
211+
provider: "bitbucket",
212+
operation: "getRepositoryCloneUrls",
213+
cwd: "/workspace",
214+
detail: "credential token abc123 was rejected",
215+
cause: {
216+
_tag: "BitbucketRepositoryLocatorError",
217+
repository: "t3code",
218+
detail: "credential token abc123 was rejected",
219+
},
220+
}),
221+
),
222+
});
223+
224+
return Effect.gen(function* () {
225+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
226+
const error = yield* Effect.flip(
227+
service.lookupRepository({ provider: "bitbucket", repository: "t3code" }),
228+
);
229+
230+
assert.strictEqual(error.detail, "The source control operation could not be completed.");
231+
assert.notInclude(error.message, "abc123");
232+
}).pipe(Effect.provide(makeLayer({ provider })));
233+
});
234+
235+
it.effect("keeps arbitrary Bitbucket request failures out of the repository message", () =>
236+
Effect.gen(function* () {
237+
const cause = new BitbucketApi.BitbucketRequestError({
238+
operation: "getRepository",
239+
cause: new Error("credential token abc123 was rejected"),
240+
});
241+
const provider = yield* BitbucketSourceControlProvider.make.pipe(
242+
Effect.provide(
243+
Layer.mock(BitbucketApi.BitbucketApi)({
244+
getRepositoryCloneUrls: () => Effect.fail(cause),
245+
}),
246+
),
247+
);
248+
249+
yield* Effect.gen(function* () {
250+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
251+
const error = yield* Effect.flip(
252+
service.lookupRepository({
253+
provider: "bitbucket",
254+
repository: "pingdotgg/t3code",
255+
cwd: "/workspace",
256+
}),
257+
);
258+
259+
assert.strictEqual(error.detail, "The source control operation could not be completed.");
260+
assert.notInclude(error.message, "abc123");
261+
assert.notInclude(error.message, cause.detail);
262+
}).pipe(Effect.provide(makeLayer({ provider })));
263+
}),
264+
);
265+
155266
it.effect("clones a looked-up repository into the requested destination", () =>
156267
Effect.gen(function* () {
157268
const fs = yield* FileSystem.FileSystem;

‎apps/server/src/sourceControl/SourceControlRepositoryService.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Schedule from "effect/Schedule";
77
import * as Schema from "effect/Schema";
88

99
import {
10+
SourceControlProviderError,
1011
SourceControlRepositoryError,
1112
type SourceControlCloneRepositoryInput,
1213
type SourceControlCloneRepositoryResult,
@@ -26,8 +27,11 @@ import {
2627
type GitCloneProgressLine,
2728
} from "../project/gitCloneProgress.ts";
2829
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
30+
import * as BitbucketApi from "./BitbucketApi.ts";
2931
import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry.ts";
3032
const isSourceControlRepositoryError = Schema.is(SourceControlRepositoryError);
33+
const isSourceControlProviderError = Schema.is(SourceControlProviderError);
34+
const isBitbucketRepositoryLocatorError = Schema.is(BitbucketApi.BitbucketRepositoryLocatorError);
3135

3236
export class SourceControlRepositoryService extends Context.Service<
3337
SourceControlRepositoryService,
@@ -92,7 +96,12 @@ function mapRepositoryError(operation: string, provider: SourceControlProviderKi
9296
: new SourceControlRepositoryError({
9397
operation,
9498
provider,
95-
detail: "The source control operation could not be completed.",
99+
detail:
100+
isSourceControlProviderError(cause) &&
101+
cause.provider === "bitbucket" &&
102+
isBitbucketRepositoryLocatorError(cause.cause)
103+
? BitbucketApi.BitbucketRepositoryLocatorError.detail
104+
: "The source control operation could not be completed.",
96105
cause,
97106
}),
98107
);

0 commit comments

Comments
 (0)