GeezerVison is showing github.com — fix(server): repository lookups no longer send GitHub tokens to arbit… · pingdotgg/t3code@80b7b0f
File treeExpand file treeCollapse file tree Open diff view settings packages/source-control-github/src/server
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/source-control-github/src/server/GitHubCredentials.test.ts +13Lines changed: 13 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -148,4 +148,17 @@ describe("GitHubCredentials", () => { | 148 | 148 | ); | 149 | 149 | }).pipe(Effect.provide(layer)); | 150 | 150 | }); | | 151 | + | | 152 | + it.effect("never hands a token to a host that is not a plain hostname", () => { | | 153 | + vi.stubEnv("GH_TOKEN", "env-token"); | | 154 | + const { layer, calls } = harness(); | | 155 | + return Effect.gen(function* () { | | 156 | + const credentials = yield* GitHubCredentials.GitHubCredentials; | | 157 | + for (const host of ["evil.com#.ghe.com", "evil.com/.ghe.com", "[email protected]"]) { | | 158 | + expect(GitHubCredentials.environmentToken(host, { GH_TOKEN: "env-token" })).toBeNull(); | | 159 | + expect((yield* Effect.flip(credentials.get(host)))._tag).toBe("GitHubNotSignedInError"); | | 160 | + } | | 161 | + expect(calls).toEqual([]); | | 162 | + }).pipe(Effect.provide(layer)); | | 163 | + }); | 151 | 164 | }); |
Collapse file Copy file name to clipboardExpand all lines: packages/source-control-github/src/server/GitHubCredentials.ts +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -96,6 +96,14 @@ function normalizeHost(host: string): string { | 96 | 96 | return host.trim().toLowerCase(); | 97 | 97 | } | 98 | 98 |
| | 99 | +const HOSTNAME = | | 100 | + /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)*(?::\d{1,5})?$/; | | 101 | + | | 102 | +/** API URLs are built from the host text, so anything but a bare hostname could redirect a token. */ | | 103 | +export function isGitHubHost(host: string): boolean { | | 104 | + return HOSTNAME.test(host); | | 105 | +} | | 106 | + | 99 | 107 | function isGitHubDotCom(host: string): boolean { | 100 | 108 | return host === "github.com" || host.endsWith(".ghe.com"); | 101 | 109 | } | @@ -105,6 +113,7 @@ export function environmentToken( | 105 | 113 | host: string, | 106 | 114 | env: Readonly<Record<string, string | undefined>>, | 107 | 115 | ): string | null { | | 116 | + if (!isGitHubHost(host)) return null; | 108 | 117 | const names = isGitHubDotCom(host) | 109 | 118 | ? ["GH_TOKEN", "GITHUB_TOKEN"] | 110 | 119 | : env.GH_HOST?.trim().toLowerCase() === host | @@ -222,6 +231,7 @@ export const make = Effect.gen(function* () { | 222 | 231 | return GitHubCredentials.of({ | 223 | 232 | get: Effect.fn("GitHubCredentials.get")(function* (rawHost) { | 224 | 233 | const host = normalizeHost(rawHost); | | 234 | + if (!isGitHubHost(host)) return yield* new GitHubNotSignedInError({ host }); | 225 | 235 | const choice = yield* hostChoice(host); | 226 | 236 | if (choice?.enabled === false) { | 227 | 237 | return yield* new GitHubHostDisabledError({ host }); | |
Collapse file Copy file name to clipboardExpand all lines: packages/source-control-github/src/server/gitHubRepositoryResolution.test.ts +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import { assert, describe, it } from "@effect/vitest"; | 2 | 2 |
| 3 | 3 | import { | 4 | 4 | gitHubApiHostForRemote, | | 5 | + parseGitHubRepositorySelector, | 5 | 6 | selectGitHubBaseRepository, | 6 | 7 | } from "./gitHubRepositoryResolution.ts"; | 7 | 8 |
| @@ -89,3 +90,22 @@ describe("gitHubApiHostForRemote", () => { | 89 | 90 | | 90 | 91 | }); | 91 | 92 | }); | | 93 | + | | 94 | +describe("parseGitHubRepositorySelector", () => { | | 95 | + it("reads host/owner/name only when the host is a plain hostname", () => { | | 96 | + assert.deepStrictEqual(parseGitHubRepositorySelector("GHE.corp:8443/acme/web", "github.com"), { | | 97 | + host: "ghe.corp:8443", | | 98 | + owner: "acme", | | 99 | + name: "web", | | 100 | + }); | | 101 | + for (const selector of [ | | 102 | + "evil.com#.ghe.com/acme/web", | | 103 | + "evil.com?.ghe.com/acme/web", | | 104 | + | | 105 | + "evil.com\\.ghe.com/acme/web", | | 106 | + "-evil.com/acme/web", | | 107 | + ]) { | | 108 | + assert.strictEqual(parseGitHubRepositorySelector(selector, "github.com"), null, selector); | | 109 | + } | | 110 | + }); | | 111 | +}); |
Collapse file Copy file name to clipboardExpand all lines: packages/source-control-github/src/server/gitHubRepositoryResolution.ts +4-2Lines changed: 4 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ import { normalizeGitRemoteUrl } from "@t3tools/shared/git"; | 2 | 2 | import { isSshRemoteUrl } from "@t3tools/shared/sourceControl"; | 3 | 3 | import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definition"; | 4 | 4 | import { definition } from "../shared/definition.ts"; | | 5 | +import { isGitHubHost } from "./GitHubCredentials.ts"; | 5 | 6 |
| 6 | 7 | export interface GitHubRepositoryLocator { | 7 | 8 | readonly host: string; | @@ -114,8 +115,9 @@ export function parseGitHubRepositorySelector( | 114 | 115 | } | 115 | 116 | const parts = trimmed.split("/").filter(Boolean); | 116 | 117 | if (parts.length === 2) return { host: defaultHost, owner: parts[0]!, name: parts[1]! }; | 117 | | - if (parts.length === 3) | 118 | | - return { host: parts[0]!.toLowerCase(), owner: parts[1]!, name: parts[2]! }; | | 118 | + const host = parts[0]?.toLowerCase(); | | 119 | + if (parts.length === 3 && host !== undefined && isGitHubHost(host)) | | 120 | + return { host, owner: parts[1]!, name: parts[2]! }; | 119 | 121 | return null; | 120 | 122 | } | 121 | 123 |
| |
|