Skip to content

Commit 4c36345

Browse files
jltoblergitster
authored andcommitted
fast-import: add 'abort-if-invalid' mode to '--signed-commits=<mode>'
The '--signed-commits=<mode>' option for git-fast-import(1) configures how signed commits are handled when encountered. In cases where an invalid commit signature is encountered, a user may wish to abort the operation entirely. Introduce an 'abort-if-invalid' mode to do so. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 6d35cc4 commit 4c36345

File tree

6 files changed

+24
-3
lines changed

6 files changed

+24
-3
lines changed

Documentation/git-fast-import.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ already trusted to run their own code.
9090
commit signatures and replaces invalid signatures with newly created ones.
9191
Valid signatures are left unchanged. If `<keyid>` is provided, that key is
9292
used for signing; otherwise the configured default signing key is used.
93+
* `abort-if-invalid` will make this program die when encountering a signed
94+
commit that is unable to be verified.
9395

9496
Options for Frontends
9597
~~~~~~~~~~~~~~~~~~~~~

builtin/fast-export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ static int parse_opt_sign_mode(const struct option *opt,
6565
return 0;
6666

6767
if (parse_sign_mode(arg, val, NULL) || (*val == SIGN_STRIP_IF_INVALID) ||
68-
(*val == SIGN_SIGN_IF_INVALID))
68+
(*val == SIGN_SIGN_IF_INVALID) || (*val == SIGN_ABORT_IF_INVALID))
6969
return error(_("unknown %s mode: %s"), opt->long_name, arg);
7070

7171
return 0;

builtin/fast-import.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2892,6 +2892,9 @@ static void handle_signature_if_invalid(struct strbuf *new_data,
28922892
ret = verify_commit_buffer(tmp_buf.buf, tmp_buf.len, &signature_check);
28932893

28942894
if (ret) {
2895+
if (mode == SIGN_ABORT_IF_INVALID)
2896+
die(_("aborting due to invalid signature"));
2897+
28952898
warn_invalid_signature(&signature_check, msg->buf, mode);
28962899

28972900
if (mode == SIGN_SIGN_IF_INVALID) {
@@ -2983,6 +2986,7 @@ static void parse_new_commit(const char *arg)
29832986
case SIGN_VERBATIM:
29842987
case SIGN_STRIP_IF_INVALID:
29852988
case SIGN_SIGN_IF_INVALID:
2989+
case SIGN_ABORT_IF_INVALID:
29862990
import_one_signature(&sig_sha1, &sig_sha256, v);
29872991
break;
29882992

@@ -3068,7 +3072,8 @@ static void parse_new_commit(const char *arg)
30683072
encoding);
30693073

30703074
if ((signed_commit_mode == SIGN_STRIP_IF_INVALID ||
3071-
signed_commit_mode == SIGN_SIGN_IF_INVALID) &&
3075+
signed_commit_mode == SIGN_SIGN_IF_INVALID ||
3076+
signed_commit_mode == SIGN_ABORT_IF_INVALID) &&
30723077
(sig_sha1.hash_algo || sig_sha256.hash_algo))
30733078
handle_signature_if_invalid(&new_data, &sig_sha1, &sig_sha256,
30743079
&msg, signed_commit_mode);
@@ -3115,6 +3120,9 @@ static void handle_tag_signature(struct strbuf *msg, const char *name)
31153120
case SIGN_ABORT:
31163121
die(_("encountered signed tag; use "
31173122
"--signed-tags=<mode> to handle it"));
3123+
case SIGN_ABORT_IF_INVALID:
3124+
die(_("'abort-if-invalid' is not a valid mode for "
3125+
"git fast-import with --signed-tags=<mode>"));
31183126
case SIGN_STRIP_IF_INVALID:
31193127
die(_("'strip-if-invalid' is not a valid mode for "
31203128
"git fast-import with --signed-tags=<mode>"));

gpg-interface.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,8 @@ int parse_sign_mode(const char *arg, enum sign_mode *mode, const char **keyid)
11641164
*mode = SIGN_WARN_STRIP;
11651165
} else if (!strcmp(arg, "strip")) {
11661166
*mode = SIGN_STRIP;
1167+
} else if (!strcmp(arg, "abort-if-invalid")) {
1168+
*mode = SIGN_ABORT_IF_INVALID;
11671169
} else if (!strcmp(arg, "strip-if-invalid")) {
11681170
*mode = SIGN_STRIP_IF_INVALID;
11691171
} else if (!strcmp(arg, "sign-if-invalid")) {

gpg-interface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ void print_signature_buffer(const struct signature_check *sigc,
115115
/* Modes for --signed-tags=<mode> and --signed-commits=<mode> options. */
116116
enum sign_mode {
117117
SIGN_ABORT,
118+
SIGN_ABORT_IF_INVALID,
118119
SIGN_WARN_VERBATIM,
119120
SIGN_VERBATIM,
120121
SIGN_WARN_STRIP,

t/t9305-fast-import-signatures.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test_expect_success RUST,GPG 'strip both OpenPGP signatures with --signed-commit
103103
test_line_count = 2 out
104104
'
105105

106-
for mode in strip-if-invalid sign-if-invalid
106+
for mode in strip-if-invalid sign-if-invalid abort-if-invalid
107107
do
108108
test_expect_success GPG "import commit with no signature with --signed-commits=$mode" '
109109
git fast-export main >output &&
@@ -135,6 +135,14 @@ do
135135
# corresponding `data <length>` command would have to be changed too.
136136
sed "s/OpenPGP signed commit/OpenPGP forged commit/" output >modified &&
137137
138+
if test "$mode" = abort-if-invalid
139+
then
140+
test_must_fail git -C new fast-import --quiet \
141+
--signed-commits=$mode <modified >log 2>&1 &&
142+
test_grep "aborting due to invalid signature" log &&
143+
return 0
144+
fi &&
145+
138146
git -C new fast-import --quiet --signed-commits=$mode <modified >log 2>&1 &&
139147
140148
IMPORTED=$(git -C new rev-parse --verify refs/heads/openpgp-signing) &&

0 commit comments

Comments
 (0)