|
7 | 7 | applyUpdate, |
8 | 8 | deviceScope, |
9 | 9 | divergentDevices, |
| 10 | + isValidScope, |
10 | 11 | mergeRemote, |
11 | 12 | projectScope, |
12 | 13 | readSyncState, |
@@ -37,6 +38,47 @@ describe('config sync (settings layer)', () => { |
37 | 38 | expect(s.entries[deviceScope('B')].config).toEqual({ shard: [0, 5] }); |
38 | 39 | }); |
39 | 40 |
|
| 41 | + it('validates scopes: project / device:<id> / secrets, rejects junk', () => { |
| 42 | + expect(isValidScope('project')).toBe(true); |
| 43 | + expect(isValidScope('device:abc123')).toBe(true); |
| 44 | + expect(isValidScope('secrets')).toBe(true); |
| 45 | + expect(isValidScope('secret:beyond')).toBe(true); |
| 46 | + expect(isValidScope('secrets:beyond')).toBe(true); |
| 47 | + // junk |
| 48 | + expect(isValidScope('')).toBe(false); |
| 49 | + expect(isValidScope('device:')).toBe(false); |
| 50 | + expect(isValidScope('haxor')).toBe(false); |
| 51 | + expect(isValidScope('__proto__')).toBe(false); |
| 52 | + expect(isValidScope(42)).toBe(false); |
| 53 | + expect(isValidScope(null)).toBe(false); |
| 54 | + }); |
| 55 | + |
| 56 | + it('rejects a write with an invalid scope', () => { |
| 57 | + const p = paths(); |
| 58 | + expect(() => |
| 59 | + applyUpdate(p, 'demo', { scope: 'garbage', config: { x: 1 }, deviceId: 'A' }) |
| 60 | + ).toThrow(/Invalid sync scope/); |
| 61 | + // nothing was persisted |
| 62 | + expect(readSyncState(p, 'demo').revision).toBe(0); |
| 63 | + }); |
| 64 | + |
| 65 | + it('mergeRemote drops entries with invalid scopes', () => { |
| 66 | + const p = paths(); |
| 67 | + const remote: SyncState = { |
| 68 | + version: 1, |
| 69 | + revision: 3, |
| 70 | + entries: { |
| 71 | + [projectScope()]: { scope: projectScope(), config: { ok: true }, revision: 1, updatedAt: '2026-06-01T00:00:00Z', deviceId: 'B' }, |
| 72 | + haxor: { scope: 'haxor', config: { evil: true }, revision: 3, updatedAt: '2026-06-01T00:00:00Z', deviceId: 'B' } |
| 73 | + }, |
| 74 | + acks: {} |
| 75 | + }; |
| 76 | + const { state, changed } = mergeRemote(p, 'demo', remote); |
| 77 | + expect(changed).toBe(true); |
| 78 | + expect(state.entries[projectScope()].config).toEqual({ ok: true }); |
| 79 | + expect(state.entries.haxor).toBeUndefined(); |
| 80 | + }); |
| 81 | + |
40 | 82 | it('records the author as having acked its own write', () => { |
41 | 83 | const p = paths(); |
42 | 84 | applyUpdate(p, 'demo', { scope: projectScope(), config: {}, deviceId: 'A' }); |
|
0 commit comments