Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security Solution][Lists] Escape quotes in list ids and quote the id in KQL query #93176

Merged
merged 4 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion x-pack/plugins/lists/server/routes/delete_list_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '../../common/schemas';
import { getSavedObjectType } from '../services/exception_lists/utils';
import { ExceptionListClient } from '../services/exception_lists/exception_list_client';
import { escapeQuotes } from '../services/utils/escape_query';

import { getExceptionListClient, getListClient } from '.';

Expand Down Expand Up @@ -142,7 +143,7 @@ const getReferencedExceptionLists = async (
(item) =>
`${getSavedObjectType({
namespaceType: item.namespace_type,
})}.attributes.list_id: ${item.list_id}`
})}.attributes.list_id: "${escapeQuotes(item.list_id)}"`
)
.join(' OR ');
return exceptionLists.findExceptionList({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ describe('find_exception_list_items', () => {
savedObjectType: ['exception-list'],
});
expect(filter).toEqual(
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: some-list-id)'
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "some-list-id")'
);
});

test('It should create a filter escaping quotes in list ids', () => {
const filter = getExceptionListsItemFilter({
filter: [],
listId: ['list-id-"-with-quote'],
savedObjectType: ['exception-list'],
});
expect(filter).toEqual(
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "list-id-\\"-with-quote")'
);
});

Expand All @@ -29,7 +40,7 @@ describe('find_exception_list_items', () => {
savedObjectType: ['exception-list'],
});
expect(filter).toEqual(
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: some-list-id) AND exception-list.attributes.name: "Sample Endpoint Exception List")'
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "some-list-id") AND exception-list.attributes.name: "Sample Endpoint Exception List")'
);
});

Expand All @@ -40,7 +51,7 @@ describe('find_exception_list_items', () => {
savedObjectType: ['exception-list', 'exception-list-agnostic'],
});
expect(filter).toEqual(
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2)'
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "list-1") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-2")'
);
});

Expand All @@ -51,7 +62,7 @@ describe('find_exception_list_items', () => {
savedObjectType: ['exception-list', 'exception-list-agnostic'],
});
expect(filter).toEqual(
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) AND exception-list.attributes.name: "Sample Endpoint Exception List") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2)'
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "list-1") AND exception-list.attributes.name: "Sample Endpoint Exception List") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-2")'
);
});

Expand All @@ -62,7 +73,7 @@ describe('find_exception_list_items', () => {
savedObjectType: ['exception-list', 'exception-list-agnostic', 'exception-list-agnostic'],
});
expect(filter).toEqual(
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-3)'
'(exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "list-1") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-2") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-3")'
);
});

Expand All @@ -73,7 +84,7 @@ describe('find_exception_list_items', () => {
savedObjectType: ['exception-list', 'exception-list-agnostic', 'exception-list-agnostic'],
});
expect(filter).toEqual(
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) AND exception-list.attributes.name: "Sample Endpoint Exception List") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-3)'
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "list-1") AND exception-list.attributes.name: "Sample Endpoint Exception List") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-2") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-3")'
);
});

Expand All @@ -88,7 +99,7 @@ describe('find_exception_list_items', () => {
savedObjectType: ['exception-list', 'exception-list-agnostic', 'exception-list-agnostic'],
});
expect(filter).toEqual(
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: list-1) AND exception-list.attributes.name: "Sample Endpoint Exception List 1") OR ((exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-2) AND exception-list.attributes.name: "Sample Endpoint Exception List 2") OR ((exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: list-3) AND exception-list.attributes.name: "Sample Endpoint Exception List 3")'
'((exception-list.attributes.list_type: item AND exception-list.attributes.list_id: "list-1") AND exception-list.attributes.name: "Sample Endpoint Exception List 1") OR ((exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-2") AND exception-list.attributes.name: "Sample Endpoint Exception List 2") OR ((exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.list_id: "list-3") AND exception-list.attributes.name: "Sample Endpoint Exception List 3")'
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
SortFieldOrUndefined,
SortOrderOrUndefined,
} from '../../../common/schemas';
import { escapeQuotes } from '../utils/escape_query';

import { getSavedObjectTypes, transformSavedObjectsToFoundExceptionListItem } from './utils';
import { getExceptionList } from './get_exception_list';
Expand Down Expand Up @@ -89,7 +90,8 @@ export const getExceptionListsItemFilter = ({
savedObjectType: SavedObjectType[];
}): string => {
return listId.reduce((accum, singleListId, index) => {
const listItemAppend = `(${savedObjectType[index]}.attributes.list_type: item AND ${savedObjectType[index]}.attributes.list_id: ${singleListId})`;
const escapedListId = escapeQuotes(singleListId);
const listItemAppend = `(${savedObjectType[index]}.attributes.list_type: item AND ${savedObjectType[index]}.attributes.list_id: "${escapedListId}")`;
const listItemAppendWithFilter =
filter[index] != null ? `(${listItemAppend} AND ${filter[index]})` : listItemAppend;
if (accum === '') {
Expand Down Expand Up @@ -117,8 +119,9 @@ export const findValueListExceptionListItems = async ({
sortField,
sortOrder,
}: FindValueListExceptionListsItems): Promise<FoundExceptionListItemSchema | null> => {
const escapedValueListId = escapeQuotes(valueListId);
const savedObjectsFindResponse = await savedObjectsClient.find<ExceptionListSoSchema>({
filter: `(exception-list.attributes.list_type: item AND exception-list.attributes.entries.list.id:${valueListId}) OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.entries.list.id:${valueListId}) `,
filter: `(exception-list.attributes.list_type: item AND exception-list.attributes.entries.list.id:"${escapedValueListId}") OR (exception-list-agnostic.attributes.list_type: item AND exception-list-agnostic.attributes.entries.list.id:"${escapedValueListId}") `,
page,
perPage,
sortField,
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/lists/server/services/utils/escape_query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export const escapeQuotes = (str: string): string => {
return str.replace(/[\\"]/g, '\\$&');
};
Copy link
Contributor

@FrankHassanabad FrankHassanabad Mar 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the same as this one?
x-pack/plugins/data_enhanced/public/autocomplete/providers/kql_query_suggestion/lib/escape_kuery.ts

That looks like it only works client side and not server and isn't part of a common section for usage in both areas which is a bummer.

Wish it was in one common spot. I'm fine with this duplicated here though. I would just maybe? copy maybe over their tests and put a note that there is a duplication between the two in case someone sees them deviate later/change.

The other tests above and the e2e is more than good enough though, wouldn't split hairs on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep it's the same function, definitely would like to have it in a common place along with the other escaping utilities (and maybe a more robust parameterization scheme like Ross suggested above). For now though it seems a simple enough function to just do the ol' copy paste.

Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('get_query_filter', () => {
minimum_should_match: 1,
should: [
{
match: {
match_phrase: {
list_id: 'list-123',
},
},
Expand Down Expand Up @@ -74,7 +74,7 @@ describe('get_query_filter', () => {
minimum_should_match: 1,
should: [
{
match: {
match_phrase: {
list_id: 'list-123',
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { DslQuery, EsQueryConfig } from 'src/plugins/data/common';

import { Filter, Query, esQuery } from '../../../../../../src/plugins/data/server';

import { escapeQuotes } from './escape_query';

export interface GetQueryFilterOptions {
filter: string;
}
Expand Down Expand Up @@ -41,7 +43,10 @@ export const getQueryFilterWithListId = ({
filter,
listId,
}: GetQueryFilterWithListIdOptions): GetQueryFilterReturn => {
const escapedListId = escapeQuotes(listId);
const filterWithListId =
filter.trim() !== '' ? `list_id: ${listId} AND (${filter})` : `list_id: ${listId}`;
filter.trim() !== ''
? `list_id: "${escapedListId}" AND (${filter})`
: `list_id: "${escapedListId}"`;
return getQueryFilter({ filter: filterWithListId });
};
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ export default ({ getService }: FtrProviderContext) => {
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
});

it('should delete a single list with a list id containing non-alphanumeric characters', async () => {
// create a list
const id = `some""-list-id"(1)`;
await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send({
...getCreateMinimalListSchemaMock(),
id,
})
.expect(200);

// delete the list by its list id
const { body } = await supertest
.delete(`${LIST_URL}?id=${id}`)
.set('kbn-xsrf', 'true')
.expect(200);

const bodyToCompare = removeListServerGeneratedProperties(body);
expect(bodyToCompare).to.eql(getListResponseMockWithoutAutoGeneratedValues());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks for the e2e test here.

});

it('should delete a single list using an auto generated id', async () => {
// add a list
const { body: bodyWithCreatedList } = await supertest
Expand Down