Skip to content

Commit

Permalink
fast-export, fast-import: add support for signed-commits
Browse files Browse the repository at this point in the history
fast-export has a --signed-tags= option that controls how to handle tag
signatures.  However, there is no equivalent for commit signatures; it
just silently strips the signature out of the commit (analogously to
--signed-tags=strip).

While signatures are generally problematic for fast-export/fast-import
(because hashes are likely to change), if they're going to support tag
signatures, there's no reason to not also support commit signatures.

So, implement a --signed-commits= option that mirrors the --signed-tags=
option.

On the fast-export side, try to be as much like signed-tags as possible,
in both implementation and in user-interface.  This will changes the
default behavior to '--signed-commits=abort' from what is now
'--signed-commits=strip'.  In order to provide an escape hatch for users
of third-party tools that call fast-export and do not yet know of the
--signed-commits= option, add an environment variable
'FAST_EXPORT_SIGNED_COMMITS_NOABORT=1' that changes the default to
'--signed-commits=warn-strip'.

Signed-off-by: Phillip Johnston <[email protected]>
  • Loading branch information
phillipjohnston committed Sep 18, 2024
1 parent c49920e commit 355fa1e
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 19 deletions.
11 changes: 11 additions & 0 deletions Documentation/git-fast-export.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ they will be exported, but you will see a warning. 'verbatim' and
transformations affecting tags will be performed, or if you do not
care that the resulting tag will have an invalid signature.

--signed-commits=(verbatim|warn-verbatim|warn-strip|strip|abort)::
Specify how to handle signed commits. Behaves exactly as
'--signed-tags', but for commits.
+
Earlier versions this command that did not have '--signed-commits'
behaved as if '--signed-commits=strip'. As an escape hatch for users
of tools that call 'git fast-export' but do not yet support
'--signed-commits', you may set the environment variable
'FAST_EXPORT_SIGNED_COMMITS_NOABORT=1' in order to change the default
from 'abort' to 'warn-strip'.

--tag-of-filtered-object=(abort|drop|rewrite)::
Specify how to handle tags whose tagged object is filtered out.
Since revisions and files to export can be limited by path,
Expand Down
18 changes: 18 additions & 0 deletions Documentation/git-fast-import.txt
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,21 @@ and control the current import process. More detailed discussion
Create or update a branch with a new commit, recording one logical
change to the project.

////
Yes, it's intentional that the 'gpgsig' line doesn't have a trailing
`LF`; the the definition of `data` has a byte-count prefix, so it
doesn't need an `LF` to act as a terminator (and `data` also already
includes an optional trailing `LF?` just in case you want to include
one).
////

....
'commit' SP <ref> LF
mark?
original-oid?
('author' (SP <name>)? SP LT <email> GT SP <when> LF)?
'committer' (SP <name>)? SP LT <email> GT SP <when> LF
('gpgsig' SP <alg> LF data)?
('encoding' SP <encoding> LF)?
data
('from' SP <commit-ish> LF)?
Expand Down Expand Up @@ -505,6 +514,15 @@ that was selected by the --date-format=<fmt> command-line option.
See ``Date Formats'' above for the set of supported formats, and
their syntax.

`gpgsig`
^^^^^^^^

The optional `gpgsig` command is used to include a PGP/GPG signature
that signs the commit data.

Here <alg> specifies which hashing algorithm is used for this
signature, either `sha1` or `sha256`.

`encoding`
^^^^^^^^^^
The optional `encoding` command indicates the encoding of the commit
Expand Down
116 changes: 97 additions & 19 deletions builtin/fast-export.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ static const char *fast_export_usage[] = {
N_("git fast-export [<rev-list-opts>]"),
NULL
};

