Skip to content

Conversation

shahzad31
Copy link
Contributor

@shahzad31 shahzad31 commented Jul 19, 2025

Fixes #225498

Summary

Needed to add $ before e.g template ${url} so that it gets escaped during heartbeat vars parsing.

Before being passed to heartbeat

step("blame me",async () =>{
    const url = "https://www.google.com";
    await page.goto(`${url}`);
})

After being passed to heartbeat

        step("bname me",async () =>{
            const url = "https://www.google.com";
            await page.goto(`$${url}`);
        })

You can take a look at inspect config flyout in monitor form

image

@github-actions github-actions bot added the author:obs-ux-management PRs authored by the obs ux management team label Jul 19, 2025
@shahzad31 shahzad31 added backport:version Backport to applied version labels release_note:skip Skip the PR/issue when compiling release notes release_note:fix and removed release_note:skip Skip the PR/issue when compiling release notes labels Jul 19, 2025
@shahzad31 shahzad31 marked this pull request as ready for review July 19, 2025 22:30
@shahzad31 shahzad31 requested a review from a team as a code owner July 19, 2025 22:30
@shahzad31 shahzad31 requested a review from Copilot July 21, 2025 08:42
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an issue with template literal escaping in inline synthetic monitoring scripts. When template literals like ${url} are used in inline scripts, they need to be escaped as $${url} to prevent conflicts during heartbeat variable parsing.

  • Introduces a new inlineSourceFormatter function that escapes template literals by adding an extra $ prefix
  • Updates both public and private formatters to use the new escaping logic for inline source code
  • Adds test coverage for the template literal escaping functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
browser.ts Updates public formatter to use inlineSourceFormatter for SOURCE_INLINE config
formatting_utils.ts (private) Adds special handling for SOURCE_INLINE in stringToJsonFormatter
formatting_utils.ts Implements the inlineSourceFormatter function with template literal escaping
formatting_utils.test.ts Adds test case for template literal escaping functionality
Comments suppressed due to low confidence (1)

x-pack/solutions/observability/plugins/synthetics/server/synthetics_service/formatters/formatting_utils.ts:126

  • The function name 'escapeTemplateLiterals' is misleading. The function doesn't escape template literals themselves, but rather escapes variable interpolation syntax within template literals. Consider renaming to 'escapeTemplateInterpolation' or 'escapeVariableSubstitution'.
function escapeTemplateLiterals(script: string): string {

@botelastic botelastic bot added the Team:obs-ux-management Observability Management User Experience Team label Jul 21, 2025
@elasticmachine
Copy link
Contributor

Pinging @elastic/obs-ux-management-team (Team:obs-ux-management)

shahzad31 and others added 2 commits July 21, 2025 10:43
…etics_service/formatters/formatting_utils.ts

Co-authored-by: Copilot <[email protected]>
…etics_service/formatters/formatting_utils.ts

Co-authored-by: Copilot <[email protected]>
@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #58 / Search solution tests Search Home page Solution Nav - Search search home page with existing indices "before each" hook for "displays the feedbacks link"

Metrics [docs]

✅ unchanged

History

…etics_service/formatters/formatting_utils.ts
Copy link
Contributor

@cesco-f cesco-f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shahzad31 shahzad31 enabled auto-merge (squash) July 21, 2025 12:14
@shahzad31 shahzad31 merged commit 3102200 into elastic:main Jul 21, 2025
18 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.17, 8.18, 8.19, 9.0, 9.1

https://github.com/elastic/kibana/actions/runs/16417258446

@shahzad31 shahzad31 deleted the escape-vars branch July 21, 2025 12:45
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Jul 21, 2025
…ic#228696)

## Summary

Needed to add $ before e.g template ${url} so that it gets escaped
during heartbeat vars parsing.

### Before being passed to heartbeat

```
step("blame me",async () =>{
    const url = "https://www.google.com";
    await page.goto(`${url}`);
})
```

### After being passed to heartbeat

```
        step("bname me",async () =>{
            const url = "https://www.google.com";
            await page.goto(`$${url}`);
        })
```

You can take a look at inspect config flyout in monitor form

<img width="1728" height="879" alt="image"
src="https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076"
/>

---------

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit 3102200)
kibanamachine pushed a commit to kibanamachine/kibana that referenced this pull request Jul 21, 2025
…ic#228696)

