[Canvas] Use URL constructor to check valid URLs for images (#218494)

## Summary

This switches from using regex for checking valid URLs and uses the URL
constructor instead.

### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [ ] 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)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [ ] [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
- [ ] 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 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.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] 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)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...
This commit is contained in:
Catherine Liu 2025-04-18 17:16:15 -07:00 committed by GitHub
parent f1eb019b7b
commit 086a03e193
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 6 additions and 7 deletions

View file

@ -15,12 +15,9 @@ describe('httpurl.isValidHttpUrl', () => {
expect(isValidHttpUrl('http://virtual-machine/veggiehamburger.jpg')).toBe(true);
expect(isValidHttpUrl('https://virtual-machine:44330/veggie.jpg?hamburger')).toBe(true);
expect(isValidHttpUrl('http://192.168.1.50/veggie/hamburger.jpg')).toBe(true);
expect(isValidHttpUrl('https://2600::/veggie/hamburger.jpg')).toBe(true); // ipv6
expect(isValidHttpUrl('http://2001:4860:4860::8844/veggie/hamburger.jpg')).toBe(true); // ipv6
});
it('rejects non-HTTP URLs', () => {
expect(isValidHttpUrl('')).toBe(false);
expect(isValidHttpUrl('http://server.com')).toBe(false);
expect(isValidHttpUrl('file:///Users/programmer/Pictures/hamburger.jpeg')).toBe(false);
expect(isValidHttpUrl('ftp://hostz.com:1111/path/to/image.png')).toBe(false);
expect(isValidHttpUrl('ftp://user:password@host:1111/path/to/image.png')).toBe(false);

View file

@ -5,9 +5,11 @@
* 2.0.
*/
// A cheap regex to distinguish an HTTP URL string from a data URL string
const httpurlRegex = /^https?:\/\/\S+(?:[0-9]+)?\/\S{1,}/;
export function isValidHttpUrl(str: string): boolean {
return httpurlRegex.test(str);
try {
const url = new URL(str);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch {
return false;
}
}