From e2d87731dee28d3bbbf1b36508137b38c8bf6732 Mon Sep 17 00:00:00 2001
From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Date: Tue, 14 Sep 2021 06:53:59 -0400
Subject: [PATCH] Decode file name on upload value lists and fix bug with
 removing  value list (#111838) (#112056)

* Decode fileName when creating a list

* Return wait_for for delete list item

* Return back import

* Update x-pack/plugins/lists/server/services/items/write_lines_to_bulk_list_items.test.ts

Co-authored-by: Ryland Herrick <ryalnd@gmail.com>

* Use i18n for message

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Ryland Herrick <ryalnd@gmail.com>

Co-authored-by: Khristinin Nikita <nikita.khristinin@elastic.co>
Co-authored-by: Ryland Herrick <ryalnd@gmail.com>
---
 .../items/write_lines_to_bulk_list_items.test.ts | 16 ++++++++++++++++
 .../items/write_lines_to_bulk_list_items.ts      | 12 ++++++++----
 .../server/services/lists/delete_list.test.ts    |  2 +-
 .../lists/server/services/lists/delete_list.ts   |  2 +-
 4 files changed, 26 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..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
@@ -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,
@@ -23,6 +24,10 @@ 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 +66,17 @@ describe('write_lines_to_bulk_list_items', () => {
         expect.objectContaining({ value: ['127.0.0.1', '127.0.0.2'] })
       );
     });
+
+    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"`);
+      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..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';
@@ -59,17 +60,20 @@ 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: i18n.translate('xpack.lists.services.items.fileUploadFromFileSystem', {
+            defaultMessage: 'File uploaded from file system of {fileName}',
+            values: { fileName },
+          }),
           deserializer,
           esClient,
-          id: fileNameEmitted,
+          id: fileName,
           immutable: false,
           listIndex,
           meta,
-          name: fileNameEmitted,
+          name: fileName,
           serializer,
           type,
           user,
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;
   }