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] Validate length of text custom fields. #167029

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
39 changes: 39 additions & 0 deletions x-pack/plugins/cases/common/types/api/case/v1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
MAX_TITLE_LENGTH,
MAX_CATEGORY_LENGTH,
MAX_CUSTOM_FIELDS_PER_CASE,
MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH,
} from '../../../constants';
import { PathReporter } from 'io-ts/lib/PathReporter';
import { AttachmentType } from '../../domain/attachment/v1';
Expand Down Expand Up @@ -300,6 +301,25 @@ describe('CasePostRequestRt', () => {

expect(PathReporter.report(CasePostRequestRt.decode(rest))).toContain('No errors!');
});

it(`throws an error when a text customFields is longer than ${MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH}`, () => {
expect(
PathReporter.report(
CasePostRequestRt.decode({
...defaultRequest,
customFields: [
{
key: 'first_custom_field_key',
type: 'text',
field: { value: ['#'.repeat(MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH + 1)] },
},
],
})
)
).toContain(
`The length of the value is too long. The maximum length is ${MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH}.`
);
});
});

describe('CasesFindRequestRt', () => {
Expand Down Expand Up @@ -680,6 +700,25 @@ describe('CasePatchRequestRt', () => {
`The length of the field customFields is too long. Array must be of length <= ${MAX_CUSTOM_FIELDS_PER_CASE}.`
);
});

it(`throws an error when a text customFields is longer than ${MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH}`, () => {
expect(
PathReporter.report(
CasePatchRequestRt.decode({
...defaultRequest,
customFields: [
{
key: 'first_custom_field_key',
type: 'text',
field: { value: ['#'.repeat(MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH + 1)] },
},
],
})
)
).toContain(
`The length of the value is too long. The maximum length is ${MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH}.`
);
});
});

describe('CasesPatchRequestRt', () => {
Expand Down
19 changes: 18 additions & 1 deletion x-pack/plugins/cases/common/types/api/case/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,43 @@ import {
MAX_CATEGORY_FILTER_LENGTH,
MAX_ASSIGNEES_PER_CASE,
MAX_CUSTOM_FIELDS_PER_CASE,
MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH,
} from '../../../constants';
import {
limitedStringSchema,
limitedArraySchema,
NonEmptyString,
paginationSchema,
} from '../../../schema';
import { CustomFieldTextTypeRt } from '../../domain';
import {
CaseRt,
CaseSettingsRt,
CaseSeverityRt,
CasesRt,
CaseStatusRt,
CustomFieldToggle,
customFieldValue,
RelatedCaseRt,
CustomFieldRt,
} from '../../domain/case/v1';
import { CaseConnectorRt } from '../../domain/connector/v1';
import { CaseUserProfileRt, UserRt } from '../../domain/user/v1';
import { CasesStatusResponseRt } from '../stats/v1';

const CustomFieldText = rt.strict({
key: rt.string,
type: CustomFieldTextTypeRt,
field: customFieldValue(
limitedStringSchema({
fieldName: 'value',
min: 0,
max: MAX_CUSTOM_FIELD_TEXT_VALUE_LENGTH,
})
),
});

const CustomFieldRt = rt.union([CustomFieldText, CustomFieldToggle]);

const CustomFieldsRt = limitedArraySchema({
codec: CustomFieldRt,
fieldName: 'customFields',
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/common/types/domain/case/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const CaseSettingsRt = rt.strict({
syncAlerts: rt.boolean,
});

const customFieldValue = <C extends rt.Mixed>(codec: C) =>
export const customFieldValue = <C extends rt.Mixed>(codec: C) =>
rt.strict({ value: rt.union([rt.array(codec), rt.null]) });

const CustomFieldText = rt.strict({
Expand All @@ -61,7 +61,7 @@ const CustomFieldText = rt.strict({
field: customFieldValue(rt.string),
});

const CustomFieldToggle = rt.strict({
export const CustomFieldToggle = rt.strict({
key: rt.string,
type: CustomFieldToggleTypeRt,
field: customFieldValue(rt.boolean),
Expand Down