This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
start.js
204 lines (195 loc) · 6.82 KB
/
start.js
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
const path = require('path')
const fs = require('fs-extra')
const ip = require('ip')
const URL = require('url').URL
const config = require('../lib/config')
const util = require('../lib/util')
const whitelistedUrlPrefixes = require('./whitelistedUrlPrefixes')
const whitelistedUrlPatterns = require('./whitelistedUrlPatterns')
const whitelistedUrlProtocols = [
'chrome-extension:',
'chrome:',
'brave:',
'file:',
'data:',
'blob:'
]
const start = (passthroughArgs, buildConfig = config.defaultBuildConfig, options) => {
config.buildConfig = buildConfig
config.update(options)
let braveArgs = [
'--enable-logging',
'--v=' + options.v,
]
if (options.vmodule) {
braveArgs.push('--vmodule=' + options.vmodule);
}
if (options.no_sandbox) {
braveArgs.push('--no-sandbox')
}
if (options.disable_brave_extension) {
braveArgs.push('--disable-brave-extension')
}
if (options.disable_brave_rewards_extension) {
braveArgs.push('--disable-brave-rewards-extension')
}
if (options.disable_pdfjs_extension) {
braveArgs.push('--disable-pdfjs-extension')
}
if (options.disable_webtorrent_extension) {
braveArgs.push('--disable-webtorrent-extension')
}
if (options.ui_mode) {
braveArgs.push(`--ui-mode=${options.ui_mode}`)
}
if (!options.enable_brave_update) {
// This only has meaning with MacOS and official build.
braveArgs.push('--disable-brave-update')
}
if (options.enable_smart_tracking_protection) {
braveArgs.push('--enable-smart-tracking-protection')
}
if (options.single_process) {
braveArgs.push('--single-process')
}
if (options.show_component_extensions) {
braveArgs.push('--show-component-extension-options')
}
if (options.rewards) {
braveArgs.push(`--rewards=${options.rewards}`)
}
if (options.brave_ads_testing) {
braveArgs.push('--brave-ads-testing')
}
if (options.brave_ads_debug) {
braveArgs.push('--brave-ads-debug')
}
if (options.brave_ads_production) {
braveArgs.push('--brave-ads-production')
}
if (options.brave_ads_staging) {
braveArgs.push('--brave-ads-staging')
}
braveArgs = braveArgs.concat(passthroughArgs)
let user_data_dir
if (options.user_data_dir_name) {
if (process.platform === 'darwin') {
user_data_dir = path.join(process.env.HOME, 'Library', 'Application\\ Support', 'GabAI', options.user_data_dir_name)
} else if (process.platform === 'win32') {
user_data_dir = path.join(process.env.LocalAppData, 'GabAI', options.user_data_dir_name)
} else {
user_data_dir = path.join(process.env.HOME, '.config', 'GabAI', options.user_data_dir_name)
}
braveArgs.push('--user-data-dir=' + user_data_dir);
}
const networkLogFile = path.resolve(path.join(__dirname, '..', 'network_log.json'))
if (options.network_log) {
braveArgs.push(`--log-net-log=${networkLogFile}`)
braveArgs.push(`--net-log-capture-mode=IncludeSocketBytes`)
if (user_data_dir) {
// clear the data directory before doing a network test
fs.removeSync(user_data_dir.replace('\\', ''))
if (fs.existsSync(networkLogFile)) {
fs.unlinkSync(networkLogFile)
}
if (fs.existsSync('network-audit-results.json')) {
fs.unlinkSync('network-audit-results.json')
}
}
}
let cmdOptions = {
stdio: 'inherit',
timeout: options.network_log ? 120000 : undefined,
continueOnFail: options.network_log ? true : false,
shell: process.platform === 'darwin' ? true : false,
killSignal: options.network_log && process.env.RELEASE_TYPE ? 'SIGKILL' : 'SIGTERM'
}
if (options.network_log) {
console.log('Network audit started. Logging requests for the next 2min or until you quit Brave...')
}
let outputPath = options.output_path
if (!outputPath) {
if (process.platform === 'darwin') {
let outputDir = config.outputDir
if (config.shouldSign()) {
outputDir = path.join(outputDir, config.mac_signing_output_prefix)
}
outputPath = path.join(outputDir,
'Dissenter\\ Browser\\ Development.app', 'Contents', 'MacOS',
'Dissenter\\ Browser\\ Development')
} else if (process.platform === 'win32') {
outputPath = path.join(config.outputDir, 'dissenter.exe')
} else {
outputPath = path.join(config.outputDir, 'dissenter')
}
}
util.run(outputPath, braveArgs, cmdOptions)
if (options.network_log) {
let exitCode = 0
let jsonOutput = {}
// Read the network log
let jsonContent = fs.readFileSync(networkLogFile, 'utf8').trim()
// On windows netlog ends abruptly causing JSON parsing errors
if (!jsonContent.endsWith('}]}')) {
const n = jsonContent.lastIndexOf('},')
jsonContent = jsonContent.substring(0, n) + '}]}'
}
jsonOutput = JSON.parse(jsonContent)
const URL_REQUEST_TYPE = jsonOutput.constants.logSourceType.URL_REQUEST
const URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED = jsonOutput.constants.logEventTypes.URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED
const urlRequests = jsonOutput.events.filter((event) => {
if (event.type === URL_REQUEST_FAKE_RESPONSE_HEADERS_CREATED) {
// showing these helps determine which URL requests which don't
// actually hit the network
return true
}
if (event.source.type === URL_REQUEST_TYPE) {
if (!event.params) {
return false
}
const url = event.params.url
if (!url) {
return false
}
const urlParsed = new URL(url)
const hostname = urlParsed.hostname
if (/^[a-z]+$/.test(hostname)) {
// Chromium sometimes sends requests to random non-resolvable hosts
return false
}
if (whitelistedUrlProtocols.includes(urlParsed.protocol)) {
return false
}
const foundPrefix = whitelistedUrlPrefixes.find((prefix) => {
return url.startsWith(prefix)
})
const foundPattern = whitelistedUrlPatterns.find((pattern) => {
return RegExp('^' + pattern).test(url)
})
if (!foundPrefix && !foundPattern) {
// Check if the URL is a private IP
try {
if (ip.isPrivate(hostname)) {
// Warn but don't fail the audit
console.log('NETWORK AUDIT WARN:', url)
return true
}
} catch (e) {}
// This is not a whitelisted URL! log it and exit with non-zero
console.log('NETWORK AUDIT FAIL:', url)
exitCode = 1
}
return true
}
return false
})
fs.writeJsonSync('network-audit-results.json', urlRequests)
if (exitCode > 0) {
console.log(`network-audit failed. import ${networkLogFile} in chrome://net-internals for more details.`)
} else {
console.log('network audit passed.')
}
process.exit(exitCode)
}
}
module.exports = start