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

[Cases] Limit category filter in find cases API #159989

Merged
merged 6 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions x-pack/plugins/cases/common/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const MAX_DOCS_PER_PAGE = 10000 as const;
export const MAX_BULK_GET_ATTACHMENTS = MAX_DOCS_PER_PAGE;
export const MAX_CONCURRENT_SEARCHES = 10 as const;
export const MAX_BULK_GET_CASES = 1000 as const;
export const MAX_CATEGORY_FILTER_LENGTH = 100 as const;

/**
* Validation
Expand Down
19 changes: 19 additions & 0 deletions x-pack/plugins/cases/docs/openapi/bundled.json
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,25 @@
},
"example": "tag-1"
},
{
"name": "category",
"in": "query",
"description": "Filters the returned cases by category. Limited to 100 categories.",
"schema": {
"oneOf": [
{
"type": "string"
},
{
"type": "array",
"items": {
"type": "string"
}
}
]
},
"example": "my-category"
},
{
"name": "to",
"in": "query",
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/cases/docs/openapi/bundled.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ paths:
items:
type: string
example: tag-1
- name: category
in: query
description: Filters the returned cases by category. Limited to 100 categories.
schema:
oneOf:
- type: string
- type: array
items:
type: string
example: my-category
- name: to
in: query
description: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ get:
items:
type: string
example: tag-1
- name: category
in: query
description: Filters the returned cases by category. Limited to 100 categories.
schema:
oneOf:
- type: string
- type: array
items:
type: string
example: my-category
- name: to
in: query
description: >
Expand Down
11 changes: 11 additions & 0 deletions x-pack/plugins/cases/server/client/cases/find.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { v1 as uuidv1 } from 'uuid';

import type { Case } from '../../../common/api';

import { MAX_CATEGORY_FILTER_LENGTH } from '../../../common/constants';
import { flattenCaseSavedObject } from '../../common/utils';
import { mockCases } from '../../mocks';
import { createCasesClientMockArgs, createCasesClientMockFindRequest } from '../mocks';
Expand Down Expand Up @@ -103,5 +104,15 @@ describe('find', () => {
'Error: Invalid value "foobar" supplied to "searchFields"'
);
});

it(`invalid category array with > ${MAX_CATEGORY_FILTER_LENGTH} items`, async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: it(throws an error when the category array has...)

const category = Array(MAX_CATEGORY_FILTER_LENGTH + 1).fill('foobar');

const findRequest = createCasesClientMockFindRequest({ category });

await expect(find(findRequest, clientArgs)).rejects.toThrow(
`Error: Too many categories provided. The maximum allowed is ${MAX_CATEGORY_FILTER_LENGTH}`
);
});
});
});
17 changes: 15 additions & 2 deletions x-pack/plugins/cases/server/client/cases/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { isEmpty } from 'lodash';
import Boom from '@hapi/boom';

import { MAX_CATEGORY_FILTER_LENGTH } from '../../../common/constants';
import type { CasesFindResponse, CasesFindRequest } from '../../../common/api';
import {
CasesFindRequestRt,
Expand All @@ -24,6 +25,16 @@ import { LICENSING_CASE_ASSIGNMENT_FEATURE } from '../../common/constants';
import type { CasesFindQueryParams } from '../types';
import { decodeOrThrow } from '../../../common/api/runtime_types';

/**
* Throws an error if the user tries to filter by more than MAX_CATEGORY_FILTER_LENGTH categories.
*/
function throwIfCategoryParamTooLong(category?: string[] | string) {
if (Array.isArray(category) && category.length > MAX_CATEGORY_FILTER_LENGTH)
throw Boom.badRequest(
`Too many categories provided. The maximum allowed is ${MAX_CATEGORY_FILTER_LENGTH}`
);
}

/**
* Retrieves a case and optionally its comments.
*
Expand All @@ -44,8 +55,7 @@ export const find = async (
try {
const queryParams = decodeWithExcessOrThrow(CasesFindRequestRt)(params);

const { filter: authorizationFilter, ensureSavedObjectsAreAuthorized } =
await authorization.getAuthorizationFilter(Operations.findCases);
throwIfCategoryParamTooLong(queryParams.category);

/**
* Assign users to a case is only available to Platinum+
Expand All @@ -63,6 +73,9 @@ export const find = async (
licensingService.notifyUsage(LICENSING_CASE_ASSIGNMENT_FEATURE);
}

const { filter: authorizationFilter, ensureSavedObjectsAreAuthorized } =
await authorization.getAuthorizationFilter(Operations.findCases);

const queryArgs: CasesFindQueryParams = {
tags: queryParams.tags,
reporters: queryParams.reporters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { v1 as uuidv1 } from 'uuid';

import expect from '@kbn/expect';
import { CASES_URL } from '@kbn/cases-plugin/common/constants';
import { CASES_URL, MAX_CATEGORY_FILTER_LENGTH } from '@kbn/cases-plugin/common/constants';
import { Case, CaseSeverity, CaseStatuses, CommentType } from '@kbn/cases-plugin/common/api';
import { ALERTING_CASES_SAVED_OBJECT_INDEX } from '@kbn/core-saved-objects-server';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
Expand Down Expand Up @@ -349,6 +349,12 @@ export default ({ getService }: FtrProviderContext): void => {
});
});

it('unhappy path - 400s when bad category field supplied', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: it('... 400s when more than the maximum category fields are supplied')

const category = Array(MAX_CATEGORY_FILTER_LENGTH + 1).fill('foobar');

await findCases({ supertest, query: { category }, expectedHttpCode: 400 });
});

describe('search and searchField', () => {
beforeEach(async () => {
await createCase(supertest, postCaseReq);
Expand Down