-
-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy pathblock-unsafe-operations-plugin.ts
41 lines (33 loc) · 1.07 KB
/
block-unsafe-operations-plugin.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
import type { SimpleGitPlugin } from './simple-git-plugin';
import { GitPluginError } from '../errors/git-plugin-error';
import type { SimpleGitPluginConfig } from '../types';
function isConfigSwitch(arg: string) {
return arg.trim().toLowerCase() === '-c';
}
function preventProtocolOverride(arg: string, next: string) {
if (!isConfigSwitch(arg)) {
return;
}
if (!/^\s*protocol(.[a-z]+)?.allow/.test(next)) {
return;
}
throw new GitPluginError(
undefined,
'unsafe',
'Configuring protocol.allow is not permitted without enabling allowUnsafeExtProtocol'
);
}
export function blockUnsafeOperationsPlugin({
allowUnsafeProtocolOverride = false,
}: SimpleGitPluginConfig['unsafe'] = {}): SimpleGitPlugin<'spawn.args'> {
return {
type: 'spawn.args',
action(args, _context) {
args.forEach((current, index) => {
const next = index < args.length ? args[index + 1] : '';
allowUnsafeProtocolOverride || preventProtocolOverride(current, next);
});
return args;
},
};
}