|
| 1 | +import { beforeEach, describe, expect, vi } from "vitest"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Revoking a CLI personal access token has to be permanent. Logging in again mints a new secret; |
| 5 | + * it never reinstates the revoked row, and the revoked string never authenticates or gets handed |
| 6 | + * back out over the unauthenticated token endpoint. |
| 7 | + */ |
| 8 | + |
| 9 | +const db = vi.hoisted(() => ({ client: null as any })); |
| 10 | + |
| 11 | +vi.mock("~/db.server", () => ({ |
| 12 | + get prisma() { |
| 13 | + return db.client; |
| 14 | + }, |
| 15 | + get $replica() { |
| 16 | + return db.client; |
| 17 | + }, |
| 18 | +})); |
| 19 | +vi.mock("~/env.server", () => ({ |
| 20 | + env: { |
| 21 | + SESSION_SECRET: "test-session-secret-for-cli-pat-revocation", |
| 22 | + ENCRYPTION_KEY: "0123456789abcdef0123456789abcdef", |
| 23 | + }, |
| 24 | +})); |
| 25 | +vi.mock("~/services/logger.server", () => ({ |
| 26 | + logger: { debug: vi.fn(), error: vi.fn(), warn: vi.fn(), info: vi.fn() }, |
| 27 | +})); |
| 28 | +vi.mock("~/services/rbac.server", () => ({ |
| 29 | + rbac: { isUsingPlugin: async () => false, setTokenRole: async () => ({ ok: true }) }, |
| 30 | +})); |
| 31 | + |
| 32 | +import { postgresTest } from "@internal/testcontainers"; |
| 33 | +import { |
| 34 | + authenticatePersonalAccessToken, |
| 35 | + createAuthorizationCode, |
| 36 | + createPersonalAccessToken, |
| 37 | + createPersonalAccessTokenFromAuthorizationCode, |
| 38 | + getPersonalAccessTokenFromAuthorizationCode, |
| 39 | + revokePersonalAccessToken, |
| 40 | +} from "~/services/personalAccessToken.server"; |
| 41 | + |
| 42 | +vi.setConfig({ testTimeout: 60_000 }); |
| 43 | + |
| 44 | +let userCounter = 0; |
| 45 | + |
| 46 | +async function createUser(prisma: any) { |
| 47 | + userCounter += 1; |
| 48 | + return prisma.user.create({ |
| 49 | + data: { |
| 50 | + email: `cli-pat-revocation-${userCounter}-${Date.now()}@example.com`, |
| 51 | + authenticationMethod: "MAGIC_LINK", |
| 52 | + }, |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +/** Runs the consent-screen mint the CLI login flow triggers, and returns what it minted. */ |
| 57 | +async function cliLogin(userId: string) { |
| 58 | + const code = await createAuthorizationCode(); |
| 59 | + const minted = await createPersonalAccessTokenFromAuthorizationCode(code.code, userId); |
| 60 | + return { code: code.code, minted }; |
| 61 | +} |
| 62 | + |
| 63 | +function secretOf(minted: unknown): string { |
| 64 | + return (minted as { token: string }).token; |
| 65 | +} |
| 66 | + |
| 67 | +describe("CLI personal access token revocation", () => { |
| 68 | + beforeEach(() => { |
| 69 | + db.client = null; |
| 70 | + }); |
| 71 | + |
| 72 | + postgresTest("a later login reuses a live CLI token", async ({ prisma }) => { |
| 73 | + db.client = prisma; |
| 74 | + const user = await createUser(prisma); |
| 75 | + |
| 76 | + const first = await cliLogin(user.id); |
| 77 | + const second = await cliLogin(user.id); |
| 78 | + |
| 79 | + expect(second.minted.id).toBe(first.minted.id); |
| 80 | + expect(await prisma.personalAccessToken.count({ where: { userId: user.id } })).toBe(1); |
| 81 | + }); |
| 82 | + |
| 83 | + postgresTest( |
| 84 | + "a revoked CLI token is not revived by a later login, and its secret stays rejected", |
| 85 | + async ({ prisma }) => { |
| 86 | + db.client = prisma; |
| 87 | + const user = await createUser(prisma); |
| 88 | + |
| 89 | + const first = await cliLogin(user.id); |
| 90 | + const revokedSecret = secretOf(first.minted); |
| 91 | + expect(await authenticatePersonalAccessToken(revokedSecret)).toMatchObject({ |
| 92 | + userId: user.id, |
| 93 | + }); |
| 94 | + |
| 95 | + await revokePersonalAccessToken(first.minted.id, user.id); |
| 96 | + |
| 97 | + const second = await cliLogin(user.id); |
| 98 | + const freshSecret = secretOf(second.minted); |
| 99 | + |
| 100 | + expect(second.minted.id).not.toBe(first.minted.id); |
| 101 | + expect(freshSecret).not.toBe(revokedSecret); |
| 102 | + |
| 103 | + const revokedRow = await prisma.personalAccessToken.findFirst({ |
| 104 | + where: { id: first.minted.id }, |
| 105 | + }); |
| 106 | + expect(revokedRow?.revokedAt).toBeInstanceOf(Date); |
| 107 | + expect(revokedRow?.name).toBe("cli"); |
| 108 | + |
| 109 | + expect(await authenticatePersonalAccessToken(revokedSecret)).toBeUndefined(); |
| 110 | + expect(await authenticatePersonalAccessToken(freshSecret)).toMatchObject({ |
| 111 | + userId: user.id, |
| 112 | + }); |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + postgresTest("revoking again after re-login leaves both secrets dead", async ({ prisma }) => { |
| 117 | + db.client = prisma; |
| 118 | + const user = await createUser(prisma); |
| 119 | + |
| 120 | + const first = await cliLogin(user.id); |
| 121 | + await revokePersonalAccessToken(first.minted.id, user.id); |
| 122 | + |
| 123 | + const second = await cliLogin(user.id); |
| 124 | + await revokePersonalAccessToken(second.minted.id, user.id); |
| 125 | + |
| 126 | + const third = await cliLogin(user.id); |
| 127 | + |
| 128 | + expect(third.minted.id).not.toBe(first.minted.id); |
| 129 | + expect(third.minted.id).not.toBe(second.minted.id); |
| 130 | + expect(await authenticatePersonalAccessToken(secretOf(first.minted))).toBeUndefined(); |
| 131 | + expect(await authenticatePersonalAccessToken(secretOf(second.minted))).toBeUndefined(); |
| 132 | + }); |
| 133 | + |
| 134 | + postgresTest( |
| 135 | + "a token revoked under a different name does not block a fresh CLI login", |
| 136 | + async ({ prisma }) => { |
| 137 | + db.client = prisma; |
| 138 | + const user = await createUser(prisma); |
| 139 | + |
| 140 | + const dashboardToken = await createPersonalAccessToken({ name: "laptop", userId: user.id }); |
| 141 | + await revokePersonalAccessToken(dashboardToken.id, user.id); |
| 142 | + |
| 143 | + const login = await cliLogin(user.id); |
| 144 | + |
| 145 | + expect(login.minted.id).not.toBe(dashboardToken.id); |
| 146 | + expect(await authenticatePersonalAccessToken(dashboardToken.token)).toBeUndefined(); |
| 147 | + } |
| 148 | + ); |
| 149 | +}); |
| 150 | + |
| 151 | +describe("the authorization code token endpoint", () => { |
| 152 | + beforeEach(() => { |
| 153 | + db.client = null; |
| 154 | + }); |
| 155 | + |
| 156 | + postgresTest("discloses the token it minted", async ({ prisma }) => { |
| 157 | + db.client = prisma; |
| 158 | + const user = await createUser(prisma); |
| 159 | + |
| 160 | + const login = await cliLogin(user.id); |
| 161 | + |
| 162 | + const disclosed = await getPersonalAccessTokenFromAuthorizationCode(login.code); |
| 163 | + expect(disclosed.token?.token).toBe(secretOf(login.minted)); |
| 164 | + }); |
| 165 | + |
| 166 | + postgresTest( |
| 167 | + "stops disclosing a token that was revoked after the code was bound", |
| 168 | + async ({ prisma }) => { |
| 169 | + db.client = prisma; |
| 170 | + const user = await createUser(prisma); |
| 171 | + |
| 172 | + const login = await cliLogin(user.id); |
| 173 | + await revokePersonalAccessToken(login.minted.id, user.id); |
| 174 | + |
| 175 | + await expect(getPersonalAccessTokenFromAuthorizationCode(login.code)).rejects.toThrow( |
| 176 | + "Invalid authorization code, or code expired" |
| 177 | + ); |
| 178 | + } |
| 179 | + ); |
| 180 | + |
| 181 | + postgresTest("discloses the live token a later login reused", async ({ prisma }) => { |
| 182 | + db.client = prisma; |
| 183 | + const user = await createUser(prisma); |
| 184 | + |
| 185 | + const first = await cliLogin(user.id); |
| 186 | + const second = await cliLogin(user.id); |
| 187 | + |
| 188 | + const disclosed = await getPersonalAccessTokenFromAuthorizationCode(second.code); |
| 189 | + expect(disclosed.token?.token).toBe(secretOf(first.minted)); |
| 190 | + }); |
| 191 | + |
| 192 | + postgresTest("reports an unapproved code as not ready yet", async ({ prisma }) => { |
| 193 | + db.client = prisma; |
| 194 | + |
| 195 | + const code = await createAuthorizationCode(); |
| 196 | + |
| 197 | + const disclosed = await getPersonalAccessTokenFromAuthorizationCode(code.code); |
| 198 | + expect(disclosed.token).toBeNull(); |
| 199 | + }); |
| 200 | +}); |
0 commit comments