Skip to content

Commit efed002

Browse files
Sebastian Göttegitster
authored andcommitted
merge/pull: verify GPG signatures of commits being merged
When --verify-signatures is specified on the command-line of git-merge or git-pull, check whether the commits being merged have good gpg signatures and abort the merge in case they do not. This allows e.g. auto-deployment from untrusted repo hosts. Signed-off-by: Sebastian Götte <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f8aae8d commit efed002

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

Documentation/merge-options.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ option can be used to override --squash.
8383
Pass merge strategy specific option through to the merge
8484
strategy.
8585

86+
--verify-signatures::
87+
--no-verify-signatures::
88+
Verify that the commits being merged have good GPG signatures and abort the
89+
merge in case they do not.
90+
8691
--summary::
8792
--no-summary::
8893
Synonyms to --stat and --no-stat; these are deprecated and will be

builtin/merge.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static const char * const builtin_merge_usage[] = {
4949
static int show_diffstat = 1, shortlog_len = -1, squash;
5050
static int option_commit = 1, allow_fast_forward = 1;
5151
static int fast_forward_only, option_edit = -1;
52-
static int allow_trivial = 1, have_message;
52+
static int allow_trivial = 1, have_message, verify_signatures;
5353
static int overwrite_ignore = 1;
5454
static struct strbuf merge_msg = STRBUF_INIT;
5555
static struct strategy **use_strategies;
@@ -199,6 +199,8 @@ static struct option builtin_merge_options[] = {
199199
OPT_BOOLEAN(0, "ff-only", &fast_forward_only,
200200
N_("abort if fast-forward is not possible")),
201201
OPT_RERERE_AUTOUPDATE(&allow_rerere_auto),
202+
OPT_BOOL(0, "verify-signatures", &verify_signatures,
203+
N_("Verify that the named commit has a valid GPG signature")),
202204
OPT_CALLBACK('s', "strategy", &use_strategies, N_("strategy"),
203205
N_("merge strategy to use"), option_parse_strategy),
204206
OPT_CALLBACK('X', "strategy-option", &xopts, N_("option=value"),
@@ -1233,6 +1235,36 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12331235
usage_with_options(builtin_merge_usage,
12341236
builtin_merge_options);
12351237

1238+
if (verify_signatures) {
1239+
for (p = remoteheads; p; p = p->next) {
1240+
struct commit *commit = p->item;
1241+
char hex[41];
1242+
struct signature_check signature_check;
1243+
memset(&signature_check, 0, sizeof(signature_check));
1244+
1245+
check_commit_signature(commit, &signature_check);
1246+
1247+
strcpy(hex, find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
1248+
switch (signature_check.result) {
1249+
case 'G':
1250+
break;
1251+
case 'B':
1252+
die(_("Commit %s has a bad GPG signature "
1253+
"allegedly by %s."), hex, signature_check.signer);
1254+
default: /* 'N' */
1255+
die(_("Commit %s does not have a GPG signature."), hex);
1256+
}
1257+
if (verbosity >= 0 && signature_check.result == 'G')
1258+
printf(_("Commit %s has a good GPG signature by %s\n"),
1259+
hex, signature_check.signer);
1260+
1261+
free(signature_check.gpg_output);
1262+
free(signature_check.gpg_status);
1263+
free(signature_check.signer);
1264+
free(signature_check.key);
1265+
}
1266+
}
1267+
12361268
strbuf_addstr(&buf, "merge");
12371269
for (p = remoteheads; p; p = p->next)
12381270
strbuf_addf(&buf, " %s", merge_remote_util(p->item)->name);

git-pull.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test -z "$(git ls-files -u)" || die_conflict
3939
test -f "$GIT_DIR/MERGE_HEAD" && die_merge
4040

4141
strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
42-
log_arg= verbosity= progress= recurse_submodules=
42+
log_arg= verbosity= progress= recurse_submodules= verify_signatures=
4343
merge_args= edit=
4444
curr_branch=$(git symbolic-ref -q HEAD)
4545
curr_branch_short="${curr_branch#refs/heads/}"
@@ -125,6 +125,12 @@ do
125125
--no-recurse-submodules)
126126
recurse_submodules=--no-recurse-submodules
127127
;;
128+
--verify-signatures)
129+
verify_signatures=--verify-signatures
130+
;;
131+
--no-verify-signatures)
132+
verify_signatures=--no-verify-signatures
133+
;;
128134
--d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
129135
dry_run=--dry-run
130136
;;
@@ -283,7 +289,7 @@ true)
283289
eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
284290
;;
285291
*)
286-
eval="git-merge $diffstat $no_commit $edit $squash $no_ff $ff_only"
292+
eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
287293
eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
288294
eval="$eval \"\$merge_name\" HEAD $merge_head"
289295
;;

t/t7612-merge-verify-signatures.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/sh
2+
3+
test_description='merge signature verification tests'
4+
. ./test-lib.sh
5+
. "$TEST_DIRECTORY/lib-gpg.sh"
6+
7+
test_expect_success GPG 'create signed commits' '
8+
echo 1 >file && git add file &&
9+
test_tick && git commit -m initial &&
10+
git tag initial &&
11+
12+
git checkout -b side-signed &&
13+
echo 3 >elif && git add elif &&
14+
test_tick && git commit -S -m "signed on side" &&
15+
git checkout initial &&
16+
17+
git checkout -b side-unsigned &&
18+
echo 3 >foo && git add foo &&
19+
test_tick && git commit -m "unsigned on side" &&
20+
git checkout initial &&
21+
22+
git checkout -b side-bad &&
23+
echo 3 >bar && git add bar &&
24+
test_tick && git commit -S -m "bad on side" &&
25+
git cat-file commit side-bad >raw &&
26+
sed -e "s/bad/forged bad/" raw >forged &&
27+
git hash-object -w -t commit forged >forged.commit &&
28+
git checkout initial &&
29+
30+
git checkout master
31+
'
32+
33+
test_expect_success GPG 'merge unsigned commit with verification' '
34+
test_must_fail git merge --ff-only --verify-signatures side-unsigned 2>mergeerror &&
35+
test_i18ngrep "does not have a GPG signature" mergeerror
36+
'
37+
38+
test_expect_success GPG 'merge commit with bad signature with verification' '
39+
test_must_fail git merge --ff-only --verify-signatures $(cat forged.commit) 2>mergeerror &&
40+
test_i18ngrep "has a bad GPG signature" mergeerror
41+
'
42+
43+
test_expect_success GPG 'merge signed commit with verification' '
44+
git merge --verbose --ff-only --verify-signatures side-signed >mergeoutput &&
45+
test_i18ngrep "has a good GPG signature" mergeoutput
46+
'
47+
48+
test_expect_success GPG 'merge commit with bad signature without verification' '
49+
git merge $(cat forged.commit)
50+
'
51+
52+
test_done

0 commit comments

Comments
 (0)