From 0e793ba60b7239ec0ef2604067b1fe2d72684cfb Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 4 Oct 2024 01:03:23 +1000 Subject: [PATCH] [8.x] [Console] Fix code scanning alert (#194700) (#194836) # Backport This will backport the following commits from `main` to `8.x`: - [[Console] Fix code scanning alert (#194700)](https://github.com/elastic/kibana/pull/194700) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Ignacio Rivas --- src/plugins/console/public/lib/utils/index.ts | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/plugins/console/public/lib/utils/index.ts b/src/plugins/console/public/lib/utils/index.ts index 0386627c044c7..c896d52aa627c 100644 --- a/src/plugins/console/public/lib/utils/index.ts +++ b/src/plugins/console/public/lib/utils/index.ts @@ -54,9 +54,28 @@ export function formatRequestBodyDoc(data: string[], indent: boolean) { }; } +// Regular expression to match different types of comments: +// - Block comments, single and multiline (/* ... */) +// - Single-line comments (// ...) +// - Hash comments (# ...) export function hasComments(data: string) { - // matches single line and multiline comments - const re = /(\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+\/)|(\/\/.*)|(#.*)/; + /* + 1. (\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\/) + - (\/\*): Matches the start of a block comment + - [^*]*: Matches any number of characters that are NOT an asterisk (*), to avoid prematurely closing the comment. + - \*+: Matches one or more asterisks (*), which is part of the block comment closing syntax. + - (?:[^/*][^*]*\*+)*: This non-capturing group ensures that any characters between asterisks and slashes are correctly matched and prevents mismatching on nested or unclosed comments. + - \*\/: Matches the closing of a block comment + + 2. (\/\/.*) + - Matches single-line comments starting with '//'. + - .*: Matches any characters that follow until the end of the line. + + 3. (#.*) + - Matches single-line comments starting with a hash (#). + - .*: Matches any characters that follow until the end of the line. + */ + const re = /(\/\*[^*]*\*+(?:[^/*][^*]*\*+)*\/)|(\/\/.*)|(#.*)/; return re.test(data); }