Skip to content
TypeScript

fix(auth): accept a typed pairing code regardless of letter case (#22089)

Victor Vogel5aa81a31718d
diff --git a/apps/server/src/auth/PairingGrantStore.test.ts b/apps/server/src/auth/PairingGrantStore.test.ts
index 561633d3524..4e940cd36cc 100644
--- a/apps/server/src/auth/PairingGrantStore.test.ts
+++ b/apps/server/src/auth/PairingGrantStore.test.ts
@@ -1,6 +1,7 @@
import { AuthStandardClientScopes } from "@t3tools/contracts";
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
+import * as Crypto from "effect/Crypto";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
@@ -45,11 +46,11 @@ const layerPairingGrantStore = (
);
it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
- it.effect("issues one-time bootstrap tokens that can only be consumed once", () =>
+ it.effect("accepts lowercase one-time tokens exactly once", () =>
Effect.gen(function* () {
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
const issued = yield* bootstrapCredentials.issueOneTimeToken({ label: "Julius iPhone" });
- const first = yield* bootstrapCredentials.consume(issued.credential);
+ const first = yield* bootstrapCredentials.consume(issued.credential.toLowerCase());
const second = yield* Effect.flip(bootstrapCredentials.consume(issued.credential));
expect(first.method).toBe("one-time-token");
@@ -59,7 +60,16 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
expect(issued.label).toBe("Julius iPhone");
expect(second._tag).toBe("UnknownBootstrapCredentialError");
expect(second.message).toContain("Unknown bootstrap credential");
- }).pipe(Effect.provide(layerPairingGrantStore())),
+ }).pipe(
+ Effect.provide(layerPairingGrantStore()),
+ Effect.provideServiceEffect(
+ Crypto.Crypto,
+ Effect.map(Crypto.Crypto, (crypto) => ({
+ ...crypto,
+ randomBytes: (size) => Effect.succeed(new Uint8Array(size).fill(8)),
+ })),
+ ),
+ ),
);
it.effect("atomically consumes a one-time token when multiple requests race", () =>
@@ -124,6 +134,23 @@ it.layer(NodeServices.layer)("PairingGrantStore.layer", (it) => {
}).pipe(Effect.provide(layerPairingGrantStore())),
);
+ it.effect("matches a seeded desktop bootstrap credential exactly", () =>
+ Effect.gen(function* () {
+ const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
+ const exact = yield* bootstrapCredentials.consume("abcdefghjkmn");
+ const otherCase = yield* Effect.flip(bootstrapCredentials.consume("ABCDEFGHJKMN"));
+
+ expect(exact.method).toBe("desktop-bootstrap");
+ expect(otherCase._tag).toBe("UnknownBootstrapCredentialError");
+ }).pipe(
+ Effect.provide(
+ layerPairingGrantStore({
+ desktopBootstrapToken: "abcdefghjkmn",
+ }),
+ ),
+ ),
+ );
+
it.effect("reports seeded desktop bootstrap credentials as expired after their ttl", () =>
Effect.gen(function* () {
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
diff --git a/apps/server/src/auth/PairingGrantStore.ts b/apps/server/src/auth/PairingGrantStore.ts
index 4999fef9c49..440dbe3788a 100644
--- a/apps/server/src/auth/PairingGrantStore.ts
+++ b/apps/server/src/auth/PairingGrantStore.ts
@@ -254,6 +254,11 @@ const PAIRING_TOKEN_LENGTH = 12;
const PAIRING_TOKEN_REJECTION_LIMIT =
Math.floor(256 / PAIRING_TOKEN_ALPHABET.length) * PAIRING_TOKEN_ALPHABET.length;
+const PAIRING_TOKEN_PATTERN = new RegExp(
+ `^[${PAIRING_TOKEN_ALPHABET}]{${PAIRING_TOKEN_LENGTH}}$`,
+ "i",
+);
+
const make = Effect.gen(function* () {
const crypto = yield* Crypto.Crypto;
const config = yield* ServerConfig.ServerConfig;
@@ -532,10 +537,15 @@ const make = Effect.gen(function* () {
return yield* seededResult.error;
}
+ // Only credentials in the issued pairing-code format are case-insensitive.
+ const pairingCredential = PAIRING_TOKEN_PATTERN.test(credential)
+ ? credential.toUpperCase()
+ : credential;
+
// Check scopes in the UPDATE so rejected requests cannot spend one-time links.
const consumed = yield* pairingLinks
.consumeAvailable({
- credential,
+ credential: pairingCredential,
proofKeyThumbprint: input?.proofKeyThumbprint ?? null,
...(input?.requestedScopes !== undefined
? { requestedScopes: input.requestedScopes }
@@ -560,7 +570,7 @@ const make = Effect.gen(function* () {
}
const matching = yield* pairingLinks
- .getByCredential({ credential })
+ .getByCredential({ credential: pairingCredential })
.pipe(Effect.mapError((cause) => new BootstrapCredentialLookupError({ cause })));
if (Option.isNone(matching)) {
return yield* new UnknownBootstrapCredentialError({});