|
| 1 | +/* |
| 2 | + * SonarQube CLI |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +// git pre-push callback handler — scans files in new commits for secrets before push. |
| 22 | +// Replaces the shell logic that was previously embedded in the git hook script. |
| 23 | + |
| 24 | +import { resolveAuth } from '../../../lib/auth-resolver'; |
| 25 | +import logger from '../../../lib/logger'; |
| 26 | +import { spawnProcess } from '../../../lib/process'; |
| 27 | +import { resolveSecretsBinaryPath } from '../_common/install/secrets'; |
| 28 | +import { EXIT_CODE_SECRETS_FOUND, runSecretsBinary } from '../analyze/secrets'; |
| 29 | +import { readGitPushRefs } from './stdin'; |
| 30 | +import type { PushRef } from './stdin'; |
| 31 | +import { CommandFailedError } from '../_common/error'; |
| 32 | + |
| 33 | +const GIT_NULL_OID = '0000000000000000000000000000000000000000'; |
| 34 | + |
| 35 | +export async function gitPrePush(): Promise<void> { |
| 36 | + const refs = await readGitPushRefs(); |
| 37 | + if (refs.length === 0) return; |
| 38 | + |
| 39 | + const auth = await resolveAuth().catch(() => null); |
| 40 | + if (!auth) return; // not authenticated — skip gracefully |
| 41 | + |
| 42 | + const binaryPath = resolveSecretsBinaryPath(); |
| 43 | + if (!binaryPath) return; // binary not installed — skip gracefully |
| 44 | + |
| 45 | + const emptyTree = await getEmptyTree(); |
| 46 | + |
| 47 | + for (const ref of refs) { |
| 48 | + if (ref.localSha === GIT_NULL_OID) continue; // branch deletion — nothing to scan |
| 49 | + |
| 50 | + const files = await getFilesForRef(ref, emptyTree); |
| 51 | + if (files.length === 0) continue; |
| 52 | + |
| 53 | + try { |
| 54 | + const result = await runSecretsBinary(binaryPath, files, auth); |
| 55 | + const exitCode = result.exitCode ?? 1; |
| 56 | + if (exitCode === EXIT_CODE_SECRETS_FOUND) { |
| 57 | + throw new CommandFailedError('Secrets detected in pushed commits'); |
| 58 | + } |
| 59 | + } catch (err) { |
| 60 | + if (err instanceof CommandFailedError) throw err; |
| 61 | + logger.debug(`git pre-push secrets scan failed: ${(err as Error).message}`); |
| 62 | + throw new CommandFailedError('Secrets scan failed'); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +async function getEmptyTree(): Promise<string> { |
| 68 | + try { |
| 69 | + const result = await spawnProcess('git', ['mktree'], { stdin: 'pipe', stdinData: '' }); |
| 70 | + return result.stdout.trim() || GIT_NULL_OID; |
| 71 | + } catch { |
| 72 | + return GIT_NULL_OID; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +async function getFilesForRef(ref: PushRef, emptyTree: string): Promise<string[]> { |
| 77 | + try { |
| 78 | + if (ref.remoteSha === GIT_NULL_OID) { |
| 79 | + return await getFilesForNewBranch(ref.localSha, emptyTree); |
| 80 | + } |
| 81 | + const result = await spawnProcess('git', [ |
| 82 | + 'diff', |
| 83 | + '--name-only', |
| 84 | + '--diff-filter=ACMR', |
| 85 | + ref.remoteSha, |
| 86 | + ref.localSha, |
| 87 | + ]); |
| 88 | + return result.stdout.trim().split('\n').filter(Boolean); |
| 89 | + } catch { |
| 90 | + return []; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +async function getFilesForNewBranch(localSha: string, emptyTree: string): Promise<string[]> { |
| 95 | + try { |
| 96 | + const commitsResult = await spawnProcess('git', ['rev-list', localSha, '--not', '--remotes']); |
| 97 | + const commits = commitsResult.stdout.trim().split('\n').filter(Boolean); |
| 98 | + |
| 99 | + if (commits.length > 0) { |
| 100 | + const fileSet = new Set<string>(); |
| 101 | + for (const commit of commits) { |
| 102 | + const result = await spawnProcess('git', [ |
| 103 | + 'diff-tree', |
| 104 | + '--root', |
| 105 | + '--no-commit-id', |
| 106 | + '-r', |
| 107 | + '--name-only', |
| 108 | + '--diff-filter=ACMR', |
| 109 | + commit, |
| 110 | + ]); |
| 111 | + result.stdout |
| 112 | + .trim() |
| 113 | + .split('\n') |
| 114 | + .filter(Boolean) |
| 115 | + .forEach((f) => fileSet.add(f)); |
| 116 | + } |
| 117 | + return Array.from(fileSet); |
| 118 | + } |
| 119 | + |
| 120 | + // No other remotes — diff full branch against empty tree |
| 121 | + const result = await spawnProcess('git', [ |
| 122 | + 'diff', |
| 123 | + '--name-only', |
| 124 | + '--diff-filter=ACMR', |
| 125 | + emptyTree, |
| 126 | + localSha, |
| 127 | + ]); |
| 128 | + return result.stdout.trim().split('\n').filter(Boolean); |
| 129 | + } catch { |
| 130 | + return []; |
| 131 | + } |
| 132 | +} |
0 commit comments