|
| 1 | +import { Action, Step } from '../../actions'; |
| 2 | +import { getAPIs } from '../../../config'; |
| 3 | +import { spawn } from 'node:child_process'; |
| 4 | +import fs from 'node:fs/promises'; |
| 5 | +import { PathLike } from 'node:fs'; |
| 6 | + |
| 7 | +const EXIT_CODE = 99; |
| 8 | + |
| 9 | +function runCommand( |
| 10 | + cwd: string, |
| 11 | + command: string, |
| 12 | + args: readonly string[] = [], |
| 13 | +): Promise<{ |
| 14 | + exitCode: number | null; |
| 15 | + stdout: string; |
| 16 | + stderr: string; |
| 17 | +}> { |
| 18 | + return new Promise((resolve, reject) => { |
| 19 | + const child = spawn(command, args, { cwd, shell: true }); |
| 20 | + |
| 21 | + let stdout = ''; |
| 22 | + let stderr = ''; |
| 23 | + |
| 24 | + child.stdout.on('data', (data) => { |
| 25 | + stdout += data?.toString() ?? ''; |
| 26 | + }); |
| 27 | + |
| 28 | + child.stderr.on('data', (data) => { |
| 29 | + stderr += data?.toString() ?? ''; |
| 30 | + }); |
| 31 | + |
| 32 | + child.on('close', (exitCode) => { |
| 33 | + resolve({ exitCode, stdout, stderr }); |
| 34 | + }); |
| 35 | + |
| 36 | + child.on('error', (err) => { |
| 37 | + reject(err); |
| 38 | + }); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +type ConfigOptions = { |
| 43 | + enabled: boolean; |
| 44 | + ignoreGitleaksAllow: boolean; |
| 45 | + noColor: boolean; |
| 46 | + configPath: string | undefined; |
| 47 | +}; |
| 48 | + |
| 49 | +const DEFAULT_CONFIG: ConfigOptions = { |
| 50 | + // adding gitleaks into main git-proxy for now as default off |
| 51 | + // in the future will likely be moved to a plugin where it'll be default on |
| 52 | + enabled: false, |
| 53 | + ignoreGitleaksAllow: true, |
| 54 | + noColor: false, |
| 55 | + configPath: undefined, |
| 56 | +}; |
| 57 | + |
| 58 | +function isRecord(value: unknown): value is Record<string, unknown> { |
| 59 | + return typeof value === 'object' && value !== null; |
| 60 | +} |
| 61 | + |
| 62 | +async function fileIsReadable(path: PathLike): Promise<boolean> { |
| 63 | + try { |
| 64 | + if (!(await fs.stat(path)).isFile()) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + await fs.access(path, fs.constants.R_OK); |
| 68 | + return true; |
| 69 | + } catch (e) { |
| 70 | + return false; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +const getPluginConfig = async (): Promise<ConfigOptions> => { |
| 75 | + const userConfig = getAPIs(); |
| 76 | + if (typeof userConfig !== 'object') { |
| 77 | + return DEFAULT_CONFIG; |
| 78 | + } |
| 79 | + if (!Object.hasOwn(userConfig, 'gitleaks')) { |
| 80 | + return DEFAULT_CONFIG; |
| 81 | + } |
| 82 | + const gitleaksConfig = userConfig.gitleaks; |
| 83 | + if (!isRecord(gitleaksConfig)) { |
| 84 | + return DEFAULT_CONFIG; |
| 85 | + } |
| 86 | + |
| 87 | + let configPath: string | undefined = undefined; |
| 88 | + if (typeof gitleaksConfig.configPath === 'string') { |
| 89 | + const userConfigPath = gitleaksConfig.configPath.trim(); |
| 90 | + if (userConfigPath.length > 0 && (await fileIsReadable(userConfigPath))) { |
| 91 | + configPath = userConfigPath; |
| 92 | + } else { |
| 93 | + console.error('could not read file at the config path provided, will not be fed to gitleaks'); |
| 94 | + throw new Error("could not check user's config path"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // TODO: integrate zod |
| 99 | + return { |
| 100 | + enabled: |
| 101 | + typeof gitleaksConfig.enabled === 'boolean' ? gitleaksConfig.enabled : DEFAULT_CONFIG.enabled, |
| 102 | + ignoreGitleaksAllow: |
| 103 | + typeof gitleaksConfig.ignoreGitleaksAllow === 'boolean' |
| 104 | + ? gitleaksConfig.ignoreGitleaksAllow |
| 105 | + : DEFAULT_CONFIG.ignoreGitleaksAllow, |
| 106 | + noColor: |
| 107 | + typeof gitleaksConfig.noColor === 'boolean' ? gitleaksConfig.noColor : DEFAULT_CONFIG.noColor, |
| 108 | + configPath, |
| 109 | + }; |
| 110 | +}; |
| 111 | + |
| 112 | +const exec = async (req: any, action: Action): Promise<Action> => { |
| 113 | + const step = new Step('gitleaks'); |
| 114 | + |
| 115 | + let config: ConfigOptions | undefined = undefined; |
| 116 | + try { |
| 117 | + config = await getPluginConfig(); |
| 118 | + } catch (e) { |
| 119 | + console.error('failed to get gitleaks config, please fix the error:', e); |
| 120 | + action.error = true; |
| 121 | + step.setError('failed setup gitleaks, please contact an administrator\n'); |
| 122 | + action.addStep(step); |
| 123 | + return action; |
| 124 | + } |
| 125 | + |
| 126 | + const { commitFrom, commitTo } = action; |
| 127 | + const workingDir = `${action.proxyGitPath}/${action.repoName}`; |
| 128 | + console.log(`Scanning range with gitleaks: ${commitFrom}:${commitTo}`, workingDir); |
| 129 | + |
| 130 | + try { |
| 131 | + const gitRootCommit = await runCommand(workingDir, 'git', [ |
| 132 | + 'rev-list', |
| 133 | + '--max-parents=0', |
| 134 | + 'HEAD', |
| 135 | + ]); |
| 136 | + if (gitRootCommit.exitCode !== 0) { |
| 137 | + throw new Error('failed to run git'); |
| 138 | + } |
| 139 | + const rootCommit = gitRootCommit.stdout.trim(); |
| 140 | + |
| 141 | + const gitleaksArgs = [ |
| 142 | + `--exit-code=${EXIT_CODE}`, |
| 143 | + '--platform=none', |
| 144 | + config.configPath ? `--config=${config.configPath}` : undefined, // allow for custom config |
| 145 | + config.ignoreGitleaksAllow ? '--ignore-gitleaks-allow' : undefined, // force scanning for security |
| 146 | + '--no-banner', // reduce git-proxy error output |
| 147 | + config.noColor ? '--no-color' : undefined, // colour output should appear properly in the console |
| 148 | + '--redact', // avoid printing the contents |
| 149 | + '--verbose', |
| 150 | + 'git', |
| 151 | + // not using --no-merges to be sure we're scanning the diff |
| 152 | + // only add ^ if the commitFrom isn't the repo's rootCommit |
| 153 | + `--log-opts='--first-parent ${rootCommit === commitFrom ? rootCommit : `${commitFrom}^`}..${commitTo}'`, |
| 154 | + ].filter((v) => typeof v === 'string'); |
| 155 | + const gitleaks = await runCommand(workingDir, 'gitleaks', gitleaksArgs); |
| 156 | + |
| 157 | + if (gitleaks.exitCode !== 0) { |
| 158 | + // any failure |
| 159 | + step.error = true; |
| 160 | + if (gitleaks.exitCode !== EXIT_CODE) { |
| 161 | + step.setError('failed to run gitleaks, please contact an administrator\n'); |
| 162 | + } else { |
| 163 | + // exit code matched our gitleaks findings exit code |
| 164 | + // newline prefix to avoid tab indent at the start |
| 165 | + step.setError('\n' + gitleaks.stdout + gitleaks.stderr); |
| 166 | + } |
| 167 | + } else { |
| 168 | + console.log('succeded'); |
| 169 | + console.log(gitleaks.stderr); |
| 170 | + } |
| 171 | + } catch (e) { |
| 172 | + action.error = true; |
| 173 | + step.setError('failed to spawn gitleaks, please contact an administrator\n'); |
| 174 | + action.addStep(step); |
| 175 | + return action; |
| 176 | + } |
| 177 | + |
| 178 | + action.addStep(step); |
| 179 | + return action; |
| 180 | +}; |
| 181 | + |
| 182 | +exec.displayName = 'gitleaks.exec'; |
| 183 | + |
| 184 | +export { exec }; |
0 commit comments