From 8a01850a4365ceec673b391f13ca985244da054d Mon Sep 17 00:00:00 2001 From: Khristinin Nikita Date: Thu, 9 Sep 2021 15:47:54 +0200 Subject: [PATCH 1/5] Decode fileName when creating a list --- .../write_lines_to_bulk_list_items.test.ts | 19 +++++++++++++++++-- .../items/write_lines_to_bulk_list_items.ts | 8 ++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts index c3856fde9b2c3..173f250d5c4d6 100644 --- a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts +++ b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts @@ -6,6 +6,7 @@ */ import { getListItemResponseMock } from '../../../common/schemas/response/list_item_schema.mock'; +import { createListIfItDoesNotExist } from '../lists/create_list_if_it_does_not_exist'; import { LinesResult, @@ -16,13 +17,16 @@ import { getImportListItemsToStreamOptionsMock, getWriteBufferToItemsOptionsMock, } from './write_lines_to_bulk_list_items.mock'; - -import { createListItemsBulk } from '.'; +import { createListItemsBulk } from './create_list_items_bulk'; jest.mock('./create_list_items_bulk', () => ({ createListItemsBulk: jest.fn(), })); +jest.mock('../lists/create_list_if_it_does_not_exist', () => ({ + createListIfItDoesNotExist: jest.fn(), +})); + describe('write_lines_to_bulk_list_items', () => { beforeEach(() => { jest.clearAllMocks(); @@ -61,6 +65,17 @@ describe('write_lines_to_bulk_list_items', () => { expect.objectContaining({ value: ['127.0.0.1', '127.0.0.2'] }) ); }); + + test('It creates a list by calling "createListIfItDoesNotExist" with a correctly decoded file name', async () => { + const options = getImportListItemsToStreamOptionsMock(); + const promise = importListItemsToStream({ ...options, listId: undefined }); + options.stream.push(`--\nContent-Disposition: attachment; filename="%22Filename%22.txt"`); + options.stream.push(null); + await promise; + expect(createListIfItDoesNotExist).toBeCalledWith( + expect.objectContaining({ id: `"Filename".txt`, name: `"Filename".txt` }) + ); + }); }); describe('writeBufferToItems', () => { diff --git a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts index 89a6bdbc77878..ab3bec3feb487 100644 --- a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts +++ b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts @@ -59,17 +59,17 @@ export const importListItemsToStream = ({ let list: ListSchema | null = null; readBuffer.on('fileName', async (fileNameEmitted: string) => { readBuffer.pause(); - fileName = fileNameEmitted; + fileName = decodeURIComponent(fileNameEmitted); if (listId == null) { list = await createListIfItDoesNotExist({ - description: `File uploaded from file system of ${fileNameEmitted}`, + description: `File uploaded from file system of ${fileName}`, deserializer, esClient, - id: fileNameEmitted, + id: fileName, immutable: false, listIndex, meta, - name: fileNameEmitted, + name: fileName, serializer, type, user, From dbde40ac4bdb60982b0247d4dfab0e3790a9be29 Mon Sep 17 00:00:00 2001 From: Khristinin Nikita Date: Fri, 10 Sep 2021 11:28:56 +0200 Subject: [PATCH 2/5] Return wait_for for delete list item --- x-pack/plugins/lists/server/services/lists/delete_list.test.ts | 2 +- x-pack/plugins/lists/server/services/lists/delete_list.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/lists/server/services/lists/delete_list.test.ts b/x-pack/plugins/lists/server/services/lists/delete_list.test.ts index 9ceecbc299bab..f379fd977f51a 100644 --- a/x-pack/plugins/lists/server/services/lists/delete_list.test.ts +++ b/x-pack/plugins/lists/server/services/lists/delete_list.test.ts @@ -61,7 +61,7 @@ describe('delete_list', () => { const deleteQuery = { id: LIST_ID, index: LIST_INDEX, - refresh: false, + refresh: 'wait_for', }; expect(options.esClient.delete).toHaveBeenNthCalledWith(1, deleteQuery); }); diff --git a/x-pack/plugins/lists/server/services/lists/delete_list.ts b/x-pack/plugins/lists/server/services/lists/delete_list.ts index b9a55e107ab76..517723fc227de 100644 --- a/x-pack/plugins/lists/server/services/lists/delete_list.ts +++ b/x-pack/plugins/lists/server/services/lists/delete_list.ts @@ -42,7 +42,7 @@ export const deleteList = async ({ await esClient.delete({ id, index: listIndex, - refresh: false, + refresh: 'wait_for', }); return list; } From 4906111cf7b3575e0128203cc30c1df93148f562 Mon Sep 17 00:00:00 2001 From: Khristinin Nikita Date: Fri, 10 Sep 2021 11:59:48 +0200 Subject: [PATCH 3/5] Return back import --- .../services/items/write_lines_to_bulk_list_items.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts index 173f250d5c4d6..f1c266e0a0642 100644 --- a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts +++ b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts @@ -17,7 +17,8 @@ import { getImportListItemsToStreamOptionsMock, getWriteBufferToItemsOptionsMock, } from './write_lines_to_bulk_list_items.mock'; -import { createListItemsBulk } from './create_list_items_bulk'; + +import { createListItemsBulk } from '.'; jest.mock('./create_list_items_bulk', () => ({ createListItemsBulk: jest.fn(), From 5653aca39ccd2d7a4d8db079435ebe221d521ccb Mon Sep 17 00:00:00 2001 From: Khristinin Nikita Date: Mon, 13 Sep 2021 18:38:27 +0200 Subject: [PATCH 4/5] Update x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts Co-authored-by: Ryland Herrick --- .../services/items/write_lines_to_bulk_list_items.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts index f1c266e0a0642..78098fde59827 100644 --- a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts +++ b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts @@ -67,7 +67,7 @@ describe('write_lines_to_bulk_list_items', () => { ); }); - test('It creates a list by calling "createListIfItDoesNotExist" with a correctly decoded file name', async () => { + it('creates a list with a decoded file name', async () => { const options = getImportListItemsToStreamOptionsMock(); const promise = importListItemsToStream({ ...options, listId: undefined }); options.stream.push(`--\nContent-Disposition: attachment; filename="%22Filename%22.txt"`); From d61276dfaa7454e09ffe73ad15c48e272c0c024f Mon Sep 17 00:00:00 2001 From: Khristinin Nikita Date: Mon, 13 Sep 2021 21:54:15 +0200 Subject: [PATCH 5/5] Use i18n for message --- .../server/services/items/write_lines_to_bulk_list_items.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts index ab3bec3feb487..edd78e350054d 100644 --- a/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts +++ b/x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.ts @@ -17,6 +17,7 @@ import type { Type, } from '@kbn/securitysolution-io-ts-list-types'; import { Version } from '@kbn/securitysolution-io-ts-types'; +import { i18n } from '@kbn/i18n'; import { createListIfItDoesNotExist } from '../lists/create_list_if_it_does_not_exist'; import { ConfigType } from '../../config'; @@ -62,7 +63,10 @@ export const importListItemsToStream = ({ fileName = decodeURIComponent(fileNameEmitted); if (listId == null) { list = await createListIfItDoesNotExist({ - description: `File uploaded from file system of ${fileName}`, + description: i18n.translate('xpack.lists.services.items.fileUploadFromFileSystem', { + defaultMessage: 'File uploaded from file system of {fileName}', + values: { fileName }, + }), deserializer, esClient, id: fileName,