gpg-interface: remove other signature headers before verifying

When we have a multiply signed commit, we need to remove the signature in the header before verifying the object, since the trailing signature will not be over both pieces of data. Do so, and verify that we validate the signature appropriately. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Feb 11, 2021 at 02:08 UTC 9b27b49240f6bf760ff58d917491bec0981aaf9f
2 files changed +27
gpg-interface.c
+2
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "commit.h"
3 #include "config.h"
4 #include "run-command.h"
5 #include "strbuf.h"
@@ -366,6 +367,7 @@ int parse_signature(const char *buf, size_t size, struct strbuf *payload, struct
367 size_t match = parse_signed_buffer(buf, size);
368 if (match != size) {
369 strbuf_add(payload, buf, match);
370 + remove_signature(payload);
371 strbuf_add(signature, buf + match, size - match);
372 return 1;
373 }
t/t7004-tag.sh
+25
@@ -17,6 +17,13 @@ tag_exists () {
17 git show-ref --quiet --verify refs/tags/"$1"
18 }
19
20 +test_expect_success 'setup' '
21 + test_oid_cache <<-EOM
22 + othersigheader sha1:gpgsig-sha256
23 + othersigheader sha256:gpgsig
24 + EOM
25 +'
26 +
27 test_expect_success 'listing all tags in an empty tree should succeed' '
28 git tag -l &&
29 git tag
@@ -1371,6 +1378,24 @@ test_expect_success GPG \
1378 'test_config gpg.program echo &&
1379 test_must_fail git tag -s -m tail tag-gpg-failure'
1380
1381 +# try to produce invalid signature
1382 +test_expect_success GPG 'git verifies tag is valid with double signature' '
1383 + git tag -s -m tail tag-gpg-double-sig &&
1384 + git cat-file tag tag-gpg-double-sig >tag &&
1385 + othersigheader=$(test_oid othersigheader) &&
1386 + sed -ne "/^\$/q;p" tag >new-tag &&
1387 + cat <<-EOM >>new-tag &&
1388 + $othersigheader -----BEGIN PGP SIGNATURE-----
1389 + someinvaliddata
1390 + -----END PGP SIGNATURE-----
1391 + EOM
1392 + sed -e "1,/^tagger/d" tag >>new-tag &&
1393 + new_tag=$(git hash-object -t tag -w new-tag) &&
1394 + git update-ref refs/tags/tag-gpg-double-sig $new_tag &&
1395 + git verify-tag tag-gpg-double-sig &&
1396 + git fsck
1397 +'
1398 +
1399 # try to sign with bad user.signingkey
1400 test_expect_success GPGSM \
1401 'git tag -s fails if gpgsm is misconfigured (bad key)' \