Skip to content

Commit 08ccf0d

Browse files
committed
fix(keyring-sdk): reject negative migration state versions
A negative version passed the upper-bound check and made `steps.slice(version)` use negative-index semantics, silently applying the wrong subset of migration steps instead of throwing. Also fixes a docs formatting issue flagged by CI.
1 parent 6152ce5 commit 08ccf0d

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

packages/keyring-sdk/docs/migrations.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ Versioned state is stored as an envelope:
66

77
```json5
88
{
9-
"version": 1,
10-
"data": { /* Keyring state */ }
9+
version: 1,
10+
data: {
11+
// Keyring state
12+
},
1113
}
1214
```
1315

packages/keyring-sdk/src/migration.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ describe('apply', () => {
270270
'State version 5 is newer than the latest migration version 1',
271271
);
272272
});
273+
274+
it('throws when state version is negative', async () => {
275+
const migrations = createMigrations().add({ migrate: (data) => data });
276+
277+
await expect(migrations.apply({ version: -1, data: {} })).rejects.toThrow(
278+
'State version -1 is invalid; it cannot be negative',
279+
);
280+
});
273281
});
274282

275283
describe('when migrate is async', () => {

packages/keyring-sdk/src/migration.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ async function applySteps<Data extends Json>(
155155
const latestVersion = steps.length;
156156
const { version, data: initialData } = getVersionAndData(state);
157157

158+
if (version < 0) {
159+
throw new Error(
160+
`State version ${version} is invalid; it cannot be negative`,
161+
);
162+
}
163+
158164
if (version > latestVersion) {
159165
throw new Error(
160166
`State version ${version} is newer than the latest migration version ${latestVersion}`,

0 commit comments

Comments
 (0)