-
Notifications
You must be signed in to change notification settings - Fork 713
feat: enhance dashboard variable handling for tabs and panels #9410
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
base: main
Are you sure you want to change the base?
Conversation
|
Failed to generate code suggestions for PR |
| : variable.value; | ||
|
|
||
| const placeholders = [ | ||
| `\${${variable.name}}`, |
Check failure
Code scanning / CodeQL
Useless regular-expression character escape High
regular expression
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
To correctly escape special regex characters (like $, {, and }) in placeholder strings intended for use in a regex pattern, we need to ensure that each of these characters is replaced in the string with two backslashes (i.e., \\$), so that the resulting string (when passed to the RegExp constructor) results in the correct escape (e.g., \$). The best way is to use a regex escape helper function that properly escapes all regex special characters, ensuring robust and maintainable code. This function should be defined close to its usage or at the module level.
What to change:
- Add a function (such as
escapeRegExp) that escapes special regex characters in a given string. - Replace the current triple
.replace()chain on line 281-283 with a single call to that function for clarity, safety, and extensibility.
Methods/imports needed:
No external imports are needed; just a local helper function.
File/lines to change:
- In
web/src/utils/dashboard/variables/variablesUtils.ts, add theescapeRegExpfunction near the top. - In the code where
placeholderis passed tonew RegExp, useescapeRegExp(placeholder)instead of the current.replace()chain.
-
Copy modified lines R1-R8 -
Copy modified line R288
| @@ -1,3 +1,11 @@ | ||
| /** | ||
| * Escapes special regex characters in a string so it can safely be used in a RegExp. | ||
| * @param str string to escape | ||
| */ | ||
| function escapeRegExp(str: string): string { | ||
| return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| export const formatInterval = (interval: any) => { | ||
| switch (true) { | ||
| // 0.01s | ||
| @@ -277,10 +285,7 @@ | ||
|
|
||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| escapeRegExp(placeholder), | ||
| "g", | ||
| ), | ||
| value, |
|
|
||
| const placeholders = [ | ||
| `\${${variable.name}}`, | ||
| `\${${variable.name}:csv}`, |
Check failure
Code scanning / CodeQL
Useless regular-expression character escape High
regular expression
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
To properly escape $, {, and } in the context of generating a regular expression from string placeholders, we need to make sure that the string passed to the RegExp constructor contains \\$, \\{, and \\}. In JavaScript string literals, \\ is interpreted as a single backslash, so placeholder.replace(/\$/g, "\\$") results in \$ in the pattern, which is correct. However, CodeQL is flagging \$ as a useless escape in string literals because in some JS contexts, \$ is equivalent to $, potentially leading to confusion or misinterpretation. But, since this is being used to build a RegExp pattern, using "\\$" in the string results in \$ in the regex pattern, which does match a literal dollar sign.
However, the best way to future-proof and clarify intent is to ensure you're using double backslashes ("\\$", "\\{", "\\}") in the string replacement so that the resulting RegExp pattern contains \$, \{, and \}. Double check that each replacement has two backslashes in the string literal.
Thus, in file web/src/utils/dashboard/variables/variablesUtils.ts, in the code block starting at line 280:
- Edit
.replace(/\$/g, "\\$")to.replace(/\$/g, "\\\$") - Edit
.replace(/\{/g, "\\{")to.replace(/\{/g, "\\\{") - Edit
.replace(/\}/g, "\\}")to.replace(/\}/g, "\\\}")
While the previous code may have worked, using two backslashes to produce the correct escaped character is canonical, and avoids "useless regular-expression escape" alerts.
No additional imports or dependencies are required.
-
Copy modified lines R281-R283
| @@ -278,9 +278,9 @@ | ||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| .replace(/\$/g, "\\\$") | ||
| .replace(/\{/g, "\\\{") | ||
| .replace(/\}/g, "\\\}"), | ||
| "g", | ||
| ), | ||
| value, |
| const placeholders = [ | ||
| `\${${variable.name}}`, | ||
| `\${${variable.name}:csv}`, | ||
| `\${${variable.name}:pipe}`, |
Check failure
Code scanning / CodeQL
Useless regular-expression character escape High
regular expression
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
In general terms, the correct way to construct a RegExp from a string with embedded variable placeholders is to escape only those characters that actually have special meaning in RegExp patterns. In the context of this code, rather than blindly escaping the $, {, and } characters, you should escape those which could be interpreted as meta-characters in RegExp.
For this snippet, replacing only special RegExp characters (like ., *, +, ?, ^, $, |, (, ), [, ], {, }, \) is appropriate. The more robust fix is to use a utility function to escape all RegExp metacharacters in the placeholder string before passing it to new RegExp.
File/region to edit: Only change the code within web/src/utils/dashboard/variables/variablesUtils.ts, at the region where placeholder strings are turned into regex patterns (around lines 278–283).
Required changes:
- Define a utility function, e.g.,
escapeRegExp, that will escape all RegExp meta-characters. - Use this function to escape the placeholder when constructing the RegExp pattern, rather than selectively replacing
\$,\{, and\}. - No imports necessary: this utility function can be written inline.
-
Copy modified lines R278-R281 -
Copy modified line R284
| @@ -275,12 +275,13 @@ | ||
| } | ||
| } | ||
|
|
||
| // Escape special RegExp characters in the placeholder string | ||
| function escapeRegExp(string: string): string { | ||
| return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| escapeRegExp(placeholder), | ||
| "g", | ||
| ), | ||
| value, |
| `\${${variable.name}}`, | ||
| `\${${variable.name}:csv}`, | ||
| `\${${variable.name}:pipe}`, | ||
| `\${${variable.name}:doublequote}`, |
Check failure
Code scanning / CodeQL
Useless regular-expression character escape High
regular expression
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
To fix this issue, we must ensure that metacharacters in the placeholder strings are correctly escaped in the string used to construct the RegExp. This requires doubling the backslash so that when the string is interpreted by the RegExp constructor, the resulting regex will match literal metacharacters. Specifically, change .replace(/\$/g, "\\$") to .replace(/\$/g, "\\\$") (or equivalently using .replaceAll("$", "\\\$")), and similarly for { and }. This will yield \\$ in the string, resulting in a regex that matches a literal $.
Edit only the string replacement logic (lines 281-283) in the file web/src/utils/dashboard/variables/variablesUtils.ts to use double backslashes for escaping metacharacters.
No additional imports or methods are required.
-
Copy modified lines R281-R283
| @@ -278,9 +278,9 @@ | ||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| .replace(/\$/g, "\\\$") | ||
| .replace(/\{/g, "\\\{") | ||
| .replace(/\}/g, "\\\}"), | ||
| "g", | ||
| ), | ||
| value, |
| `\${${variable.name}:csv}`, | ||
| `\${${variable.name}:pipe}`, | ||
| `\${${variable.name}:doublequote}`, | ||
| `\${${variable.name}:singlequote}`, |
Check failure
Code scanning / CodeQL
Useless regular-expression character escape High
regular expression
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
The problem arises because .replace(/\$/g, "\\$") (and similarly for { and }) replaces a plain dollar sign with a single backslash, so that the produced pattern looks like \$ in the string literal provided to RegExp. However, since JavaScript string literals interpret \ itself as an escape character, the string "\\$" passed to RegExp becomes \$ in the resulting pattern, which will match a literal dollar. However, by only replacing with a single backslash "\", the string ends up containing \$, which in RegExp is just equivalent to $, not a literal dollar.
To correctly escape these characters in dynamically constructed RegExp patterns, replace each $, {, and } with double backslashes (\\$, \\{, \\}) so that the string passed to RegExp contains \\$, which is interpreted in the RegExp engine as \$ (literal dollar sign). This ensures you safely match the literal placeholder regardless of context.
You should update lines 281–283 to replace each character with two backslashes (i.e., .replace(/\$/g, "\\\\$")). This should be done for all three replacements ($, {, }).
-
Copy modified lines R281-R283
| @@ -278,9 +278,9 @@ | ||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| .replace(/\$/g, "\\\\$") | ||
| .replace(/\{/g, "\\\\{") | ||
| .replace(/\}/g, "\\\\}"), | ||
| "g", | ||
| ), | ||
| value, |
| `\${${variable.name}:pipe}`, | ||
| `\${${variable.name}:doublequote}`, | ||
| `\${${variable.name}:singlequote}`, | ||
| `\$${variable.name}`, |
Check failure
Code scanning / CodeQL
Useless regular-expression character escape High
regular expression
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
The best way to fix this problem is to ensure that each placeholder string is properly escaped for use in the RegExp constructor, so it matches the literal string representation, not a RegExp meta-character. This means replacing .replace(/\$/g, "\\$") with .replace(/\$/g, "\\$") (which actually produces a single backslash in the string, so for RegExp it must be double-backslashed: \\\\$). The same applies for the braces ({, }): use double backslashes to ensure proper escaping.
In summary:
- Replace
.replace(/\$/g, "\\$")with.replace(/\$/g, "\\$")for use in RegExp string, but this will produce\${variable.name}still — to match literal$, supply\\$in the RegExp string. - The most reliable approach is to use a utility function to escape all RegExp meta-characters (not just
$,{,}) in the placeholder, ensuring the pattern matches the literal placeholder.
Thus, the fix is:
- Introduce a function, e.g.,
escapeRegExp, that escapes all RegExp meta-characters in the placeholder. - Replace the multiple
.replace()calls on the placeholder with a single call toescapeRegExp(placeholder). - Update the code around line 281–283 accordingly.
You only need the escapeRegExp function and update the invocation within variablesUtils.ts.
-
Copy modified lines R1-R5 -
Copy modified line R285
| @@ -1,3 +1,8 @@ | ||
| // Utility: escapes all RegExp metacharacters in a string | ||
| function escapeRegExp(str: string): string { | ||
| return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| export const formatInterval = (interval: any) => { | ||
| switch (true) { | ||
| // 0.01s | ||
| @@ -277,10 +282,7 @@ | ||
|
|
||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| escapeRegExp(placeholder), | ||
| "g", | ||
| ), | ||
| value, |
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
To correctly escape any metacharacters in the RegExp pattern being built, including the backslash, we should escape all RegExp meta-characters within placeholder. The single best way is to use a purpose-built escaping function for RegExp patterns (such as escape-string-regexp), but if using only built-in methods, escape all relevant characters. In this snippet, add a replacement for backslash (\\). The change should be made at the call constructing the RegExp pattern, at lines 281–283, so that the string used to build the RegExp is properly escaped for all metacharacters that appear in placeholders.
We only need to change the pattern building to also escape backslashes (\), matching the spirit and scope of the flagged line. No further changes or imports are needed.
-
Copy modified line R281
| @@ -278,6 +278,7 @@ | ||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\\/g, "\\\\") // Escape backslash | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), |
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
To correctly escape placeholder for safe embedding inside a regular expression, we should ensure all regular expression special characters are escaped, but at minimum backslashes, as highlighted. The best way is to use a utility function that escapes all regex metacharacters, e.g. a function like escapeRegExp(s) which replaces all regex metacharacters with escaped forms, including the backslash (\). This prevents malformed or dangerous regular expressions and ensures all placeholder occurrences are replaced as intended.
The fix is to replace:
placeholder
.replace(/\$/g, "\\$")
.replace(/\{/g, "\\{")
.replace(/\}/g, "\\}")with a call to an escaping function that escapes all regex special characters. Add a utility function inside this file (since no external imports were present/shown), e.g.:
function escapeRegExp(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}and in the .replace() call, use
new RegExp(escapeRegExp(placeholder), "g")Update the lines where the regex is constructed accordingly. No new imports or external dependencies are needed.
-
Copy modified lines R1-R8 -
Copy modified line R288
| @@ -1,3 +1,11 @@ | ||
| /** | ||
| * Escapes a string for safe embedding in a RegExp (replaces all RegExp metacharacters). | ||
| * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping | ||
| */ | ||
| function escapeRegExp(s: string): string { | ||
| return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| export const formatInterval = (interval: any) => { | ||
| switch (true) { | ||
| // 0.01s | ||
| @@ -277,10 +285,7 @@ | ||
|
|
||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| escapeRegExp(placeholder), | ||
| "g", | ||
| ), | ||
| value, |
| placeholder | ||
| .replace(/\$/g, "\\$") |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 16 days ago
To robustly escape all potentially dangerous special characters when building a regular expression from user-provided input, use a well-tested method to escape regex metacharacters. The best (and simplest) approach is to adopt a utility such as escape-string-regexp if dependencies are allowed, or otherwise copy a safe, standard implementation of it. The fix is to process placeholder with this escaping function before using it as the pattern in RegExp. This change should be made where the RegExp is constructed (lines 279–283), replacing the series of chained .replace()s.
If dependencies are not wanted, a one-liner escape utility can be added directly to the file.
No changes to the rest of the replacement logic are needed.
-
Copy modified lines R1-R8 -
Copy modified line R288
| @@ -1,3 +1,11 @@ | ||
| /** | ||
| * Escape all RegExp special characters in a string. | ||
| */ | ||
| function escapeRegExp(string: string): string { | ||
| // From MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping | ||
| return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| export const formatInterval = (interval: any) => { | ||
| switch (true) { | ||
| // 0.01s | ||
| @@ -277,10 +285,7 @@ | ||
|
|
||
| processedContent = processedContent.replace( | ||
| new RegExp( | ||
| placeholder | ||
| .replace(/\$/g, "\\$") | ||
| .replace(/\{/g, "\\{") | ||
| .replace(/\}/g, "\\}"), | ||
| escapeRegExp(placeholder), | ||
| "g", | ||
| ), | ||
| value, |
1c24fa4 to
86ddad9
Compare
d9e700a to
23ca318
Compare
d45c2cb to
4c0a451
Compare
6b896cb to
c91c6fa
Compare
c91c6fa to
37e28d9
Compare
f45a411 to
faa331e
Compare
… management - Added lazy loading capability to VariablesValueSelector for improved performance. - Introduced a new composable `useVariablesLoadingManager` to handle cascade decisions and dependency resolution. - Updated RenderDashboardCharts.vue to support lazy loading for both global and tab-level variables. - Implemented visibility tracking for panels to trigger loading of variables only when needed. - Enhanced logging for better debugging and tracking of variable updates and dependencies.
… in RenderDashboardCharts
and working on its api call thing
… VariablesValueSelector
…ling and loading logic - Introduced `useVariablesLoadingManager` composable to manage variable loading states and dependencies. - Removed legacy variable management code and replaced it with the new composable's methods. - Updated variable data handling to ensure proper loading states for global, tab, and panel variables. - Enhanced initial load handling to prevent premature API calls and ensure all variables are loaded before proceeding. - Improved logging for better debugging and tracking of variable loading states.
faa331e to
f43febc
Compare
Uh oh!
There was an error while loading. Please reload this page.