forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkCryptoImplementation.js
140 lines (123 loc) · 3.6 KB
/
checkCryptoImplementation.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
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
const Step = require('../../actions').Step;
// Common encryption-related patterns and keywords
const CRYPTO_PATTERNS = {
// Known non-standard encryption algorithms
nonStandardAlgorithms: [
'xor\\s*\\(',
'rot13',
'caesar\\s*cipher',
'custom\\s*encrypt',
'simple\\s*encrypt',
'homebrew\\s*crypto',
'custom\\s*hash'
],
// Suspicious operations that might indicate custom crypto Implementation
suspiciousOperations: [
'bit\\s*shift',
'bit\\s*rotate',
'\\^=',
'\\^',
'>>>',
'<<<',
'shuffle\\s*bytes'
],
// Common encryption-related variable names
suspiciousVariables: [
'cipher',
'encrypt',
'decrypt',
'scramble',
'salt(?!\\w)',
'iv(?!\\w)',
'nonce'
]
};
function analyzeCodeForCrypto(diffContent) {
const issues = [];
// Check for above mentioned cryto Patterns
if(!diffContent) return issues;
CRYPTO_PATTERNS.nonStandardAlgorithms.forEach(pattern => {
const regex = new RegExp(pattern, 'gi');
const matches = diffContent.match(regex);
if (matches) {
issues.push({
type: 'non_standard_algorithm',
pattern: pattern,
matches: matches,
severity: 'high',
message: `Detected possible non-standard encryption algorithm: ${matches.join(', ')}`
});
}
});
CRYPTO_PATTERNS.suspiciousOperations.forEach(pattern => {
const regex = new RegExp(pattern, 'gi');
const matches = diffContent.match(regex);
if (matches) {
issues.push({
type: 'suspicious_operation',
pattern: pattern,
matches: matches,
severity: 'medium',
message: `Detected suspicious cryptographic operation: ${matches.join(', ')}`
});
}
});
CRYPTO_PATTERNS.suspiciousVariables.forEach(pattern => {
const regex = new RegExp(pattern, 'gi');
const matches = diffContent.match(regex);
if (matches) {
issues.push({
type: 'suspicious_variable',
pattern: pattern,
matches: matches,
severity: 'low',
message: `Detected potential encryption-related variable: ${matches.join(', ')}`
});
}
});
return issues;
}
const exec = async (req, action) => {
const step = new Step('checkCryptoImplementation');
try {
let hasIssues = false;
const allIssues = [];
for (const commit of action.commitData) {
const diff = commit.diff || '';
const issues = analyzeCodeForCrypto(diff);
if (issues.length > 0) {
hasIssues = true;
allIssues.push({
commit: commit.hash,
issues: issues
});
}
}
if (hasIssues) {
step.error = true;
const errorMessage = allIssues.map(commitIssues => {
return `Commit ${commitIssues.commit}:\n` +
commitIssues.issues.map(issue =>
`- ${issue.severity.toUpperCase()}: ${issue.message}`
).join('\n');
}).join('\n\n');
step.setError(
'\n\nYour push has been blocked.\n' +
'Potential non-standard cryptographic implementations detected:\n\n' +
`${errorMessage}\n\n` +
'Please use standard cryptographic libraries instead of custom implementations.\n' +
'Recommended: Use established libraries like crypto, node-forge, or Web Crypto API.\n'
);
}
action.addStep(step);
return action;
} catch (error) {
step.error = true;
step.setError(`Error analyzing crypto implementation: ${error.message}`);
action.addStep(step);
return action;
}
};
// exec.displayName = 'checkCryptoImplementation.exec';
exports.exec = exec;
exports.analyzeCodeForCrypto = analyzeCodeForCrypto;