Skip to content

Commit d292bcd

Browse files
committed
fix(review): require read access before publishing attachments
1 parent c96b6da commit d292bcd

2 files changed

Lines changed: 58 additions & 14 deletions

File tree

‎apps/server/src/auth/RpcAuthorization.test.ts‎

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
AuthSettingsWriteScope,
77
DEFAULT_SERVER_SETTINGS,
88
ProviderDriverKind,
9+
ProjectId,
910
ProviderInstanceId,
1011
SecretProviderId,
1112
ThreadId,
@@ -318,30 +319,37 @@ describe("thread context authorization", () => {
318319
);
319320
});
320321

321-
it.effect("separates host file URLs from readable attachment URLs", () =>
322+
it.effect("separates filesystem reads, attachment reads, and hosted publication", () =>
322323
Effect.gen(function* () {
323324
const group = WsRpcGroup.omit(
324325
...[...WsRpcGroup.requests.keys()].filter(
325326
(
326327
tag,
327-
): tag is Exclude<keyof typeof RPC_REQUIRED_SCOPES, typeof WS_METHODS.assetsCreateUrl> =>
328-
tag !== WS_METHODS.assetsCreateUrl,
328+
): tag is Exclude<
329+
keyof typeof RPC_REQUIRED_SCOPES,
330+
typeof WS_METHODS.assetsCreateUrl | typeof WS_METHODS.pullRequestsUploadAttachment
331+
> => tag !== WS_METHODS.assetsCreateUrl && tag !== WS_METHODS.pullRequestsUploadAttachment,
329332
),
330333
);
331334
let handled = 0;
332-
const client = yield* RpcTest.makeClient(group).pipe(
333-
Effect.provide(
334-
Layer.mergeAll(
335-
group.toLayerHandler(WS_METHODS.assetsCreateUrl, () =>
336-
Effect.sync(() => {
337-
handled++;
338-
return { relativeUrl: "/api/assets/file", expiresAt: 1 };
339-
}),
340-
),
341-
RpcAuthorization.layer([AuthOrchestrationReadScope]),
342-
),
335+
let published = 0;
336+
const handlers = Layer.merge(
337+
group.toLayerHandler(WS_METHODS.assetsCreateUrl, () =>
338+
Effect.sync(() => {
339+
handled++;
340+
return { relativeUrl: "/api/assets/file", expiresAt: 1 };
341+
}),
342+
),
343+
group.toLayerHandler(WS_METHODS.pullRequestsUploadAttachment, () =>
344+
Effect.sync(() => {
345+
published++;
346+
return { url: "https://github.com/user-attachments/assets/image" };
347+
}),
343348
),
344349
);
350+
const client = yield* RpcTest.makeClient(group).pipe(
351+
Effect.provide(Layer.merge(handlers, RpcAuthorization.layer([AuthOrchestrationReadScope]))),
352+
);
345353
yield* client[WS_METHODS.assetsCreateUrl]({
346354
resource: { _tag: "attachment", attachmentId: "image" },
347355
});
@@ -358,6 +366,39 @@ it.effect("separates host file URLs from readable attachment URLs", () =>
358366
});
359367
}
360368
expect(handled).toBe(1);
369+
const attachment = {
370+
projectId: ProjectId.make("project"),
371+
repository: "org/repo",
372+
number: 1,
373+
attachmentId: "pending-00000000-0000-4000-8000-000000000001-png",
374+
name: "image.png",
375+
mimeType: "image/png" as const,
376+
sizeBytes: 3,
377+
};
378+
expect(
379+
yield* client[WS_METHODS.pullRequestsUploadAttachment](attachment).pipe(Effect.flip),
380+
).toMatchObject({ requiredPermission: AuthSourceControlWriteScope });
381+
expect(published).toBe(0);
382+
for (const scopes of [
383+
[AuthSourceControlWriteScope],
384+
[AuthSourceControlWriteScope, AuthOrchestrationReadScope],
385+
]) {
386+
const publisher = yield* RpcTest.makeClient(group).pipe(
387+
Effect.provide(Layer.merge(handlers, RpcAuthorization.layer(scopes))),
388+
);
389+
const operation = publisher[WS_METHODS.pullRequestsUploadAttachment](attachment);
390+
if (!scopes.includes(AuthOrchestrationReadScope)) {
391+
expect(yield* operation.pipe(Effect.flip)).toMatchObject({
392+
requiredPermission: AuthOrchestrationReadScope,
393+
});
394+
expect(published).toBe(0);
395+
} else {
396+
expect(yield* operation).toEqual({
397+
url: "https://github.com/user-attachments/assets/image",
398+
});
399+
expect(published).toBe(1);
400+
}
401+
}
361402
}).pipe(Effect.scoped),
362403
);
363404

‎packages/contracts/src/clientRpcPermissions.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ export function clientRpcRequiredScopes(
8787
method: string,
8888
input: unknown,
8989
): readonly AuthEnvironmentScope[] {
90+
// Publishing staged bytes needs the same read grant as an attachment URL, plus PR write.
91+
if (method === WS_METHODS.pullRequestsUploadAttachment)
92+
return [AuthSourceControlWriteScope, AuthOrchestrationReadScope];
9093
if (method === WS_METHODS.gitPreparePullRequestThread && input !== undefined) {
9194
const payload = decodePrepareThread(input);
9295
if (payload.mode === "worktree" && payload.threadId !== undefined)

0 commit comments

Comments
 (0)