-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
binary-integrity-check-source.js
50 lines (37 loc) · 1.94 KB
/
binary-integrity-check-source.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
const origError = Error
// eslint-disable-next-line no-unused-vars
function stackIntegrityCheck (options) {
const originalStackTrace = origError.prepareStackTrace
origError.prepareStackTrace = function (_, stack) {
return stack
}
const tempError = new origError
origError.captureStackTrace(tempError, arguments.callee)
let stack = tempError.stack
origError.prepareStackTrace = originalStackTrace
options.stackToMatch.forEach((functionName, index) => {
if (stack[index].getFunctionName() !== functionName || !['evalmachine.<anonymous>', '<embedded>'].includes(stack[index].getFileName())) {
throw new Error(`Integrity check failed at index ${ index } with function name ${stack[index].getFunctionName()} and expected function name ${functionName} from ${stack[index].getFileName()}`)
}
})
}
// eslint-disable-next-line no-unused-vars
function fileIntegrityCheck (options) {
const fs = options.require('fs')
const crypto = options.require('crypto')
// eslint-disable-next-line no-undef
const mainIndexHash = crypto.createHmac('md5', 'HMAC_SECRET').update(fs.readFileSync(options.pathResolver.resolve('./index.js'), 'utf8')).digest('hex')
if (mainIndexHash !== 'MAIN_INDEX_HASH') {
throw new Error(`Integrity check failed for main index.js file`)
}
// eslint-disable-next-line no-undef
const bytenodeHash = crypto.createHmac('md5', 'HMAC_SECRET').update(fs.readFileSync(options.pathResolver.resolve('./node_modules/bytenode/lib/index.js'), 'utf8')).digest('hex')
if (bytenodeHash !== 'BYTENODE_HASH') {
throw new Error(`Integrity check failed for main bytenode.js file`)
}
// eslint-disable-next-line no-undef
const indexJscHash = crypto.createHmac('md5', 'HMAC_SECRET').update(fs.readFileSync(options.pathResolver.resolve('./packages/server/index.jsc'), 'utf8')).digest('hex')
if (indexJscHash !== 'INDEX_JSC_HASH') {
throw new Error(`Integrity check failed for main server index.jsc file`)
}
}