forked from yeswehack/yeswehack_vdp_finder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground-script.js
More file actions
277 lines (231 loc) · 8.33 KB
/
background-script.js
File metadata and controls
277 lines (231 loc) · 8.33 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const API_ALL_SCOPES_URL = "https://firebounty.com/api/v1/scope/all/url_only/"
const PULIC_SUFFIX_LIST_URL = "https://publicsuffix.org/list/public_suffix_list.dat"
const SECURITY_TXT_PATHS = ['/.well-known/security.txt', '/security.txt'];
const SECURITY_TXT_CACHE_DURATION = 1000 * 60 * 60 * 24
const ANDROID_APP_REGEX = /^[a-z0-9]+\.([a-z0-9]+\.)*[a-z0-9]+$/
const PLAY_STORE_REGEX = /^https?:\/\/play\.google\.com\/store\/apps\/details/
const PLAY_STORE_TESTING_REGEX = /^https:\/\/play\.google\.com\/apps\/testing\/([a-z0-9]+\.([a-z0-9]+\.)*[a-z0-9]+)/
const IOS_APP_REGEX = /^id[0-9]+$/
const APP_STORE_REGEX = /^https?:\/\/(itunes|apps)\.apple\.com\//
const CACHE = { programs: [], security_txt: {}, tab_info: {}, last_programs_update: null, suffix_list: [] };
function get_android_id(uri) {
const url = new URL(uri)
const id = url.searchParams.get("id")
return id ? id : null
}
function get_ios_id(uri) {
const url = new URL(uri)
const id = url.pathname.split("/").pop();
return IOS_APP_REGEX.exec(id) ? id : null
}
function update_color() {
chrome.browserAction.setIcon({ path: "res/images/FF_ext_icon_" + color + ".svg" });
}
function getTab() {
return new Promise(resolve => {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
resolve(tabs[0])
})
})
}
function findTopDomain(url) {
const top = CACHE.suffix_list.find(top => url.endsWith(top))
if (!top) {
return url
}
const pos = url.length - top.length
return url.substr(0, pos).split(".").pop()
}
function get_domain_info(urlStr) {
let url;
let lax = false;
try {
url = new URL(urlStr)
} catch (e) {
return {}
}
let programs = CACHE.programs.filter(pgm => pgm.web_scopes.some(reg => reg.exec(url)))
if (PLAY_STORE_REGEX.exec(urlStr)) {
const app_id = get_android_id(urlStr)
const android_programs = CACHE.programs.filter(pgm => pgm.android_scopes.some(id => id == app_id))
programs = [...programs, ...android_programs]
}
if (APP_STORE_REGEX.exec(urlStr)) {
const app_id = get_ios_id(urlStr)
const ios_programs = CACHE.programs.filter(pgm => pgm.ios_scopes.some(id => id == app_id))
programs = [...programs, ...ios_programs]
}
if (programs.length == 0) {
const topDomain = findTopDomain(url.hostname)
if (topDomain) {
programs = CACHE.programs.filter(pgm => pgm.name.toLowerCase().replace(/ /g, "").indexOf(topDomain) >= 0)
if (programs.length)
lax = true;
}
}
const hostname = url.hostname
const security_txt = CACHE.security_txt[hostname] || { found: false }
let color = "red"
if (programs.length) {
color = lax ? "orange" : "green"
} else if (security_txt.found) {
color = "orange"
}
chrome.browserAction.setIcon({ path: "res/images/FF_ext_icon_" + color + ".svg" });
return { programs: programs, security_txt: security_txt, color: color, last_programs_update: CACHE.last_programs_update, lax: lax }
}
function scope_to_ios_id(scope) {
if (IOS_APP_REGEX.exec(scope)) {
return scope
}
if (APP_STORE_REGEX.exec(scope)) {
return get_ios_id(scope)
}
return null
}
function scope_to_android_id(scope) {
if (ANDROID_APP_REGEX.exec(scope)) {
return scope
}
if (PLAY_STORE_REGEX.exec(scope)) {
return get_android_id(scope)
}
if (match = PLAY_STORE_TESTING_REGEX.exec(scope)) {
return match[1]
}
return null
}
function scope_to_regex(scope) {
// extract protocol
let proto = 'https?://'
if ((match = /^(https?:\/\/).*/.exec(scope))) {
proto = match[1]
scope = scope.substr(proto.length)
}
// escape all regex char except *, * is replaced by .*
let reg = scope.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/[*]/g, '.$&')
// most of the time *.example.com also means example.com
if (reg.startsWith(".*\\.")) {
reg = `(?:.+\\.)?${reg.substr(4)}`
}
return new RegExp(`^${proto}${reg}`)
}
function convert_program(program) {
const web_scopes = program.scopes.in_scopes.filter(scope => scope.scope_type == "web_application")
.map(scope => scope_to_regex(scope.scope))
const android_scopes = program.scopes.in_scopes.filter(scope => scope.scope_type == "android_application")
.map(scope => scope_to_android_id(scope.scope)).filter(id => id !== null)
const ios_scopes = program.scopes.in_scopes.filter(scope => scope.scope_type == "ios_application")
.map(scope => scope_to_ios_id(scope.scope)).filter(id => id !== null)
if (program.tag == "cvd" &&
web_scopes.length == 0 &&
android_scopes.length == 0 &&
ios_scopes.length == 0) {
return {
...program,
web_scopes: [scope_to_regex(new URL(program.url).hostname)],
android_scopes: [],
ios_scopes: []
}
}
return { ...program, web_scopes: web_scopes, android_scopes: android_scopes, ios_scopes: ios_scopes }
}
async function update_public_suffix_list() {
await fetch(PULIC_SUFFIX_LIST_URL).then(r => r.text()).then(data => {
const suffix_list = data.split("\n")
.filter(line => !line.startsWith("//") && line)
.map(line => `.${line}`)
.sort((a, b) => b.length - a.length)
CACHE.suffix_list = suffix_list
})
}
async function update_all_scopes(force_update) {
const cache = force_update ? 'reload' : 'no-cache'
const scopes = await fetch(API_ALL_SCOPES_URL, { cache: cache }).then(r => r.json()).catch(err => {
console.error(`Request all scopes failed with error: ${err}`);
return
})
if (scopes) {
const programs = scopes.pgms.map(convert_program)
CACHE.programs = programs
CACHE.last_programs_update = new Date().toLocaleString();
}
}
async function check_security_txt(urlStr, force_update) {
let hostname, protocol;
try {
const url = new URL(urlStr)
hostname = url.hostname
protocol = url.protocol
if (["http:", "https:"].indexOf(protocol) < 0)
return
} catch (e) {
return;
}
const current_date = new Date()
const entry = CACHE.security_txt[hostname]
/* Check if already visited */
if (!force_update && entry && (current_date - entry.last_update) < SECURITY_TXT_CACHE_DURATION) {
return
}
/* Reset entry */
CACHE.security_txt[hostname] = {
last_update: current_date,
content: "",
found: false,
}
for (let i = 0; i < SECURITY_TXT_PATHS.length; ++i) {
const path = SECURITY_TXT_PATHS[i]
const security_txt_url = `${protocol}//${hostname}${path}`
const content = await fetch(security_txt_url, { cache: 'no-cache', redirect: 'manual' })
.then(r => r.status == 200 && r.text())
.then(txt => txt && !txt.startsWith("<") && txt)
if (content) {
/* update and stop if found */
CACHE.security_txt[hostname].content = content
CACHE.security_txt[hostname].url = security_txt_url
CACHE.security_txt[hostname].found = true
return
}
}
}
async function update_cache(force_update) {
return Promise.all([
update_public_suffix_list(),
update_all_scopes(force_update)
])
}
chrome.tabs.onUpdated.addListener(_ => {
getTab().then(tab => {
check_security_txt(tab.url, false).then(_ => {
get_domain_info(tab.url)
})
})
});
chrome.tabs.onActivated.addListener(_ => {
getTab().then(tab => {
check_security_txt(tab.url, false).then(_ => {
get_domain_info(tab.url)
})
})
});
chrome.runtime.onInstalled.addListener(_ => {
update_cache(false)
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
switch (request.msg) {
case "FETCH_ALL_SCOPES":
Promise.all([
update_cache(true),
check_security_txt(request.data)
]).then(sendResponse).catch(_ => console.error("ERROR"))
break;
case "GET_DOMAIN_INFO":
sendResponse(get_domain_info(request.data))
break;
default:
console.error(`Unhandled message "${request.msg}"`)
break;
}
return true;
})