forked from Txio-labs/txio-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (103 loc) · 3.51 KB
/
Copy pathindex.js
File metadata and controls
120 lines (103 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { app, BrowserWindow, shell, session, protocol, net } from "electron"
import path from "path"
import { fileURLToPath } from "url"
import crypto from "crypto"
import fs from "fs"
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// Scope navigation to the app's own packaged directory, not the entire file:// scheme.
// This prevents a compromised renderer from navigating to arbitrary local files
// (e.g. file:///etc/passwd) while still allowing in-app navigation.
const APP_ROOT_URL = pathToFileURL(path.join(__dirname, "..")).href
const CSP_NONCE = crypto.randomBytes(16).toString("base64")
const CSP = [
"default-src 'self'",
`script-src 'self' 'nonce-${CSP_NONCE}'`,
"style-src 'self' 'unsafe-inline'",
"img-src 'self' data: blob: https:",
"font-src 'self' data:",
"connect-src 'self' https: wss:",
"frame-src 'self' https:",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
].join("; ")
const createWindow = () => {
const win = new BrowserWindow({
width: 1200,
height: 800,
icon: path.join(__dirname, "assets", "logo-B-VeRwKK.png"),
webPreferences: {
sandbox: true,
contextIsolation: true,
nodeIntegration: false,
webSecurity: true,
},
})
win.webContents.setWindowOpenHandler(({ url }) => {
try {
const parsed = new URL(url)
if (parsed.protocol === "http:" || parsed.protocol === "https:") {
shell.openExternal(url)
}
} catch {
// ignore invalid URLs
}
return { action: "deny" }
})
win.webContents.on("console-message", (event, level, message, line, sourceId) => {
if (message.toLowerCase().includes("content security policy") || message.toLowerCase().includes("csp")) {
console.error(`CSP Violation: ${message}`)
}
})
win.webContents.on("will-navigate", (event, url) => {
if (!url.startsWith(APP_ROOT_URL)) {
event.preventDefault()
}
})
win.loadFile(path.join(__dirname, "../dist/index.html"))
}
app.whenReady().then(() => {
protocol.handle("file", async (request) => {
try {
const filePath = fileURLToPath(request.url)
const content = await fs.promises.readFile(filePath)
const ext = path.extname(filePath).toLowerCase()
let mimeType = "application/octet-stream"
if (ext === ".html") mimeType = "text/html"
else if (ext === ".js") mimeType = "text/javascript"
else if (ext === ".css") mimeType = "text/css"
else if (ext === ".png") mimeType = "image/png"
else if (ext === ".svg") mimeType = "image/svg+xml"
else if (ext === ".json") mimeType = "application/json"
else if (ext === ".woff2") mimeType = "font/woff2"
if (ext === ".html") {
let html = content.toString("utf-8")
html = html.replace(/<script(?=[\s>])/g, `<script nonce="${CSP_NONCE}"`)
return new Response(html, { headers: { "content-type": mimeType } })
}
return new Response(content, { headers: { "content-type": mimeType } })
} catch (err) {
return new Response("Not Found", { status: 404 })
}
})
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({
responseHeaders: {
...details.responseHeaders,
"Content-Security-Policy": [CSP],
},
})
})
createWindow()
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit()
}
})