Skip to content

Commit

Permalink
[Cases] Validate length of text custom fields. (#167029)
Browse files Browse the repository at this point in the history
## Summary

This PR limits the length of the property value for `text` custom
fields.
  • Loading branch information
adcoelho authored Sep 22, 2023
1 parent 04fc134 commit 661a856
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
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

0 comments on commit 661a856

Please sign in to comment.