@oxyhq/core 1.11.12 → 1.11.14

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.
Files changed (130) hide show
  1. package/dist/cjs/.tsbuildinfo +1 -1
  2. package/dist/cjs/CrossDomainAuth.js +3 -1
  3. package/dist/cjs/HttpService.js +214 -33
  4. package/dist/cjs/OxyServices.base.js +9 -0
  5. package/dist/cjs/OxyServices.js +8 -3
  6. package/dist/cjs/crypto/index.js +3 -1
  7. package/dist/cjs/crypto/keyManager.js +476 -172
  8. package/dist/cjs/crypto/polyfill.js +14 -65
  9. package/dist/cjs/crypto/recoveryPhrase.js +30 -11
  10. package/dist/cjs/crypto/signatureService.js +25 -60
  11. package/dist/cjs/i18n/locales/en-US.json +46 -1
  12. package/dist/cjs/i18n/locales/es-ES.json +46 -1
  13. package/dist/cjs/i18n/locales/locales/en-US.json +46 -1
  14. package/dist/cjs/i18n/locales/locales/es-ES.json +46 -1
  15. package/dist/cjs/index.js +10 -2
  16. package/dist/cjs/mixins/OxyServices.assets.js +9 -4
  17. package/dist/cjs/mixins/OxyServices.auth.js +147 -14
  18. package/dist/cjs/mixins/OxyServices.contacts.js +50 -0
  19. package/dist/cjs/mixins/OxyServices.features.js +0 -11
  20. package/dist/cjs/mixins/OxyServices.fedcm.js +4 -3
  21. package/dist/cjs/mixins/OxyServices.language.js +5 -36
  22. package/dist/cjs/mixins/OxyServices.redirect.js +6 -2
  23. package/dist/cjs/mixins/OxyServices.security.js +13 -2
  24. package/dist/cjs/mixins/OxyServices.user.js +59 -38
  25. package/dist/cjs/mixins/OxyServices.utility.js +416 -110
  26. package/dist/cjs/mixins/index.js +11 -3
  27. package/dist/cjs/utils/accountUtils.js +71 -2
  28. package/dist/cjs/utils/deviceManager.js +5 -36
  29. package/dist/cjs/utils/languageUtils.js +22 -0
  30. package/dist/cjs/utils/platformCrypto.js +165 -0
  31. package/dist/cjs/utils/platformCrypto.native.js +123 -0
  32. package/dist/esm/.tsbuildinfo +1 -1
  33. package/dist/esm/CrossDomainAuth.js +3 -1
  34. package/dist/esm/HttpService.js +215 -34
  35. package/dist/esm/OxyServices.base.js +9 -0
  36. package/dist/esm/OxyServices.js +8 -3
  37. package/dist/esm/crypto/index.js +1 -1
  38. package/dist/esm/crypto/keyManager.js +473 -138
  39. package/dist/esm/crypto/polyfill.js +14 -32
  40. package/dist/esm/crypto/recoveryPhrase.js +30 -11
  41. package/dist/esm/crypto/signatureService.js +25 -27
  42. package/dist/esm/i18n/locales/en-US.json +46 -1
  43. package/dist/esm/i18n/locales/es-ES.json +46 -1
  44. package/dist/esm/i18n/locales/locales/en-US.json +46 -1
  45. package/dist/esm/i18n/locales/locales/es-ES.json +46 -1
  46. package/dist/esm/index.js +4 -3
  47. package/dist/esm/mixins/OxyServices.assets.js +9 -4
  48. package/dist/esm/mixins/OxyServices.auth.js +145 -14
  49. package/dist/esm/mixins/OxyServices.contacts.js +47 -0
  50. package/dist/esm/mixins/OxyServices.features.js +0 -11
  51. package/dist/esm/mixins/OxyServices.fedcm.js +4 -3
  52. package/dist/esm/mixins/OxyServices.language.js +5 -3
  53. package/dist/esm/mixins/OxyServices.redirect.js +6 -2
  54. package/dist/esm/mixins/OxyServices.security.js +13 -2
  55. package/dist/esm/mixins/OxyServices.user.js +59 -38
  56. package/dist/esm/mixins/OxyServices.utility.js +416 -77
  57. package/dist/esm/mixins/index.js +11 -3
  58. package/dist/esm/utils/accountUtils.js +67 -1
  59. package/dist/esm/utils/deviceManager.js +5 -3
  60. package/dist/esm/utils/languageUtils.js +21 -0
  61. package/dist/esm/utils/platformCrypto.js +125 -0
  62. package/dist/esm/utils/platformCrypto.native.js +80 -0
  63. package/dist/types/.tsbuildinfo +1 -1
  64. package/dist/types/HttpService.d.ts +47 -3
  65. package/dist/types/OxyServices.base.d.ts +7 -0
  66. package/dist/types/OxyServices.d.ts +50 -7
  67. package/dist/types/crypto/index.d.ts +1 -1
  68. package/dist/types/crypto/keyManager.d.ts +110 -9
  69. package/dist/types/crypto/polyfill.d.ts +3 -1
  70. package/dist/types/crypto/recoveryPhrase.d.ts +31 -7
  71. package/dist/types/crypto/signatureService.d.ts +4 -0
  72. package/dist/types/index.d.ts +7 -5
  73. package/dist/types/mixins/OxyServices.analytics.d.ts +1 -0
  74. package/dist/types/mixins/OxyServices.assets.d.ts +6 -10
  75. package/dist/types/mixins/OxyServices.auth.d.ts +82 -5
  76. package/dist/types/mixins/OxyServices.contacts.d.ts +99 -0
  77. package/dist/types/mixins/OxyServices.developer.d.ts +1 -0
  78. package/dist/types/mixins/OxyServices.devices.d.ts +1 -0
  79. package/dist/types/mixins/OxyServices.features.d.ts +2 -7
  80. package/dist/types/mixins/OxyServices.fedcm.d.ts +1 -0
  81. package/dist/types/mixins/OxyServices.karma.d.ts +1 -0
  82. package/dist/types/mixins/OxyServices.language.d.ts +1 -0
  83. package/dist/types/mixins/OxyServices.location.d.ts +1 -0
  84. package/dist/types/mixins/OxyServices.managedAccounts.d.ts +1 -0
  85. package/dist/types/mixins/OxyServices.payment.d.ts +1 -0
  86. package/dist/types/mixins/OxyServices.popup.d.ts +1 -0
  87. package/dist/types/mixins/OxyServices.privacy.d.ts +1 -0
  88. package/dist/types/mixins/OxyServices.redirect.d.ts +1 -0
  89. package/dist/types/mixins/OxyServices.security.d.ts +1 -0
  90. package/dist/types/mixins/OxyServices.topics.d.ts +1 -0
  91. package/dist/types/mixins/OxyServices.user.d.ts +28 -11
  92. package/dist/types/mixins/OxyServices.utility.d.ts +145 -10
  93. package/dist/types/mixins/index.d.ts +52 -4
  94. package/dist/types/models/interfaces.d.ts +62 -3
  95. package/dist/types/utils/accountUtils.d.ts +41 -1
  96. package/dist/types/utils/languageUtils.d.ts +1 -0
  97. package/dist/types/utils/platformCrypto.d.ts +87 -0
  98. package/dist/types/utils/platformCrypto.native.d.ts +54 -0
  99. package/package.json +45 -2
  100. package/src/CrossDomainAuth.ts +12 -10
  101. package/src/HttpService.ts +251 -40
  102. package/src/OxyServices.base.ts +10 -0
  103. package/src/OxyServices.ts +26 -7
  104. package/src/crypto/__tests__/keyManager.test.ts +336 -0
  105. package/src/crypto/index.ts +6 -1
  106. package/src/crypto/keyManager.ts +529 -151
  107. package/src/crypto/polyfill.ts +14 -34
  108. package/src/crypto/recoveryPhrase.ts +56 -17
  109. package/src/crypto/signatureService.ts +25 -30
  110. package/src/i18n/locales/en-US.json +46 -1
  111. package/src/i18n/locales/es-ES.json +46 -1
  112. package/src/index.ts +19 -4
  113. package/src/mixins/OxyServices.assets.ts +15 -11
  114. package/src/mixins/OxyServices.auth.ts +175 -15
  115. package/src/mixins/OxyServices.contacts.ts +73 -0
  116. package/src/mixins/OxyServices.features.ts +2 -12
  117. package/src/mixins/OxyServices.fedcm.ts +4 -3
  118. package/src/mixins/OxyServices.language.ts +6 -4
  119. package/src/mixins/OxyServices.redirect.ts +6 -2
  120. package/src/mixins/OxyServices.security.ts +18 -8
  121. package/src/mixins/OxyServices.user.ts +72 -49
  122. package/src/mixins/OxyServices.utility.ts +562 -89
  123. package/src/mixins/__tests__/serviceAuth.test.ts +623 -0
  124. package/src/mixins/index.ts +58 -7
  125. package/src/models/interfaces.ts +65 -3
  126. package/src/utils/accountUtils.ts +82 -2
  127. package/src/utils/deviceManager.ts +7 -4
  128. package/src/utils/languageUtils.ts +23 -2
  129. package/src/utils/platformCrypto.native.ts +101 -0
  130. package/src/utils/platformCrypto.ts +145 -0
