Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.

Security - Add Content Security Policy #2288

Merged
merged 8 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions browser/src/Services/Configuration/FileConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as fs from "fs"
import * as isError from "lodash/isError"
import * as mkdirp from "mkdirp"
import * as path from "path"
import * as vm from "vm"

import "rxjs/add/operator/debounceTime"
import { Subject } from "rxjs/Subject"
Expand All @@ -18,6 +19,7 @@ import { Event, IEvent } from "oni-types"

import { IConfigurationProvider } from "./Configuration"
import { IConfigurationValues } from "./IConfigurationValues"
// import * as Utility from "./../../Utility"

const CONFIG_UPDATE_DEBOUNCE_TIME = 100 /*ms */

Expand Down Expand Up @@ -126,15 +128,30 @@ export class FileConfigurationProvider implements IConfigurationProvider {
if (fs.existsSync(this._configurationFilePath)) {
try {
const configurationContent = fs.readFileSync(this._configurationFilePath, "utf-8")

// Wrap as commonjs module and execute it to use current file path, and so resolve module relativly to current process
const module = { exports: {} }
Function("require", "exports", "module", configurationContent)(
(global as any).require,
module.exports,
module,
)
userRuntimeConfig = promoteConfigurationToRootLevel(module.exports)
const script = new vm.Script(configurationContent, {
filename: __filename,
})

const windowAsAny = window as any
const sandbox = {
console,
__filename,
__dirname,
module: {} as any,
require: (str: string) => {
const val = windowAsAny.require(str)
return val
},
exports: {},
}
const context = vm.createContext(sandbox)
script.runInContext(context)

const exports = sandbox.module
? sandbox.module.exports || sandbox.exports
: sandbox.exports

userRuntimeConfig = promoteConfigurationToRootLevel(exports)
} catch (e) {
e.message =
"[Config Error] Failed to parse " +
Expand Down
38 changes: 38 additions & 0 deletions index.dev.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>ONI</title>
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">
<style>
html, body {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
overflow: hidden;
}
body {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 14px;
}

script {
display: none;
}

@keyframes spinner-rotate {
0% { transform: rotateY(0deg); }
100% { transform: rotateY(360deg); }
}

.webpack-loading-image {
animation: spinner-rotate 2s linear infinite;
}
</style>
</head>
<body>
<div id="host"></div>
<script src="preload.js"></script>
</body>
</html>
13 changes: 2 additions & 11 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="UTF-8">

<meta http-equiv="Content-Security-Policy" content="default-src *; img-src blob: data: *; script-src 'self' http://localhost:8191; style-src 'self' 'unsafe-inline'; media-src blob: data: mediastream: *;">
<title>ONI</title>
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">
<style>
Expand Down Expand Up @@ -34,15 +34,6 @@
</head>
<body>
<div id="host"></div>
<script>
console.timeStamp("browser.domloaded")
var path = "lib/browser/bundle.js"
if(process.env.ONI_WEBPACK_LOAD) {
path = "scripts/dev_webpack_loader.js"
}
var scriptTag = document.createElement("script");
scriptTag.src = path
document.body.appendChild(scriptTag);
</script>
<script src="preload.js"></script>
</body>
</html>
4 changes: 3 additions & 1 deletion main/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export function createWindow(

const rootPath = path.join(__dirname, "..", "..", "..")
const iconPath = path.join(rootPath, "images", "oni.ico")
const indexPath = path.join(rootPath, "index.html?react_perf")

const indexFileName = process.env.ONI_WEBPACK_LOAD ? "index.dev.html" : "index.html"
const indexPath = path.join(rootPath, indexFileName + "?react_perf")
// Create the browser window.
// TODO: Do we need to use non-ico for other platforms?
let currentWindow = new BrowserWindow({
Expand Down
12 changes: 12 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (!process.env.ONI_WEBPACK_LOAD) {
window.eval = global.eval = () => console.warn("eval is not available")
}

console.timeStamp("browser.domloaded")
var path = "lib/browser/bundle.js"
if (process.env.ONI_WEBPACK_LOAD) {
path = "scripts/dev_webpack_loader.js"
}
var scriptTag = document.createElement("script")
scriptTag.src = path
document.body.appendChild(scriptTag)