forked from juice-shop/juice-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsnUtil.ts
153 lines (144 loc) · 5.01 KB
/
rsnUtil.ts
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
import { retrieveCodeSnippet } from '../routes/vulnCodeSnippet'
import colors from 'colors/safe'
const Diff = require('diff')
const fs = require('fs')
const fixesPath = 'data/static/codefixes'
const cacheFile = 'rsn/cache.json'
type CacheData = Record<string, {
added: number[]
removed: number[]
}>
function readFiles () {
const files = fs.readdirSync(fixesPath)
const keys = files.filter((file: string) => !file.endsWith('.info.yml') && !file.endsWith('.editorconfig'))
return keys
}
function writeToFile (json: CacheData) {
fs.writeFileSync(cacheFile, JSON.stringify(json, null, '\t'))
}
function getDataFromFile () {
const data = fs.readFileSync(cacheFile).toString()
return JSON.parse(data)
}
function filterString (text: string) {
text = text.replace(/\r/g, '')
return text
}
const checkDiffs = async (keys: string[]) => {
const data: CacheData = keys.reduce((prev, curr) => {
return {
...prev,
[curr]: {
added: [],
removed: []
}
}
}, {})
for (const val of keys) {
await retrieveCodeSnippet(val.split('_')[0])
.then(snippet => {
if (snippet == null) return
process.stdout.write(val + ': ')
const fileData = fs.readFileSync(fixesPath + '/' + val).toString()
const diff = Diff.diffLines(filterString(fileData), filterString(snippet.snippet))
let line = 0
for (const part of diff) {
if (part.removed) continue
const prev = line
line += part.count
if (!(part.added)) continue
for (let i = 0; i < part.count; i++) {
if (!snippet.vulnLines.includes(prev + i + 1) && !snippet.neutralLines.includes(prev + i + 1)) {
process.stdout.write(colors.red(colors.inverse(prev + i + 1 + '')))
process.stdout.write(' ')
data[val].added.push(prev + i + 1)
} else if (snippet.vulnLines.includes(prev + i + 1)) {
process.stdout.write(colors.red(colors.bold(prev + i + 1 + ' ')))
} else if (snippet.neutralLines.includes(prev + i + 1)) {
process.stdout.write(colors.red(prev + i + 1 + ' '))
}
}
}
line = 0
let norm = 0
for (const part of diff) {
if (part.added) {
norm--
continue
}
const prev = line
line += part.count
if (!(part.removed)) continue
let temp = norm
for (let i = 0; i < part.count; i++) {
if (!snippet.vulnLines.includes(prev + i + 1 - norm) && !snippet.neutralLines.includes(prev + i + 1 - norm)) {
process.stdout.write(colors.green(colors.inverse((prev + i + 1 - norm + ''))))
process.stdout.write(' ')
data[val].removed.push(prev + i + 1 - norm)
} else if (snippet.vulnLines.includes(prev + i + 1 - norm)) {
process.stdout.write(colors.green(colors.bold(prev + i + 1 - norm + ' ')))
} else if (snippet.neutralLines.includes(prev + i + 1 - norm)) {
process.stdout.write(colors.green(prev + i + 1 - norm + ' '))
}
temp++
}
norm = temp
}
process.stdout.write('\n')
})
.catch(err => {
console.log(err)
})
}
return data
}
async function seePatch (file: string) {
const fileData = fs.readFileSync(fixesPath + '/' + file).toString()
const snippet = await retrieveCodeSnippet(file.split('_')[0])
if (snippet == null) return
const patch = Diff.structuredPatch(file, file, filterString(snippet.snippet), filterString(fileData))
console.log(colors.bold(file + '\n'))
for (const hunk of patch.hunks) {
for (const line of hunk.lines) {
if (line[0] === '-') {
console.log(colors.red(line))
} else if (line[0] === '+') {
console.log(colors.green(line))
} else {
console.log(line)
}
}
}
console.log('---------------------------------------')
}
function checkData (data: CacheData, fileData: CacheData) {
const filesWithDiff = []
for (const key in data) {
const fileDataValueAdded = fileData[key].added.sort((a, b) => a - b)
const dataValueAdded = data[key].added.sort((a, b) => a - b)
const fileDataValueRemoved = fileData[key].added.sort((a, b) => a - b)
const dataValueAddedRemoved = data[key].added.sort((a, b) => a - b)
if (fileDataValueAdded.length === dataValueAdded.length && fileDataValueRemoved.length === dataValueAddedRemoved.length) {
if (!dataValueAdded.every((val: number, ind: number) => fileDataValueAdded[ind] === val)) {
console.log(colors.red(key))
filesWithDiff.push(key)
}
if (!dataValueAddedRemoved.every((val: number, ind: number) => fileDataValueRemoved[ind] === val)) {
console.log(colors.red(key))
filesWithDiff.push(key)
}
} else {
console.log(colors.red(key))
filesWithDiff.push(key)
}
}
return filesWithDiff
}
export {
checkDiffs,
writeToFile,
getDataFromFile,
readFiles,
seePatch,
checkData
}