static int progress;
static enum signed_tag_mode { SIGNED_TAG_ABORT, VERBATIM, WARN_VERBATIM, WARN_STRIP, STRIP } signed_tag_mode = SIGNED_TAG_ABORT;
enum sign_mode { SIGN_ABORT, SIGN_VERBATIM, SIGN_STRIP, SIGN_WARN_VERBATIM, SIGN_WARN_STRIP };
static enum sign_mode signed_tag_mode = SIGN_ABORT;
static enum sign_mode signed_commit_mode = SIGN_ABORT;
static enum tag_of_filtered_mode { TAG_FILTERING_ABORT, DROP, REWRITE } tag_of_filtered_mode = TAG_FILTERING_ABORT;
static enum reencode_mode { REENCODE_ABORT, REENCODE_YES, REENCODE_NO } reencode_mode = REENCODE_ABORT;
static int fake_missing_tagger;
Expand All @@ -49,23 +50,23 @@ static int anonymize;
static struct hashmap anonymized_seeds;
static struct revision_sources revision_sources;

static int parse_opt_signed_tag_mode(const struct option *opt,
static int parse_opt_sign_mode(const struct option *opt,
const char *arg, int unset)
{
enum signed_tag_mode *val = opt->value;
enum sign_mode *val = opt->value;

if (unset || !strcmp(arg, "abort"))
*val = SIGNED_TAG_ABORT;
*val = SIGN_ABORT;
else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
*val = VERBATIM;
*val = SIGN_VERBATIM;
else if (!strcmp(arg, "warn-verbatim") || !strcmp(arg, "warn"))
*val = WARN_VERBATIM;
*val = SIGN_WARN_VERBATIM;
else if (!strcmp(arg, "warn-strip"))
*val = WARN_STRIP;
*val = SIGN_WARN_STRIP;
else if (!strcmp(arg, "strip"))
*val = STRIP;
*val = SIGN_STRIP;
else
return error("Unknown signed-tags mode: %s", arg);
return error("Unknown %s mode: %s", opt->long_name, arg);
return 0;
}

Expand Down Expand Up @@ -607,6 +608,46 @@ static void anonymize_ident_line(const char **beg, const char **end)
*end = out->buf + out->len;
}

/*
* find_commit_multiline_header is similar to find_commit_header,
* except that it handles multi-line headers, rathar than simply
* returning the first line of the header.
*
* The returned string has had the ' ' line continuation markers
* removed, and points to staticly allocated memory (not to memory
* within 'msg'), so it is only valid until the next call to
* find_commit_multiline_header.
*
* If the header is found, then *end is set to point at the '\n' in
* msg that immediately follows the header value.
*/
static const char *find_commit_multiline_header(const char *msg,
const char *key,
const char **end)
{
static struct strbuf val = STRBUF_INIT;
const char *bol, *eol;
size_t len;

strbuf_reset(&val);

bol = find_commit_header(msg, key, &len);
if (!bol)
return NULL;
eol = bol + len;
strbuf_add(&val, bol, len);

while (eol[0] == '\n' && eol[1] == ' ') {
bol = eol + 2;
eol = strchrnul(bol, '\n');
strbuf_addch(&val, '\n');
strbuf_add(&val, bol, eol - bol);
}

*end = eol;
return val.buf;
}

