Skip to content

Commit 5be9e62

Browse files
fix(server): constrain automatic title link lookups
1 parent 38af2a8 commit 5be9e62

7 files changed

Lines changed: 69 additions & 60 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ for (const kind of ["pull", "issues"]) {
409409
assert.deepStrictEqual(input.args, [
410410
"api",
411411
"--hostname",
412-
"github.example.test",
412+
"github.com",
413413
"repos/owner/repo/issues/42",
414414
"--jq",
415415
"{title, body}",
@@ -427,7 +427,7 @@ for (const kind of ["pull", "issues"]) {
427427
});
428428
const lookup = provider.resolveLink?.({
429429
cwd: "/unrelated",
430-
url: new URL(`https://github.example.test/owner/repo/${kind}/42`),
430+
url: new URL(`https://github.com/owner/repo/${kind}/42`),
431431
});
432432
assert.ok(lookup);
433433
assert.deepStrictEqual(yield* lookup, {
@@ -437,7 +437,7 @@ for (const kind of ["pull", "issues"]) {
437437
assert.strictEqual(
438438
provider.resolveLink?.({
439439
cwd: "/unrelated",
440-
url: new URL("https://github.example.test/owner/repo"),
440+
url: new URL("https://github.com/owner/repo"),
441441
}),
442442
undefined,
443443
);

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -215,32 +215,21 @@ export const make = Effect.gen(function* () {
215215
);
216216
};
217217

218-
return SourceControlProvider.SourceControlProvider.of({
219-
kind: "github",
220-
resolveLink: (input) => {
221-
const match = /^\/([\w.-]+)\/([\w.-]+)\/(?:pull|issues)\/([1-9]\d*)(?:\/.*)?$/.exec(
222-
input.url.pathname,
223-
);
224-
if (!match) return undefined;
225-
return Effect.fn("GitHubSourceControlProvider.resolveLink")(
226-
function* () {
227-
const result = yield* github.execute({
228-
cwd: input.cwd,
229-
args: [
230-
"api",
231-
"--hostname",
232-
input.url.host,
233-
`repos/${match[1]}/${match[2]}/issues/${match[3]}`,
234-
"--jq",
235-
"{title, body}",
236-
],
237-
env: { GH_PROMPT_DISABLED: "1" },
238-
timeoutMs: 3_000,
239-
maxOutputBytes: 32_000,
240-
});
241-
const subject = yield* decodeLinkSubject(result.stdout);
242-
return { title: subject.title, body: subject.body };
243-
},
218+
const readLinkSubject = Effect.fn("GitHubSourceControlProvider.readLinkSubject")(function* (
219+
input: { readonly cwd: string; readonly url: URL },
220+
endpoint: string,
221+
) {
222+
return yield* github
223+
.execute({
224+
cwd: input.cwd,
225+
args: ["api", "--hostname", input.url.host, endpoint, "--jq", "{title, body}"],
226+
env: { GH_PROMPT_DISABLED: "1" },
227+
timeoutMs: 3_000,
228+
maxOutputBytes: 32_000,
229+
})
230+
.pipe(
231+
Effect.flatMap((result) => decodeLinkSubject(result.stdout)),
232+
Effect.map((subject) => ({ title: subject.title, body: subject.body })),
244233
Effect.mapError(
245234
(cause) =>
246235
new SourceControlProviderError({
@@ -251,7 +240,19 @@ export const make = Effect.gen(function* () {
251240
cause,
252241
}),
253242
),
254-
)();
243+
);
244+
});
245+
246+
return SourceControlProvider.SourceControlProvider.of({
247+
kind: "github",
248+
resolveLink: (input) => {
249+
// Automatic enrichment must not send ambient CLI credentials to a host from message text.
250+
if (input.url.host !== "github.com") return undefined;
251+
const match = /^\/([\w.-]+)\/([\w.-]+)\/(?:pull|issues)\/([1-9]\d*)(?:\/.*)?$/.exec(
252+
input.url.pathname,
253+
);
254+
if (!match) return undefined;
255+
return readLinkSubject(input, `repos/${match[1]}/${match[2]}/issues/${match[3]}`);
255256
},
256257
listChangeRequests,
257258
getChangeRequest: (input) =>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ for (const kind of ["merge_requests", "issues"]) {
235235
assert.deepStrictEqual(input.args, [
236236
"api",
237237
"--hostname",
238-
"gitlab.example.test",
238+
"gitlab.com",
239239
`projects/group%2Fsubgroup%2Fproject/${kind}/42`,
240240
]);
241241
assert.strictEqual(input.maxOutputBytes, 32_000);
@@ -254,7 +254,7 @@ for (const kind of ["merge_requests", "issues"]) {
254254
});
255255
const lookup = provider.resolveLink?.({
256256
cwd: "/unrelated",
257-
url: new URL(`https://gitlab.example.test/group/subgroup/project/-/${kind}/42`),
257+
url: new URL(`https://gitlab.com/group/subgroup/project/-/${kind}/42`),
258258
});
259259
assert.ok(lookup);
260260
assert.deepStrictEqual(yield* lookup, {
@@ -264,7 +264,7 @@ for (const kind of ["merge_requests", "issues"]) {
264264
assert.strictEqual(
265265
provider.resolveLink?.({
266266
cwd: "/unrelated",
267-
url: new URL("https://gitlab.example.test/owner/repo"),
267+
url: new URL("https://gitlab.com/owner/repo"),
268268
}),
269269
undefined,
270270
);

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,20 @@ export const discovery = {
112112
export const make = Effect.gen(function* () {
113113
const gitlab = yield* GitLabCli.GitLabCli;
114114

115-
return SourceControlProvider.SourceControlProvider.of({
116-
kind: "gitlab",
117-
resolveLink: (input) => {
118-
const match = /^\/(.+)\/-\/(merge_requests|issues)\/([1-9]\d*)(?:\/.*)?$/.exec(
119-
input.url.pathname,
120-
);
121-
if (!match) return undefined;
122-
return Effect.fn("GitLabSourceControlProvider.resolveLink")(
123-
function* () {
124-
const result = yield* gitlab.execute({
125-
cwd: input.cwd,
126-
args: [
127-
"api",
128-
"--hostname",
129-
input.url.host,
130-
`projects/${encodeURIComponent(match[1]!)}/${match[2]}/${match[3]}`,
131-
],
132-
timeoutMs: 3_000,
133-
maxOutputBytes: 32_000,
134-
});
135-
const subject = yield* decodeLinkSubject(result.stdout);
136-
return { title: subject.title, body: subject.description };
137-
},
115+
const readLinkSubject = Effect.fn("GitLabSourceControlProvider.readLinkSubject")(function* (
116+
input: { readonly cwd: string; readonly url: URL },
117+
endpoint: string,
118+
) {
119+
return yield* gitlab
120+
.execute({
121+
cwd: input.cwd,
122+
args: ["api", "--hostname", input.url.host, endpoint],
123+
timeoutMs: 3_000,
124+
maxOutputBytes: 32_000,
125+
})
126+
.pipe(
127+
Effect.flatMap((result) => decodeLinkSubject(result.stdout)),
128+
Effect.map((subject) => ({ title: subject.title, body: subject.description })),
138129
Effect.mapError(
139130
(cause) =>
140131
new SourceControlProviderError({
@@ -145,7 +136,22 @@ export const make = Effect.gen(function* () {
145136
cause,
146137
}),
147138
),
148-
)();
139+
);
140+
});
141+
142+
return SourceControlProvider.SourceControlProvider.of({
143+
kind: "gitlab",
144+
resolveLink: (input) => {
145+
// Automatic enrichment must not send ambient CLI credentials to a host from message text.
146+
if (input.url.host !== "gitlab.com") return undefined;
147+
const match = /^\/(.+)\/-\/(merge_requests|issues)\/([1-9]\d*)(?:\/.*)?$/.exec(
148+
input.url.pathname,
149+
);
150+
if (!match) return undefined;
151+
return readLinkSubject(
152+
input,
153+
`projects/${encodeURIComponent(match[1]!)}/${match[2]}/${match[3]}`,
154+
);
149155
},
150156
listChangeRequests: (input) => {
151157
const source = SourceControlProvider.sourceControlRefFromInput(input);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ it.effect(
319319
for (const [url, expected] of [
320320
["https://github.com/team/project/issues/1", { title: "GitHub issue", body: null }],
321321
[
322-
"https://gitlab.example.test/team/sub/project/-/merge_requests/2",
322+
"https://gitlab.com/team/sub/project/-/merge_requests/2",
323323
{ title: "GitLab MR", body: "Nested project" },
324324
],
325325
] as const) {
@@ -329,6 +329,8 @@ it.effect(
329329
}
330330
for (const url of [
331331
"https://example.test/team/project/issues/1",
332+
"https://github.attacker.test/team/project/issues/1",
333+
"https://gitlab.attacker.test/team/project/-/issues/1",
332334
"https://github.com/team/project",
333335
"https://codeberg.org/team/project/issues/1",
334336
"https://bitbucket.org/team/project/pull-requests/1",

‎apps/server/src/textGeneration/ThreadTitleLinks.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ it.effect(
2323
const result = yield* resolveThreadTitleLinks({
2424
cwd: "/tmp/project",
2525
message:
26-
"https://docs.test/guide https://forge.test/change/1#discussion https://forge.test/change/1?view=full https://forge.test/change/2. https://forge.test/change/3",
26+
"https://docs.test/guide [https://forge.test/change/1] https://forge.test/change/1#discussion https://forge.test/change/1?view=full `https://forge.test/change/2` https://forge.test/change/2. https://forge.test/change/3",
2727
}).pipe(
2828
Effect.provide(
2929
registry({

‎apps/server/src/textGeneration/ThreadTitleLinks.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const resolveThreadTitleLinks = Effect.fn("resolveThreadTitleLinks")(func
1313
}) {
1414
const providers = yield* SourceControlProviderRegistry.SourceControlProviderRegistry;
1515
const links = new Map<string, NonNullable<ReturnType<typeof providers.resolveLink>>>();
16-
for (const match of input.message.matchAll(/https:\/\/[^\s<>"')]+/g)) {
16+
for (const match of input.message.matchAll(/https:\/\/[^\s<>"')\]`]+/g)) {
1717
let url: URL;
1818
try {
1919
url = new URL(match[0].replace(/[.,;!?]+$/, ""));

0 commit comments

Comments
 (0)