forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckCryptoImplementation.test.js
More file actions
223 lines (193 loc) · 6.42 KB
/
checkCryptoImplementation.test.js
File metadata and controls
223 lines (193 loc) · 6.42 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
const { expect } = require('chai');
const { analyzeCodeForCrypto, exec } = require('../src/proxy/processors/push-action/checkCryptoImplementation.js');
describe('Crypto Implementation Check Plugin', () => {
describe('analyzeCodeForCrypto', () => {
it('should detect non-standard encryption algorithms', () => {
const testCode = `
function customEncrypt(data) {
return data.split('').map(char =>
String.fromCharCode(char.charCodeAt(0) ^ 0x7F)
).join('');
}
`;
const issues = analyzeCodeForCrypto(testCode);
expect(issues).to.have.lengthOf.at.least(1);
expect(issues.some(i => i.type === 'non_standard_algorithm')).to.be.true;
});
it('should detect suspicious bit operations', () => {
const testCode = `
function scrambleData(data) {
let result = '';
for(let i = 0; i < data.length; i++) {
result += String.fromCharCode(data.charCodeAt(i) >>> 2);
}
return result;
}
`;
const issues = analyzeCodeForCrypto(testCode);
expect(issues).to.have.lengthOf.at.least(1);
expect(issues.some(i => i.type === 'suspicious_operation')).to.be.true;
});
it('should detect suspicious variable names', () => {
const testCode = `
const cipher = {};
let salt = generateRandomBytes(16);
const iv = new Uint8Array(12);
`;
const issues = analyzeCodeForCrypto(testCode);
expect(issues).to.have.lengthOf.at.least(3);
expect(issues.some(i => i.type === 'suspicious_variable')).to.be.true;
});
it('should not flag standard crypto library usage', () => {
const testCode = `
const crypto = require('crypto');
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
`;
const issues = analyzeCodeForCrypto(testCode);
expect(issues.filter(i => i.severity === 'high')).to.have.lengthOf(0);
});
it('should handle empty input', () => {
const issues = analyzeCodeForCrypto('');
expect(issues).to.be.an('array').that.is.empty;
});
it('should handle null or undefined input', () => {
expect(analyzeCodeForCrypto(null)).to.be.an('array').that.is.empty;
expect(analyzeCodeForCrypto(undefined)).to.be.an('array').that.is.empty;
});
});
describe('exec', () => {
it('should handle empty diff content', async () => {
const req = {};
const action = {
commitData: [{
hash: '123abc',
diff: ''
}],
addStep: function(step) { this.step = step; }
};
const result = await exec(req, action);
expect(result.step.error).to.be.false;
});
it('should handle undefined diff content', async () => {
const req = {};
const action = {
commitData: [{
hash: '123abc'
// diff is undefined
}],
addStep: function(step) { this.step = step; }
};
const result = await exec(req, action);
expect(result.step.error).to.be.false;
});
it('should handle empty commitData array', async () => {
const req = {};
const action = {
commitData: [],
addStep: function(step) { this.step = step; }
};
const result = await exec(req, action);
expect(result.step.error).to.be.false;
});
it('should block commits with non-standard crypto implementations', async () => {
const req = {};
const action = {
commitData: [{
hash: '123abc',
diff: `
function customEncrypt(data) {
return data.split('').map(char =>
String.fromCharCode(char.charCodeAt(0) ^ 0x7F)
).join('');
}
`
}],
addStep: function(step) { this.step = step; }
};
const result = await exec(req, action);
expect(result.step.error).to.be.true;
});
it('should allow commits without crypto issues', async () => {
const req = {};
const action = {
commitData: [{
hash: '123abc',
diff: `
function normalFunction() {
return 'Hello World';
}
`
}],
addStep: function(step) { this.step = step; }
};
const result = await exec(req, action);
expect(result.step.error).to.be.false;
});
it('should handle multiple commits', async () => {
const req = {};
const action = {
commitData: [
{
hash: '123abc',
diff: `function safe() { return true; }`
},
{
hash: '456def',
diff: `
function rot13(str) {
return str.replace(/[a-zA-Z]/g, c =>
String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26)
);
}
`
}
],
addStep: function(step) { this.step = step; }
};
const result = await exec(req, action);
expect(result.step).to.have.property('error', true);
});
it('should handle errors gracefully', async () => {
const req = {};
const action = {
commitData: null,
addStep: function(step) { this.step = step; }
};
const result = await exec(req, action);
expect(result.step.error).to.be.true;
});
});
describe('Pattern Detection', () => {
it('should detect various forms of XOR encryption', () => {
const testCases = [
`function encrypt(a, b) { return a ^= b; }`,
`const result = data ^ key;`,
`function xor(plaintext, key) { return plaintext ^ key; }`,
`return char ^ 0xFF;`
];
testCases.forEach(testCode => {
const issues = analyzeCodeForCrypto(testCode);
const hasXORIssue = issues.some(issue =>
issue.type === 'suspicious_operation' ||
issue.message.toLowerCase().includes('xor')
);
expect(hasXORIssue, `Failed to detect XOR in: ${testCode}`).to.be.true;
});
});
it('should detect custom hash implementations', () => {
const testCode = `
function customHash(input) {
let hash = 0;
for(let i = 0; i < input.length; i++) {
hash = ((hash << 5) - hash) + input.charCodeAt(i);
hash = hash & hash;
}
return hash;
}
`;
const issues = analyzeCodeForCrypto(testCode);
expect(issues).to.have.lengthOf.at.least(1);
expect(issues.some(i => i.severity === 'high')).to.be.true;
});
});
});