@@ -58,4 +58,56 @@ describe("resolvePendingDeviceApprovalState", () => {
|
58 | 58 | }, |
59 | 59 | }); |
60 | 60 | }); |
| 61 | + |
| 62 | +it("drops non-string role entries from malformed pairing records instead of crashing", () => { |
| 63 | +// Legacy/malformed on-disk pairing records can carry non-string roles/role (blind-cast JSON); |
| 64 | +// before the shared-normalizer guard these crashed normalizeRoleList on .trim(). |
| 65 | +type PendingArg = Parameters<typeof resolvePendingDeviceApprovalState>[0]; |
| 66 | +type PairedArg = NonNullable<Parameters<typeof resolvePendingDeviceApprovalState>[1]>; |
| 67 | +expect( |
| 68 | +resolvePendingDeviceApprovalState( |
| 69 | +{ |
| 70 | +roles: [123, "operator", null, " admin "], |
| 71 | +role: 5, |
| 72 | +scopes: ["operator.read"], |
| 73 | +} as unknown as PendingArg, |
| 74 | +{ |
| 75 | +roles: [null, "operator"], |
| 76 | +role: 9, |
| 77 | +scopes: ["operator.read"], |
| 78 | +} as unknown as PairedArg, |
| 79 | +), |
| 80 | +).toEqual({ |
| 81 | +kind: "role-upgrade", |
| 82 | +requested: { |
| 83 | +roles: ["admin", "operator"], |
| 84 | +scopes: ["operator.read"], |
| 85 | +}, |
| 86 | +approved: { |
| 87 | +roles: ["operator"], |
| 88 | +scopes: ["operator.read"], |
| 89 | +}, |
| 90 | +}); |
| 91 | +}); |
| 92 | + |
| 93 | +it("drops a non-string token role without crashing", () => { |
| 94 | +type PairedArg = NonNullable<Parameters<typeof resolvePendingDeviceApprovalState>[1]>; |
| 95 | +expect( |
| 96 | +resolvePendingDeviceApprovalState({ role: "operator", scopes: ["operator.read"] }, { |
| 97 | +roles: ["operator"], |
| 98 | +scopes: ["operator.read"], |
| 99 | +tokens: { t1: { role: 7, revokedAtMs: null } }, |
| 100 | +} as unknown as PairedArg), |
| 101 | +).toEqual({ |
| 102 | +kind: "role-upgrade", |
| 103 | +requested: { |
| 104 | +roles: ["operator"], |
| 105 | +scopes: ["operator.read"], |
| 106 | +}, |
| 107 | +approved: { |
| 108 | +roles: [], |
| 109 | +scopes: ["operator.read"], |
| 110 | +}, |
| 111 | +}); |
| 112 | +}); |
61 | 113 | }); |