diff --git a/src/plugins/csp_configuration_provider/server/provider.test.ts b/src/plugins/csp_configuration_provider/server/provider.test.ts index b5077202622f..09c68115dc09 100644 --- a/src/plugins/csp_configuration_provider/server/provider.test.ts +++ b/src/plugins/csp_configuration_provider/server/provider.test.ts @@ -107,6 +107,32 @@ describe('Provider', () => { ); }); + it('returns empty when value field is missing in opensearch response', async () => { + const cspRules = "frame-ancestors 'self'"; + + opensearchClient.search.mockImplementation(() => { + return opensearchClientMock.createSuccessTransportRequestPromise({ + hits: { + hits: [ + { + _source: { + value_typo: cspRules, + }, + }, + ], + }, + } as SearchResponse); + }); + + const client = new OpenSearchCspClient(opensearchClient); + const getValue = await client.get(INDEX_NAME, INDEX_DOCUMENT_NAME); + + expect(getValue).toBe(''); + expect(opensearchClient.search).toBeCalledWith( + getSearchInput(INDEX_NAME, INDEX_DOCUMENT_NAME) + ); + }); + it('returns empty string when opensearch errors happen', async () => { opensearchClient.search.mockImplementation(() => { return opensearchClientMock.createErrorTransportRequestPromise(new Error(ERROR_MESSAGE)); diff --git a/src/plugins/csp_configuration_provider/server/provider.ts b/src/plugins/csp_configuration_provider/server/provider.ts index 17001aaa5624..1a59ea80209b 100644 --- a/src/plugins/csp_configuration_provider/server/provider.ts +++ b/src/plugins/csp_configuration_provider/server/provider.ts @@ -52,7 +52,7 @@ export class OpenSearchCspClient implements CspClient { body: query, }); - value = data?.body?.hits?.hits[0]?._source?.value; + value = data?.body?.hits?.hits[0]?._source?.value || ''; } catch (e) { // eslint-disable-next-line no-console console.error(