static char *reencode_message(const char *in_msg,
const char *in_encoding, size_t in_encoding_len)
{
Expand All @@ -626,6 +667,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
const char *author, *author_end, *committer, *committer_end;
const char *encoding;
size_t encoding_len;
const char *signature_alg = NULL, *signature;
const char *message;
char *reencoded = NULL;
struct commit_list *p;
Expand All @@ -651,14 +693,19 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
committer++;
commit_buffer_cursor = committer_end = strchrnul(committer, '\n');

/* find_commit_header() gets a `+ 1` because
* commit_buffer_cursor points at the trailing "\n" at the end
* of the previous line, but find_commit_header() wants a
/* find_commit_header() and find_commit_multiline_header() get
* a `+ 1` because commit_buffer_cursor points at the trailing
* "\n" at the end of the previous line, but they want a
* pointer to the beginning of the next line. */
encoding = find_commit_header(commit_buffer_cursor + 1, "encoding", &encoding_len);
if (encoding)
commit_buffer_cursor = encoding + encoding_len;

if ((signature = find_commit_multiline_header(commit_buffer_cursor + 1, "gpgsig", &commit_buffer_cursor)))
signature_alg = "sha1";
else if ((signature = find_commit_multiline_header(commit_buffer_cursor + 1, "gpgsig-sha256", &commit_buffer_cursor)))
signature_alg = "sha256";

message = strstr(commit_buffer_cursor, "\n\n");
if (message)
message += 2;
Expand Down Expand Up @@ -719,6 +766,29 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
printf("%.*s\n%.*s\n",
(int)(author_end - author), author,
(int)(committer_end - committer), committer);
if (signature)
switch(signed_commit_mode) {
case SIGN_ABORT:
die("encountered signed commit %s; use "
"--signed-commits=<mode> to handle it",
oid_to_hex(&commit->object.oid));
case SIGN_WARN_VERBATIM:
warning("exporting signed commit %s",
oid_to_hex(&commit->object.oid));
/* fallthru */
case SIGN_VERBATIM:
printf("gpgsig %s\ndata %u\n%s",
signature_alg,
(unsigned)strlen(signature),
signature);
break;
case SIGN_WARN_STRIP:
warning("stripping signature from commit %s",
oid_to_hex(&commit->object.oid));
/* fallthru */
case SIGN_STRIP:
break;
}
if (!reencoded && encoding)
printf("encoding %.*s\n", (int)encoding_len, encoding);
printf("data %u\n%s",
Expand Down Expand Up @@ -834,21 +904,21 @@ static void handle_tag(const char *name, struct tag *tag)
"\n-----BEGIN PGP SIGNATURE-----\n");
if (signature)
switch(signed_tag_mode) {
case SIGNED_TAG_ABORT:
case SIGN_ABORT:
die("encountered signed tag %s; use "
"--signed-tags=<mode> to handle it",
oid_to_hex(&tag->object.oid));
case WARN_VERBATIM:
case SIGN_WARN_VERBATIM:
warning("exporting signed tag %s",
oid_to_hex(&tag->object.oid));
/* fallthru */
case VERBATIM:
case SIGN_VERBATIM:
break;
case WARN_STRIP:
case SIGN_WARN_STRIP:
warning("stripping signature from tag %s",
oid_to_hex(&tag->object.oid));
/* fallthru */
case STRIP:
case SIGN_STRIP:
message_size = signature + 1 - message;
break;
}
Expand Down Expand Up @@ -1191,6 +1261,7 @@ static int parse_opt_anonymize_map(const struct option *opt,

int cmd_fast_export(int argc, const char **argv, const char *prefix)
{
const char *env_signed_commits_noabort;
struct rev_info revs;
struct commit *commit;
char *export_filename = NULL,
Expand All @@ -1204,7 +1275,10 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
N_("show progress after <n> objects")),
OPT_CALLBACK(0, "signed-tags", &signed_tag_mode, N_("mode"),
N_("select handling of signed tags"),
parse_opt_signed_tag_mode),
parse_opt_sign_mode),
OPT_CALLBACK(0, "signed-commits", &signed_commit_mode, N_("mode"),
N_("select handling of signed commits"),
parse_opt_sign_mode),
OPT_CALLBACK(0, "tag-of-filtered-object", &tag_of_filtered_mode, N_("mode"),
N_("select handling of tags that tag filtered objects"),
parse_opt_tag_of_filtered_mode),
Expand Down Expand Up @@ -1245,6 +1319,10 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)
if (argc == 1)
usage_with_options (fast_export_usage, options);

env_signed_commits_noabort = getenv("FAST_EXPORT_SIGNED_COMMITS_NOABORT");
if (env_signed_commits_noabort && *env_signed_commits_noabort)
signed_commit_mode = SIGN_WARN_STRIP;

/* we handle encodings */
git_config(git_default_config, NULL);

Expand Down
23 changes: 23 additions & 0 deletions builtin/fast-import.c
Original file line number Diff line number Diff line change
Expand Up @@ -2698,10 +2698,13 @@ static struct hash_list *parse_merge(unsigned int *count)

