Skip to content

Commit 94195b3

Browse files
committed
fix: prevent bootstrap pairing scope changes before redemption
1 parent 7a9f770 commit 94195b3

3 files changed

Lines changed: 112 additions & 3 deletions

File tree

‎src/infra/device-bootstrap.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,54 @@ describe("device bootstrap tokens", () => {
9797
expect(parsed[issued.token]?.publicKey).toBe("public-key-123");
9898
});
9999

100+
it("rejects changing the requested profile while a bound use is pending", async () => {
101+
const baseDir = await createTempDir();
102+
const issued = await issueDeviceBootstrapToken({
103+
baseDir,
104+
profile: {
105+
roles: ["operator"],
106+
scopes: ["operator.approvals", "operator.read", "operator.write"],
107+
},
108+
});
109+
110+
await expect(
111+
verifyBootstrapToken(baseDir, issued.token, {
112+
role: "operator",
113+
scopes: ["operator.read"],
114+
}),
115+
).resolves.toEqual({ ok: true });
116+
await expect(
117+
verifyBootstrapToken(baseDir, issued.token, {
118+
role: "operator",
119+
scopes: ["operator.write", "operator.approvals"],
120+
}),
121+
).resolves.toEqual({ ok: false, reason: "bootstrap_token_invalid" });
122+
await expect(
123+
verifyBootstrapToken(baseDir, issued.token, {
124+
role: "operator",
125+
scopes: ["operator.read"],
126+
}),
127+
).resolves.toEqual({ ok: true });
128+
129+
await expect(
130+
redeemDeviceBootstrapTokenProfile({
131+
baseDir,
132+
token: issued.token,
133+
role: "operator",
134+
scopes: ["operator.read"],
135+
}),
136+
).resolves.toEqual({
137+
recorded: true,
138+
fullyRedeemed: false,
139+
});
140+
await expect(
141+
verifyBootstrapToken(baseDir, issued.token, {
142+
role: "operator",
143+
scopes: ["operator.write", "operator.approvals"],
144+
}),
145+
).resolves.toEqual({ ok: true });
146+
});
147+
100148
it("loads the issued bootstrap profile for a valid token", async () => {
101149
const baseDir = await createTempDir();
102150
const issued = await issueDeviceBootstrapToken({ baseDir });

‎src/infra/device-bootstrap.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type DeviceBootstrapTokenRecord = {
2323
publicKey?: string;
2424
profile?: DeviceBootstrapProfile;
2525
redeemedProfile?: DeviceBootstrapProfile;
26+
pendingProfile?: DeviceBootstrapProfile;
2627
roles?: string[];
2728
scopes?: string[];
2829
issuedAtMs: number;
@@ -67,6 +68,35 @@ function resolvePersistedRedeemedProfile(
6768
return normalizeDeviceBootstrapProfile(record.redeemedProfile);
6869
}
6970

71+
function resolvePersistedPendingProfile(
72+
record: Partial<DeviceBootstrapTokenRecord>,
73+
): DeviceBootstrapProfile | null {
74+
return record.pendingProfile ? normalizeDeviceBootstrapProfile(record.pendingProfile) : null;
75+
}
76+
77+
function resolveRequestedBootstrapProfile(params: {
78+
role: string;
79+
scopes: readonly string[];
80+
}): DeviceBootstrapProfile {
81+
return normalizeDeviceBootstrapProfile({
82+
roles: [params.role],
83+
scopes: resolveBootstrapProfileScopesForRole(params.role, params.scopes),
84+
});
85+
}
86+
87+
function sameBootstrapProfile(
88+
left: DeviceBootstrapProfile,
89+
right: DeviceBootstrapProfile,
90+
): boolean {
91+
if (left.roles.length !== right.roles.length || left.scopes.length !== right.scopes.length) {
92+
return false;
93+
}
94+
return (
95+
left.roles.every((role, index) => role === right.roles[index]) &&
96+
left.scopes.every((scope, index) => scope === right.scopes[index])
97+
);
98+
}
99+
70100
function resolveIssuedBootstrapProfile(params: {
71101
profile?: DeviceBootstrapProfileInput;
72102
roles?: readonly string[];
@@ -173,10 +203,12 @@ async function loadState(baseDir?: string): Promise<DeviceBootstrapStateFile> {
173203
typeof record.token === "string" && record.token.trim().length > 0 ? record.token : tokenKey;
174204
const issuedAtMs = typeof record.issuedAtMs === "number" ? record.issuedAtMs : 0;
175205
const profile = resolvePersistedBootstrapProfile(record);
206+
const pendingProfile = resolvePersistedPendingProfile(record);
176207
state[tokenKey] = {
177208
token,
178209
profile,
179210
redeemedProfile: resolvePersistedRedeemedProfile(record),
211+
...(pendingProfile ? { pendingProfile } : {}),
180212
deviceId: typeof record.deviceId === "string" ? record.deviceId : undefined,
181213
publicKey: typeof record.publicKey === "string" ? record.publicKey : undefined,
182214
issuedAtMs,
@@ -304,18 +336,33 @@ export async function redeemDeviceBootstrapTokenProfile(params: {
304336
}
305337
const [tokenKey, record] = found;
306338
const issuedProfile = resolvePersistedBootstrapProfile(record);
339+
const pendingProfile = resolvePersistedPendingProfile(record);
307340
const redeemedProfile = normalizeDeviceBootstrapProfile({
308341
roles: [...resolvePersistedRedeemedProfile(record).roles, params.role],
309342
scopes: [
310343
...resolvePersistedRedeemedProfile(record).scopes,
311344
...resolveBootstrapProfileScopesForRole(params.role, params.scopes),
312345
],
313346
});
314-
state[tokenKey] = {
347+
const nextPendingProfile =
348+
pendingProfile &&
349+
!bootstrapProfileSatisfiesProfile({
350+
actualProfile: redeemedProfile,
351+
requiredProfile: pendingProfile,
352+
})
353+
? pendingProfile
354+
: undefined;
355+
const nextRecord: DeviceBootstrapTokenRecord = {
315356
...record,
316357
profile: issuedProfile,
317358
redeemedProfile,
318359
};
360+
if (nextPendingProfile) {
361+
nextRecord.pendingProfile = nextPendingProfile;
362+
} else {
363+
delete nextRecord.pendingProfile;
364+
}
365+
state[tokenKey] = nextRecord;
319366
await persistState(state, params.baseDir);
320367
return {
321368
recorded: true,
@@ -368,6 +415,10 @@ export async function verifyDeviceBootstrapToken(params: {
368415
) {
369416
return { ok: false, reason: "bootstrap_token_invalid" };
370417
}
418+
const requestedProfile = resolveRequestedBootstrapProfile({
419+
role,
420+
scopes: params.scopes,
421+
});
371422

372423
const boundDeviceId = record.deviceId?.trim();
373424
const boundPublicKey =
@@ -378,9 +429,14 @@ export async function verifyDeviceBootstrapToken(params: {
378429
if (boundDeviceId !== deviceId || boundPublicKey !== publicKey) {
379430
return { ok: false, reason: "bootstrap_token_invalid" };
380431
}
432+
const pendingProfile = resolvePersistedPendingProfile(record);
433+
if (pendingProfile && !sameBootstrapProfile(pendingProfile, requestedProfile)) {
434+
return { ok: false, reason: "bootstrap_token_invalid" };
435+
}
381436
state[tokenKey] = {
382437
...record,
383438
profile: allowedProfile,
439+
pendingProfile: pendingProfile ?? requestedProfile,
384440
deviceId,
385441
publicKey,
386442
lastUsedAtMs: Date.now(),
@@ -392,6 +448,7 @@ export async function verifyDeviceBootstrapToken(params: {
392448
state[tokenKey] = {
393449
...record,
394450
profile: allowedProfile,
451+
pendingProfile: requestedProfile,
395452
deviceId,
396453
publicKey,
397454
lastUsedAtMs: Date.now(),

‎src/infra/device-pairing.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ describe("device pairing tokens", () => {
590590
const issued = await issueDeviceBootstrapToken({
591591
baseDir,
592592
roles: ["operator"],
593-
scopes: ["operator.read"],
593+
scopes: ["operator.approvals", "operator.read", "operator.write"],
594594
});
595595

596596
await expect(
@@ -620,11 +620,15 @@ describe("device pairing tokens", () => {
620620
deviceId: "device-1",
621621
publicKey: "public-key-1",
622622
role: "operator",
623-
scopes: ["operator.admin"],
623+
scopes: ["operator.write", "operator.approvals"],
624624
baseDir,
625625
}),
626626
).resolves.toEqual({ ok: false, reason: "bootstrap_token_invalid" });
627627

628+
const pending = await listDevicePairing(baseDir);
629+
expect(pending.pending).toHaveLength(1);
630+
expect(pending.pending[0]?.scopes).toEqual(["operator.read"]);
631+
628632
await approveDevicePairing(
629633
first.request.requestId,
630634
{ callerScopes: ["operator.read"] },

0 commit comments

Comments
 (0)