[ML] Set noopener feature when opening non-Kibana custom URLs (#36205) (#36272)

* [ML] Set noopener feature when opening non Kibana custom URLs

* [ML] Refactor calls to open window into separate utils function
This commit is contained in:
Pete Harverson 2019-05-08 15:59:04 +01:00 committed by GitHub
parent 9079b260fe
commit b30723ab96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 7 deletions

View file

@ -29,7 +29,7 @@ import { parseInterval } from '../../../common/util/parse_interval';
import { getFieldTypeFromMapping } from '../../services/mapping_service';
import { ml } from '../../services/ml_api_service';
import { mlJobService } from '../../services/job_service';
import { getUrlForRecord } from '../../util/custom_url_utils';
import { getUrlForRecord, openCustomUrlWindow } from '../../util/custom_url_utils';
import { formatHumanReadableDateTimeSeconds } from '../../util/date_utils';
import { getIndexPatterns } from '../../util/index_utils';
import { replaceStringTokens } from '../../util/string_utils';
@ -119,8 +119,7 @@ export const LinksMenu = injectI18n(class LinksMenu extends Component {
// Replace any tokens in the configured url_value with values from the source record,
// and then open link in a new tab/window.
const urlPath = replaceStringTokens(customUrl.url_value, record, true);
window.open(urlPath, '_blank');
openCustomUrlWindow(urlPath, customUrl);
}).catch((resp) => {
console.log('openCustomUrl(): error loading categoryDefinition:', resp);
toastNotifications.addDanger(intl.formatMessage({
@ -135,7 +134,7 @@ export const LinksMenu = injectI18n(class LinksMenu extends Component {
// Replace any tokens in the configured url_value with values from the source record,
// and then open link in a new tab/window.
const urlPath = getUrlForRecord(customUrl, record);
window.open(urlPath, '_blank');
openCustomUrlWindow(urlPath, customUrl);
}
};

View file

@ -26,7 +26,7 @@ import {
import { toastNotifications } from 'ui/notify';
import { isValidLabel } from '../../../util/custom_url_utils';
import { isValidLabel, openCustomUrlWindow } from '../../../util/custom_url_utils';
import { getTestUrl } from '../../../jobs/components/custom_url_editor/utils';
import { parseInterval } from '../../../../common/util/parse_interval';
@ -107,7 +107,7 @@ export const CustomUrlList = injectI18n(class CustomUrlList extends Component {
if (index < customUrls.length) {
getTestUrl(job, customUrls[index])
.then((testUrl) => {
window.open(testUrl, '_blank');
openCustomUrlWindow(testUrl, customUrls[index]);
})
.catch((resp) => {
console.log('Error obtaining URL for test:', resp);

View file

@ -36,6 +36,7 @@ import {
loadSavedDashboards,
loadIndexPatterns,
} from '../edit_utils';
import { openCustomUrlWindow } from '../../../../../util/custom_url_utils';
import { FormattedMessage, injectI18n } from '@kbn/i18n/react';
@ -135,7 +136,7 @@ class CustomUrlsUI extends Component {
.then((customUrl) => {
getTestUrl(job, customUrl)
.then((testUrl) => {
window.open(testUrl, '_blank');
openCustomUrlWindow(testUrl, customUrl);
})
.catch((resp) => {
console.log('Error obtaining URL for test:', resp);

View file

@ -61,6 +61,23 @@ export function getUrlForRecord(urlConfig, record) {
}
}
// Opens the specified URL in a new window. The behaviour (for example whether
// it opens in a new tab or window) is determined from the original configuration
// object which indicates whether it is opening a Kibana page running on the same server.
// 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) {
window.open(fullUrl, '_blank');
} else {
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;
}
}
}
// Returns whether the url_value of the supplied config is for
// a Kibana page running on the same server as this ML plugin.
function isKibanaUrl(urlConfig) {