-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
613 lines (509 loc) · 21.1 KB
/
index.js
File metadata and controls
613 lines (509 loc) · 21.1 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
"use strict";
// Dependencies
const { LocalMongo } = require("./modules/localmongo")
const { execSync } = require("child_process")
const compression = require("compression")
const { parse } = require("smol-toml")
const chalk = require("chalk").default
const express = require("express")
const helmet = require("helmet")
const ky = require("ky").default
const path = require("path")
const fs = require("fs")
// Variables
const config = parse(fs.readFileSync("./config.toml", "utf8"))
const web = express()
const port = config.web.port
const dbClient = new LocalMongo("./data")
const falsePositivesCol = db.collection("false_positives")
const monitoredCol = db.collection("monitored")
const usersCol = db.collection("users")
const scansCol = db.collection("scans")
const fixedCol = db.collection("fixed")
const db = dbClient.db("gitfaye")
const MAX_CONCURRENT_SCANS = 6
const scanQueue = []
var activeScans = 0
var adminUser;
// Functions
const log = (type, message)=>{
if(type === "i") console.log(`${chalk.cyanBright("[GITFAYE]")}${chalk.blueBright("INFORMATION")} ${message}`)
}
const initAdmin = async()=>{ // Of course we fucking add the token account itself (default).
try {
// Variables
adminUser = await ky.get("https://api.github.com/user", {
headers: { authorization: `token ${config.github.token}` }
}).json()
// Core
if (!usersCol.findOne({ login: adminUser.login })) usersCol.insertOne({
login: adminUser.login,
avatar_url: adminUser.avatar_url,
public_repos: adminUser.public_repos,
name: adminUser.name,
bio: adminUser.bio
})
}catch{}
}
const runScan = async(username, repoName, defaultBranch)=>{
// Variables
const scanId = `${username}/${repoName}`
var existing = scansCol.findOne({ id: scanId })
if (existing && existing.status === "running") return
// Core
if (!existing) {
existing = { id: scanId, username, repo: repoName, status: "running", startedAt: Date.now(), leaks: [], vulnerabilities: [], scannedCommits: [] }
scansCol.insertOne(existing)
} else {
existing.status = "running"
existing.startedAt = Date.now()
scansCol.updateOne({ id: scanId }, existing)
}
try {
const headers = { authorization: `token ${config.github.token}` }
if (!defaultBranch) {
const r = await ky.get(`https://api.github.com/repos/${username}/${repoName}`, { headers }).json()
defaultBranch = r.default_branch
}
const commits = await ky.get(`https://api.github.com/repos/${username}/${repoName}/commits`, {
headers,
searchParams: { sha: defaultBranch, per_page: 10 }
}).json()
var allVulns = existing.vulnerabilities || []
var allLeaks = existing.leaks || []
var scannedCommits = existing.scannedCommits || []
// Avoid redundancy
const seenVulnerabilities = new Set(allVulns.map((v) => v.id))
const seenLeaks = new Set(allLeaks.map((l) => l.id))
const unseenCommits = commits.filter((c) => !scannedCommits.includes(c.sha))
for ( const commit of unseenCommits ) {
// Variables
const treeData = await ky.get(`https://api.github.com/repos/${username}/${repoName}/git/trees/${commit.sha}?recursive=1`, { headers }).json()
const tree = treeData.tree || []
const commitVulns = filesMatchesPlugin(tree)
//! For files scan
commitVulns.forEach((v) => {
const id = `${v.name}-${v.matchedPaths.join(",")}`
if (!seenVulnerabilities.has(id)) {
allVulns.push({ ...v, id, commit: commit.sha.substring(0, 7) })
seenVulnerabilities.add(id)
}
})
//! For string scan
const textFiles = tree.filter((f) => f.type === "blob" && f.size < 50000 && /\.(js|json|txt|md|yml|yaml|toml|py)$/i.test(f.path)).slice(0, 5)
for ( const file of textFiles ) {
try {
const blob = await ky.get(`https://raw.githubusercontent.com/${username}/${repoName}/${commit.sha}/${file.path}`, { headers }).text()
// String Matcher
const fileLeaks = stringMatchesPlugin(blob);
fileLeaks.forEach((l) => {
const id = `${l.name}-${file.path}-${l.risk}`
if (!seenLeaks.has(id)) {
allLeaks.push({ ...l, id, path: file.path, commit: commit.sha.substring(0, 7) })
seenLeaks.add(id)
}
})
//! Hacked NPMJS check
if (file.path === "package.json") {
try {
// Variables
const pkgJson = JSON.parse(blob)
const hackedDetections = await hackedNpmjsPlugin(pkgJson)
// Core
hackedDetections.forEach((d) => {
const id = `HACKED-${d.name}-${d.version}`
if (!seenVulnerabilities.has(id)) {
allVulns.push({ ...d, id, path: file.path, commit: commit.sha.substring(0, 7) })
seenVulnerabilities.add(id)
}
})
} catch{}
}
} catch { }
}
scannedCommits.push(commit.sha)
}
scansCol.updateOne({ id: scanId }, {
...existing,
status: "completed",
vulnerabilities: allVulns,
leaks: allLeaks,
scannedCommits: scannedCommits,
completedAt: Date.now()
})
} catch {
scansCol.updateOne({ id: scanId }, { ...existing, status: "failed" })
}
}
const processScanQueue = ()=>{
// Variables
if (activeScans >= MAX_CONCURRENT_SCANS || !scanQueue.length) return
const { username, repoName, defaultBranch } = scanQueue.shift()
// Core
activeScans++
runScan(username, repoName, defaultBranch).finally(() => {
activeScans--
processScanQueue()
})
}
const queueScan = (username, repoName, defaultBranch)=>{
// Variables
if (scanQueue.some((s) => s.username === username && s.repoName === repoName)) return
// Core
scanQueue.push({ username, repoName, defaultBranch })
processScanQueue()
}
// Plugin Management
if (process.argv.includes("--update-plugins")) {
log("i", "Updating the current plugins...")
log("i", "Deleting the current plugins...")
if (fs.existsSync(path.join(__dirname, "plugins"))) fs.rmSync(path.join(__dirname, "plugins"), { recursive: true, force: true })
}
if (!fs.existsSync(path.join(__dirname, "plugins"))) {
log("i", "Downloading the plugins...")
try {
execSync("git clone https://github.com/firstdecree/gitfaye-plugins.git plugins", {
cwd: __dirname,
stdio: "ignore"
})
}catch{}
}
// Late-Dependencies
const filesMatchesPlugin = require("./plugins/files-matches")
const hackedNpmjsPlugin = require("./plugins/hacked-npmjs")
const stringMatchesPlugin = require("./plugins/string-matches")
// Configurations
//* Express
web.set("view engine", "ejs")
web.set("views", path.join(__dirname, "views"))
web.use(helmet({ contentSecurityPolicy: false }))
web.use(compression({ level: 1 }))
web.use(express.urlencoded({ extended: true }))
web.use(express.json())
web.use(express.static(path.join(__dirname, "public")))
// Main
initAdmin()
//* API
web.post("/api/add-user", async (req, res) => {
// Variables
const { username } = req.body
// Validations
if (!username) return res.redirect("/")
// Core
try {
const githubUser = await ky.get(`https://api.github.com/users/${username}`, {
headers: { authorization: `token ${config.github.token}` }
}).json()
if (!usersCol.findOne({ login: githubUser.login })) usersCol.insertOne({
login: githubUser.login,
avatar_url: githubUser.avatar_url,
public_repos: githubUser.public_repos,
name: githubUser.name,
bio: githubUser.bio
})
res.redirect("/defense")
} catch {
res.redirect("/defense")
}
})
web.get("/:username/remove", async (req, res) => {
// Variables
const { username } = req.params
// Validations
if(!username) return res.redirect("/")
// Core
usersCol.deleteOne({ login: username })
res.redirect("/defense")
})
web.get("/:username/scan-all", async (req, res) => {
// Variables
const { username } = req.params
// Validations
if(!username) return res.redirect("/")
// Core
try {
const repos = await ky.get(`https://api.github.com/users/${username}/repos`, {
headers: { authorization: `token ${config.github.token}` },
searchParams: { per_page: 100 }
}).json()
for (const r of repos) queueScan(username, r.name, r.default_branch)
} catch {}
res.redirect("/defense")
})
web.get("/scan-monitored", async (req, res) => {
// Core
try {
const monitored = monitoredCol.findMany()
for (const m of monitored) queueScan(m.username, m.repoName, m.defaultBranch)
} catch { console.error("Scan Monitored Error:", e.message) }
res.redirect(req.get("Referrer") || "/monitored")
})
//* EJS Main
web.get("/defense", async (req, res) => {
// Variables
const users = usersCol.findMany()
const scans = scansCol.findMany()
const monitored = monitoredCol.findMany()
const falsePositives = falsePositivesCol.findMany()
// Core
const fixed = fixedCol.findMany()
res.render("defense", { users, scans, monitored, falsePositives, fixed, queueLength: scanQueue.length, activeScans })
})
web.get("/monitored", async (req, res) => {
// Variables
const page = Math.max(1, parseInt(req.query.page) || 1)
const PER_PAGE = 10
const allMonitored = monitoredCol.findMany()
allMonitored.sort((a, b) => a.id.localeCompare(b.id))
const total = allMonitored.length
const scans = scansCol.findMany()
const paginated = allMonitored.slice((page - 1) * PER_PAGE, page * PER_PAGE).map((m) => {
const scan = scans.find((s) => s.id === m.id)
return { ...m, lastChecked: scan ? scan.completedAt : null }
})
const totalPages = Math.ceil(total / PER_PAGE)
// Core
res.render("monitored", {
monitoredRepos: paginated,
page,
totalPages,
hasNext: page < totalPages,
hasPrev: page > 1
})
})
web.get("/detections", async (req, res) => {
// Variables
const page = Math.max(1, parseInt(req.query.page) || 1)
const PER_PAGE = 20
const scans = scansCol.findMany().filter((s) => s.status === "completed")
var allDetections = []
// Core
for ( const scan of scans ) {
if (scan.leaks) for ( const leak of scan.leaks ) allDetections.push({ type: "leak", repo: scan.repo, username: scan.username, date: scan.completedAt, ...leak })
if (scan.vulnerabilities) for ( const vuln of scan.vulnerabilities ) allDetections.push({ type: "vulnerability", repo: scan.repo, username: scan.username, date: scan.completedAt, ...vuln })
}
allDetections.sort((a, b) => b.date - a.date) // Sort by date descending
// Filter out false positives by repository
const fpSet = new Set(falsePositivesCol.findMany().map((f) => f.fpKey || `${f.username}/${f.repo}/${f.detectionId}`))
const fixSet = new Set(fixedCol.findMany().map((f) => f.fixKey || `${f.username}/${f.repo}/${f.detectionId}`))
const showHidden = req.query.show === "hidden"
const showFixes = req.query.show === "fixes"
const processedDetections = allDetections.map((d) => {
const key = `${d.username}/${d.repo}/${d.id}`
return { ...d, isHidden: fpSet.has(key), isFixed: fixSet.has(key) }
})
var filteredDetections = processedDetections.filter((d) => !d.isHidden && !d.isFixed)
if (showHidden) filteredDetections = processedDetections.filter((d) => d.isHidden)
if (showFixes) filteredDetections = processedDetections.filter((d) => d.isFixed)
const paginated = filteredDetections.slice((page - 1) * PER_PAGE, page * PER_PAGE)
const hasNext = filteredDetections.length > page * PER_PAGE
res.render("detections", {
detections: paginated,
page,
hasNext,
hasPrev: page > 1,
showHidden,
showFixes
})
})
//* EJS Bottom
web.get("/:username", async (req, res) => {
// Variables
const { username } = req.params
// Validations
const dbUser = usersCol.findOne({ login: username })
if (!dbUser) return res.redirect("/")
// Core
try {
const ghUser = await ky.get(`https://api.github.com/users/${username}`, {
headers: { authorization: `token ${config.github.token}` }
}).json()
var fullInfo = ghUser
if (adminUser && username === adminUser.login) {
fullInfo = await ky.get("https://api.github.com/user", {
headers: { authorization: `token ${config.github.token}` }
}).json()
}
res.render("dashboard", {
user: fullInfo,
isAdmin: adminUser && username === adminUser.login
})
} catch {
res.redirect("/")
}
})
//! END 1
web.get("/:username/repositories", async (req, res) => {
// Variables
const { username } = req.params
const page = Math.max(1, parseInt(req.query.page) || 1)
const sort = ["updated", "created", "pushed", "full_name"].includes(req.query.sort) ? req.query.sort : "updated"
const type = ["all", "owner", "public", "private", "member"].includes(req.query.type) ? req.query.type : "all"
const language = req.query.language || ""
const q = req.query.q || ""
const PER_PAGE = 10
// Validations
if(!username) return res.redirect("/")
// Core
try {
// Variables
const ghUser = await ky.get(`https://api.github.com/users/${username}`, {
headers: { authorization: `token ${config.github.token}` }
}).json()
const repos = await ky.get(`https://api.github.com/users/${username}/repos`, {
headers: { authorization: `token ${config.github.token}` },
searchParams: { per_page: 100, sort, type: type === "all" ? "owner" : type } // GitHub API "type" for /users/:u/repos is limited
}).json()
// Core
const allFiltered = repos.filter((r) => {
const matchLang = language ? (r.language || "").toLowerCase() === language.toLowerCase() : true
const matchQ = q ? (r.name + (r.description || "")).toLowerCase().includes(q.toLowerCase()) : true
return matchLang && matchQ
})
const totalFiltered = allFiltered.length
const pageRepos = allFiltered.slice((page - 1) * PER_PAGE, page * PER_PAGE)
const hasNext = totalFiltered > page * PER_PAGE
const languages = [...new Set(repos.map((r) => r.language).filter(Boolean))].sort()
const monitoredList = monitoredCol.findMany().filter((m) => m.username.toLowerCase() === username.toLowerCase()).map((m) => m.repoName.toLowerCase())
res.render("repositories", {
user: ghUser,
repos: pageRepos,
page,
hasNext,
hasPrev: page > 1,
sort,
type,
language,
q,
languages,
orgs: [],
monitoredList
})
} catch {
res.redirect(`/${username}`)
}
})
web.get("/:username/monitor-all", async (req, res) => {
// Variables
const { username } = req.params
// Validations
if(!username) return res.redirect("/")
// Core
try {
const repos = await ky.get(`https://api.github.com/users/${username}/repos`, {
headers: { authorization: `token ${config.github.token}` },
searchParams: { per_page: 100 }
}).json();
for ( const r of repos ) {
const scanId = `${username}/${r.name}`
if (!monitoredCol.findOne({ id: scanId })) monitoredCol.insertOne({ id: scanId, username, repoName: r.name, defaultBranch: r.default_branch })
}
} catch {}
res.redirect(req.get("Referrer") || `/${username}/repositories`)
})
web.get("/:username/repository/:name/scan", async (req, res) => {
// Variables
const { username, name } = req.params
// Core
queueScan(username, name)
res.redirect(req.get("Referrer") || `/${username}/repositories`);
})
web.get("/:username/repository/:name/monitor", async (req, res) => {
// Variables
const { username, name } = req.params
const scanId = `${username}/${name}`
// COre
if (!monitoredCol.findOne({ id: scanId })) monitoredCol.insertOne({ id: scanId, username, repoName: name })
res.redirect(req.get("Referrer") || `/${username}/repositories`)
})
web.get("/:username/repository/:name/unmonitor", async (req, res) => {
// Variables
const { username, name } = req.params
const scanId = `${username}/${name}`
// Core
monitoredCol.deleteOne({ id: scanId })
res.redirect(req.get("Referrer") || `/${username}/repositories`)
})
//! END 2
web.get("/:username/repository/:repo/detection/:id/ignore", async (req, res) => {
// Variables
const { username, repo, id } = req.params
const fpKey = `${username}/${repo}/${id}`
// Core
if (!falsePositivesCol.findOne({ fpKey })) falsePositivesCol.insertOne({ fpKey, detectionId: id, username, repo, addedAt: Date.now() })
res.redirect(req.get("Referrer") || "/detections")
})
web.get("/:username/repository/:repo/detection/:id/remove-ignore", async (req, res) => {
// Variables
const { username, repo, id } = req.params
// Core
falsePositivesCol.deleteOne({ detectionId: id, username, repo })
res.redirect(req.get("Referrer") || "/detections")
})
web.get("/:username/repository/:repo/detection/:id/fixed", async (req, res) => {
// Variables
const { username, repo, id } = req.params
const fixKey = `${username}/${repo}/${id}`
// Core
if (!fixedCol.findOne({ fixKey })) fixedCol.insertOne({ fixKey, detectionId: id, username, repo, addedAt: Date.now() })
res.redirect(req.get("Referrer") || "/detections")
})
web.get("/:username/repository/:repo/detection/:id/remove-fixed", async (req, res) => {
// Variables
const { username, repo, id } = req.params
// Core
fixedCol.deleteOne({ detectionId: id, username, repo })
res.redirect(req.get("Referrer") || "/detections")
})
web.get("/:username/repository/:name", async (req, res) => {
// Variables
const { username, name } = req.params
const filePath = req.query.path || ""
const branch = req.query.branch || ""
// Core
try {
// Variables
const headers = { authorization: `token ${config.github.token}` }
const repoPath = `${username}/${name}`
const [ repoInfo, branches, langs, contributorsList ] = await Promise.all([
ky.get(`https://api.github.com/repos/${repoPath}`, { headers }).json(),
ky.get(`https://api.github.com/repos/${repoPath}/branches`, { headers }).json(),
ky.get(`https://api.github.com/repos/${repoPath}/languages`, { headers }).json(),
ky.get(`https://api.github.com/repos/${repoPath}/contributors`, { headers }).json()
])
const ref = branch || repoInfo.default_branch
const [ rawContents, commits ] = await Promise.all([
ky.get(`https://api.github.com/repos/${repoPath}/contents/${filePath}`, { headers, searchParams: { ref } }).json(),
ky.get(`https://api.github.com/repos/${repoPath}/commits`, { headers, searchParams: { sha: ref, per_page: 1, ...(filePath ? { path: filePath } : {}) } }).json()
])
const contents = Array.isArray(rawContents) ? rawContents : []
// Core
var readmeContent;
try {
const rd = await ky.get(`https://api.github.com/repos/${repoPath}/readme`, { headers, searchParams: { ref } }).json()
readmeContent = Buffer.from(rd.content, "base64").toString("utf8")
} catch { }
res.render("repository", {
user: { login: username }, // Use fake user obj for simple header compat
repo: repoInfo,
contents,
lastCommit: commits[0],
branches,
currentBranch: ref,
currentPath: filePath,
readme: readmeContent,
languages: langs,
contributors: contributorsList,
commitCount: 0 // Skipping calculation for speed
})
} catch { res.redirect(`/${username}/repositories`) }
})
//* Handlers
setInterval(() => {
log("i", "Running scheduled monitor checks (every 30 minutes)...")
//* The monitored repositories
const monitored = monitoredCol.findMany();
for ( const m of monitored ) queueScan(m.username, m.repoName, m.defaultBranch)
}, 30 * 60 * 1000) // 30 Minutes
web.use("/{*any}", (req, res)=>res.redirect("/"))
web.listen(port, () => log("i", `GitFaye running on http://localhost:${port}`))