@oxyhq/core 1.11.13 → 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.
- package/dist/cjs/.tsbuildinfo +1 -1
- package/dist/cjs/index.js +5 -2
- package/dist/cjs/mixins/OxyServices.auth.js +133 -27
- package/dist/cjs/mixins/OxyServices.utility.js +405 -75
- package/dist/cjs/utils/languageUtils.js +22 -0
- package/dist/esm/.tsbuildinfo +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/mixins/OxyServices.auth.js +131 -27
- package/dist/esm/mixins/OxyServices.utility.js +405 -75
- package/dist/esm/utils/languageUtils.js +21 -0
- package/dist/types/.tsbuildinfo +1 -1
- package/dist/types/OxyServices.d.ts +14 -4
- package/dist/types/index.d.ts +3 -2
- package/dist/types/mixins/OxyServices.auth.d.ts +72 -11
- package/dist/types/mixins/OxyServices.utility.d.ts +144 -10
- package/dist/types/utils/languageUtils.d.ts +1 -0
- package/package.json +18 -2
- package/src/OxyServices.ts +17 -3
- package/src/index.ts +3 -1
- package/src/mixins/OxyServices.auth.ts +160 -28
- package/src/mixins/OxyServices.utility.ts +551 -87
- package/src/mixins/__tests__/serviceAuth.test.ts +623 -0
- package/src/utils/languageUtils.ts +23 -2
|
@@ -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
|
+
});
|
|
@@ -154,7 +154,7 @@ export function getNativeLanguageName(languageCode: string | null | undefined):
|
|
|
154
154
|
export function normalizeLanguageCode(lang?: string | null): string {
|
|
155
155
|
if (!lang) return FALLBACK_LANGUAGE;
|
|
156
156
|
if (lang.includes('-')) return lang;
|
|
157
|
-
|
|
157
|
+
|
|
158
158
|
const map: Record<string, string> = {
|
|
159
159
|
en: 'en-US',
|
|
160
160
|
es: 'es-ES',
|
|
@@ -168,7 +168,28 @@ export function normalizeLanguageCode(lang?: string | null): string {
|
|
|
168
168
|
zh: 'zh-CN',
|
|
169
169
|
ar: 'ar-SA',
|
|
170
170
|
};
|
|
171
|
-
|
|
171
|
+
|
|
172
172
|
return map[lang] || lang;
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
/**
|
|
176
|
+
* RTL language detection.
|
|
177
|
+
*
|
|
178
|
+
* Returns `true` when the given BCP-47 tag or bare language code is one
|
|
179
|
+
* of the right-to-left scripts we ship UI for. Apps use this to drive
|
|
180
|
+
* `I18nManager.allowRTL(true)` / `forceRTL(...)` on React Native and the
|
|
181
|
+
* `<html dir="rtl">` attribute on web.
|
|
182
|
+
*
|
|
183
|
+
* Includes Arabic (`ar`), Hebrew (`he` / legacy `iw`), Persian (`fa`),
|
|
184
|
+
* Urdu (`ur`) plus their common region variants. Unknown tags are
|
|
185
|
+
* treated as LTR.
|
|
186
|
+
*/
|
|
187
|
+
const RTL_LANGUAGE_BASES = new Set(['ar', 'he', 'iw', 'fa', 'ur']);
|
|
188
|
+
|
|
189
|
+
export function isRTLLocale(locale?: string | null): boolean {
|
|
190
|
+
if (!locale) return false;
|
|
191
|
+
const base = locale.toLowerCase().split('-')[0];
|
|
192
|
+
if (!base) return false;
|
|
193
|
+
return RTL_LANGUAGE_BASES.has(base);
|
|
194
|
+
}
|
|
195
|
+
|