-
Notifications
You must be signed in to change notification settings - Fork 981
/
Copy pathlifecycle_handlers.test.ts
314 lines (255 loc) · 10.7 KB
/
lifecycle_handlers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import {
createCustomHeadersPreResponseHandler,
createVersionCheckPostAuthHandler,
createXsrfPostAuthHandler,
} from './lifecycle_handlers';
import { httpServerMock } from './http_server.mocks';
import { HttpConfig } from './http_config';
import {
OpenSearchDashboardsRequest,
RouteMethod,
OpenSearchDashboardsRouteOptions,
} from './router';
const createConfig = (partial: Partial<HttpConfig>): HttpConfig => partial as HttpConfig;
const forgeRequest = ({
headers = {},
path = '/',
method = 'get',
opensearchDashboardsRouteOptions,
}: Partial<{
headers: Record<string, string>;
path: string;
method: RouteMethod;
opensearchDashboardsRouteOptions: OpenSearchDashboardsRouteOptions;
}>): OpenSearchDashboardsRequest => {
return httpServerMock.createOpenSearchDashboardsRequest({
headers,
path,
method,
opensearchDashboardsRouteOptions,
});
};
describe('xsrf post-auth handler', () => {
let toolkit: ReturnType<typeof httpServerMock.createToolkit>;
let responseFactory: ReturnType<typeof httpServerMock.createLifecycleResponseFactory>;
beforeEach(() => {
toolkit = httpServerMock.createToolkit();
responseFactory = httpServerMock.createLifecycleResponseFactory();
});
describe('non destructive methods', () => {
it('accepts requests without version or xsrf header', () => {
const config = createConfig({ xsrf: { whitelist: [], disableProtection: false } });
const handler = createXsrfPostAuthHandler(config);
const request = forgeRequest({ method: 'get', headers: {} });
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(result).toEqual('next');
});
});
describe('destructive methods', () => {
it('accepts requests with xsrf header', () => {
const config = createConfig({ xsrf: { whitelist: [], disableProtection: false } });
const handler = createXsrfPostAuthHandler(config);
const request = forgeRequest({ method: 'post', headers: { 'osd-xsrf': 'xsrf' } });
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(result).toEqual('next');
});
it('accepts requests with version header', () => {
const config = createConfig({ xsrf: { whitelist: [], disableProtection: false } });
const handler = createXsrfPostAuthHandler(config);
const request = forgeRequest({ method: 'post', headers: { 'osd-version': 'some-version' } });
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(result).toEqual('next');
});
it('returns a bad request if called without xsrf or version header', () => {
const config = createConfig({ xsrf: { whitelist: [], disableProtection: false } });
const handler = createXsrfPostAuthHandler(config);
const request = forgeRequest({ method: 'post' });
responseFactory.badRequest.mockReturnValue('badRequest' as any);
const result = handler(request, responseFactory, toolkit);
expect(toolkit.next).not.toHaveBeenCalled();
expect(responseFactory.badRequest).toHaveBeenCalledTimes(1);
expect(responseFactory.badRequest.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"body": "Request must contain a osd-xsrf header.",
}
`);
expect(result).toEqual('badRequest');
});
it('accepts requests if protection is disabled', () => {
const config = createConfig({ xsrf: { whitelist: [], disableProtection: true } });
const handler = createXsrfPostAuthHandler(config);
const request = forgeRequest({ method: 'post', headers: {} });
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(result).toEqual('next');
});
it('accepts requests if path is whitelisted', () => {
const config = createConfig({
xsrf: { whitelist: ['/some-path'], disableProtection: false },
});
const handler = createXsrfPostAuthHandler(config);
const request = forgeRequest({ method: 'post', headers: {}, path: '/some-path' });
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(result).toEqual('next');
});
it('accepts requests if xsrf protection on a route is disabled', () => {
const config = createConfig({
xsrf: { whitelist: [], disableProtection: false },
});
const handler = createXsrfPostAuthHandler(config);
const request = forgeRequest({
method: 'post',
headers: {},
path: '/some-path',
opensearchDashboardsRouteOptions: {
xsrfRequired: false,
},
});
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(result).toEqual('next');
});
});
});
describe('versionCheck post-auth handler', () => {
let toolkit: ReturnType<typeof httpServerMock.createToolkit>;
let responseFactory: ReturnType<typeof httpServerMock.createLifecycleResponseFactory>;
beforeEach(() => {
toolkit = httpServerMock.createToolkit();
responseFactory = httpServerMock.createLifecycleResponseFactory();
});
it('forward the request to the next interceptor if header matches', () => {
const handler = createVersionCheckPostAuthHandler('actual-version');
const request = forgeRequest({ headers: { 'osd-version': 'actual-version' } });
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(result).toBe('next');
});
it('returns a badRequest error if header does not match', () => {
const handler = createVersionCheckPostAuthHandler('actual-version');
const request = forgeRequest({ headers: { 'osd-version': 'another-version' } });
responseFactory.badRequest.mockReturnValue('badRequest' as any);
const result = handler(request, responseFactory, toolkit);
expect(toolkit.next).not.toHaveBeenCalled();
expect(responseFactory.badRequest).toHaveBeenCalledTimes(1);
expect(responseFactory.badRequest.mock.calls[0][0]).toMatchInlineSnapshot(`
Object {
"body": Object {
"attributes": Object {
"expected": "actual-version",
"got": "another-version",
},
"message": "Browser client is out of date, please refresh the page (\\"osd-version\\" header was \\"another-version\\" but should be \\"actual-version\\")",
},
}
`);
expect(result).toBe('badRequest');
});
it('forward the request to the next interceptor if header is not present', () => {
const handler = createVersionCheckPostAuthHandler('actual-version');
const request = forgeRequest({ headers: {} });
toolkit.next.mockReturnValue('next' as any);
const result = handler(request, responseFactory, toolkit);
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(responseFactory.badRequest).not.toHaveBeenCalled();
expect(result).toBe('next');
});
});
describe('customHeaders pre-response handler', () => {
let toolkit: ReturnType<typeof httpServerMock.createToolkit>;
beforeEach(() => {
toolkit = httpServerMock.createToolkit();
});
it('adds the osd-name header to the response', () => {
const config = createConfig({ name: 'my-server-name' });
const handler = createCustomHeadersPreResponseHandler(config as HttpConfig);
handler({} as any, {} as any, toolkit);
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(toolkit.next).toHaveBeenCalledWith({ headers: { 'osd-name': 'my-server-name' } });
});
it('adds the custom headers defined in the configuration', () => {
const config = createConfig({
name: 'my-server-name',
customResponseHeaders: {
headerA: 'value-A',
headerB: 'value-B',
},
});
const handler = createCustomHeadersPreResponseHandler(config as HttpConfig);
handler({} as any, {} as any, toolkit);
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(toolkit.next).toHaveBeenCalledWith({
headers: {
'osd-name': 'my-server-name',
headerA: 'value-A',
headerB: 'value-B',
},
});
});
it('preserve the osd-name value from server.name if definied in custom headders ', () => {
const config = createConfig({
name: 'my-server-name',
customResponseHeaders: {
'osd-name': 'custom-name',
headerA: 'value-A',
headerB: 'value-B',
},
});
const handler = createCustomHeadersPreResponseHandler(config as HttpConfig);
handler({} as any, {} as any, toolkit);
expect(toolkit.next).toHaveBeenCalledTimes(1);
expect(toolkit.next).toHaveBeenCalledWith({
headers: {
'osd-name': 'my-server-name',
headerA: 'value-A',
headerB: 'value-B',
},
});
});
});