feat(auth): separate source control write permissions (#9787)
Co-authored-by: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
28ce9d1c17
commit
2eda8ac6a3
59 files changed
+3628
-315
No files matched your search
@@ -1,9 +1,14 @@
|
||||
import { createCommandPermissions } from "./commandPermissions.ts";
|
||||
import { RpcPermissionGuard } from "../rpc/client.ts";
|
||||
import { vi } from "vite-plus/test";
|
||||
import {
|
||||
EnvironmentId,
|
||||
AuthSourceControlWriteScope,
|
||||
ProjectId,
|
||||
PullRequestOperationError,
|
||||
WS_METHODS,
|
||||
type PullRequestStack,
|
||||
type AuthSessionState,
|
||||
} from "@t3tools/contracts";
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import * as Data from "effect/Data";
|
||||
@@ -194,6 +199,7 @@ for (const scenario of [
|
||||
: route(WS_METHODS.pullRequestsRunAction, input)
|
||||
).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.provideService(GitHubRoutingPermissions, {
|
||||
...trustedRouting,
|
||||
get: () => Effect.succeed(trusted ? "read-write" : "off"),
|
||||
@@ -301,6 +307,7 @@ it.effect("shares active read identity probes while keeping later reads and muta
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
);
|
||||
}),
|
||||
),
|
||||
@@ -554,6 +561,7 @@ it.effect.each(["github", "gitlab", "bitbucket", "azure-devops"] as const)(
|
||||
}).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
);
|
||||
const environment = provider === "github" ? "local" : "origin";
|
||||
@@ -720,10 +728,12 @@ it.effect.each(["default", "origin-off", "destination-off", "read-only"] as cons
|
||||
}).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
);
|
||||
yield* permission === "default"
|
||||
? request
|
||||
: request.pipe(
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.provideService(GitHubRoutingPermissions, {
|
||||
...trustedRouting,
|
||||
get: (entry) =>
|
||||
@@ -842,6 +852,7 @@ it.effect.each(
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
// This also represents a user enabling the stale catalog entry after re-resolution.
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
);
|
||||
yield* stored === "unavailable"
|
||||
? route
|
||||
@@ -897,6 +908,7 @@ it.live.each(["origin", "alternate"] as const)(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.flip,
|
||||
);
|
||||
expect(error._tag).toBe("PullRequestOperationError");
|
||||
@@ -958,6 +970,7 @@ it.effect.each(
|
||||
}).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
);
|
||||
const fiber = yield* request.pipe(Effect.forkChild);
|
||||
@@ -1014,6 +1027,7 @@ it.live("keeps source workspace metadata when an alternate answers a detail read
|
||||
}).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
);
|
||||
expect(result).toEqual({
|
||||
@@ -1101,6 +1115,7 @@ it.live(
|
||||
}).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
);
|
||||
}),
|
||||
@@ -1649,6 +1664,7 @@ it.effect.each([false, true])(
|
||||
}).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, environmentRegistry),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, { authorize: () => Effect.void }),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
|
||||
);
|
||||
expect(result).toEqual({ state: "open", checks: [] });
|
||||
@@ -1980,3 +1996,78 @@ it.effect("refreshes stack state after reopening and head SHAs after a turn", ()
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
// Transport fixtures have a source-control-only session; authorization edge cases
|
||||
// are exercised by commandPermissions.test.ts.
|
||||
vi.mock("./session.ts", () => ({
|
||||
createEnvironmentSessionAtoms: () => ({ sessionStateAtom: grantedSessions }),
|
||||
}));
|
||||
const grantedSessions = Atom.family((_id: EnvironmentId) =>
|
||||
Atom.make<AsyncResult.AsyncResult<AuthSessionState>>(
|
||||
AsyncResult.success({
|
||||
authenticated: true,
|
||||
auth: {
|
||||
policy: "remote-reachable" as const,
|
||||
bootstrapMethods: [],
|
||||
sessionMethods: [],
|
||||
sessionCookieName: "test",
|
||||
},
|
||||
scopes: [AuthSourceControlWriteScope],
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("denies a routed write when only the origin has source-control permission", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
let writes = 0;
|
||||
const identity = { host: "github.com", provider: "github", viewer: "test", accountId: "123" };
|
||||
const client = {
|
||||
[WS_METHODS.pullRequestsRouting]: () => Effect.succeed(identity),
|
||||
[WS_METHODS.pullRequestsRoutingIdentity]: () => Effect.succeed(identity),
|
||||
[WS_METHODS.pullRequestsRunAction]: () =>
|
||||
Effect.sync(() => {
|
||||
writes++;
|
||||
}),
|
||||
} as unknown as WsRpcProtocolClient;
|
||||
const fixture = yield* makeTestRuntime(client, client);
|
||||
const local = EnvironmentId.make("local-environment");
|
||||
const stop = fixture.registry.mount(grantedSessions(local));
|
||||
yield* Effect.addFinalizer(() => Effect.sync(stop));
|
||||
fixture.registry.set(
|
||||
grantedSessions(local),
|
||||
AsyncResult.success({
|
||||
authenticated: true,
|
||||
scopes: [],
|
||||
auth: {
|
||||
policy: "remote-reachable" as const,
|
||||
bootstrapMethods: [],
|
||||
sessionMethods: [],
|
||||
sessionCookieName: "test",
|
||||
},
|
||||
}),
|
||||
);
|
||||
const error = yield* createPullRequestRouter()(WS_METHODS.pullRequestsRunAction, {
|
||||
projectId: ProjectId.make("project"),
|
||||
repository: "acme/repo",
|
||||
number: 1,
|
||||
action: "merge",
|
||||
}).pipe(
|
||||
Effect.provideService(EnvironmentRegistry.EnvironmentRegistry, fixture.environmentRegistry),
|
||||
Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, fixture.supervisor),
|
||||
Effect.provideService(GitHubRoutingPermissions, trustedRouting),
|
||||
Effect.provideService(RpcPermissionGuard, {
|
||||
authorize: (id, method, input) =>
|
||||
createCommandPermissions(fixture.runtime, method).authorize(
|
||||
fixture.registry,
|
||||
id,
|
||||
input,
|
||||
),
|
||||
}),
|
||||
Effect.flip,
|
||||
);
|
||||
expect(error._tag).toBe("EnvironmentAuthorizationError");
|
||||
expect(writes).toBe(0);
|
||||
}),
|
||||
),
|
||||
);
|
||||
Reference in new issue
Block a user