Skip to content

Commit 5aa81a3

Browse files
only21milclaudemaria-rcks
authored
fix(auth): accept a typed pairing code regardless of letter case (pingdotgg#22089)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent c839797 commit 5aa81a3

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

‎apps/server/src/auth/PairingGrantStore.test.ts‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AuthStandardClientScopes } from "@t3tools/contracts";
22
import * as NodeServices from "@effect/platform-node/NodeServices";
33
import { expect, it } from "@effect/vitest";
4+
import * as Crypto from "effect/Crypto";
45
import * as Duration from "effect/Duration";
56
import * as Effect from "effect/Effect";
67
import * as Layer from "effect/Layer";
@@ -45,11 +46,11 @@ const layerPairingGrantStore = (
4546
);
4647

4748
it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
48-
it.effect("issues one-time bootstrap tokens that can only be consumed once", () =>
49+
it.effect("accepts lowercase one-time tokens exactly once", () =>
4950
Effect.gen(function* () {
5051
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
5152
const issued = yield* bootstrapCredentials.issueOneTimeToken({ label: "Julius iPhone" });
52-
const first = yield* bootstrapCredentials.consume(issued.credential);
53+
const first = yield* bootstrapCredentials.consume(issued.credential.toLowerCase());
5354
const second = yield* Effect.flip(bootstrapCredentials.consume(issued.credential));
5455

5556
expect(first.method).toBe("one-time-token");
@@ -59,7 +60,16 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
5960
expect(issued.label).toBe("Julius iPhone");
6061
expect(second._tag).toBe("UnknownBootstrapCredentialError");
6162
expect(second.message).toContain("Unknown bootstrap credential");
62-
}).pipe(Effect.provide(layerPairingGrantStore())),
63+
}).pipe(
64+
Effect.provide(layerPairingGrantStore()),
65+
Effect.provideServiceEffect(
66+
Crypto.Crypto,
67+
Effect.map(Crypto.Crypto, (crypto) => ({
68+
...crypto,
69+
randomBytes: (size) => Effect.succeed(new Uint8Array(size).fill(8)),
70+
})),
71+
),
72+
),
6373
);
6474

6575
it.effect("atomically consumes a one-time token when multiple requests race", () =>
@@ -124,6 +134,23 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
124134
}).pipe(Effect.provide(layerPairingGrantStore())),
125135
);
126136

137+
it.effect("matches a seeded desktop bootstrap credential exactly", () =>
138+
Effect.gen(function* () {
139+
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
140+
const exact = yield* bootstrapCredentials.consume("abcdefghjkmn");
141+
const otherCase = yield* Effect.flip(bootstrapCredentials.consume("ABCDEFGHJKMN"));
142+
143+
expect(exact.method).toBe("desktop-bootstrap");
144+
expect(otherCase._tag).toBe("UnknownBootstrapCredentialError");
145+
}).pipe(
146+
Effect.provide(
147+
layerPairingGrantStore({
148+
desktopBootstrapToken: "abcdefghjkmn",
149+
}),
150+
),
151+
),
152+
);
153+
127154
it.effect("reports seeded desktop bootstrap credentials as expired after their ttl", () =>
128155
Effect.gen(function* () {
129156
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;

‎apps/server/src/auth/PairingGrantStore.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ const PAIRING_TOKEN_LENGTH = 12;
254254
const PAIRING_TOKEN_REJECTION_LIMIT =
255255
Math.floor(256 / PAIRING_TOKEN_ALPHABET.length) * PAIRING_TOKEN_ALPHABET.length;
256256

257+
const PAIRING_TOKEN_PATTERN = new RegExp(
258+
`^[${PAIRING_TOKEN_ALPHABET}]{${PAIRING_TOKEN_LENGTH}}$`,
259+
"i",
260+
);
261+
257262
const make = Effect.gen(function* () {
258263
const crypto = yield* Crypto.Crypto;
259264
const config = yield* ServerConfig.ServerConfig;
@@ -532,10 +537,15 @@ const make = Effect.gen(function* () {
532537
return yield* seededResult.error;
533538
}
534539

540+
// Only credentials in the issued pairing-code format are case-insensitive.
541+
const pairingCredential = PAIRING_TOKEN_PATTERN.test(credential)
542+
? credential.toUpperCase()
543+
: credential;
544+
535545
// Check scopes in the UPDATE so rejected requests cannot spend one-time links.
536546
const consumed = yield* pairingLinks
537547
.consumeAvailable({
538-
credential,
548+
credential: pairingCredential,
539549
proofKeyThumbprint: input?.proofKeyThumbprint ?? null,
540550
...(input?.requestedScopes !== undefined
541551
? { requestedScopes: input.requestedScopes }
@@ -560,7 +570,7 @@ const make = Effect.gen(function* () {
560570
}
561571

562572
const matching = yield* pairingLinks
563-
.getByCredential({ credential })
573+
.getByCredential({ credential: pairingCredential })
564574
.pipe(Effect.mapError((cause) => new BootstrapCredentialLookupError({ cause })));
565575
if (Option.isNone(matching)) {
566576
return yield* new UnknownBootstrapCredentialError({});

0 commit comments

Comments
 (0)