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] Fixes exception item comment validation on newline chars \n #202063

Merged
merged 19 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
7e809bd
initial tests and changes
dhurley14 Nov 27, 2024
376ae06
provide custom validation for non empty strings in openapi and zod
dhurley14 Dec 3, 2024
416dcd3
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Dec 3, 2024
8c6ec80
[CI] Auto-commit changed files from 'yarn openapi:bundle'
kibanamachine Dec 3, 2024
70042e1
Merge remote-tracking branch 'upstream/main' into fix-exception-comme…
dhurley14 Dec 3, 2024
cd92dc6
Merge branch 'fix-exception-comment-regex' of github.com:dhurley14/ki…
dhurley14 Dec 3, 2024
1881b1b
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Dec 4, 2024
a8f3428
cleanup
dhurley14 Dec 4, 2024
09b275e
remove typeof check, possibly use the standard zod trim function inst…
dhurley14 Dec 4, 2024
f033b4a
Merge remote-tracking branch 'upstream/main' into fix-exception-comme…
dhurley14 Dec 5, 2024
3c9a456
update unit tests, add space between import and function declaration,…
dhurley14 Dec 5, 2024
78bea3f
forgot 'characters'
dhurley14 Dec 5, 2024
61065f5
regenerate spec to include changes in description
dhurley14 Dec 5, 2024
6cbee35
[CI] Auto-commit changed files from 'yarn openapi:bundle'
kibanamachine Dec 5, 2024
f295332
[CI] Auto-commit changed files from 'make api-docs'
kibanamachine Dec 5, 2024
4cc57da
update snapshot and expected error messages
dhurley14 Dec 9, 2024
854b593
merge with main
dhurley14 Dec 9, 2024
624c48c
Merge branch 'main' into fix-exception-comment-regex
dhurley14 Dec 9, 2024
560a533
Merge branch 'main' into fix-exception-comment-regex
dhurley14 Dec 10, 2024
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
24 changes: 12 additions & 12 deletions oas_docs/output/kibana.serverless.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40331,9 +40331,9 @@ components:
- assistant
type: string
Security_AI_Assistant_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_AI_Assistant_API_NormalizedAnonymizationFieldError:
type: object
Expand Down Expand Up @@ -43097,9 +43097,9 @@ components:
- severity
- $ref: '#/components/schemas/Security_Detections_API_NewTermsRuleCreateFields'
Security_Detections_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Detections_API_NormalizedRuleAction:
additionalProperties: false
Expand Down Expand Up @@ -46301,9 +46301,9 @@ components:
- text
type: string
Security_Endpoint_Exceptions_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Endpoint_Exceptions_API_PlatformErrorResponse:
type: object
Expand Down Expand Up @@ -46604,9 +46604,9 @@ components:
required:
- hostStatuses
Security_Endpoint_Management_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Endpoint_Management_API_NoParametersRequestSchema:
type: object
Expand Down Expand Up @@ -47788,9 +47788,9 @@ components:
- text
type: string
Security_Exceptions_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Exceptions_API_PlatformErrorResponse:
type: object
Expand Down Expand Up @@ -48034,9 +48034,9 @@ components:
- text
type: string
Security_Lists_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Lists_API_PlatformErrorResponse:
type: object
Expand Down
24 changes: 12 additions & 12 deletions oas_docs/output/kibana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47829,9 +47829,9 @@ components:
- assistant
type: string
Security_AI_Assistant_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_AI_Assistant_API_NormalizedAnonymizationFieldError:
type: object
Expand Down Expand Up @@ -50775,9 +50775,9 @@ components:
- severity
- $ref: '#/components/schemas/Security_Detections_API_NewTermsRuleCreateFields'
Security_Detections_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Detections_API_NormalizedRuleAction:
additionalProperties: false
Expand Down Expand Up @@ -53986,9 +53986,9 @@ components:
- text
type: string
Security_Endpoint_Exceptions_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Endpoint_Exceptions_API_PlatformErrorResponse:
type: object
Expand Down Expand Up @@ -54289,9 +54289,9 @@ components:
required:
- hostStatuses
Security_Endpoint_Management_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Endpoint_Management_API_NoParametersRequestSchema:
type: object
Expand Down Expand Up @@ -55473,9 +55473,9 @@ components:
- text
type: string
Security_Exceptions_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Exceptions_API_PlatformErrorResponse:
type: object
Expand Down Expand Up @@ -55719,9 +55719,9 @@ components:
- text
type: string
Security_Lists_API_NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
Security_Lists_API_PlatformErrorResponse:
type: object
Expand Down
14 changes: 14 additions & 0 deletions packages/kbn-openapi-common/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-openapi-common'],
};
8 changes: 3 additions & 5 deletions packages/kbn-openapi-common/schemas/primitives.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
*/

import { z } from '@kbn/zod';
import { isNonEmptyString } from '@kbn/zod-helpers';

