[ML] Set noopener feature for custom URLs which use absolute URLs (#39161) (#39243)

* [ML] Set noopener feature for custom URLs which use absolute URLs

* [ML] Add comments around regex testing for external URL
This commit is contained in:
Pete Harverson 2019-06-19 14:56:07 +01:00 committed by GitHub
parent 7c1ed64c03
commit 0468537fd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -67,10 +67,13 @@ export function getUrlForRecord(urlConfig, record) {
// fullUrl is the complete URL, including the base path, with any dollar delimited tokens
// from the urlConfig having been substituted with values from an anomaly record.
export function openCustomUrlWindow(fullUrl, urlConfig) {
if (isKibanaUrl(urlConfig) === true) {
// Run through a regex to test whether the url_value starts with a protocol scheme.
if (/^(?:[a-z]+:)?\/\//i.test(urlConfig.url_value) === false) {
window.open(fullUrl, '_blank');
} else {
// Add noopener and noreferrr properties for external URLs.
const newWindow = window.open(fullUrl, '_blank', 'noopener,noreferrer');
// Expect newWindow to be null, but just in case if not, reset the opener link.
if (newWindow !== undefined && newWindow !== null) {
newWindow.opener = null;
@ -79,7 +82,7 @@ export function openCustomUrlWindow(fullUrl, urlConfig) {
}
// Returns whether the url_value of the supplied config is for
// a Kibana page running on the same server as this ML plugin.
// a Kibana Discover or Dashboard page running on the same server as this ML plugin.
function isKibanaUrl(urlConfig) {
const urlValue = urlConfig.url_value;
return urlValue.startsWith('kibana#/discover') || urlValue.startsWith('kibana#/dashboard');