Skip to content

Commit

Permalink
Fixes microsoft/monaco-editor#601: Set window.opener to null
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Oct 24, 2017
1 parent 5a1717d commit 914c81e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/vs/base/browser/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1054,3 +1054,18 @@ export function computeScreenAwareSize(cssPx: number): number {
const screenPx = window.devicePixelRatio * cssPx;
return Math.max(1, Math.floor(screenPx)) / window.devicePixelRatio;
}

/**
* See https://github.com/Microsoft/monaco-editor/issues/601
* To protect against malicious code in the linked site, particularly phishing attempts,
* the window.opener should be set to null to prevent the linked site from having access
* to change the location of the current page.
* See https://mathiasbynens.github.io/rel-noopener/
*/
export function windowOpenNoOpener(url: string): void {
let newTab = window.open();
if (newTab) {
newTab.opener = null;
newTab.location.href = url;
}
}
2 changes: 1 addition & 1 deletion src/vs/editor/standalone/browser/simpleServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class SimpleEditorService implements IEditorService {
let schema = data.resource.scheme;
if (schema === Schemas.http || schema === Schemas.https) {
// This is a fully qualified http or https URL
window.open(data.resource.toString());
dom.windowOpenNoOpener(data.resource.toString());
return this.editor;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/vs/platform/opener/browser/openerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

import URI from 'vs/base/common/uri';
import * as dom from 'vs/base/browser/dom';
import { parse } from 'vs/base/common/marshalling';
import { Schemas } from 'vs/base/common/network';
import { TPromise } from 'vs/base/common/winjs.base';
Expand Down Expand Up @@ -41,7 +42,7 @@ export class OpenerService implements IOpenerService {
let promise: TPromise<any>;
if (scheme === Schemas.http || scheme === Schemas.https) {
// open http
window.open(resource.toString(true));
dom.windowOpenNoOpener(resource.toString(true));

} else if (scheme === 'command' && CommandsRegistry.getCommand(path)) {
// execute as command
Expand Down

0 comments on commit 914c81e

Please sign in to comment.