|
1 | 1 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
2 | | -import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { createHash } from "node:crypto"; |
| 3 | +import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs"; |
3 | 4 | import { tmpdir } from "node:os"; |
4 | 5 | import { join } from "node:path"; |
5 | 6 | import { |
@@ -136,6 +137,68 @@ describe("native main token refresh", () => { |
136 | 137 | expect((failure as MainAccountTokenRefreshError).reason).toBe("transient"); |
137 | 138 | }); |
138 | 139 |
|
| 140 | + test("rejects a recovery journal whose basenames do not belong to its transaction", async () => { |
| 141 | + const authPath = join(home, "auth.json"); |
| 142 | + const original = JSON.stringify({ tokens: { refresh_token: "old-refresh", account_id: "account-main" } }); |
| 143 | + const replacement = JSON.stringify({ tokens: { access_token: "new-access", refresh_token: "new-refresh" } }); |
| 144 | + const fileTransactionId = "11111111-1111-4111-8111-111111111111"; |
| 145 | + const stagedBasename = `.opencodex-native-main-refresh.${fileTransactionId}.new`; |
| 146 | + const journalPath = join(home, ".opencodex-native-main-refresh.json"); |
| 147 | + const digest = (value: string) => createHash("sha256").update(value).digest("hex"); |
| 148 | + writeFileSync(authPath, original); |
| 149 | + writeFileSync(join(home, stagedBasename), replacement); |
| 150 | + writeFileSync(journalPath, JSON.stringify({ |
| 151 | + version: 1, |
| 152 | + transactionId: "22222222-2222-4222-8222-222222222222", |
| 153 | + stagedBasename, |
| 154 | + previousBasename: `.opencodex-native-main-refresh.${fileTransactionId}.previous`, |
| 155 | + phase: "prepared", |
| 156 | + expectedSha256: digest(original), |
| 157 | + replacementSha256: digest(replacement), |
| 158 | + })); |
| 159 | + let attempts = 0; |
| 160 | + |
| 161 | + const failure = await getValidMainAccountToken({ |
| 162 | + refreshToken: async () => { |
| 163 | + attempts += 1; |
| 164 | + throw new Error("must not refresh through malformed recovery state"); |
| 165 | + }, |
| 166 | + }).catch(error => error); |
| 167 | + |
| 168 | + expect(failure).toBeInstanceOf(MainAccountTokenRefreshError); |
| 169 | + expect((failure as MainAccountTokenRefreshError).reason).toBe("transient"); |
| 170 | + expect(attempts).toBe(0); |
| 171 | + expect(readFileSync(authPath, "utf8")).toBe(original); |
| 172 | + expect(existsSync(journalPath)).toBe(true); |
| 173 | + }); |
| 174 | + |
| 175 | + test("rejects hash-ambiguous recovery state without consuming it", async () => { |
| 176 | + const authPath = join(home, "auth.json"); |
| 177 | + const original = JSON.stringify({ tokens: { refresh_token: "old-refresh", account_id: "account-main" } }); |
| 178 | + const transactionId = "11111111-1111-4111-8111-111111111111"; |
| 179 | + const stagedBasename = `.opencodex-native-main-refresh.${transactionId}.new`; |
| 180 | + const journalPath = join(home, ".opencodex-native-main-refresh.json"); |
| 181 | + const hash = createHash("sha256").update(original).digest("hex"); |
| 182 | + writeFileSync(authPath, original); |
| 183 | + writeFileSync(join(home, stagedBasename), original); |
| 184 | + writeFileSync(journalPath, JSON.stringify({ |
| 185 | + version: 1, |
| 186 | + transactionId, |
| 187 | + stagedBasename, |
| 188 | + previousBasename: `.opencodex-native-main-refresh.${transactionId}.previous`, |
| 189 | + phase: "prepared", |
| 190 | + expectedSha256: hash, |
| 191 | + replacementSha256: hash, |
| 192 | + })); |
| 193 | + |
| 194 | + const failure = await getValidMainAccountToken().catch(error => error); |
| 195 | + |
| 196 | + expect(failure).toBeInstanceOf(MainAccountTokenRefreshError); |
| 197 | + expect((failure as MainAccountTokenRefreshError).reason).toBe("transient"); |
| 198 | + expect(readFileSync(authPath, "utf8")).toBe(original); |
| 199 | + expect(existsSync(journalPath)).toBe(true); |
| 200 | + }); |
| 201 | + |
139 | 202 | test("keeps a joiner alive when the refresh owner cancels", async () => { |
140 | 203 | writeFileSync(join(home, "auth.json"), JSON.stringify({ |
141 | 204 | tokens: { |
|
0 commit comments