## Summary

Needed to add $ before e.g template ${url} so that it gets escaped
during heartbeat vars parsing.

### Before being passed to heartbeat

```
step("blame me",async () =>{
    const url = "https://www.google.com";
    await page.goto(`${url}`);
})
```

### After being passed to heartbeat

```
        step("bname me",async () =>{
            const url = "https://www.google.com";
            await page.goto(`$${url}`);
        })
```

You can take a look at inspect config flyout in monitor form

<img width="1728" height="879" alt="image"
src="https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076"
/>

---------

Co-authored-by: Copilot <[email protected]>
(cherry picked from commit 3102200)
@kibanamachine
Copy link
Contributor

💔 Some backports could not be created

Status Branch Result
8.17 Backport failed because of merge conflicts
8.18 Backport failed because of merge conflicts
8.19
9.0 Backport failed because of merge conflicts
9.1

Note: Successful backport PRs will be merged automatically after passing CI.

Manual backport

To create the backport manually run:

node scripts/backport --pr 228696

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Jul 21, 2025
…228696) (#228776)

# Backport

This will backport the following commits from `main` to `9.1`:
- [[Synthetics] Fix escape vars template literal inline script !!
(#228696)](#228696)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-07-21T12:41:07Z","message":"[Synthetics]
Fix escape vars template literal inline script !! (#228696)\n\n##
Summary\n\nNeeded to add $ before e.g template ${url} so that it gets
escaped\nduring heartbeat vars parsing.\n\n### Before being passed to
heartbeat\n\n```\nstep(\"blame me\",async () =>{\n const url =
\"https://www.google.com\";\n await page.goto(`${url}`);\n})\n```\n\n###
After being passed to heartbeat\n\n```\n step(\"bname me\",async ()
=>{\n const url = \"https://www.google.com\";\n await
page.goto(`${url}`);\n })\n```\n\nYou can take a look at inspect config
flyout in monitor form \n\n<img width=\"1728\" height=\"879\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076\"\n/>\n\n---------\n\nCo-authored-by:
Copilot
<[email protected]>","sha":"31022002548f6112ead4cd6db2052f9dd1cbe789","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:obs-ux-management","backport:version","v8.18.0","v9.1.0","v8.19.0","author:obs-ux-management","v9.2.0","v8.17.9","v9.0.5"],"title":"[Synthetics]
Fix escape vars template literal inline script
!!","number":228696,"url":"https://github.com/elastic/kibana/pull/228696","mergeCommit":{"message":"[Synthetics]
Fix escape vars template literal inline script !! (#228696)\n\n##
Summary\n\nNeeded to add $ before e.g template ${url} so that it gets
escaped\nduring heartbeat vars parsing.\n\n### Before being passed to
heartbeat\n\n```\nstep(\"blame me\",async () =>{\n const url =
\"https://www.google.com\";\n await page.goto(`${url}`);\n})\n```\n\n###
After being passed to heartbeat\n\n```\n step(\"bname me\",async ()
=>{\n const url = \"https://www.google.com\";\n await
page.goto(`${url}`);\n })\n```\n\nYou can take a look at inspect config
flyout in monitor form \n\n<img width=\"1728\" height=\"879\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076\"\n/>\n\n---------\n\nCo-authored-by:
Copilot
<[email protected]>","sha":"31022002548f6112ead4cd6db2052f9dd1cbe789"}},"sourceBranch":"main","suggestedTargetBranches":["8.18","9.1","8.19","8.17","9.0"],"targetPullRequestStates":[{"branch":"8.18","label":"v8.18.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.1","label":"v9.1.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/228696","number":228696,"mergeCommit":{"message":"[Synthetics]
Fix escape vars template literal inline script !! (#228696)\n\n##
Summary\n\nNeeded to add $ before e.g template ${url} so that it gets
escaped\nduring heartbeat vars parsing.\n\n### Before being passed to
heartbeat\n\n```\nstep(\"blame me\",async () =>{\n const url =
\"https://www.google.com\";\n await page.goto(`${url}`);\n})\n```\n\n###
After being passed to heartbeat\n\n```\n step(\"bname me\",async ()
=>{\n const url = \"https://www.google.com\";\n await
page.goto(`${url}`);\n })\n```\n\nYou can take a look at inspect config
flyout in monitor form \n\n<img width=\"1728\" height=\"879\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076\"\n/>\n\n---------\n\nCo-authored-by:
Copilot
<[email protected]>","sha":"31022002548f6112ead4cd6db2052f9dd1cbe789"}},{"branch":"8.17","label":"v8.17.9","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.0","label":"v9.0.5","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Shahzad <[email protected]>
Co-authored-by: Copilot <[email protected]>
Bluefinger pushed a commit to Bluefinger/kibana that referenced this pull request Jul 22, 2025
…ic#228696)

## Summary

Needed to add $ before e.g template ${url} so that it gets escaped
during heartbeat vars parsing.

### Before being passed to heartbeat

```
step("blame me",async () =>{
    const url = "https://www.google.com";
    await page.goto(`${url}`);
})
```

### After being passed to heartbeat

```
        step("bname me",async () =>{
            const url = "https://www.google.com";
            await page.goto(`$${url}`);
        })
```

You can take a look at inspect config flyout in monitor form 

<img width="1728" height="879" alt="image"
src="https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076"
/>

---------

Co-authored-by: Copilot <[email protected]>
@kibanamachine kibanamachine added the backport missing Added to PRs automatically when the are determined to be missing a backport. label Jul 22, 2025
@kibanamachine
Copy link
Contributor

Looks like this PR has backport PRs but they still haven't been merged. Please merge them ASAP to keep the branches relatively in sync.
cc: @shahzad31

kibanamachine added a commit that referenced this pull request Jul 22, 2025
…#228696) (#228775)

# Backport

This will backport the following commits from `main` to `8.19`:
- [[Synthetics] Fix escape vars template literal inline script !!
(#228696)](#228696)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"[email protected]"},"sourceCommit":{"committedDate":"2025-07-21T12:41:07Z","message":"[Synthetics]
Fix escape vars template literal inline script !! (#228696)\n\n##
Summary\n\nNeeded to add $ before e.g template ${url} so that it gets
escaped\nduring heartbeat vars parsing.\n\n### Before being passed to
heartbeat\n\n```\nstep(\"blame me\",async () =>{\n const url =
\"https://www.google.com\";\n await page.goto(`${url}`);\n})\n```\n\n###
After being passed to heartbeat\n\n```\n step(\"bname me\",async ()
=>{\n const url = \"https://www.google.com\";\n await
page.goto(`${url}`);\n })\n```\n\nYou can take a look at inspect config
flyout in monitor form \n\n<img width=\"1728\" height=\"879\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076\"\n/>\n\n---------\n\nCo-authored-by:
Copilot
<[email protected]>","sha":"31022002548f6112ead4cd6db2052f9dd1cbe789","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:obs-ux-management","backport:version","v8.18.0","v9.1.0","v8.19.0","author:obs-ux-management","v9.2.0","v8.17.9","v9.0.5"],"title":"[Synthetics]
Fix escape vars template literal inline script
!!","number":228696,"url":"https://github.com/elastic/kibana/pull/228696","mergeCommit":{"message":"[Synthetics]
Fix escape vars template literal inline script !! (#228696)\n\n##
Summary\n\nNeeded to add $ before e.g template ${url} so that it gets
escaped\nduring heartbeat vars parsing.\n\n### Before being passed to
heartbeat\n\n```\nstep(\"blame me\",async () =>{\n const url =
\"https://www.google.com\";\n await page.goto(`${url}`);\n})\n```\n\n###
After being passed to heartbeat\n\n```\n step(\"bname me\",async ()
=>{\n const url = \"https://www.google.com\";\n await
page.goto(`${url}`);\n })\n```\n\nYou can take a look at inspect config
flyout in monitor form \n\n<img width=\"1728\" height=\"879\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076\"\n/>\n\n---------\n\nCo-authored-by:
Copilot
<[email protected]>","sha":"31022002548f6112ead4cd6db2052f9dd1cbe789"}},"sourceBranch":"main","suggestedTargetBranches":["8.18","9.1","8.19","8.17","9.0"],"targetPullRequestStates":[{"branch":"8.18","label":"v8.18.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.1","label":"v9.1.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/228696","number":228696,"mergeCommit":{"message":"[Synthetics]
Fix escape vars template literal inline script !! (#228696)\n\n##
Summary\n\nNeeded to add $ before e.g template ${url} so that it gets
escaped\nduring heartbeat vars parsing.\n\n### Before being passed to
heartbeat\n\n```\nstep(\"blame me\",async () =>{\n const url =
\"https://www.google.com\";\n await page.goto(`${url}`);\n})\n```\n\n###
After being passed to heartbeat\n\n```\n step(\"bname me\",async ()
=>{\n const url = \"https://www.google.com\";\n await
page.goto(`${url}`);\n })\n```\n\nYou can take a look at inspect config
flyout in monitor form \n\n<img width=\"1728\" height=\"879\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076\"\n/>\n\n---------\n\nCo-authored-by:
Copilot
<[email protected]>","sha":"31022002548f6112ead4cd6db2052f9dd1cbe789"}},{"branch":"8.17","label":"v8.17.9","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"9.0","label":"v9.0.5","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Shahzad <[email protected]>
Co-authored-by: Copilot <[email protected]>
@kibanamachine kibanamachine removed the backport missing Added to PRs automatically when the are determined to be missing a backport. label Jul 22, 2025
kertal pushed a commit to kertal/kibana that referenced this pull request Jul 25, 2025
…ic#228696)

## Summary

Needed to add $ before e.g template ${url} so that it gets escaped
during heartbeat vars parsing.

### Before being passed to heartbeat

```
step("blame me",async () =>{
    const url = "https://www.google.com";
    await page.goto(`${url}`);
})
```

### After being passed to heartbeat

```
        step("bname me",async () =>{
            const url = "https://www.google.com";
            await page.goto(`$${url}`);
        })
```

You can take a look at inspect config flyout in monitor form 

<img width="1728" height="879" alt="image"
src="https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076"
/>

---------

Co-authored-by: Copilot <[email protected]>
crespocarlos pushed a commit to crespocarlos/kibana that referenced this pull request Jul 25, 2025
…ic#228696)

## Summary

Needed to add $ before e.g template ${url} so that it gets escaped
during heartbeat vars parsing.

### Before being passed to heartbeat

```
step("blame me",async () =>{
    const url = "https://www.google.com";
    await page.goto(`${url}`);
})
```

### After being passed to heartbeat

```
        step("bname me",async () =>{
            const url = "https://www.google.com";
            await page.goto(`$${url}`);
        })
```

You can take a look at inspect config flyout in monitor form 

<img width="1728" height="879" alt="image"
src="https://github.com/user-attachments/assets/0078ff6d-c581-45db-b87e-384b96d52076"
/>

---------

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author:obs-ux-management PRs authored by the obs ux management team backport:version Backport to applied version labels release_note:fix Team:obs-ux-management Observability Management User Experience Team v8.19.0 v9.1.0 v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Synthetics] Use client/server validation to guard against JS template strings in UI monitors
4 participants