@peers-app/peers-sdk 0.10.3 → 0.10.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,13 @@ function signUserObject(user, secretKey) {
19
19
  * @throws Error if signature is invalid or unauthorized
20
20
  */
21
21
  function verifyUserSignature(user, oldUser) {
22
+ if (!user.signature) {
23
+ // Allow unsigned stubs only when no signed record already exists
24
+ if (oldUser?.signature) {
25
+ throw new Error('Cannot overwrite a signed user record with an unsigned one');
26
+ }
27
+ return;
28
+ }
22
29
  (0, keys_1.verifyObjectSignature)(user);
23
30
  const signerPublicKey = (0, keys_1.getPublicKeyFromObjectSignature)(user) ?? '';
24
31
  // New user: signature must match their own public key
@@ -92,9 +92,9 @@ describe('User Permissions Logic', () => {
92
92
  const signedUpdatedUser = (0, user_permissions_1.signUserObject)(updatedUser, userKeys.secretKey);
93
93
  expect(() => (0, user_permissions_1.verifyUserSignature)(signedUpdatedUser, originalUser)).toThrow('Public key changes are not currently supported');
94
94
  });
95
- it('should handle missing signature gracefully', () => {
95
+ it('should allow missing signature when no existing record', () => {
96
96
  const userWithoutSignature = { ...testUser };
97
- expect(() => (0, user_permissions_1.verifyUserSignature)(userWithoutSignature)).toThrow();
97
+ expect(() => (0, user_permissions_1.verifyUserSignature)(userWithoutSignature)).not.toThrow();
98
98
  });
99
99
  });
100
100
  describe('verifyUserSignature - Group Context (With Signature)', () => {
@@ -154,23 +154,51 @@ describe('User Permissions Logic', () => {
154
154
  });
155
155
  });
156
156
  describe('verifyUserSignature - Signature Requirements', () => {
157
- it('should reject updates without signature when signature is required', () => {
157
+ it('should allow unsigned record when no existing record', () => {
158
158
  const userWithoutSignature = { ...testUser };
159
- expect(() => (0, user_permissions_1.verifyUserSignature)(userWithoutSignature, undefined)).toThrow();
159
+ expect(() => (0, user_permissions_1.verifyUserSignature)(userWithoutSignature, undefined)).not.toThrow();
160
160
  });
161
161
  it('should accept valid signature when signature is provided', () => {
162
162
  const signedUser = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
163
163
  expect(() => (0, user_permissions_1.verifyUserSignature)(signedUser, undefined)).not.toThrow();
164
164
  });
165
- it('should reject empty signature when signature is required', () => {
165
+ it('should allow empty signature when no existing record', () => {
166
166
  const userWithEmptySignature = { ...testUser, signature: '' };
167
- expect(() => (0, user_permissions_1.verifyUserSignature)(userWithEmptySignature, undefined)).toThrow();
167
+ expect(() => (0, user_permissions_1.verifyUserSignature)(userWithEmptySignature, undefined)).not.toThrow();
168
168
  });
169
- it('should reject whitespace-only signature when signature is required', () => {
169
+ it('should reject whitespace-only signature (treated as a bad signature, not a stub)', () => {
170
170
  const userWithWhitespaceSignature = { ...testUser, signature: ' ' };
171
171
  expect(() => (0, user_permissions_1.verifyUserSignature)(userWithWhitespaceSignature, undefined)).toThrow();
172
172
  });
173
173
  });
174
+ describe('verifyUserSignature - Unsigned stubs', () => {
175
+ it('should allow unsigned stub when no existing record', () => {
176
+ const stub = { ...testUser, signature: undefined };
177
+ expect(() => (0, user_permissions_1.verifyUserSignature)(stub)).not.toThrow();
178
+ });
179
+ it('should allow unsigned stub to overwrite an existing unsigned stub', () => {
180
+ const existingStub = { ...testUser, signature: undefined };
181
+ const incomingStub = { ...testUser, name: 'Updated Name', signature: undefined };
182
+ expect(() => (0, user_permissions_1.verifyUserSignature)(incomingStub, existingStub)).not.toThrow();
183
+ });
184
+ it('should reject unsigned stub when a signed record already exists', () => {
185
+ const signedExisting = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
186
+ const incomingStub = { ...testUser, signature: undefined };
187
+ expect(() => (0, user_permissions_1.verifyUserSignature)(incomingStub, signedExisting))
188
+ .toThrow('Cannot overwrite a signed user record with an unsigned one');
189
+ });
190
+ it('should reject unsigned stub with different keys when signed record exists', () => {
191
+ const signedExisting = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
192
+ const incomingStub = { ...testUser, publicKey: otherUserKeys.publicKey, signature: undefined };
193
+ expect(() => (0, user_permissions_1.verifyUserSignature)(incomingStub, signedExisting))
194
+ .toThrow('Cannot overwrite a signed user record with an unsigned one');
195
+ });
196
+ it('should allow a signed record to replace an unsigned stub', () => {
197
+ const existingStub = { ...testUser, signature: undefined };
198
+ const signedIncoming = (0, user_permissions_1.signUserObject)(testUser, userKeys.secretKey);
199
+ expect(() => (0, user_permissions_1.verifyUserSignature)(signedIncoming, existingStub)).not.toThrow();
200
+ });
201
+ });
174
202
  describe('edge cases and error handling', () => {
175
203
  it('should handle undefined user gracefully', () => {
176
204
  // @ts-ignore - Testing edge case
@@ -314,6 +314,9 @@ class Connection {
314
314
  await this.emit('reset');
315
315
  const remoteDeviceInfoSigned = await this.emit('requestDeviceInfo');
316
316
  const remoteDeviceInfo = (0, keys_1.openSignedObject)(remoteDeviceInfoSigned);
317
+ if (remoteDeviceInfoSigned.publicKey !== remoteDeviceInfo.publicKey) {
318
+ throw new Error('Device info signing key does not match claimed identity key');
319
+ }
317
320
  const handshake = await this.initiateHandshake(remoteAddress, remoteDeviceInfo);
318
321
  const handshakeResponseBox = await this.emit('completeHandshake', handshake);
319
322
  const handshakeResponse = await this.localDevice.openBoxedAndSignedData(handshakeResponseBox);
@@ -276,4 +276,61 @@ describe(connection_1.Connection, () => {
276
276
  expect(result).toMatch(/Remote device's system clock is too far out of sync/);
277
277
  });
278
278
  it.todo("should handle RPC calls that return undefined");
279
+ // ─── Security: key confusion attack ────────────────────────────────────────
280
+ //
281
+ // openSignedObject verifies the signature using signedObj.publicKey (the
282
+ // envelope key — the ACTUAL signer). Identity is then recorded from
283
+ // contents.publicKey (the CLAIMED key). There is no check that these match.
284
+ //
285
+ // A malicious peer can sign a handshake with their real key KA while
286
+ // claiming victim's publicKey KV in the payload contents. The signature
287
+ // check passes, but the server registers KV as the connected identity.
288
+ //
289
+ // Fix: after openSignedObject succeeds, assert
290
+ // signedHandshake.publicKey === signedHandshake.contents.publicKey
291
+ //
292
+ describe('Security: key confusion — signing key vs claimed identity key', () => {
293
+ it('rejects handshake where signing key (KA) differs from claimed identity key in contents (KV)', async () => {
294
+ const serverAddress = 'http://localhost';
295
+ const { clientSocket, serverSocket } = createTestSocketPair();
296
+ const attackerDevice = new device_1.Device();
297
+ const victimDevice = new device_1.Device(); // identity the attacker wants to impersonate
298
+ const serverDevice = new device_1.Device();
299
+ const attackerConnection = new connection_1.Connection(clientSocket, attackerDevice);
300
+ const serverConnection = new connection_1.Connection(serverSocket, serverDevice, [serverAddress]);
301
+ // Craft a malicious handshake:
302
+ // signedObj.publicKey = KA (attacker's real signing key — used for verification)
303
+ // contents.publicKey = KV (victim's signing key — recorded as identity)
304
+ // contents.userId = victim's userId
305
+ // contents.publicBoxKey = KA_box (attacker's box key, kept so the server
306
+ // boxes its response for the attacker and
307
+ // the attacker can decrypt it to finish the
308
+ // handshake — making the whole flow succeed)
309
+ const originalGetHandshake = attackerDevice.getHandshake.bind(attackerDevice);
310
+ attackerDevice.getHandshake = function (connectionId, addr) {
311
+ const real = originalGetHandshake(connectionId, addr);
312
+ const maliciousContents = {
313
+ ...real.contents, // keep connectionId, timestamp, serverAddress
314
+ userId: victimDevice.userId, // claim victim's userId
315
+ publicKey: victimDevice.publicKey, // claim victim's Ed25519 key (KV)
316
+ // publicBoxKey stays as attacker's own box key (KA_box) so the server
317
+ // encrypts its handshake response for a key the attacker can open
318
+ };
319
+ // Sign with attacker's secret key:
320
+ // signedObj.publicKey = KA (actual signer)
321
+ // contents.publicKey = KV (claimed identity) — DIFFERENT
322
+ return attackerDevice.signObjectWithSecretKey(maliciousContents);
323
+ };
324
+ // The server must reject this — the signing key (KA) does not match the
325
+ // claimed identity key in the payload contents (KV).
326
+ // Server-side errors travel back through the RPC layer as plain objects
327
+ // { error: '...', errorType: 'RPC_ERROR' } rather than Error instances.
328
+ const result = await attackerConnection.doHandshake(serverAddress).catch(err => err);
329
+ const errorMessage = result instanceof Error ? result.message : result?.error ?? String(result);
330
+ expect(errorMessage).toMatch(/signing key does not match claimed identity key/i);
331
+ // Neither side should be left in a verified state
332
+ expect(attackerConnection.verified).toBe(false);
333
+ expect(serverConnection.verified).toBe(false);
334
+ });
335
+ });
279
336
  });
@@ -80,6 +80,9 @@ class Device {
80
80
  handshakeResponse(remoteHandshake, connectionId, thisServerAddress) {
81
81
  try {
82
82
  const deviceInfo = (0, keys_1.openSignedObject)(remoteHandshake);
83
+ if (remoteHandshake.publicKey !== deviceInfo.publicKey) {
84
+ throw new Error('Handshake signing key does not match claimed identity key');
85
+ }
83
86
  if (deviceInfo.connectionId !== connectionId) {
84
87
  throw new Error(`Invalid connectionId ${deviceInfo.connectionId}, expected ${connectionId}`);
85
88
  }
@@ -73,21 +73,18 @@ describe(device_1.Device, () => {
73
73
  const handshake = client.getHandshake(c1.connectionId, 'localhost');
74
74
  expect(() => server.handshakeResponse(handshake, c2.connectionId, 'localhost')).toThrow(/Invalid connectionId/);
75
75
  });
76
- it("should throw when the client can't unbox the data", async () => {
77
- const c1 = {
78
- connectionId: (0, utils_1.newid)(),
79
- };
80
- const c2 = {
81
- connectionId: c1.connectionId,
82
- };
76
+ it("should reject a handshake signed by a different key than the one claimed in the payload (key confusion)", async () => {
77
+ const connectionId = (0, utils_1.newid)();
78
+ // Imposter holds a random secret key but claims the real client's publicKey/publicBoxKey.
79
+ // getHandshake signs with the random key, so signedObj.publicKey != contents.publicKey.
83
80
  const clientImposter = new device_1.Device(userId1, client.deviceId, {
84
81
  secretKey: (0, keys_1.newKeys)().secretKey,
85
82
  publicKey: client.publicKey,
86
83
  publicBoxKey: client.publicBoxKey
87
84
  });
88
- const handshake = clientImposter.getHandshake(c1.connectionId, 'localhost');
89
- const response = server.handshakeResponse(handshake, c2.connectionId, 'localhost');
90
- // expect(() => clientImposter.openBoxWithSecretKey(response)).toThrow(/Message was null or verification failed/);
85
+ const handshake = clientImposter.getHandshake(connectionId, 'localhost');
86
+ expect(() => server.handshakeResponse(handshake, connectionId, 'localhost'))
87
+ .toThrow(/signing key does not match claimed identity key/i);
91
88
  });
92
89
  it("should detect when requests and responses are being forwarded through a different address", async () => {
93
90
  const connToServer = {
@@ -1,4 +1,3 @@
1
- import { UserContext } from "../context";
2
1
  import { IDeviceInfo } from "../data";
3
2
  import { TrustLevel } from "./socket.type";
4
- export declare function getTrustLevelFn(me: Pick<IDeviceInfo, 'userId' | 'publicKey' | 'publicBoxKey'>, serverUrl?: string, injectedUserContext?: UserContext): (deviceInfo: IDeviceInfo, registerNew?: boolean) => Promise<TrustLevel>;
3
+ export declare function getTrustLevelFn(me: Pick<IDeviceInfo, 'userId' | 'publicKey' | 'publicBoxKey'>, serverUrl?: string): (deviceInfo: IDeviceInfo, registerNew?: boolean) => Promise<TrustLevel>;
@@ -4,9 +4,9 @@ exports.getTrustLevelFn = getTrustLevelFn;
4
4
  const context_1 = require("../context");
5
5
  const data_1 = require("../data");
6
6
  const socket_type_1 = require("./socket.type");
7
- function getTrustLevelFn(me, serverUrl, injectedUserContext) {
7
+ function getTrustLevelFn(me, serverUrl) {
8
8
  return async function getTrustLevel(deviceInfo, registerNew) {
9
- const userContext = injectedUserContext ?? await (0, context_1.getUserContext)();
9
+ const userContext = await (0, context_1.getUserContext)();
10
10
  const userDataContext = userContext.userDataContext;
11
11
  // if this is my own device it is trusted
12
12
  if (deviceInfo.userId === me.userId && deviceInfo.publicKey === me.publicKey && deviceInfo.publicBoxKey === me.publicBoxKey) {
@@ -42,34 +42,32 @@ function getTrustLevelFn(me, serverUrl, injectedUserContext) {
42
42
  return device.trustLevel;
43
43
  }
44
44
  // TODO check user trust level, if they are untrusted, return immediately
45
- if (user && device && !(deviceInfo.userId === device.userId && deviceInfo.userId === user.userId && deviceInfo.publicKey === user.publicKey && deviceInfo.publicBoxKey === user.publicBoxKey)) {
46
- // The stored user record has different public keys than what this device is presenting.
47
- //
48
- // By the time getTrustLevel is called the handshake has already been
49
- // cryptographically verified: the connecting device proved ownership of
50
- // deviceInfo.publicKey by signing the IDeviceHandshake with the
51
- // corresponding secret key (verified in Device.handshakeResponse via
52
- // openSignedObject). The stored record is therefore stale — most
53
- // commonly because it was synced from a relay before the first direct
54
- // WebRTC connection and the relay's copy had wrong/empty keys.
55
- //
56
- // Rather than returning Untrusted (which would permanently block this
57
- // device on every reconnect after the first), we refresh the stored
58
- // keys with the verified values. The signature is cleared so a
59
- // subsequent sync of the real signed user record can replace this
60
- // interim version.
61
- if (deviceInfo.userId === device.userId) {
62
- user.publicKey = deviceInfo.publicKey;
63
- user.publicBoxKey = deviceInfo.publicBoxKey;
64
- const { signature: _sig, ...userWithoutSig } = user;
65
- await (0, data_1.Users)(userDataContext).save(userWithoutSig, { restoreIfDeleted: true });
66
- user = userWithoutSig;
67
- }
68
- else {
69
- // userId mismatch — genuinely untrusted
70
- return socket_type_1.TrustLevel.Untrusted;
45
+ // If the user isn't in our personal DB, check all group data contexts.
46
+ // Group data takes a back seat to personal data we never write to those
47
+ // group records here, and we never copy a group-sourced user into the
48
+ // personal DB (that requires an explicit user action).
49
+ let groupUser = null;
50
+ let groupTrustLevel;
51
+ if (!user) {
52
+ for (const [, groupContext] of userContext.groupDataContexts) {
53
+ const found = await (0, data_1.Users)(groupContext).get(deviceInfo.userId);
54
+ if (!found)
55
+ continue;
56
+ groupUser = groupUser ?? found; // all groups sync the same signed record
57
+ const gtl = await (0, data_1.UserTrustLevels)(groupContext).get(deviceInfo.userId);
58
+ if (gtl?.trustLevel !== undefined) {
59
+ groupTrustLevel = groupTrustLevel === undefined
60
+ ? gtl.trustLevel
61
+ : Math.min(groupTrustLevel, gtl.trustLevel);
62
+ }
71
63
  }
72
64
  }
65
+ const effectiveUser = user ?? groupUser;
66
+ if (effectiveUser && device && !(deviceInfo.userId === device.userId && deviceInfo.userId === effectiveUser.userId && deviceInfo.publicKey === effectiveUser.publicKey && deviceInfo.publicBoxKey === effectiveUser.publicBoxKey)) {
67
+ // deviceInfo does not align with local info about user and device, return Untrusted
68
+ // TODO check if user has changed their public keys
69
+ return socket_type_1.TrustLevel.Untrusted;
70
+ }
73
71
  if (userTrustLevel?.trustLevel && userTrustLevel.trustLevel >= socket_type_1.TrustLevel.Trusted && device?.trustLevel && device.trustLevel >= socket_type_1.TrustLevel.Trusted) {
74
72
  device.lastSeen = new Date();
75
73
  await (0, data_1.Devices)(userDataContext).save(device, { restoreIfDeleted: true });
@@ -101,8 +99,10 @@ function getTrustLevelFn(me, serverUrl, injectedUserContext) {
101
99
  }
102
100
  trustLevel = device.trustLevel;
103
101
  let newUser = false;
104
- let staleUserKeys = false;
105
- if (!user) {
102
+ if (!user && !groupUser) {
103
+ // Truly unknown user — create an unsigned stub in the personal DB so
104
+ // subsequent connections can match against it until a signed record
105
+ // arrives via sync.
106
106
  user = {
107
107
  userId: deviceInfo.userId,
108
108
  publicKey: deviceInfo.publicKey,
@@ -112,16 +112,6 @@ function getTrustLevelFn(me, serverUrl, injectedUserContext) {
112
112
  trustLevel = socket_type_1.TrustLevel.NewUser;
113
113
  newUser = true;
114
114
  }
115
- else if (user.publicKey !== deviceInfo.publicKey || user.publicBoxKey !== deviceInfo.publicBoxKey) {
116
- // User record exists but has stale keys (e.g. from a sync that arrived
117
- // before this direct connection). Refresh them with the verified keys
118
- // so subsequent connections don't hit the mismatch check above.
119
- user.publicKey = deviceInfo.publicKey;
120
- user.publicBoxKey = deviceInfo.publicBoxKey;
121
- const { signature: _sig, ...userWithoutSig } = user;
122
- user = userWithoutSig;
123
- staleUserKeys = true;
124
- }
125
115
  // TODO - reimplement this, checks with peers to see if this is an untrusted device
126
116
  // let remoteTrustLevel = TrustLevel.Unknown;
127
117
  // for (const [serverUrl, conn] of Object.entries(getActiveConnections())) {
@@ -157,14 +147,16 @@ function getTrustLevelFn(me, serverUrl, injectedUserContext) {
157
147
  weakInsert: true,
158
148
  });
159
149
  }
160
- else if (staleUserKeys) {
161
- await (0, data_1.Users)(userDataContext).save(user, { restoreIfDeleted: true });
162
- }
163
150
  // device.trustLevel = remoteTrustLevel || trustLevel;
164
151
  await (0, data_1.Devices)(userDataContext).save(device, {
165
152
  restoreIfDeleted: true,
166
153
  weakInsert: true,
167
154
  });
155
+ // If the user was found only in group contexts (not personal), apply the minimum
156
+ // trust level we observed across those groups as a floor on the result.
157
+ if (groupTrustLevel !== undefined) {
158
+ return Math.min(device.trustLevel, groupTrustLevel);
159
+ }
168
160
  return device.trustLevel;
169
161
  };
170
162
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,553 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const get_trust_level_fn_1 = require("./get-trust-level-fn");
4
+ const socket_type_1 = require("./socket.type");
5
+ const utils_1 = require("../utils");
6
+ jest.mock('../context', () => ({
7
+ getUserContext: jest.fn(),
8
+ }));
9
+ jest.mock('../data', () => ({
10
+ Devices: jest.fn(),
11
+ Users: jest.fn(),
12
+ UserTrustLevels: jest.fn(),
13
+ }));
14
+ const context_1 = require("../context");
15
+ const data_1 = require("../data");
16
+ const mockGetUserContext = context_1.getUserContext;
17
+ const mockDevices = data_1.Devices;
18
+ const mockUsers = data_1.Users;
19
+ const mockUserTrustLevels = data_1.UserTrustLevels;
20
+ function makeDeviceTable(deviceRecord = null) {
21
+ return {
22
+ get: jest.fn().mockResolvedValue(deviceRecord),
23
+ save: jest.fn().mockResolvedValue(undefined),
24
+ };
25
+ }
26
+ function makeUsersTable(userRecord = null) {
27
+ return {
28
+ get: jest.fn().mockResolvedValue(userRecord),
29
+ save: jest.fn().mockResolvedValue(undefined),
30
+ };
31
+ }
32
+ function makeTrustTable(trustRecord = null) {
33
+ return {
34
+ get: jest.fn().mockResolvedValue(trustRecord),
35
+ };
36
+ }
37
+ function makeTestIdentity() {
38
+ return {
39
+ userId: (0, utils_1.newid)(),
40
+ publicKey: `pk_${(0, utils_1.newid)()}`,
41
+ publicBoxKey: `pbk_${(0, utils_1.newid)()}`,
42
+ deviceId: (0, utils_1.newid)(),
43
+ };
44
+ }
45
+ function setupMocks(deviceRecord = null, userRecord = null, trustRecord = null) {
46
+ const mockUserDataContext = {};
47
+ mockGetUserContext.mockResolvedValue({
48
+ userDataContext: mockUserDataContext,
49
+ groupDataContexts: new Map(),
50
+ });
51
+ const devTable = makeDeviceTable(deviceRecord);
52
+ const usersTable = makeUsersTable(userRecord);
53
+ const trustTable = makeTrustTable(trustRecord);
54
+ mockDevices.mockReturnValue(devTable);
55
+ mockUsers.mockReturnValue(usersTable);
56
+ mockUserTrustLevels.mockReturnValue(trustTable);
57
+ return { devTable, usersTable, trustTable };
58
+ }
59
+ describe('getTrustLevelFn', () => {
60
+ beforeEach(() => {
61
+ jest.clearAllMocks();
62
+ });
63
+ // ─── Own-device fast path ───────────────────────────────────────────────────
64
+ describe('own device (userId + publicKey + publicBoxKey all match)', () => {
65
+ it('returns NewDevice when device is newly seen (< 2 days old)', async () => {
66
+ const me = makeTestIdentity();
67
+ const deviceInfo = { ...me };
68
+ const recentDate = new Date(Date.now() - 1000 * 60 * 60 * 24); // 1 day ago
69
+ const existingDevice = {
70
+ deviceId: me.deviceId,
71
+ userId: me.userId,
72
+ firstSeen: recentDate,
73
+ lastSeen: recentDate,
74
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
75
+ };
76
+ const { devTable } = setupMocks(existingDevice);
77
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
78
+ const result = await getTrustLevel(deviceInfo);
79
+ expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
80
+ expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.NewDevice }), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
81
+ });
82
+ it('returns Trusted when own device has been seen for more than 2 days', async () => {
83
+ const me = makeTestIdentity();
84
+ const deviceInfo = { ...me };
85
+ const oldDate = new Date(Date.now() - 1000 * 60 * 60 * 24 * 3); // 3 days ago
86
+ const existingDevice = {
87
+ deviceId: me.deviceId,
88
+ userId: me.userId,
89
+ firstSeen: oldDate,
90
+ lastSeen: oldDate,
91
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
92
+ };
93
+ setupMocks(existingDevice);
94
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
95
+ const result = await getTrustLevel(deviceInfo);
96
+ expect(result).toBe(socket_type_1.TrustLevel.Trusted);
97
+ });
98
+ it('creates a new device record if own device has not been seen before', async () => {
99
+ const me = makeTestIdentity();
100
+ const deviceInfo = { ...me };
101
+ const { devTable } = setupMocks(null); // no existing device
102
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me, 'https://myserver.example.com');
103
+ const result = await getTrustLevel(deviceInfo);
104
+ expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
105
+ expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({
106
+ deviceId: me.deviceId,
107
+ userId: me.userId,
108
+ serverUrl: 'https://myserver.example.com',
109
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
110
+ }), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
111
+ });
112
+ it('does not look up Users or UserTrustLevels for own device', async () => {
113
+ const me = makeTestIdentity();
114
+ const deviceInfo = { ...me };
115
+ setupMocks(null);
116
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
117
+ await getTrustLevel(deviceInfo);
118
+ expect(mockUsers).not.toHaveBeenCalled();
119
+ expect(mockUserTrustLevels).not.toHaveBeenCalled();
120
+ });
121
+ it('updates lastSeen on every call for own device', async () => {
122
+ const me = makeTestIdentity();
123
+ const deviceInfo = { ...me };
124
+ const pastDate = new Date(Date.now() - 1000 * 60 * 5); // 5 minutes ago
125
+ const existingDevice = {
126
+ deviceId: me.deviceId,
127
+ userId: me.userId,
128
+ firstSeen: pastDate,
129
+ lastSeen: pastDate,
130
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
131
+ };
132
+ const { devTable } = setupMocks(existingDevice);
133
+ const before = Date.now();
134
+ await (0, get_trust_level_fn_1.getTrustLevelFn)(me)(deviceInfo);
135
+ const after = Date.now();
136
+ const savedDevice = devTable.save.mock.calls[0][0];
137
+ expect(savedDevice.lastSeen.getTime()).toBeGreaterThanOrEqual(before);
138
+ expect(savedDevice.lastSeen.getTime()).toBeLessThanOrEqual(after);
139
+ });
140
+ });
141
+ // ─── Blocked device early exit ──────────────────────────────────────────────
142
+ describe('explicitly blocked device (trustLevel < Unknown)', () => {
143
+ it('returns Untrusted immediately without saving any records', async () => {
144
+ const me = makeTestIdentity();
145
+ const other = makeTestIdentity();
146
+ const blockedDevice = {
147
+ deviceId: other.deviceId,
148
+ userId: other.userId,
149
+ firstSeen: new Date(),
150
+ lastSeen: new Date(),
151
+ trustLevel: socket_type_1.TrustLevel.Untrusted,
152
+ };
153
+ const { devTable } = setupMocks(blockedDevice);
154
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
155
+ const result = await getTrustLevel(other);
156
+ // All three tables are fetched in parallel before the block check
157
+ expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
158
+ // Should not save anything — returns early before any writes
159
+ expect(devTable.save).not.toHaveBeenCalled();
160
+ });
161
+ it('returns Malicious immediately for malicious-flagged devices', async () => {
162
+ const me = makeTestIdentity();
163
+ const other = makeTestIdentity();
164
+ const maliciousDevice = {
165
+ deviceId: other.deviceId,
166
+ userId: other.userId,
167
+ firstSeen: new Date(),
168
+ lastSeen: new Date(),
169
+ trustLevel: socket_type_1.TrustLevel.Malicious,
170
+ };
171
+ setupMocks(maliciousDevice);
172
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
173
+ const result = await getTrustLevel(other);
174
+ expect(result).toBe(socket_type_1.TrustLevel.Malicious);
175
+ });
176
+ });
177
+ // ─── Key-mismatch guard ─────────────────────────────────────────────────────
178
+ describe('key-mismatch guard (user AND device both exist)', () => {
179
+ it('returns Untrusted when publicKey does not match stored user record', async () => {
180
+ const me = makeTestIdentity();
181
+ const other = makeTestIdentity();
182
+ const existingUser = {
183
+ userId: other.userId,
184
+ publicKey: `different_pk_${(0, utils_1.newid)()}`,
185
+ publicBoxKey: other.publicBoxKey,
186
+ name: 'Alice',
187
+ };
188
+ const existingDevice = {
189
+ deviceId: other.deviceId,
190
+ userId: other.userId,
191
+ firstSeen: new Date(),
192
+ lastSeen: new Date(),
193
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
194
+ };
195
+ setupMocks(existingDevice, existingUser);
196
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
197
+ const result = await getTrustLevel(other);
198
+ expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
199
+ });
200
+ it('returns Untrusted when publicBoxKey does not match stored user record', async () => {
201
+ const me = makeTestIdentity();
202
+ const other = makeTestIdentity();
203
+ const existingUser = {
204
+ userId: other.userId,
205
+ publicKey: other.publicKey,
206
+ publicBoxKey: `different_pbk_${(0, utils_1.newid)()}`,
207
+ name: 'Alice',
208
+ };
209
+ const existingDevice = {
210
+ deviceId: other.deviceId,
211
+ userId: other.userId,
212
+ firstSeen: new Date(),
213
+ lastSeen: new Date(),
214
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
215
+ };
216
+ setupMocks(existingDevice, existingUser);
217
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
218
+ const result = await getTrustLevel(other);
219
+ expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
220
+ });
221
+ it('returns Untrusted when device.userId does not match deviceInfo.userId', async () => {
222
+ const me = makeTestIdentity();
223
+ const other = makeTestIdentity();
224
+ const existingUser = {
225
+ userId: other.userId,
226
+ publicKey: other.publicKey,
227
+ publicBoxKey: other.publicBoxKey,
228
+ name: 'Alice',
229
+ };
230
+ const existingDevice = {
231
+ deviceId: other.deviceId,
232
+ userId: (0, utils_1.newid)(), // different userId stored on device record
233
+ firstSeen: new Date(),
234
+ lastSeen: new Date(),
235
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
236
+ };
237
+ setupMocks(existingDevice, existingUser);
238
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
239
+ const result = await getTrustLevel(other);
240
+ expect(result).toBe(socket_type_1.TrustLevel.Untrusted);
241
+ });
242
+ it('does NOT return Untrusted when only device exists (no user record yet)', async () => {
243
+ const me = makeTestIdentity();
244
+ const other = makeTestIdentity();
245
+ const existingDevice = {
246
+ deviceId: other.deviceId,
247
+ userId: other.userId,
248
+ firstSeen: new Date(),
249
+ lastSeen: new Date(),
250
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
251
+ };
252
+ // user record is null — mismatch check is skipped
253
+ setupMocks(existingDevice, null);
254
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
255
+ const result = await getTrustLevel(other);
256
+ expect(result).not.toBe(socket_type_1.TrustLevel.Untrusted);
257
+ });
258
+ it('does NOT return Untrusted when only user exists (no device record yet)', async () => {
259
+ const me = makeTestIdentity();
260
+ const other = makeTestIdentity();
261
+ const existingUser = {
262
+ userId: other.userId,
263
+ publicKey: `different_pk_${(0, utils_1.newid)()}`, // key mismatch present but device is missing
264
+ publicBoxKey: other.publicBoxKey,
265
+ name: 'Alice',
266
+ };
267
+ // device record is null — mismatch check is skipped
268
+ setupMocks(null, existingUser);
269
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
270
+ const result = await getTrustLevel(other);
271
+ expect(result).not.toBe(socket_type_1.TrustLevel.Untrusted);
272
+ });
273
+ });
274
+ // ─── Trusted fast path ──────────────────────────────────────────────────────
275
+ describe('trusted fast path', () => {
276
+ it('returns Trusted immediately when device and userTrustLevel are both >= Trusted', async () => {
277
+ const me = makeTestIdentity();
278
+ const other = makeTestIdentity();
279
+ const existingUser = {
280
+ userId: other.userId,
281
+ publicKey: other.publicKey,
282
+ publicBoxKey: other.publicBoxKey,
283
+ name: 'Alice',
284
+ };
285
+ const existingDevice = {
286
+ deviceId: other.deviceId,
287
+ userId: other.userId,
288
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30),
289
+ lastSeen: new Date(),
290
+ trustLevel: socket_type_1.TrustLevel.Trusted,
291
+ };
292
+ const trustRecord = {
293
+ userId: other.userId,
294
+ trustLevel: socket_type_1.TrustLevel.Trusted,
295
+ };
296
+ const { devTable } = setupMocks(existingDevice, existingUser, trustRecord);
297
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
298
+ const result = await getTrustLevel(other);
299
+ expect(result).toBe(socket_type_1.TrustLevel.Trusted);
300
+ expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.Trusted }), expect.objectContaining({ restoreIfDeleted: true }));
301
+ });
302
+ it('does NOT trigger fast path when device is Trusted but userTrustLevel is not', async () => {
303
+ const me = makeTestIdentity();
304
+ const other = makeTestIdentity();
305
+ const existingUser = {
306
+ userId: other.userId,
307
+ publicKey: other.publicKey,
308
+ publicBoxKey: other.publicBoxKey,
309
+ name: 'Alice',
310
+ };
311
+ const existingDevice = {
312
+ deviceId: other.deviceId,
313
+ userId: other.userId,
314
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 10),
315
+ lastSeen: new Date(),
316
+ trustLevel: socket_type_1.TrustLevel.Trusted,
317
+ };
318
+ const trustRecord = {
319
+ userId: other.userId,
320
+ trustLevel: socket_type_1.TrustLevel.Known, // below Trusted
321
+ };
322
+ const { devTable } = setupMocks(existingDevice, existingUser, trustRecord);
323
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
324
+ await getTrustLevel(other);
325
+ // save was called via the normal path (weakInsert), not the fast path
326
+ expect(devTable.save).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ weakInsert: true }));
327
+ });
328
+ it('does NOT trigger fast path when userTrustLevel is Trusted but device is not', async () => {
329
+ const me = makeTestIdentity();
330
+ const other = makeTestIdentity();
331
+ const existingUser = {
332
+ userId: other.userId,
333
+ publicKey: other.publicKey,
334
+ publicBoxKey: other.publicBoxKey,
335
+ name: 'Alice',
336
+ };
337
+ const existingDevice = {
338
+ deviceId: other.deviceId,
339
+ userId: other.userId,
340
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 10),
341
+ lastSeen: new Date(),
342
+ trustLevel: socket_type_1.TrustLevel.Known, // below Trusted
343
+ };
344
+ const trustRecord = {
345
+ userId: other.userId,
346
+ trustLevel: socket_type_1.TrustLevel.Trusted,
347
+ };
348
+ setupMocks(existingDevice, existingUser, trustRecord);
349
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
350
+ const result = await getTrustLevel(other);
351
+ expect(result).not.toBe(socket_type_1.TrustLevel.Trusted);
352
+ });
353
+ });
354
+ // ─── New user / new device resolution ───────────────────────────────────────
355
+ describe('new user and new device', () => {
356
+ it('returns NewDevice and saves a user stub when both user and device are unseen', async () => {
357
+ const me = makeTestIdentity();
358
+ const other = makeTestIdentity();
359
+ const { devTable, usersTable } = setupMocks(null, null, null);
360
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me, 'https://server.example.com');
361
+ const result = await getTrustLevel(other);
362
+ // The local `trustLevel` variable is set to NewUser (10), but the function returns
363
+ // `device.trustLevel` which remains NewDevice (20). The NewUser assignment was
364
+ // intended for the now-commented-out `device.trustLevel = remoteTrustLevel || trustLevel`.
365
+ expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
366
+ expect(usersTable.save).toHaveBeenCalledWith(expect.objectContaining({
367
+ userId: other.userId,
368
+ publicKey: other.publicKey,
369
+ publicBoxKey: other.publicBoxKey,
370
+ name: 'https://server.example.com',
371
+ }), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
372
+ expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({
373
+ deviceId: other.deviceId,
374
+ userId: other.userId,
375
+ serverUrl: 'https://server.example.com',
376
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
377
+ }), expect.objectContaining({ restoreIfDeleted: true, weakInsert: true }));
378
+ });
379
+ it('uses a fallback name from userId suffix when serverUrl is not provided', async () => {
380
+ const me = makeTestIdentity();
381
+ const other = makeTestIdentity();
382
+ const { usersTable } = setupMocks(null, null, null);
383
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me); // no serverUrl
384
+ await getTrustLevel(other);
385
+ const savedUser = usersTable.save.mock.calls[0][0];
386
+ expect(savedUser.name).toBe(`Unnamed_${other.userId.substring(20)}`);
387
+ });
388
+ it('does not save a user stub when user already exists', async () => {
389
+ const me = makeTestIdentity();
390
+ const other = makeTestIdentity();
391
+ const existingUser = {
392
+ userId: other.userId,
393
+ publicKey: other.publicKey,
394
+ publicBoxKey: other.publicBoxKey,
395
+ name: 'Alice',
396
+ };
397
+ const { usersTable } = setupMocks(null, existingUser, null);
398
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
399
+ await getTrustLevel(other);
400
+ expect(usersTable.save).not.toHaveBeenCalled();
401
+ });
402
+ });
403
+ // ─── Device aging ────────────────────────────────────────────────────────────
404
+ describe('device aging', () => {
405
+ it('returns NewDevice for a device first seen < 7 days ago', async () => {
406
+ const me = makeTestIdentity();
407
+ const other = makeTestIdentity();
408
+ const existingUser = {
409
+ userId: other.userId,
410
+ publicKey: other.publicKey,
411
+ publicBoxKey: other.publicBoxKey,
412
+ name: 'Alice',
413
+ };
414
+ const existingDevice = {
415
+ deviceId: other.deviceId,
416
+ userId: other.userId,
417
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5), // 5 days ago
418
+ lastSeen: new Date(),
419
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
420
+ };
421
+ setupMocks(existingDevice, existingUser, null);
422
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
423
+ const result = await getTrustLevel(other);
424
+ expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
425
+ });
426
+ it('promotes device to Known when first seen > 7 days ago', async () => {
427
+ const me = makeTestIdentity();
428
+ const other = makeTestIdentity();
429
+ const existingUser = {
430
+ userId: other.userId,
431
+ publicKey: other.publicKey,
432
+ publicBoxKey: other.publicBoxKey,
433
+ name: 'Alice',
434
+ };
435
+ const existingDevice = {
436
+ deviceId: other.deviceId,
437
+ userId: other.userId,
438
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 8), // 8 days ago
439
+ lastSeen: new Date(),
440
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
441
+ };
442
+ const { devTable } = setupMocks(existingDevice, existingUser, null);
443
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
444
+ const result = await getTrustLevel(other);
445
+ expect(result).toBe(socket_type_1.TrustLevel.Known);
446
+ expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.Known }), expect.anything());
447
+ });
448
+ it('bumps existing device with Unknown trust level up to NewDevice', async () => {
449
+ const me = makeTestIdentity();
450
+ const other = makeTestIdentity();
451
+ const existingUser = {
452
+ userId: other.userId,
453
+ publicKey: other.publicKey,
454
+ publicBoxKey: other.publicBoxKey,
455
+ name: 'Alice',
456
+ };
457
+ const existingDevice = {
458
+ deviceId: other.deviceId,
459
+ userId: other.userId,
460
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 3), // 3 days, under 7-day threshold
461
+ lastSeen: new Date(),
462
+ trustLevel: socket_type_1.TrustLevel.Unknown,
463
+ };
464
+ const { devTable } = setupMocks(existingDevice, existingUser, null);
465
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
466
+ const result = await getTrustLevel(other);
467
+ expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
468
+ expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({ trustLevel: socket_type_1.TrustLevel.NewDevice }), expect.anything());
469
+ });
470
+ it('does not downgrade a device already at Known when re-evaluated before 7 days', async () => {
471
+ const me = makeTestIdentity();
472
+ const other = makeTestIdentity();
473
+ const existingUser = {
474
+ userId: other.userId,
475
+ publicKey: other.publicKey,
476
+ publicBoxKey: other.publicBoxKey,
477
+ name: 'Alice',
478
+ };
479
+ const existingDevice = {
480
+ deviceId: other.deviceId,
481
+ userId: other.userId,
482
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5), // 5 days — under 7-day threshold
483
+ lastSeen: new Date(),
484
+ trustLevel: socket_type_1.TrustLevel.Known, // manually promoted already
485
+ };
486
+ setupMocks(existingDevice, existingUser, null);
487
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
488
+ const result = await getTrustLevel(other);
489
+ expect(result).toBe(socket_type_1.TrustLevel.Known);
490
+ });
491
+ it('updates lastSeen on the device record each time', async () => {
492
+ const me = makeTestIdentity();
493
+ const other = makeTestIdentity();
494
+ const existingUser = {
495
+ userId: other.userId,
496
+ publicKey: other.publicKey,
497
+ publicBoxKey: other.publicBoxKey,
498
+ name: 'Alice',
499
+ };
500
+ const pastDate = new Date(Date.now() - 1000 * 60 * 60); // 1 hour ago
501
+ const existingDevice = {
502
+ deviceId: other.deviceId,
503
+ userId: other.userId,
504
+ firstSeen: new Date(Date.now() - 1000 * 60 * 60 * 24 * 2),
505
+ lastSeen: pastDate,
506
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
507
+ };
508
+ const { devTable } = setupMocks(existingDevice, existingUser, null);
509
+ const before = Date.now();
510
+ await (0, get_trust_level_fn_1.getTrustLevelFn)(me)(other);
511
+ const after = Date.now();
512
+ const savedDevice = devTable.save.mock.calls[0][0];
513
+ expect(savedDevice.lastSeen.getTime()).toBeGreaterThanOrEqual(before);
514
+ expect(savedDevice.lastSeen.getTime()).toBeLessThanOrEqual(after);
515
+ });
516
+ });
517
+ // ─── Existing user, no device ────────────────────────────────────────────────
518
+ describe('existing user but no device record', () => {
519
+ it('creates a new device record with NewDevice trust level', async () => {
520
+ const me = makeTestIdentity();
521
+ const other = makeTestIdentity();
522
+ const existingUser = {
523
+ userId: other.userId,
524
+ publicKey: other.publicKey,
525
+ publicBoxKey: other.publicBoxKey,
526
+ name: 'Alice',
527
+ };
528
+ const { devTable } = setupMocks(null, existingUser, null);
529
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me, 'https://relay.example.com');
530
+ const result = await getTrustLevel(other);
531
+ expect(result).toBe(socket_type_1.TrustLevel.NewDevice);
532
+ expect(devTable.save).toHaveBeenCalledWith(expect.objectContaining({
533
+ deviceId: other.deviceId,
534
+ userId: other.userId,
535
+ serverUrl: 'https://relay.example.com',
536
+ trustLevel: socket_type_1.TrustLevel.NewDevice,
537
+ }), expect.anything());
538
+ });
539
+ });
540
+ // ─── Parallel DB lookups ────────────────────────────────────────────────────
541
+ describe('parallel DB lookups for non-self devices', () => {
542
+ it('queries Users, Devices and UserTrustLevels in parallel', async () => {
543
+ const me = makeTestIdentity();
544
+ const other = makeTestIdentity();
545
+ setupMocks(null, null, null);
546
+ const getTrustLevel = (0, get_trust_level_fn_1.getTrustLevelFn)(me);
547
+ await getTrustLevel(other);
548
+ expect(mockDevices).toHaveBeenCalled();
549
+ expect(mockUsers).toHaveBeenCalled();
550
+ expect(mockUserTrustLevels).toHaveBeenCalled();
551
+ });
552
+ });
553
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peers-app/peers-sdk",
3
- "version": "0.10.3",
3
+ "version": "0.10.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/peers-app/peers-sdk.git"