Skip to content

Commit 2f92b67

Browse files
committed
fix(gitlab): fetch uploads only from hosts glab is signed in to, or gitlab.com
The upload URL comes from text anyone can write into a merge request, so the server must not request an arbitrary host with it.
1 parent 37aa955 commit 2f92b67

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

‎packages/source-control-gitlab/src/server/gitLabMediaFetch.test.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const API = `https://gitlab.example.com/api/v4/projects/group%2Fapp/uploads/${SE
1616
function fetchWith(
1717
token: string | null,
1818
respond: (url: string, authorization: string | undefined) => Response,
19+
url = UPLOAD,
1920
) {
2021
const requests: Array<[string, string | undefined]> = [];
2122
const invalidated: Array<string> = [];
@@ -36,7 +37,7 @@ function fetchWith(
3637
);
3738
const status = Effect.gen(function* () {
3839
const fetchMedia = yield* GitLabMediaFetch.make;
39-
return (yield* fetchMedia({ url: UPLOAD, headers: {} }))?.status;
40+
return (yield* fetchMedia({ url, headers: {} }))?.status;
4041
}).pipe(Effect.provide(layer), Effect.scoped);
4142
return { status, requests, invalidated };
4243
}
@@ -65,11 +66,20 @@ describe("GitLabMediaFetch", () => {
6566
});
6667
});
6768

68-
it.effect("loads the upload itself without a login, as a public project serves it", () => {
69-
const { status, requests } = fetchWith(null, () => new Response(null, { status: 200 }));
69+
it.effect("loads a gitlab.com upload without a login, as a public project serves it", () => {
70+
const upload = `https://gitlab.com/group/app/uploads/${SECRET}/shot.png`;
71+
const { status, requests } = fetchWith(null, () => new Response(null, { status: 200 }), upload);
7072
return Effect.gen(function* () {
7173
expect(yield* status).toBe(200);
72-
expect(requests).toEqual([[UPLOAD, undefined]]);
74+
expect(requests).toEqual([[upload, undefined]]);
75+
});
76+
});
77+
78+
it.effect("asks no host that glab is not signed in to", () => {
79+
const { status, requests } = fetchWith(null, () => new Response(null, { status: 200 }));
80+
return Effect.gen(function* () {
81+
expect(yield* status).toBeUndefined();
82+
expect(requests).toEqual([]);
7383
});
7484
});
7585

‎packages/source-control-gitlab/src/server/gitLabMediaFetch.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { gitLabUploadApiUrl, parseGitLabUpload } from "../shared/gitLabMedia.ts"
1313

1414
import * as GitLabCredentials from "./GitLabCredentials.ts";
1515

16+
const PUBLIC_HOST = "gitlab.com";
1617
const MAX_REDIRECTS = 3;
1718
const MANUAL_REDIRECT: RequestInit = { redirect: "manual" };
1819

@@ -54,14 +55,17 @@ export const make = Effect.gen(function* () {
5455
});
5556

5657
/**
57-
* Reads the upload through the API with the `glab` login for its host. Without a login, or
58-
* where the API refuses it (GitLab before 17.4), the upload itself still serves a public project.
58+
* Reads the upload through the API with the `glab` login for its host. Where the API refuses it
59+
* (GitLab before 17.4), or gitlab.com without a login, the upload itself serves a public project.
5960
*/
6061
return Effect.fn("GitLabMediaFetch.fetchMedia")(function* (request: SourceControlMediaRequest) {
6162
const url = new URL(request.url);
6263
const upload = parseGitLabUpload(url);
6364
if (upload === null) return null;
6465
const token = yield* credentials.get(url.host);
66+
// Anyone can write the URL into a body, so only a host `glab` is signed in to, or the public
67+
// GitLab, is ever asked.
68+
if (Option.isNone(token) && url.host !== PUBLIC_HOST) return null;
6569
if (Option.isSome(token)) {
6670
const response = yield* fetchFollowingRedirects(gitLabUploadApiUrl(upload), request.headers, {
6771
origin: upload.origin,

0 commit comments

Comments
 (0)