static void parse_new_commit(const char *arg)
{
static struct strbuf sig = STRBUF_INIT;
static struct strbuf msg = STRBUF_INIT;
struct string_list siglines = STRING_LIST_INIT_NODUP;
struct branch *b;
char *author = NULL;
char *committer = NULL;
char *sig_alg = NULL;
char *encoding = NULL;
struct hash_list *merge_list = NULL;
unsigned int merge_count;
Expand All @@ -2725,6 +2728,13 @@ static void parse_new_commit(const char *arg)
}
if (!committer)
die("Expected committer but didn't get one");
if (skip_prefix(command_buf.buf, "gpgsig ", &v)) {
sig_alg = xstrdup(v);
read_next_command();
parse_data(&sig, 0, NULL);
read_next_command();
} else
strbuf_setlen(&sig, 0);
if (skip_prefix(command_buf.buf, "encoding ", &v)) {
encoding = xstrdup(v);
read_next_command();
Expand Down Expand Up @@ -2798,10 +2808,23 @@ static void parse_new_commit(const char *arg)
strbuf_addf(&new_data,
"encoding %s\n",
encoding);
if (sig_alg) {
if (!strcmp(sig_alg, "sha1"))
strbuf_addstr(&new_data, "gpgsig ");
else if (!strcmp(sig_alg, "sha256"))
strbuf_addstr(&new_data, "gpgsig-sha256 ");
else
die("Expected gpgsig algorithm sha1 or sha256, got %s", sig_alg);
string_list_split_in_place(&siglines, sig.buf, '\n', -1);

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux32 (i386/ubuntu:focal)

builtin/fast-import.c:2818:50: passing argument 3 of 'string_list_split_in_place' makes pointer from integer without a cast [-Werror=int-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-musl (alpine)

builtin/fast-import.c:2818:64: passing argument 3 of 'string_list_split_in_place' makes pointer from integer without a cast [-Werror=int-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / pedantic (fedora)

builtin/fast-import.c:2818:64: passing argument 3 of 'string_list_split_in_place' makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / win build

builtin/fast-import.c:2818:64: passing argument 3 of 'string_list_split_in_place' makes pointer from integer without a cast [-Wint-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-asan-ubsan (ubuntu-latest)

builtin/fast-import.c:2818:50: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-gcc (ubuntu-20.04)

builtin/fast-import.c:2818:50: passing argument 3 of ‘string_list_split_in_place’ makes pointer from integer without a cast [-Werror=int-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-gcc-default (ubuntu-latest)

builtin/fast-import.c:2818:64: passing argument 3 of ‘string_list_split_in_place’ makes pointer from integer without a cast [-Werror=int-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-leaks (ubuntu-latest)

builtin/fast-import.c:2818:64: passing argument 3 of ‘string_list_split_in_place’ makes pointer from integer without a cast [-Werror=int-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-reftable (ubuntu-latest)

builtin/fast-import.c:2818:50: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-reftable-leaks (ubuntu-latest)

builtin/fast-import.c:2818:64: passing argument 3 of ‘string_list_split_in_place’ makes pointer from integer without a cast [-Werror=int-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-sha256 (ubuntu-latest)

builtin/fast-import.c:2818:50: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Werror,-Wint-conversion]

Check failure on line 2818 in builtin/fast-import.c

View workflow job for this annotation

GitHub Actions / linux-TEST-vars (ubuntu-20.04)

builtin/fast-import.c:2818:50: passing argument 3 of ‘string_list_split_in_place’ makes pointer from integer without a cast [-Werror=int-conversion]
strbuf_add_separated_string_list(&new_data, "\n ", &siglines);
strbuf_addch(&new_data, '\n');
}
strbuf_addch(&new_data, '\n');
strbuf_addbuf(&new_data, &msg);
string_list_clear(&siglines, 1);
free(author);
free(committer);
free(sig_alg);
free(encoding);

if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
Expand Down
Loading

0 comments on commit 355fa1e

Please sign in to comment.