-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles-matches.js
More file actions
51 lines (41 loc) · 1.58 KB
/
files-matches.js
File metadata and controls
51 lines (41 loc) · 1.58 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
"use strict";
// Variables
const filesMatches = require("./database/files-matches.json")
// Main
module.exports = (filesTree) => {
// Validations
if (!Array.isArray(filesTree)) return []
// Variables
const paths = filesTree.map((item) => typeof item === "string" ? item : item.path)
const detections = []
// Core
for ( const rule of filesMatches ) {
for ( const targetFile of rule.files ) {
// Variables
const isWildcard = targetFile.includes("*")
const regex = isWildcard ? new RegExp("^" + targetFile.replace(/\*/g, ".*") + "$", "i") : null
// Core
const matchedPaths = paths.filter((p) => {
// Variables
const fileName = p.split("/").pop()
// Core
if (rule.depth === 1) {
const isRoot = !p.includes("/") || p.startsWith("./") && p.split("/").length === 2
if (!isRoot) return false
if (isWildcard) return regex.test(fileName)
return p === targetFile || p === `./${targetFile}`
} else {
if (isWildcard) return regex.test(fileName)
return p.endsWith(targetFile) || p.includes(`/${targetFile}`)
}
})
if (matchedPaths.length) detections.push({
name: rule.name,
risk: rule.risk,
severity: rule.severity,
matchedPaths: matchedPaths
})
}
}
return detections
}