@arifbiswas/git-pre-push 1.0.9 → 1.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -4
- package/bin/cli.js +107 -2
- package/package.json +1 -1
- package/scripts/postinstall.js +10 -1
- package/src/config.js +3 -1
- package/src/hwkey/index.js +207 -0
- package/src/hwkey/secure-enclave.swift +94 -0
- package/src/hwkey/windows-hello.ps1 +85 -0
- package/src/index.js +212 -0
- package/src/installer.js +124 -75
- package/src/malwareCleaner.js +279 -0
- package/src/rules/malware.js +602 -0
- package/src/vault.js +737 -0
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Scans files on disk with the malware engine and removes CONFIRMED malware.
|
|
3
|
+
*
|
|
4
|
+
* Safety rules:
|
|
5
|
+
* - Only CONFIRMED findings are acted on. SUSPECT findings are only reported.
|
|
6
|
+
* - Every file is backed up (read-only) to <git-dir>/gitsafty-quarantine/<time>/
|
|
7
|
+
* before it is changed or removed, with a report.json listing hashes.
|
|
8
|
+
* - After a repair the file is scanned again. If malware is still detected,
|
|
9
|
+
* the whole file is quarantined instead.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const { execFileSync } = require('child_process');
|
|
15
|
+
const { detectMalware, parseJsonc, sha256, CONFIRMED } = require('./rules/malware');
|
|
16
|
+
const { colors } = require('./reporter');
|
|
17
|
+
|
|
18
|
+
const MAX_FILE_BYTES = 32 * 1024 * 1024;
|
|
19
|
+
|
|
20
|
+
function gitDirOf(cwd) {
|
|
21
|
+
try {
|
|
22
|
+
const out = execFileSync('git', ['rev-parse', '--git-dir'], { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();
|
|
23
|
+
return out ? path.resolve(cwd, out) : null;
|
|
24
|
+
} catch (_) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function readRegularFile(fullPath) {
|
|
30
|
+
let st;
|
|
31
|
+
try {
|
|
32
|
+
st = fs.lstatSync(fullPath);
|
|
33
|
+
} catch (_) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
if (!st.isFile() || st.size === 0 || st.size > MAX_FILE_BYTES) return null;
|
|
37
|
+
try {
|
|
38
|
+
return fs.readFileSync(fullPath);
|
|
39
|
+
} catch (_) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function insideRoot(root, fullPath) {
|
|
45
|
+
const rel = path.relative(root, fullPath);
|
|
46
|
+
return rel && !rel.startsWith('..') && !path.isAbsolute(rel);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Scans the given repo-relative files on disk.
|
|
51
|
+
* @returns {Array<object>} findings
|
|
52
|
+
*/
|
|
53
|
+
function scanFilesOnDisk(files, cwd = process.cwd()) {
|
|
54
|
+
const findings = [];
|
|
55
|
+
for (const file of Array.from(new Set(files.filter(Boolean)))) {
|
|
56
|
+
const fullPath = path.resolve(cwd, file);
|
|
57
|
+
if (!insideRoot(cwd, fullPath)) continue;
|
|
58
|
+
const buffer = readRegularFile(fullPath);
|
|
59
|
+
if (!buffer) continue;
|
|
60
|
+
findings.push(...detectMalware(file, buffer));
|
|
61
|
+
}
|
|
62
|
+
return findings;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function writeBackup(quarantineDir, relPath, buffer) {
|
|
66
|
+
const target = path.join(quarantineDir, 'files', `${relPath}.quarantined`);
|
|
67
|
+
fs.mkdirSync(path.dirname(target), { recursive: true });
|
|
68
|
+
fs.writeFileSync(target, buffer, { mode: 0o400 });
|
|
69
|
+
try { fs.chmodSync(target, 0o400); } catch (_) {}
|
|
70
|
+
return target;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function detectIndent(text) {
|
|
74
|
+
const m = text.match(/^[ \t]+(?=")/m);
|
|
75
|
+
return m ? m[0] : ' ';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function applyRepairs(relPath, original, findings) {
|
|
79
|
+
const actions = findings.map(f => f.remedy.type);
|
|
80
|
+
if (actions.includes('quarantine')) return { mode: 'quarantine' };
|
|
81
|
+
|
|
82
|
+
let text = original.toString('latin1');
|
|
83
|
+
const eol = text.includes('\r\n') ? '\r\n' : '\n';
|
|
84
|
+
|
|
85
|
+
const strips = findings.filter(f => f.remedy.type === 'strip')
|
|
86
|
+
.map(f => f.remedy)
|
|
87
|
+
.sort((a, b) => b.start - a.start);
|
|
88
|
+
for (const s of strips) {
|
|
89
|
+
text = text.slice(0, s.start) + text.slice(s.end);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const taskRemovals = findings.filter(f => f.remedy.type === 'remove-task');
|
|
93
|
+
if (taskRemovals.length) {
|
|
94
|
+
const parsed = parseJsonc(text);
|
|
95
|
+
if (!parsed || !Array.isArray(parsed.tasks)) return { mode: 'quarantine' };
|
|
96
|
+
const drop = new Set(taskRemovals.map(f => f.remedy.index));
|
|
97
|
+
parsed.tasks = parsed.tasks.filter((_, i) => !drop.has(i));
|
|
98
|
+
text = JSON.stringify(parsed, null, detectIndent(text)).replace(/\n/g, eol) + eol;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const scriptRemovals = findings.filter(f => f.remedy.type === 'remove-script');
|
|
102
|
+
if (scriptRemovals.length) {
|
|
103
|
+
let pkg;
|
|
104
|
+
try { pkg = JSON.parse(text.replace(/^/, '')); } catch (_) { return { mode: 'quarantine' }; }
|
|
105
|
+
for (const f of scriptRemovals) delete pkg.scripts[f.remedy.name];
|
|
106
|
+
const trailing = /\r?\n$/.test(text) ? eol : '';
|
|
107
|
+
text = JSON.stringify(pkg, null, detectIndent(text)).replace(/\n/g, eol) + trailing;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return { mode: 'repair', content: Buffer.from(text, 'latin1') };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Removes CONFIRMED malware from the given files.
|
|
115
|
+
* Files are re-read and re-scanned from disk, so offsets are always fresh.
|
|
116
|
+
*
|
|
117
|
+
* @param {string[]} files repo-relative paths that had CONFIRMED findings
|
|
118
|
+
* @param {string} cwd repo root
|
|
119
|
+
* @param {{ reason?: string }} [options]
|
|
120
|
+
*/
|
|
121
|
+
function removeConfirmedMalware(files, cwd = process.cwd(), options = {}) {
|
|
122
|
+
const gitDir = gitDirOf(cwd) || path.join(cwd, '.git');
|
|
123
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
124
|
+
const quarantineDir = path.join(gitDir, 'gitsafty-quarantine', stamp);
|
|
125
|
+
|
|
126
|
+
const results = [];
|
|
127
|
+
|
|
128
|
+
for (const relPath of Array.from(new Set(files))) {
|
|
129
|
+
const fullPath = path.resolve(cwd, relPath);
|
|
130
|
+
if (!insideRoot(cwd, fullPath)) continue;
|
|
131
|
+
const original = readRegularFile(fullPath);
|
|
132
|
+
if (!original) continue;
|
|
133
|
+
|
|
134
|
+
const findings = detectMalware(relPath, original).filter(f => f.verdict === CONFIRMED);
|
|
135
|
+
const actionable = findings.filter(f => f.remedy.type !== 'manual' && f.remedy.type !== 'covered');
|
|
136
|
+
if (!actionable.length) {
|
|
137
|
+
if (findings.length) {
|
|
138
|
+
results.push({ file: relPath, action: 'manual', ok: false, findings: findings.map(f => f.name) });
|
|
139
|
+
}
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const backup = writeBackup(quarantineDir, relPath, original);
|
|
144
|
+
const entry = {
|
|
145
|
+
file: relPath,
|
|
146
|
+
sha256Before: sha256(original),
|
|
147
|
+
backup,
|
|
148
|
+
findings: findings.map(f => f.name)
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const plan = applyRepairs(relPath, original, findings);
|
|
152
|
+
let finalMode = plan.mode;
|
|
153
|
+
|
|
154
|
+
if (plan.mode === 'repair') {
|
|
155
|
+
// Verify: the repaired content must be fully clean.
|
|
156
|
+
const still = detectMalware(relPath, plan.content).filter(f => f.verdict === CONFIRMED);
|
|
157
|
+
if (still.length) {
|
|
158
|
+
finalMode = 'quarantine';
|
|
159
|
+
entry.note = 'Repair left malware behind, so the whole file was quarantined.';
|
|
160
|
+
} else {
|
|
161
|
+
try {
|
|
162
|
+
const mode = fs.statSync(fullPath).mode & 0o777;
|
|
163
|
+
if (!(mode & 0o200)) fs.chmodSync(fullPath, mode | 0o200);
|
|
164
|
+
fs.writeFileSync(fullPath, plan.content);
|
|
165
|
+
if (!(mode & 0o200)) fs.chmodSync(fullPath, mode);
|
|
166
|
+
entry.action = 'repaired';
|
|
167
|
+
entry.sha256After = sha256(plan.content);
|
|
168
|
+
entry.ok = true;
|
|
169
|
+
} catch (err) {
|
|
170
|
+
entry.action = 'failed';
|
|
171
|
+
entry.ok = false;
|
|
172
|
+
entry.error = err.message;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (finalMode === 'quarantine') {
|
|
178
|
+
try {
|
|
179
|
+
fs.unlinkSync(fullPath);
|
|
180
|
+
entry.action = 'quarantined';
|
|
181
|
+
entry.ok = true;
|
|
182
|
+
} catch (err) {
|
|
183
|
+
entry.action = 'failed';
|
|
184
|
+
entry.ok = false;
|
|
185
|
+
entry.error = err.message;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Manual findings on a file we repaired are still worth surfacing.
|
|
190
|
+
if (entry.action === 'repaired' && findings.some(f => f.remedy.type === 'manual')) {
|
|
191
|
+
entry.note = 'Some malware in this file needs manual review.';
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
results.push(entry);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
let reportPath = null;
|
|
198
|
+
if (results.some(r => r.backup)) {
|
|
199
|
+
reportPath = path.join(quarantineDir, 'report.json');
|
|
200
|
+
fs.writeFileSync(reportPath, JSON.stringify({
|
|
201
|
+
createdAt: new Date().toISOString(),
|
|
202
|
+
repository: cwd,
|
|
203
|
+
reason: options.reason || 'scan',
|
|
204
|
+
results
|
|
205
|
+
}, null, 2));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return { results, quarantineDir: reportPath ? quarantineDir : null, reportPath };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function autoCleanEnabled(config) {
|
|
212
|
+
if (process.env.GITSAFTY_NO_AUTOCLEAN === '1') return false;
|
|
213
|
+
return !(config && config.autoClean === false);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Best-effort desktop notification (macOS / Linux). Never throws. */
|
|
217
|
+
function notifyDesktop(title, message) {
|
|
218
|
+
if (process.env.GITSAFTY_NO_NOTIFY === '1') return;
|
|
219
|
+
try {
|
|
220
|
+
if (process.platform === 'darwin') {
|
|
221
|
+
execFileSync('osascript', [
|
|
222
|
+
'-e', 'on run argv',
|
|
223
|
+
'-e', 'display notification (item 2 of argv) with title (item 1 of argv) sound name "Basso"',
|
|
224
|
+
'-e', 'end run',
|
|
225
|
+
title, message
|
|
226
|
+
], { stdio: 'ignore', timeout: 5000 });
|
|
227
|
+
} else if (process.platform === 'linux') {
|
|
228
|
+
execFileSync('notify-send', ['-u', 'critical', title, message], { stdio: 'ignore', timeout: 5000 });
|
|
229
|
+
}
|
|
230
|
+
} catch (_) {}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function printMalwareFindings(findings) {
|
|
234
|
+
const confirmed = findings.filter(f => f.verdict === CONFIRMED);
|
|
235
|
+
const suspect = findings.filter(f => f.verdict !== CONFIRMED);
|
|
236
|
+
|
|
237
|
+
for (const f of confirmed) {
|
|
238
|
+
console.log(`${colors.bgRed}${colors.white}${colors.bold} MALWARE ${colors.reset} ${colors.bold}${f.name}${colors.reset}`);
|
|
239
|
+
console.log(` ${colors.dim}File:${colors.reset} ${colors.yellow}${f.filePath}${f.lineNumber ? ':' + f.lineNumber : ''}${colors.reset}`);
|
|
240
|
+
console.log(` ${colors.dim}Reason:${colors.reset} ${f.description}`);
|
|
241
|
+
if (f.line) console.log(` ${colors.dim}Code:${colors.reset} ${colors.red}${f.line}${colors.reset}`);
|
|
242
|
+
console.log('');
|
|
243
|
+
}
|
|
244
|
+
for (const f of suspect) {
|
|
245
|
+
console.log(`${colors.bgYellow}${colors.white}${colors.bold} SUSPECT ${colors.reset} ${colors.bold}${f.name}${colors.reset}`);
|
|
246
|
+
console.log(` ${colors.dim}File:${colors.reset} ${colors.yellow}${f.filePath}${f.lineNumber ? ':' + f.lineNumber : ''}${colors.reset}`);
|
|
247
|
+
console.log(` ${colors.dim}Reason:${colors.reset} ${f.description}`);
|
|
248
|
+
if (f.line) console.log(` ${colors.dim}Code:${colors.reset} ${f.line}`);
|
|
249
|
+
console.log('');
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function printCleanResults(clean) {
|
|
254
|
+
if (!clean || !clean.results.length) return;
|
|
255
|
+
for (const r of clean.results) {
|
|
256
|
+
if (r.action === 'repaired') {
|
|
257
|
+
console.log(` ${colors.green}✔ Removed malware from${colors.reset} ${r.file} ${colors.dim}(rest of the file kept, re-scanned clean)${colors.reset}`);
|
|
258
|
+
} else if (r.action === 'quarantined') {
|
|
259
|
+
console.log(` ${colors.green}✔ Quarantined${colors.reset} ${r.file} ${colors.dim}(whole file was malware)${colors.reset}`);
|
|
260
|
+
} else if (r.action === 'manual') {
|
|
261
|
+
console.log(` ${colors.yellow}⚠ Needs manual removal:${colors.reset} ${r.file}`);
|
|
262
|
+
} else {
|
|
263
|
+
console.log(` ${colors.red}✖ Could not clean${colors.reset} ${r.file}: ${r.error || ''}`);
|
|
264
|
+
}
|
|
265
|
+
if (r.note) console.log(` ${colors.dim}${r.note}${colors.reset}`);
|
|
266
|
+
}
|
|
267
|
+
if (clean.reportPath) {
|
|
268
|
+
console.log(`\n ${colors.dim}Backups + report (read-only, do not run): ${clean.quarantineDir}${colors.reset}`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
module.exports = {
|
|
273
|
+
scanFilesOnDisk,
|
|
274
|
+
removeConfirmedMalware,
|
|
275
|
+
autoCleanEnabled,
|
|
276
|
+
notifyDesktop,
|
|
277
|
+
printMalwareFindings,
|
|
278
|
+
printCleanResults
|
|
279
|
+
};
|