|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { coreMock, httpServerMock } from '../../../core/server/mocks'; |
| 7 | +import { createCspRulesPreResponseHandler } from './csp_handlers'; |
| 8 | +import { MockedLogger, loggerMock } from '@osd/logging/target/mocks'; |
| 9 | + |
| 10 | +const ERROR_MESSAGE = 'Service unavailable'; |
| 11 | + |
| 12 | +describe('CSP handlers', () => { |
| 13 | + let toolkit: ReturnType<typeof httpServerMock.createToolkit>; |
| 14 | + let logger: MockedLogger; |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + toolkit = httpServerMock.createToolkit(); |
| 18 | + logger = loggerMock.create(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('adds the CSP headers provided by the client', async () => { |
| 22 | + const coreSetup = coreMock.createSetup(); |
| 23 | + const cspRulesFromIndex = "frame-ancestors 'self'"; |
| 24 | + const cspRulesFromYML = "script-src 'unsafe-eval' 'self'"; |
| 25 | + |
| 26 | + const configurationClient = { |
| 27 | + getEntityConfig: jest.fn().mockReturnValue(cspRulesFromIndex), |
| 28 | + }; |
| 29 | + |
| 30 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 31 | + |
| 32 | + const handler = createCspRulesPreResponseHandler( |
| 33 | + coreSetup, |
| 34 | + cspRulesFromYML, |
| 35 | + getConfigurationClient, |
| 36 | + logger |
| 37 | + ); |
| 38 | + const request = { |
| 39 | + method: 'get', |
| 40 | + headers: { 'sec-fetch-dest': 'document' }, |
| 41 | + }; |
| 42 | + |
| 43 | + toolkit.next.mockReturnValue('next' as any); |
| 44 | + |
| 45 | + const result = await handler(request, {} as any, toolkit); |
| 46 | + |
| 47 | + expect(result).toEqual('next'); |
| 48 | + |
| 49 | + expect(toolkit.next).toHaveBeenCalledTimes(1); |
| 50 | + |
| 51 | + expect(toolkit.next).toHaveBeenCalledWith({ |
| 52 | + headers: { |
| 53 | + 'content-security-policy': cspRulesFromIndex, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + expect(configurationClient.getEntityConfig).toBeCalledTimes(1); |
| 58 | + }); |
| 59 | + |
| 60 | + it('do not add CSP headers when the client returns empty and CSP from YML already has frame-ancestors', async () => { |
| 61 | + const coreSetup = coreMock.createSetup(); |
| 62 | + const emptyCspRules = ''; |
| 63 | + const cspRulesFromYML = "script-src 'unsafe-eval' 'self'; frame-ancestors 'self'"; |
| 64 | + |
| 65 | + const configurationClient = { |
| 66 | + getEntityConfig: jest.fn().mockReturnValue(emptyCspRules), |
| 67 | + }; |
| 68 | + |
| 69 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 70 | + |
| 71 | + const handler = createCspRulesPreResponseHandler( |
| 72 | + coreSetup, |
| 73 | + cspRulesFromYML, |
| 74 | + getConfigurationClient, |
| 75 | + logger |
| 76 | + ); |
| 77 | + const request = { |
| 78 | + method: 'get', |
| 79 | + headers: { 'sec-fetch-dest': 'document' }, |
| 80 | + }; |
| 81 | + |
| 82 | + toolkit.next.mockReturnValue('next' as any); |
| 83 | + |
| 84 | + const result = await handler(request, {} as any, toolkit); |
| 85 | + |
| 86 | + expect(result).toEqual('next'); |
| 87 | + |
| 88 | + expect(toolkit.next).toHaveBeenCalledTimes(1); |
| 89 | + expect(toolkit.next).toHaveBeenCalledWith({}); |
| 90 | + |
| 91 | + expect(configurationClient.getEntityConfig).toBeCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('add frame-ancestors CSP headers when the client returns empty and CSP from YML has no frame-ancestors', async () => { |
| 95 | + const coreSetup = coreMock.createSetup(); |
| 96 | + const emptyCspRules = ''; |
| 97 | + const cspRulesFromYML = "script-src 'unsafe-eval' 'self'"; |
| 98 | + |
| 99 | + const configurationClient = { |
| 100 | + getEntityConfig: jest.fn().mockReturnValue(emptyCspRules), |
| 101 | + }; |
| 102 | + |
| 103 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 104 | + |
| 105 | + const handler = createCspRulesPreResponseHandler( |
| 106 | + coreSetup, |
| 107 | + cspRulesFromYML, |
| 108 | + getConfigurationClient, |
| 109 | + logger |
| 110 | + ); |
| 111 | + |
| 112 | + const request = { |
| 113 | + method: 'get', |
| 114 | + headers: { 'sec-fetch-dest': 'document' }, |
| 115 | + }; |
| 116 | + |
| 117 | + toolkit.next.mockReturnValue('next' as any); |
| 118 | + |
| 119 | + const result = await handler(request, {} as any, toolkit); |
| 120 | + |
| 121 | + expect(result).toEqual('next'); |
| 122 | + |
| 123 | + expect(toolkit.next).toHaveBeenCalledTimes(1); |
| 124 | + expect(toolkit.next).toHaveBeenCalledWith({ |
| 125 | + headers: { |
| 126 | + 'content-security-policy': "frame-ancestors 'self'; " + cspRulesFromYML, |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + expect(configurationClient.getEntityConfig).toBeCalledTimes(1); |
| 131 | + }); |
| 132 | + |
| 133 | + it('do not add CSP headers when the configuration does not exist and CSP from YML already has frame-ancestors', async () => { |
| 134 | + const coreSetup = coreMock.createSetup(); |
| 135 | + const cspRulesFromYML = "script-src 'unsafe-eval' 'self'; frame-ancestors 'self'"; |
| 136 | + |
| 137 | + const configurationClient = { |
| 138 | + getEntityConfig: jest.fn().mockImplementation(() => { |
| 139 | + throw new Error(ERROR_MESSAGE); |
| 140 | + }), |
| 141 | + }; |
| 142 | + |
| 143 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 144 | + |
| 145 | + const handler = createCspRulesPreResponseHandler( |
| 146 | + coreSetup, |
| 147 | + cspRulesFromYML, |
| 148 | + getConfigurationClient, |
| 149 | + logger |
| 150 | + ); |
| 151 | + |
| 152 | + const request = { |
| 153 | + method: 'get', |
| 154 | + headers: { 'sec-fetch-dest': 'document' }, |
| 155 | + }; |
| 156 | + |
| 157 | + toolkit.next.mockReturnValue('next' as any); |
| 158 | + |
| 159 | + const result = await handler(request, {} as any, toolkit); |
| 160 | + |
| 161 | + expect(result).toEqual('next'); |
| 162 | + |
| 163 | + expect(toolkit.next).toBeCalledTimes(1); |
| 164 | + expect(toolkit.next).toBeCalledWith({}); |
| 165 | + |
| 166 | + expect(configurationClient.getEntityConfig).toBeCalledTimes(1); |
| 167 | + }); |
| 168 | + |
| 169 | + it('add frame-ancestors CSP headers when the configuration does not exist and CSP from YML has no frame-ancestors', async () => { |
| 170 | + const coreSetup = coreMock.createSetup(); |
| 171 | + const cspRulesFromYML = "script-src 'unsafe-eval' 'self'"; |
| 172 | + |
| 173 | + const configurationClient = { |
| 174 | + getEntityConfig: jest.fn().mockImplementation(() => { |
| 175 | + throw new Error(ERROR_MESSAGE); |
| 176 | + }), |
| 177 | + }; |
| 178 | + |
| 179 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 180 | + |
| 181 | + const handler = createCspRulesPreResponseHandler( |
| 182 | + coreSetup, |
| 183 | + cspRulesFromYML, |
| 184 | + getConfigurationClient, |
| 185 | + logger |
| 186 | + ); |
| 187 | + const request = { method: 'get', headers: { 'sec-fetch-dest': 'document' } }; |
| 188 | + |
| 189 | + toolkit.next.mockReturnValue('next' as any); |
| 190 | + |
| 191 | + const result = await handler(request, {} as any, toolkit); |
| 192 | + |
| 193 | + expect(result).toEqual('next'); |
| 194 | + |
| 195 | + expect(toolkit.next).toBeCalledTimes(1); |
| 196 | + expect(toolkit.next).toBeCalledWith({ |
| 197 | + headers: { |
| 198 | + 'content-security-policy': "frame-ancestors 'self'; " + cspRulesFromYML, |
| 199 | + }, |
| 200 | + }); |
| 201 | + |
| 202 | + expect(configurationClient.getEntityConfig).toBeCalledTimes(1); |
| 203 | + }); |
| 204 | + |
| 205 | + it('do not add CSP headers when request dest exists and shall skip', async () => { |
| 206 | + const coreSetup = coreMock.createSetup(); |
| 207 | + const cspRulesFromYML = "script-src 'unsafe-eval' 'self'"; |
| 208 | + |
| 209 | + const configurationClient = { |
| 210 | + getEntityConfig: jest.fn(), |
| 211 | + }; |
| 212 | + |
| 213 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 214 | + |
| 215 | + const handler = createCspRulesPreResponseHandler( |
| 216 | + coreSetup, |
| 217 | + cspRulesFromYML, |
| 218 | + getConfigurationClient, |
| 219 | + logger |
| 220 | + ); |
| 221 | + |
| 222 | + const cssSecFetchDest = 'css'; |
| 223 | + const request = { |
| 224 | + method: 'get', |
| 225 | + headers: { 'sec-fetch-dest': cssSecFetchDest }, |
| 226 | + }; |
| 227 | + |
| 228 | + toolkit.next.mockReturnValue('next' as any); |
| 229 | + |
| 230 | + const result = await handler(request, {} as any, toolkit); |
| 231 | + |
| 232 | + expect(result).toEqual('next'); |
| 233 | + |
| 234 | + expect(toolkit.next).toBeCalledTimes(1); |
| 235 | + expect(toolkit.next).toBeCalledWith({}); |
| 236 | + |
| 237 | + expect(configurationClient.getEntityConfig).toBeCalledTimes(0); |
| 238 | + }); |
| 239 | + |
| 240 | + it('do not add CSP headers when request dest does not exist', async () => { |
| 241 | + const coreSetup = coreMock.createSetup(); |
| 242 | + const cspRulesFromYML = "script-src 'unsafe-eval' 'self'"; |
| 243 | + |
| 244 | + const configurationClient = { |
| 245 | + getEntityConfig: jest.fn(), |
| 246 | + }; |
| 247 | + |
| 248 | + const getConfigurationClient = jest.fn().mockReturnValue(configurationClient); |
| 249 | + |
| 250 | + const handler = createCspRulesPreResponseHandler( |
| 251 | + coreSetup, |
| 252 | + cspRulesFromYML, |
| 253 | + getConfigurationClient, |
| 254 | + logger |
| 255 | + ); |
| 256 | + |
| 257 | + const request = { |
| 258 | + method: 'get', |
| 259 | + headers: {}, |
| 260 | + }; |
| 261 | + |
| 262 | + toolkit.next.mockReturnValue('next' as any); |
| 263 | + |
| 264 | + const result = await handler(request, {} as any, toolkit); |
| 265 | + |
| 266 | + expect(result).toEqual('next'); |
| 267 | + |
| 268 | + expect(toolkit.next).toBeCalledTimes(1); |
| 269 | + expect(toolkit.next).toBeCalledWith({}); |
| 270 | + |
| 271 | + expect(configurationClient.getEntityConfig).toBeCalledTimes(0); |
| 272 | + }); |
| 273 | +}); |
0 commit comments