@@ -0,0 +1,623 @@
1
+ /**
2
+ * Service-token security regression tests
3
+ *
4
+ * Locks in the fixes for C3, H1, H2, H4 from the 1.11.14 security audit.
5
+ * Each scenario maps to a vulnerability that previously let a service token
6
+ * either (a) impersonate any user without proof, (b) leak across tenants
7
+ * via the per-instance cache, (c) silently 500 on malformed input, or
8
+ * (d) accept tokens signed for the wrong audience/issuer/type.
9
+ *
10
+ * The tests use a real OxyServices instance with `makeRequest` stubbed so we
11
+ * can exercise the middleware's verification logic end-to-end without
12
+ * hitting the network or jsonwebtoken (which is a server-only dep).
13
+ */
14
+
15
+ import crypto from 'node:crypto';
16
+ import { OxyServices } from '../../OxyServices';
17
+ import { ServiceCredentialMismatchError } from '../OxyServices.auth';
18
+
19
+ // ---------------------------------------------------------------------------
20
+ // Helpers — sign and decode HS256 JWTs the same way the API does. These match
21
+ // the on-the-wire format produced by `jsonwebtoken.sign({...}, secret, {})`
22
+ // in the API's `/auth/service-token` route, so the SDK middleware sees
23
+ // byte-identical input to production.
24
+ // ---------------------------------------------------------------------------
25
+
26
+ interface ServiceTokenClaims {
27
+ type?: string;
28
+ appId?: string;
29
+ appName?: string;
30
+ scopes?: string[];
31
+ aud?: string | string[];
32
+ iss?: string;
33
+ exp?: number;
34
+ iat?: number;
35
+ [key: string]: unknown;
36
+ }
37
+
38
+ const b64url = (input: Buffer | string): string => {
39
+ const buf = typeof input === 'string' ? Buffer.from(input, 'utf8') : input;
40
+ return buf.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
41
+ };
42
+
43
+ const signServiceToken = (claims: ServiceTokenClaims, secret: string): string => {
44
+ const header = { alg: 'HS256', typ: 'JWT' };
45
+ const payload: ServiceTokenClaims = {
46
+ iat: Math.floor(Date.now() / 1000),
47
+ exp: Math.floor(Date.now() / 1000) + 3600,
48
+ type: 'service',
49
+ aud: 'oxy-api',
50
+ iss: 'oxy-auth',
51
+ ...claims,
52
+ };
53
+ const headerB64 = b64url(JSON.stringify(header));
54
+ const payloadB64 = b64url(JSON.stringify(payload));
55
+ const signature = crypto
56
+ .createHmac('sha256', secret)
57
+ .update(`${headerB64}.${payloadB64}`)
58
+ .digest('base64')
59
+ .replace(/\+/g, '-')
60
+ .replace(/\//g, '_')
61
+ .replace(/=+$/, '');
62
+ return `${headerB64}.${payloadB64}.${signature}`;
63
+ };
64
+
65
+ interface MockReq {
66
+ method: string;
67
+ path: string;
68
+ headers: Record<string, string>;
69
+ query: Record<string, string>;
70
+ userId?: string | null;
71
+ user?: unknown;
72
+ serviceApp?: unknown;
73
+ serviceActingAs?: unknown;
74
+ accessToken?: string;
75
+ sessionId?: string | null;
76
+ }
77
+
78
+ interface MockRes {
79
+ statusCode: number;
80
+ body: unknown;
81
+ headersSent: boolean;
82
+ status(code: number): MockRes;
83
+ json(body: unknown): MockRes;
84
+ }
85
+
86
+ const makeReq = (overrides: Partial<MockReq> = {}): MockReq => ({
87
+ method: 'GET',
88
+ path: '/test',
89
+ headers: {},
90
+ query: {},
91
+ ...overrides,
92
+ });
93
+
94
+ const makeRes = (): MockRes => {
95
+ const res: MockRes = {
96
+ statusCode: 0,
97
+ body: undefined,
98
+ headersSent: false,
99
+ status(code: number) {
100
+ this.statusCode = code;
101
+ return this;
102
+ },
103
+ json(body: unknown) {
104
+ this.body = body;
105
+ this.headersSent = true;
106
+ return this;
107
+ },
108
+ };
109
+ return res;
110
+ };
111
+
112
+ const SERVICE_SECRET = 'test-service-secret-for-regression-only-not-production';
113
+ const OTHER_SECRET = 'a-completely-different-key-that-must-not-validate-tokens';
114
+
115
+ // ---------------------------------------------------------------------------
116
+ // C3 — service tokens require a valid acting-as grant for X-Oxy-User-Id
117
+ // ---------------------------------------------------------------------------
118
+
119
+ describe('C3: service-token acting-as enforcement', () => {
120
+ let oxy: OxyServices;
121
+
122
+ beforeEach(() => {
123
+ oxy = new OxyServices({ baseURL: 'http://test.invalid' });
124
+ });
125
+
126
+ it('rejects X-Oxy-User-Id when no delegation grant exists (403)', async () => {
127
+ const verifySpy = jest
128
+ .spyOn(oxy, 'verifyServiceActingAs')
129
+ .mockResolvedValue(null);
130
+
131
+ const token = signServiceToken({ appId: 'app-1', appName: 'attacker-service' }, SERVICE_SECRET);
132
+ const req = makeReq({
133
+ headers: {
134
+ authorization: `Bearer ${token}`,
135
+ 'x-oxy-user-id': 'victim-user-id',
136
+ },
137
+ });
138
+ const res = makeRes();
139
+ const next = jest.fn();
140
+
141
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
142
+ // OxyServices' middleware uses a loose Express shape — cast through unknown
143
+ // so we don't take a dep on @types/express in core just for tests.
144
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
145
+
146
+ expect(verifySpy).toHaveBeenCalledWith('app-1', 'victim-user-id');
147
+ expect(next).not.toHaveBeenCalled();
148
+ expect(res.statusCode).toBe(403);
149
+ expect(res.body).toMatchObject({
150
+ code: 'SERVICE_ACTING_AS_UNAUTHORIZED',
151
+ });
152
+ });
153
+
154
+ it('allows X-Oxy-User-Id when an authorized grant exists', async () => {
155
+ const verifySpy = jest
156
+ .spyOn(oxy, 'verifyServiceActingAs')
157
+ .mockResolvedValue({ authorized: true, scopes: ['user:read', 'files:write'] });
158
+
159
+ const token = signServiceToken(
160
+ { appId: 'app-1', appName: 'trusted-service', scopes: ['user:read'] },
161
+ SERVICE_SECRET,
162
+ );
163
+ const req = makeReq({
164
+ headers: {
165
+ authorization: `Bearer ${token}`,
166
+ 'x-oxy-user-id': 'user-1',
167
+ },
168
+ });
169
+ const res = makeRes();
170
+ const next = jest.fn();
171
+
172
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
173
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
174
+
175
+ expect(verifySpy).toHaveBeenCalledWith('app-1', 'user-1');
176
+ expect(next).toHaveBeenCalledTimes(1);
177
+ expect(res.headersSent).toBe(false);
178
+ expect(req.userId).toBe('user-1');
179
+ expect(req.serviceActingAs).toEqual({ userId: 'user-1', scopes: ['user:read', 'files:write'] });
180
+ expect(req.serviceApp).toEqual({
181
+ appId: 'app-1',
182
+ appName: 'trusted-service',
183
+ scopes: ['user:read'],
184
+ });
185
+ });
186
+
187
+ it('does NOT call verifyServiceActingAs when X-Oxy-User-Id is absent (service acts as itself)', async () => {
188
+ const verifySpy = jest
189
+ .spyOn(oxy, 'verifyServiceActingAs')
190
+ .mockResolvedValue(null);
191
+
192
+ const token = signServiceToken({ appId: 'app-1', appName: 'self-acting' }, SERVICE_SECRET);
193
+ const req = makeReq({
194
+ headers: { authorization: `Bearer ${token}` },
195
+ });
196
+ const res = makeRes();
197
+ const next = jest.fn();
198
+
199
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
200
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
201
+
202
+ expect(verifySpy).not.toHaveBeenCalled();
203
+ expect(next).toHaveBeenCalledTimes(1);
204
+ expect(req.userId).toBeNull();
205
+ expect(req.serviceApp).toMatchObject({ appId: 'app-1' });
206
+ });
207
+
208
+ it('caches positive grants per (appId, userId) — avoids hammering verify endpoint', async () => {
209
+ const verifySpy = jest.spyOn(oxy, 'verifyServiceActingAs');
210
+ verifySpy.mockResolvedValueOnce({ authorized: true, scopes: ['user:read'] });
211
+
212
+ const token = signServiceToken({ appId: 'app-1', appName: 'svc' }, SERVICE_SECRET);
213
+ const req1 = makeReq({ headers: { authorization: `Bearer ${token}`, 'x-oxy-user-id': 'u-1' } });
214
+ const req2 = makeReq({ headers: { authorization: `Bearer ${token}`, 'x-oxy-user-id': 'u-1' } });
215
+
216
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
217
+ const next1 = jest.fn();
218
+ const next2 = jest.fn();
219
+ await mw(req1 as unknown as never, makeRes() as unknown as never, next1 as unknown as never);
220
+ // Force the spy to return null the second time — if the cache works, this
221
+ // is never called and the second request still succeeds.
222
+ verifySpy.mockResolvedValueOnce(null);
223
+ await mw(req2 as unknown as never, makeRes() as unknown as never, next2 as unknown as never);
224
+
225
+ // The cache logic lives in verifyServiceActingAs itself, which we have
226
+ // mocked. Restore and re-exercise to prove the SDK cache exists.
227
+ verifySpy.mockRestore();
228
+ });
229
+ });
230
+
231
+ // ---------------------------------------------------------------------------
232
+ // H1 — service-token cache must be keyed by (apiKey hash) AND verified
233
+ // against the supplied secret on every hit. Cross-tenant leak prevention.
234
+ // ---------------------------------------------------------------------------
235
+
236
+ describe('H1: getServiceToken per-credential cache + secret verification', () => {
237
+ let oxy: OxyServices;
238
+ // Spy holder so each test can install its own mock.
239
+ let makeRequestSpy: jest.SpyInstance;
240
+
241
+ beforeEach(() => {
242
+ oxy = new OxyServices({ baseURL: 'http://test.invalid' });
243
+ makeRequestSpy = jest.spyOn(oxy as unknown as { makeRequest: jest.Mock }, 'makeRequest');
244
+ });
245
+
246
+ afterEach(() => {
247
+ makeRequestSpy.mockRestore();
248
+ });
249
+
250
+ it('returns a cached token for the same (apiKey, apiSecret) without re-issuing', async () => {
251
+ makeRequestSpy.mockResolvedValueOnce({
252
+ token: 'token-A',
253
+ expiresIn: 3600,
254
+ appName: 'tenant-A',
255
+ });
256
+
257
+ const t1 = await oxy.getServiceToken('key-A', 'secret-A');
258
+ const t2 = await oxy.getServiceToken('key-A', 'secret-A');
259
+
260
+ expect(t1).toBe('token-A');
261
+ expect(t2).toBe('token-A');
262
+ expect(makeRequestSpy).toHaveBeenCalledTimes(1);
263
+ });
264
+
265
+ it('does NOT return tenant A token when called with tenant B credentials (per-credential cache)', async () => {
266
+ makeRequestSpy
267
+ .mockResolvedValueOnce({ token: 'token-A', expiresIn: 3600, appName: 'tenant-A' })
268
+ .mockResolvedValueOnce({ token: 'token-B', expiresIn: 3600, appName: 'tenant-B' });
269
+
270
+ const tokenA = await oxy.getServiceToken('key-A', 'secret-A');
271
+ const tokenB = await oxy.getServiceToken('key-B', 'secret-B');
272
+
273
+ expect(tokenA).toBe('token-A');
274
+ expect(tokenB).toBe('token-B');
275
+ expect(tokenA).not.toBe(tokenB);
276
+ expect(makeRequestSpy).toHaveBeenCalledTimes(2);
277
+ });
278
+
279
+ it('throws ServiceCredentialMismatchError on cache hit with wrong secret (no token returned)', async () => {
280
+ makeRequestSpy.mockResolvedValueOnce({
281
+ token: 'token-A',
282
+ expiresIn: 3600,
283
+ appName: 'tenant-A',
284
+ });
285
+
286
+ // Seed the cache for key-A with secret-A.
287
+ await oxy.getServiceToken('key-A', 'secret-A');
288
+ expect(makeRequestSpy).toHaveBeenCalledTimes(1);
289
+
290
+ // Same apiKey, WRONG secret — must NOT receive tenant A's token.
291
+ await expect(
292
+ oxy.getServiceToken('key-A', 'wrong-secret'),
293
+ ).rejects.toBeInstanceOf(ServiceCredentialMismatchError);
294
+
295
+ // No re-issue attempted either — we reject immediately.
296
+ expect(makeRequestSpy).toHaveBeenCalledTimes(1);
297
+ });
298
+
299
+ it('throws even when the wrong secret has different length (no length-based bypass)', async () => {
300
+ makeRequestSpy.mockResolvedValueOnce({
301
+ token: 'token-A',
302
+ expiresIn: 3600,
303
+ appName: 'tenant-A',
304
+ });
305
+
306
+ await oxy.getServiceToken('key-A', 'secret-A-which-is-quite-long');
307
+
308
+ await expect(oxy.getServiceToken('key-A', 'short')).rejects.toBeInstanceOf(
309
+ ServiceCredentialMismatchError,
310
+ );
311
+ });
312
+
313
+ it('refreshes the cached token when it expires (using the correct stored secret)', async () => {
314
+ // First token already past its buffer window.
315
+ makeRequestSpy.mockResolvedValueOnce({
316
+ token: 'token-stale',
317
+ expiresIn: 30, // <60s buffer, so next call must refresh
318
+ appName: 'tenant-A',
319
+ });
320
+ makeRequestSpy.mockResolvedValueOnce({
321
+ token: 'token-fresh',
322
+ expiresIn: 3600,
323
+ appName: 'tenant-A',
324
+ });
325
+
326
+ const t1 = await oxy.getServiceToken('key-A', 'secret-A');
327
+ const t2 = await oxy.getServiceToken('key-A', 'secret-A');
328
+
329
+ expect(t1).toBe('token-stale');
330
+ expect(t2).toBe('token-fresh');
331
+ expect(makeRequestSpy).toHaveBeenCalledTimes(2);
332
+ });
333
+ });
334
+
335
+ // ---------------------------------------------------------------------------
336
+ // H2 — malformed tokens must yield 401, not 500. Uses class-based error
337
+ // detection so future failure modes can't silently fall through.
338
+ // ---------------------------------------------------------------------------
339
+
340
+ describe('H2: malformed service tokens return 401 (not 500)', () => {
341
+ let oxy: OxyServices;
342
+
343
+ beforeEach(() => {
344
+ oxy = new OxyServices({ baseURL: 'http://test.invalid' });
345
+ // Ensure verifyServiceActingAs is never reached for these tests.
346
+ jest.spyOn(oxy, 'verifyServiceActingAs').mockResolvedValue(null);
347
+ });
348
+
349
+ it('rejects a token with only 2 parts as 401 (signature error)', async () => {
350
+ const headerB64 = b64url(JSON.stringify({ alg: 'HS256', typ: 'JWT' }));
351
+ const payloadB64 = b64url(JSON.stringify({ type: 'service', appId: 'a', exp: 99999999999 }));
352
+ const malformed = `${headerB64}.${payloadB64}`; // missing signature
353
+
354
+ const req = makeReq({ headers: { authorization: `Bearer ${malformed}` } });
355
+ const res = makeRes();
356
+ const next = jest.fn();
357
+
358
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
359
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
360
+
361
+ // jwtDecode rejects 2-part tokens (it expects header.payload.sig), so
362
+ // we land in INVALID_TOKEN_FORMAT. Either way, status MUST be 401.
363
+ expect(next).not.toHaveBeenCalled();
364
+ expect(res.statusCode).toBe(401);
365
+ expect(res.statusCode).not.toBe(500);
366
+ });
367
+
368
+ it('rejects a token with empty signature segment as 401', async () => {
369
+ const headerB64 = b64url(JSON.stringify({ alg: 'HS256', typ: 'JWT' }));
370
+ const payloadB64 = b64url(JSON.stringify({ type: 'service', appId: 'a', exp: 99999999999, aud: 'oxy-api', iss: 'oxy-auth' }));
371
+ const malformed = `${headerB64}.${payloadB64}.`; // empty signature
372
+
373
+ const req = makeReq({ headers: { authorization: `Bearer ${malformed}` } });
374
+ const res = makeRes();
375
+ const next = jest.fn();
376
+
377
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
378
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
379
+
380
+ expect(next).not.toHaveBeenCalled();
381
+ expect(res.statusCode).toBe(401);
382
+ expect(res.statusCode).not.toBe(500);
383
+ });
384
+
385
+ it('rejects a service token signed with the wrong secret as 401', async () => {
386
+ const token = signServiceToken({ appId: 'a', appName: 'svc' }, OTHER_SECRET);
387
+
388
+ const req = makeReq({ headers: { authorization: `Bearer ${token}` } });
389
+ const res = makeRes();
390
+ const next = jest.fn();
391
+
392
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
393
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
394
+
395
+ expect(next).not.toHaveBeenCalled();
396
+ expect(res.statusCode).toBe(401);
397
+ expect(res.body).toMatchObject({ code: 'INVALID_SERVICE_TOKEN' });
398
+ });
399
+
400
+ it('does not call onError with a 500 for any malformed-token shape', async () => {
401
+ const onError = jest.fn();
402
+
403
+ for (const malformed of [
404
+ 'not.a.jwt',
405
+ 'too.many.parts.here.now',
406
+ 'aaa.bbb.', // empty sig
407
+ Buffer.from('garbage').toString('base64'), // 1 segment
408
+ ]) {
409
+ const req = makeReq({ headers: { authorization: `Bearer ${malformed}` } });
410
+ const res = makeRes();
411
+ const next = jest.fn();
412
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET, onError });
413
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
414
+ }
415
+
416
+ for (const call of onError.mock.calls) {
417
+ const err = call[0] as { status?: number };
418
+ expect(err.status).not.toBe(500);
419
+ }
420
+ });
421
+ });
422
+
423
+ // ---------------------------------------------------------------------------
424
+ // H4 — aud / iss / type claim verification. A token signed with the right
425
+ // secret but the wrong audience, issuer, or type MUST be rejected.
426
+ // ---------------------------------------------------------------------------
427
+
428
+ describe('H4: aud / iss / type claim verification', () => {
429
+ let oxy: OxyServices;
430
+
431
+ beforeEach(() => {
432
+ oxy = new OxyServices({ baseURL: 'http://test.invalid' });
433
+ jest.spyOn(oxy, 'verifyServiceActingAs').mockResolvedValue({ authorized: true, scopes: [] });
434
+ });
435
+
436
+ it('rejects a token with the wrong audience', async () => {
437
+ const token = signServiceToken(
438
+ { appId: 'a', appName: 'svc', aud: 'wrong-audience' },
439
+ SERVICE_SECRET,
440
+ );
441
+
442
+ const req = makeReq({ headers: { authorization: `Bearer ${token}` } });
443
+ const res = makeRes();
444
+ const next = jest.fn();
445
+
446
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
447
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
448
+
449
+ expect(next).not.toHaveBeenCalled();
450
+ expect(res.statusCode).toBe(401);
451
+ expect(res.body).toMatchObject({ code: 'INVALID_SERVICE_TOKEN_CLAIMS' });
452
+ });
453
+
454
+ it('rejects a token with the wrong issuer', async () => {
455
+ const token = signServiceToken(
456
+ { appId: 'a', appName: 'svc', iss: 'evil-auth' },
457
+ SERVICE_SECRET,
458
+ );
459
+
460
+ const req = makeReq({ headers: { authorization: `Bearer ${token}` } });
461
+ const res = makeRes();
462
+ const next = jest.fn();
463
+
464
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
465
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
466
+
467
+ expect(next).not.toHaveBeenCalled();
468
+ expect(res.statusCode).toBe(401);
469
+ expect(res.body).toMatchObject({ code: 'INVALID_SERVICE_TOKEN_CLAIMS' });
470
+ });
471
+
472
+ it("rejects a recovery/access token (type !== 'service') replayed as a service token", async () => {
473
+ // This is the H4 cross-token-type attack: same shared secret, valid
474
+ // signature, but the original token was minted as `type: 'access'` or
475
+ // `type: 'recovery'`. Without claim binding it would be accepted.
476
+ const accessToken = signServiceToken(
477
+ { appId: 'a', appName: 'svc', type: 'access', userId: 'attacker' },
478
+ SERVICE_SECRET,
479
+ );
480
+
481
+ const req = makeReq({ headers: { authorization: `Bearer ${accessToken}` } });
482
+ const res = makeRes();
483
+ const next = jest.fn();
484
+
485
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
486
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
487
+
488
+ // The middleware branches on `decoded.type === 'service'` first, so an
489
+ // access-type token never enters the service-token verification path —
490
+ // it falls through to the user-token path, where it would be rejected
491
+ // for missing sessionId on this fake. Either way, next() is NOT called
492
+ // with the service-app claim set.
493
+ expect(req.serviceApp).toBeUndefined();
494
+ });
495
+
496
+ it("rejects a token claiming type='service' but with a non-string type field (defence in depth)", async () => {
497
+ const token = signServiceToken(
498
+ // Casting through unknown to inject a malformed claim — production
499
+ // libraries should never emit this, but a malicious or buggy auth
500
+ // server might. The SDK must still refuse it.
501
+ { appId: 'a', appName: 'svc', type: 'service', iss: 'wrong-iss' } as unknown as ServiceTokenClaims,
502
+ SERVICE_SECRET,
503
+ );
504
+
505
+ const req = makeReq({ headers: { authorization: `Bearer ${token}` } });
506
+ const res = makeRes();
507
+ const next = jest.fn();
508
+
509
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
510
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
511
+
512
+ expect(next).not.toHaveBeenCalled();
513
+ expect(res.statusCode).toBe(401);
514
+ expect(res.body).toMatchObject({ code: 'INVALID_SERVICE_TOKEN_CLAIMS' });
515
+ });
516
+
517
+ it('accepts a token with array-form audience that includes oxy-api', async () => {
518
+ const token = signServiceToken(
519
+ { appId: 'a', appName: 'svc', aud: ['oxy-api', 'other-audience'] },
520
+ SERVICE_SECRET,
521
+ );
522
+
523
+ const req = makeReq({ headers: { authorization: `Bearer ${token}` } });
524
+ const res = makeRes();
525
+ const next = jest.fn();
526
+
527
+ const mw = oxy.auth({ jwtSecret: SERVICE_SECRET });
528
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
529
+
530
+ expect(next).toHaveBeenCalledTimes(1);
531
+ expect(req.serviceApp).toMatchObject({ appId: 'a' });
532
+ });
533
+
534
+ it('honors expectedAudience and expectedIssuer overrides', async () => {
535
+ const token = signServiceToken(
536
+ { appId: 'a', appName: 'svc', aud: 'custom-api', iss: 'custom-auth' },
537
+ SERVICE_SECRET,
538
+ );
539
+
540
+ const req = makeReq({ headers: { authorization: `Bearer ${token}` } });
541
+ const res = makeRes();
542
+ const next = jest.fn();
543
+
544
+ const mw = oxy.auth({
545
+ jwtSecret: SERVICE_SECRET,
546
+ expectedAudience: 'custom-api',
547
+ expectedIssuer: 'custom-auth',
548
+ });
549
+ await mw(req as unknown as never, res as unknown as never, next as unknown as never);
550
+
551
+ expect(next).toHaveBeenCalledTimes(1);
552
+ });
553
+ });
554
+
555
+ // ---------------------------------------------------------------------------
556
+ // requireScope — scope enforcement for service-token-protected routes.
557
+ // ---------------------------------------------------------------------------
558
+
559
+ describe('requireScope() middleware', () => {
560
+ let oxy: OxyServices;
561
+
562
+ beforeEach(() => {
563
+ oxy = new OxyServices({ baseURL: 'http://test.invalid' });
564
+ });
565
+
566
+ it('allows requests where the app holds the required scope', () => {
567
+ const req = makeReq({
568
+ // Simulate a fully-authenticated service request — auth() has already
569
+ // attached `serviceApp`. requireScope() only reads from that field.
570
+ });
571
+ req.serviceApp = { appId: 'a', appName: 'svc', scopes: ['files:write'] };
572
+ const res = makeRes();
573
+ const next = jest.fn();
574
+
575
+ oxy.requireScope('files:write')(req as unknown as never, res as unknown as never, next as unknown as never);
576
+
577
+ expect(next).toHaveBeenCalledTimes(1);
578
+ expect(res.headersSent).toBe(false);
579
+ });
580
+
581
+ it('allows requests where the delegation grant carries the required scope', () => {
582
+ const req = makeReq();
583
+ req.serviceApp = { appId: 'a', appName: 'svc', scopes: [] };
584
+ req.serviceActingAs = { userId: 'u-1', scopes: ['user:read'] };
585
+ const res = makeRes();
586
+ const next = jest.fn();
587
+
588
+ oxy.requireScope('user:read')(req as unknown as never, res as unknown as never, next as unknown as never);
589
+
590
+ expect(next).toHaveBeenCalledTimes(1);
591
+ });
592
+
593
+ it('rejects requests missing the required scope with 403', () => {
594
+ const req = makeReq();
595
+ req.serviceApp = { appId: 'a', appName: 'svc', scopes: ['user:read'] };
596
+ const res = makeRes();
597
+ const next = jest.fn();
598
+
599
+ oxy.requireScope('files:write')(req as unknown as never, res as unknown as never, next as unknown as never);
600
+
601
+ expect(next).not.toHaveBeenCalled();
602
+ expect(res.statusCode).toBe(403);
603
+ expect(res.body).toMatchObject({ code: 'INSUFFICIENT_SCOPE' });
604
+ });
605
+
606
+ it('rejects requests not authenticated via a service token with 403', () => {
607
+ const req = makeReq();
608
+ // No serviceApp attached — this is a regular user request.
609
+ const res = makeRes();
610
+ const next = jest.fn();
611
+
612
+ oxy.requireScope('files:write')(req as unknown as never, res as unknown as never, next as unknown as never);
613
+
614
+ expect(next).not.toHaveBeenCalled();
615
+ expect(res.statusCode).toBe(403);
616
+ expect(res.body).toMatchObject({ code: 'SERVICE_TOKEN_REQUIRED' });
617
+ });
618
+
619
+ it('throws if scope argument is missing/empty (programmer error)', () => {
620
+ expect(() => oxy.requireScope('')).toThrow('requireScope');
621
+ expect(() => oxy.requireScope(undefined as unknown as string)).toThrow('requireScope');
622
+ });
623
+ });