The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 22:00 UTC
fix(server): pull requests load for credentials that cannot read /user (#18780)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Lanny Bose committed
Oct 19, 2026 at 22:34 UTC
71b2ac4f9b59ec91d671289870321acd0dfe9ba2
4 files changed
+206
-19
packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
+135
@@ -88,6 +88,7 @@ const coreResponse = (pullRequest: Readonly<Record<string, unknown>> = {}) => ({
88
type ApiCall =
89
| {
90
readonly kind: "graphql";
91
+ readonly authorization: string | undefined;
92
readonly partial?: boolean;
93
/** The generated operation's name, which the routes below match on. */
94
readonly operationName?: string;
@@ -137,6 +138,7 @@ const mockHttp = Layer.succeed(
138
: { query: "", operationName: "", variables: {} };
139
const call: ApiCall = {
140
kind: "graphql",
141
+ authorization: request.headers["authorization"],
142
host,
143
operation: "batch",
144
operationName: sent.operationName,
@@ -3742,6 +3744,139 @@ layer("GitHubPullRequestApi.layer", (it) => {
3744
}),
3745
);
3746
3747
+ it.effect("verifies a REST-forbidden account through GraphQL with the pinned credential", () =>
3748
+ Effect.gen(function* () {
3749
+ const input = { cwd: "/w", host: "github.identity-fallback.test" };
3750
+ const original = {
3751
+ token: Redacted.make("original-token"),
3752
+ fingerprint: `${input.host}:original`,
3753
+ };
3754
+ mockedCredential.mockReturnValue(Effect.succeed(original));
3755
+ mockedExecute.mockImplementation((call) =>
3756
+ Effect.gen(function* () {
3757
+ expect(yield* SourceControlRateLimit.CredentialScope).toBe(original.fingerprint);
3758
+ if (call.kind === "rest") {
3759
+ expect(call.path).toBe("user");
3760
+ expect(yield* GitHubRequestPolicy.PinnedGitHubCredential).toMatchObject({
3761
+ host: input.host,
3762
+ credentialFingerprint: original.fingerprint,
3763
+ });
3764
+ mockedCredential.mockReturnValue(
3765
+ Effect.succeed({
3766
+ token: Redacted.make("replacement-token"),
3767
+ fingerprint: `${input.host}:replacement`,
3768
+ }),
3769
+ );
3770
+ return yield* new GitHubRequestPolicy.GitHubApiResponseError({
3771
+ host: input.host,
3772
+ operation: "getRoutingIdentity",
3773
+ status: 403,
3774
+ githubErrors: ["Resource not accessible by integration"],
3775
+ });
3776
+ }
3777
+ expect(call.host).toBe(input.host);
3778
+ expect(call.authorization).toBe("Bearer original-token");
3779
+ expect(call.query).toMatch(/viewer\s*\{/);
3780
+ expect(call.query).toContain("databaseId");
3781
+ expect(call.query).toContain("login");
3782
+ return output(
3783
+ '{"data":{"viewer":{"databaseId":789,"login":"acme-app[bot]"}}}',
3784
+ false,
3785
+ false,
3786
+ graphqlQuota(0),
3787
+ );
3788
+ }),
3789
+ );
3790
+ const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3791
+ const quota = yield* GitHubQuota.GitHubQuota;
3792
+ yield* quota
3793
+ .observe(input.host, graphqlQuota(1))
3794
+ .pipe(Effect.provideService(SourceControlRateLimit.CredentialScope, original.fingerprint));
3795
+ const identity = { accountId: "789", viewer: "acme-app[bot]" };
3796
+ expect(yield* cli.getRoutingIdentity(input)).toEqual(identity);
3797
+
3798
+ mockedCredential.mockReturnValue(Effect.succeed(original));
3799
+ expect(yield* cli.getRoutingIdentity(input)).toEqual(identity);
3800
+ expect(mockedExecute).toHaveBeenCalledTimes(2);
3801
+
3802
+ const exhausted = yield* quota
3803
+ .admit(input.host, "graphql")
3804
+ .pipe(
3805
+ Effect.provideService(SourceControlRateLimit.CredentialScope, original.fingerprint),
3806
+ Effect.flip,
3807
+ );
3808
+ expect(exhausted._tag).toBe("SourceControlRateLimitPausedError");
3809
+ yield* quota
3810
+ .admit(input.host, "graphql")
3811
+ .pipe(
3812
+ Effect.provideService(
3813
+ SourceControlRateLimit.CredentialScope,
3814
+ `${input.host}:replacement`,
3815
+ ),
3816
+ );
3817
+ }),
3818
+ );
3819
+
3820
+ it.effect("rejects a null GraphQL account id without caching it", () =>
3821
+ Effect.gen(function* () {
3822
+ const input = { cwd: "/w", host: "github.identity-null.test" };
3823
+ const forbidden = new GitHubRequestPolicy.GitHubApiResponseError({
3824
+ host: input.host,
3825
+ operation: "getRoutingIdentity",
3826
+ status: 403,
3827
+ });
3828
+ mockedExecute
3829
+ .mockReturnValueOnce(Effect.fail(forbidden))
3830
+ .mockReturnValueOnce(
3831
+ Effect.succeed(output('{"data":{"viewer":{"databaseId":null,"login":"acme-app[bot]"}}}')),
3832
+ )
3833
+ .mockReturnValueOnce(Effect.fail(forbidden))
3834
+ .mockReturnValueOnce(
3835
+ Effect.succeed(output('{"data":{"viewer":{"databaseId":789,"login":"acme-app[bot]"}}}')),
3836
+ );
3837
+ const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3838
+ const error = yield* cli.getRoutingIdentity(input).pipe(Effect.flip);
3839
+ expect(error._tag).toBe("GitHubViewerLoginUnavailableError");
3840
+ expect(yield* cli.getRoutingIdentity(input)).toEqual({
3841
+ accountId: "789",
3842
+ viewer: "acme-app[bot]",
3843
+ });
3844
+ expect(mockedExecute).toHaveBeenCalledTimes(4);
3845
+ }),
3846
+ );
3847
+
3848
+ it.effect.each([
3849
+ new GitHubRequestPolicy.GitHubApiAuthenticationError({
3850
+ host: "github.identity-auth-error.test",
3851
+ operation: "getRoutingIdentity",
3852
+ }),
3853
+ new GitHubRequestPolicy.GitHubApiRateLimitError({
3854
+ host: "github.identity-rate-error.test",
3855
+ operation: "getRoutingIdentity",
3856
+ retryAt: 123_456,
3857
+ }),
3858
+ new GitHubRequestPolicy.GitHubApiRequestError({
3859
+ host: "github.identity-network-error.test",
3860
+ operation: "getRoutingIdentity",
3861
+ cause: new Error("connection refused"),
3862
+ }),
3863
+ new GitHubRequestPolicy.GitHubApiResponseError({
3864
+ host: "github.identity-server-error.test",
3865
+ operation: "getRoutingIdentity",
3866
+ status: 500,
3867
+ }),
3868
+ ])("preserves $_tag from REST identity verification without trying GraphQL", (error) =>
3869
+ Effect.gen(function* () {
3870
+ mockedExecute.mockReturnValue(Effect.fail(error));
3871
+ const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3872
+ expect(yield* cli.getRoutingIdentity({ cwd: "/w", host: error.host }).pipe(Effect.flip)).toBe(
3873
+ error,
3874
+ );
3875
+ expect(mockedExecute).toHaveBeenCalledTimes(1);
3876
+ expect(callAt(0)).toMatchObject({ kind: "rest", path: "user" });
3877
+ }),
3878
+ );
3879
+
3880
it.effect("reuses verified credentials offline and refuses an unverified replacement", () =>
3881
Effect.gen(function* () {
3882
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
packages/source-control-github/src/server/GitHubPullRequestApi.ts
+34
-17
@@ -907,6 +907,9 @@ export const make = Effect.gen(function* () {
907
const sourceControlHost = yield* SourceControlHost.SourceControlHost;
908
const fileSystem = yield* FileSystem.FileSystem;
909
const revalidateChecks = yield* makeChecksRevalidator;
910
+ const client = yield* GraphQLClient.make(PullRequestGraphQLGroup).pipe(
911
+ Effect.provide(GitHubGraphQlClient.layer),
912
+ );
913
const routingIdentities = new Map<
914
string,
915
{
@@ -915,14 +918,12 @@ export const make = Effect.gen(function* () {
918
}
919
>();
920
const identityLocks = new Map<string, { gate: Semaphore.Semaphore; users: number }>();
918
- const decodeRoutingIdentity = Schema.decodeUnknownEffect(
919
- Schema.fromJsonString(
920
- Schema.Struct({
921
- id: PositiveInt,
922
- login: TrimmedNonEmptyString,
923
- }),
924
- ),
925
- );
921
+ const RoutingIdentity = Schema.Struct({
922
+ id: PositiveInt,
923
+ login: TrimmedNonEmptyString,
924
+ });
925
+ const decodeRoutingIdentity = Schema.decodeUnknownEffect(Schema.fromJsonString(RoutingIdentity));
926
+ const decodeViewerIdentity = Schema.decodeUnknownEffect(RoutingIdentity);
927
const captureVerifiedCredential = Effect.fn("GitHubPullRequestApi.captureVerifiedCredential")(
928
function* (input: { readonly cwd: string; readonly host: string }) {
929
const unavailable = () => new GitHubViewerLoginUnavailableError({ cwd: input.cwd });
@@ -943,16 +944,36 @@ export const make = Effect.gen(function* () {
944
const cached = routingIdentities.get(key);
945
if (cached !== undefined && now - cached.at < 10 * 60_000)
946
return { ...credential, ...cached.value };
946
- // Pin this read so an auth switch cannot poison its cache entry.
947
- const response = yield* api
947
+ // Pin verification so an auth switch cannot poison its cache entry.
948
+ const identity = yield* api
949
.rest({ host, operation: "getRoutingIdentity", path: "user", allowReserve: true })
950
.pipe(
951
+ Effect.flatMap((response) =>
952
+ decodeRoutingIdentity(response.body).pipe(Effect.mapError(unavailable)),
953
+ ),
954
+ Effect.catchTags({
955
+ GitHubApiResponseError: (error) =>
956
+ Effect.gen(function* () {
957
+ if (error.status !== 403) return yield* error;
958
+ const { viewer } = yield* client.RoutingIdentityViewer({}).pipe(
959
+ GitHubGraphQlClient.call({
960
+ host,
961
+ operation: "getRoutingIdentity",
962
+ allowReserve: true,
963
+ }),
964
+ Effect.catchTags({
965
+ GraphQLClientError: () => Effect.fail(unavailable()),
966
+ }),
967
+ );
968
+ return yield* decodeViewerIdentity({
969
+ id: viewer.databaseId,
970
+ login: viewer.login,
971
+ }).pipe(Effect.mapError(unavailable));
972
+ }),
973
+ }),
974
Effect.provideService(GitHubRequestPolicy.PinnedGitHubCredential, credential),
975
Effect.provideService(SourceControlRateLimit.CredentialScope, key),
976
);
953
- const identity = yield* decodeRoutingIdentity(response.body).pipe(
954
- Effect.mapError(unavailable),
955
- );
977
const value = { accountId: String(identity.id), viewer: identity.login };
978
if (routingIdentities.size >= 128)
979
routingIdentities.delete(routingIdentities.keys().next().value!);
@@ -989,10 +1010,6 @@ export const make = Effect.gen(function* () {
1010
Effect.map(({ accountId, viewer }) => ({ accountId, viewer })),
1011
);
1012
992
- const client = yield* GraphQLClient.make(PullRequestGraphQLGroup).pipe(
993
- Effect.provide(GitHubGraphQlClient.layer),
994
- );
995
-
1013
const readError = (cwd: string, operation: string, cause: unknown) =>
1014
new GitHubPullRequestReadError({ cwd, operation, cause });
1015
packages/source-control-github/src/server/pullRequestReads.graphql
+8
-1
@@ -1,4 +1,11 @@
1
-# Reads of one pull request, or of the repository it belongs to.
1
+# Reads of the signed-in account, one pull request, or its repository.
2
+
3
+query RoutingIdentityViewer {
4
+ viewer {
5
+ databaseId
6
+ login
7
+ }
8
+}
9
10
# Resolves a listing's authors to avatars, which no listing field carries.
11
query ActorAvatars($ids: [ID!]!) {
packages/source-control-github/src/server/pullRequestReads.graphql.ts
+29
-1
@@ -82,6 +82,34 @@ export const CheckContext = Schema.Union([
82
])
83
export type CheckContext = typeof CheckContext.Type
84
85
+/** A user is an individual's account on GitHub that owns repositories and can make new content. */
86
+export const ViewerUser = Schema.Struct({
87
+ /** Identifies the primary key from the database. */
88
+ databaseId: Schema.NullOr(Shared.Int),
89
+ /** The username used to login. */
90
+ login: Schema.String
91
+})
92
+export interface ViewerUser extends Schema.Struct.Type<typeof ViewerUser.fields> {}
93
+
94
+/** `query RoutingIdentityViewer` */
95
+export const RoutingIdentityViewer = GraphQL.query("RoutingIdentityViewer", {
96
+ document: {
97
+ variables: [],
98
+ selections: [
99
+ { name: "viewer", body: ["{databaseId login}"] }
100
+ ],
101
+ fragments: []
102
+ },
103
+ result: Schema.Struct({
104
+ /** The currently authenticated user. */
105
+ viewer: ViewerUser
106
+ })
107
+})
108
+export declare namespace RoutingIdentityViewer {
109
+ export type Variables = typeof RoutingIdentityViewer.variables.Type
110
+ export type Result = typeof RoutingIdentityViewer.result.Type
111
+}
112
+
113
/** A user is an individual's account on GitHub that owns repositories and can make new content. */
114
export const ActorAvatarsNodesUser = Schema.Struct({
115
__typename: Schema.Literal("User"),
@@ -1495,4 +1523,4 @@ export declare namespace PullRequestSourceBranch {
1523
}
1524
1525
/** Every operation in pullRequestReads.graphql. */
1498
-export const PullRequestReadsGraphQLGroup = GraphQLGroup.make(ActorAvatars, PullRequestCore, PullRequestCheckContexts, PullRequestPreview, PullRequestActivity, PullRequestHeads, PullRequestNodeId, PullRequestSubject, ViewerPermissions, ReviewerCandidates, LabelCandidates, PullRequestFilesViewed, PullRequestMergeMessage, PullRequestActionState, PullRequestBranchToDelete, PullRequestSourceBranch)
1526
+export const PullRequestReadsGraphQLGroup = GraphQLGroup.make(RoutingIdentityViewer, ActorAvatars, PullRequestCore, PullRequestCheckContexts, PullRequestPreview, PullRequestActivity, PullRequestHeads, PullRequestNodeId, PullRequestSubject, ViewerPermissions, ReviewerCandidates, LabelCandidates, PullRequestFilesViewed, PullRequestMergeMessage, PullRequestActionState, PullRequestBranchToDelete, PullRequestSourceBranch)