Skip to content

[8.18] Fix fallback when Clipboard API is unavailable (#229531)#229652

Merged
kibanamachine merged 1 commit intoelastic:8.18from
kibanamachine:backport/8.18/pr-229531
Jul 28, 2025
Merged

[8.18] Fix fallback when Clipboard API is unavailable (#229531)#229652
kibanamachine merged 1 commit intoelastic:8.18from
kibanamachine:backport/8.18/pr-229531

Conversation

@kibanamachine
Copy link
Copy Markdown
Contributor

Backport

This will backport the following commits from main to 8.18:

Questions ?

Please refer to the Backport tool documentation

## Summary
When accessing Kibana from an insecure browser context (e.g.
`http://test.local`), some of Discover's "Copy to clipboard" actions
have no effect (they don't copy data to the clipboard), despite showing
a toast that says the data was copied.

## Analysis
From the main Kibana Discover page, there are several different ways to
copy to clipboard, with varying implementations:

- ✅ Table Cell > Copy value of fields.type (`copyValueToClipboard`)
- ✅ Table Header > Copy name (`copyColumnNameToClipboard`)
- ⚠️ Table Header > Copy column (`copyColumnValuesToClipboard`)
- ⚠️ Selected > Copy selection as text (`copyRowsAsTextToClipboard`)
- ⚠️ Selected > Copy documents as JSON (`copyRowsAsJsonToClipboard`)
- ✅ Document sidebar > JSON > Copy to clipboard (`copyToClipboard`)

The methods that **✅ work in both secure and insecure contexts** use
[EUI
`copyToClipboard`](https://github.com/elastic/eui/blob/6069ba651329bb2f65e6e5f8641b29e6b16198c6/packages/eui/src/services/copy/copy_to_clipboard.ts#L29-L58),
which creates a hidden text element, fills it with data, selects it, and
simulates the user invoking the "copy" action.

The methods that **⚠️ only work in secure context** call `await
window.navigator?.clipboard?.writeText(textToCopy);`. But since the
[JavaScript Clipboard
API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) is
unavailable in that context (`window.navigator.clipboard` is undefined),
this has no effect: it neither copies nor throws an exception. If an
exception is thrown, it falls back to EUI `copyToClipboard` — but since
no exception is thrown, it skips this fallback.

This problem does not manifest in the default Kibana dev environment,
`http://localhost:5601`, since [browsers consider `http://localhost`
secure even though it doesn't use
`https`](https://w3c.github.io/webappsec-secure-contexts/#localhost).

## Proposed solution
This MR fixes that fallback (by throwing an exception if the Clipboard
API is unavailable), and improves test coverage to check both when the
Clipboard API is available and unavailable. It also fixes some
false-positives in the existing tests, where some of the mocks were not
being reset between each test, leading to some execution counters
leaking from one test into the next.

## Checklist
- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
   - this MR doesn't add or change user-facing text
- [x]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
   - this MR doesn't add new features
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
   - this MR adds tests
- [x] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
   - this MR doesn't change any keys
- [x] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
   - this MR doesn't change any APIs
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
   - I don't have access to that
- [x] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
   - added release notes, but I don't have permission to add the label
- [ ] Review the [backport
guidelines](https://docs.google.com/document/d/1VyN5k91e5OVumlc0Gb9RPa3h1ewuPE705nRtioPiTvY/edit?usp=sharing)
and apply applicable `backport:*` labels.
   - I don't have access to view that document

### Identify risks
(no particular new risks)

## Release note
Fixes issue where some of Discover's "Copy to clipboard" actions only
worked when accessing Kibana in a secure browser context.

(cherry picked from commit f43256f)
@kibanamachine kibanamachine added the backport This PR is a backport of another PR label Jul 28, 2025
@kibanamachine kibanamachine enabled auto-merge (squash) July 28, 2025 14:14
@kibanamachine kibanamachine merged commit 4fe1fee into elastic:8.18 Jul 28, 2025
11 checks passed
@elasticmachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
cloudSecurityPosture 484.9KB 485.2KB +321.0B
discover 796.6KB 796.9KB +321.0B
esqlDataGrid 155.6KB 156.0KB +321.0B
securitySolution 9.1MB 9.1MB +321.0B
slo 759.6KB 760.0KB +321.0B
total +1.6KB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport This PR is a backport of another PR

3 participants