receive-pack: use find_commit_header() in check_nonce()

Use the public function find_commit_header() and remove find_header(), as it becomes unused. This is safe and appropriate because we pass the NUL-terminated payload buffer to check_nonce() instead of its start and length. The underlying strbuf push_cert cannot contain NULs, as it is built using strbuf_addstr(), only. We no longer need to call strlen(), as find_commit_header() returns the length of nonce already. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 9, 2024 at 21:41 UTC 020456cb744b16f686d9220eb4b95a8e157c3485
1 file changed +7 -23
builtin/receive-pack.c
+7 -23
@@ -593,21 +593,6 @@ static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp)
593 return strbuf_detach(&buf, NULL);
594 }
595
596 -static char *find_header(const char *msg, size_t len, const char *key,
597 - const char **next_line)
598 -{
599 - size_t out_len;
600 - const char *val = find_header_mem(msg, len, key, &out_len);
601 -
602 - if (!val)
603 - return NULL;
604 -
605 - if (next_line)
606 - *next_line = val + out_len + 1;
607 -
608 - return xmemdupz(val, out_len);
609 -}
610 -
596 /*
597 * Return zero if a and b are equal up to n bytes and nonzero if they are not.
598 * This operation is guaranteed to run in constant time to avoid leaking data.
@@ -622,13 +607,14 @@ static int constant_memequal(const char *a, const char *b, size_t n)
607 return res;
608 }
609
625 -static const char *check_nonce(const char *buf, size_t len)
610 +static const char *check_nonce(const char *buf)
611 {
627 - char *nonce = find_header(buf, len, "nonce", NULL);
612 + size_t noncelen;
613 + const char *found = find_commit_header(buf, "nonce", &noncelen);
614 + char *nonce = found ? xmemdupz(found, noncelen) : NULL;
615 timestamp_t stamp, ostamp;
616 char *bohmac, *expect = NULL;
617 const char *retval = NONCE_BAD;
631 - size_t noncelen;
618
619 if (!nonce) {
620 retval = NONCE_MISSING;
@@ -670,7 +656,6 @@ static const char *check_nonce(const char *buf, size_t len)
656 goto leave;
657 }
658
673 - noncelen = strlen(nonce);
659 expect = prepare_push_cert_nonce(service_dir, stamp);
660 if (noncelen != strlen(expect)) {
661 /* This is not even the right size. */
@@ -732,9 +717,8 @@ static int check_cert_push_options(const struct string_list *push_options)
717 buf = option + optionlen + 1;
718 options_seen++;
719 if (options_seen > push_options->nr
735 - || strncmp(push_options->items[options_seen - 1].string,
736 - option, optionlen)
737 - || push_options->items[options_seen - 1].string[optionlen])
720 + || xstrncmpz(push_options->items[options_seen - 1].string,
721 + option, optionlen))
722 return 0;
723 }
724
@@ -767,7 +751,7 @@ static void prepare_push_cert_sha1(struct child_process *proc)
751 check_signature(&sigcheck, push_cert.buf + bogs,
752 push_cert.len - bogs);
753
770 - nonce_status = check_nonce(push_cert.buf, bogs);
754 + nonce_status = check_nonce(sigcheck.payload);
755 }
756 if (!is_null_oid(&push_cert_oid)) {
757 strvec_pushf(&proc->env, "GIT_PUSH_CERT=%s",