Skip to content

Commit

Permalink
[EDR Workflows] [Osquery] Change query ID regex pattern (#176507)
Browse files Browse the repository at this point in the history
(cherry picked from commit 45f554d)
  • Loading branch information
tomsonpl committed Feb 12, 2024
1 parent 3bc8190 commit e4019f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
40 changes: 40 additions & 0 deletions x-pack/plugins/osquery/public/packs/queries/validations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

import { idHookSchemaValidation } from './validations';

describe('idSchemaValidation', () => {
it('returns undefined for valid id', () => {
expect(idHookSchemaValidation('valid-id')).toBeUndefined();
});
it('returns undefined for valid id with numbers', () => {
expect(idHookSchemaValidation('123valid_id_123')).toBeUndefined();
});
it('returns undefined for valid id with underscore _', () => {
expect(idHookSchemaValidation('valid_id')).toBeUndefined();
});
it('returns error message for invalid id with spaces', () => {
expect(idHookSchemaValidation('invalid id')).toEqual(
'Characters must be alphanumeric, _, or -'
);
});

it('returns error message for invalid id with dots', () => {
expect(idHookSchemaValidation('invalid.id')).toEqual(
'Characters must be alphanumeric, _, or -'
);
});

it('returns error message for invalid id with special characters', () => {
expect(idHookSchemaValidation('invalid@id')).toEqual(
'Characters must be alphanumeric, _, or -'
);
});
it('returns error message for invalid id just numbers', () => {
expect(idHookSchemaValidation('1232')).toEqual('Characters must be alphanumeric, _, or -');
});
});
4 changes: 3 additions & 1 deletion x-pack/plugins/osquery/public/packs/queries/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { i18n } from '@kbn/i18n';
import type { FormData, ValidationFunc } from '../../shared_imports';

export const MAX_QUERY_LENGTH = 2000;
const idPattern = /^[a-zA-Z0-9-_]+$/;

// Has to be a string, can't be just numbers, and cannot contain dot '.'
const idPattern = /^(?![0-9]+$)[a-zA-Z0-9-_]+$/;
// still used in Packs
export const idSchemaValidation: ValidationFunc<FormData, string, string> = ({ value }) => {
const valueIsValid = idPattern.test(value);
Expand Down

0 comments on commit e4019f7

Please sign in to comment.