/**
* A string that is not empty and does not contain only whitespace
* A string that does not contain only whitespace characters
*/
export type NonEmptyString = z.infer<typeof NonEmptyString>;
export const NonEmptyString = z
.string()
.min(1)
.regex(/^(?! *$).+$/);
export const NonEmptyString = z.string().min(1).superRefine(isNonEmptyString);

/**
* A universally unique identifier
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-openapi-common/schemas/primitives.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ components:
schemas:
NonEmptyString:
type: string
pattern: ^(?! *$).+$
minLength: 1
description: A string that is not empty and does not contain only whitespace
format: nonempty
description: A string that does not contain only whitespace characters

UUID:
type: string
Expand Down
44 changes: 44 additions & 0 deletions packages/kbn-openapi-common/schemas/primitives.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { NonEmptyString } from './primitives.gen';

describe('NonEmptyString', () => {
describe('accepts ', () => {
// \t\r\n\f
test('accepts newline chars', () => {
expect(() => NonEmptyString.parse('hello \nworld')).not.toThrow();
});
test('accepts tab chars', () => {
expect(() => NonEmptyString.parse('hello \tworld')).not.toThrow();
});
test('accepts carriage return chars', () => {
expect(() => NonEmptyString.parse('hello \rworld')).not.toThrow();
});
test('accepts form feed return chars', () => {
expect(() => NonEmptyString.parse('hello \fworld')).not.toThrow();
});
});
describe('rejects', () => {
test('rejects only tab chars chars', () => {
expect(() => NonEmptyString.parse('\t\t\t\t')).toThrow();
});
test('rejects only newline chars chars', () => {
expect(() => NonEmptyString.parse('\n\n\n\n\n')).toThrow();
});
test('rejects only carriage return chars chars', () => {
expect(() => NonEmptyString.parse('\r\r\r\r')).toThrow();
});
test('rejects only form feed chars chars', () => {
expect(() => NonEmptyString.parse('\f\f\f\f\f')).toThrow();
});
test('rejects comment with just spaces', () => {
expect(() => NonEmptyString.parse(' ')).toThrow();
});
});
});
1 change: 1 addition & 0 deletions packages/kbn-openapi-common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"include": ["**/*.ts"],
"kbn_references": [
"@kbn/zod",
"@kbn/zod-helpers",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import type { ZodTypeDef } from '@kbn/zod';
import { z } from '@kbn/zod';
import { requiredOptional, isValidDateMath, ArrayFromString, BooleanFromString } from '@kbn/zod-helpers';
import { requiredOptional, isValidDateMath, isNonEmptyString, ArrayFromString, BooleanFromString } from '@kbn/zod-helpers';

{{#each imports}}
import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,8 @@ z.unknown()
{{~#if (eq format 'date-math')}}.superRefine(isValidDateMath){{/if~}}
{{~#if (eq format 'uuid')}}.uuid(){{/if~}}
{{~#if pattern}}.regex(/{{pattern}}/){{/if~}}
{{~#if (eq format 'trim')}}.trim(){{/if~}}
{{~#if (eq format 'nonempty')}}.superRefine(isNonEmptyString){{/if~}}

{{~/if~}}
{{~/inline~}}
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,9 +846,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1846,9 +1846,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1846,9 +1846,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1528,9 +1528,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1528,9 +1528,9 @@ components:
- text
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
PlatformErrorResponse:
type: object
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-zod-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './src/required_optional';
export * from './src/safe_parse_result';
export * from './src/stringify_zod_error';
export * from './src/build_route_validation_with_zod';
export * from './src/non_empty_string';
19 changes: 19 additions & 0 deletions packages/kbn-zod-helpers/src/non_empty_string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import * as z from '@kbn/zod';
dhurley14 marked this conversation as resolved.
Show resolved Hide resolved

export function isNonEmptyString(input: string, ctx: z.RefinementCtx): void {
if (input.trim() === '') {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'No empty strings allowed',
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -978,9 +978,9 @@ components:
- assistant
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
NormalizedAnonymizationFieldError:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -978,9 +978,9 @@ components:
- assistant
type: string
NonEmptyString:
description: A string that is not empty and does not contain only whitespace
description: A string that does not contain only whitespace characters
format: nonempty
minLength: 1
pattern: ^(?! *$).+$
type: string
NormalizedAnonymizationFieldError:
type: object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
*/

import { z } from '@kbn/zod';
import { isNonEmptyString } from '@kbn/zod-helpers';

/**
* A string that is not empty and does not contain only whitespace
* A string that does not contain only whitespace characters
*/
export type NonEmptyString = z.infer<typeof NonEmptyString>;
export const NonEmptyString = z
.string()
.min(1)
.regex(/^(?! *$).+$/);
export const NonEmptyString = z.string().min(1).superRefine(isNonEmptyString);

/**
* A universally unique identifier
Expand Down
Loading
Loading