Skip to content

Commit d5017a1

Browse files
authored
Merge pull request libgit2#5289 from libgit2/cmn/create-with-signature-verification
commit: verify objects exist in git_commit_with_signature
2 parents 2a7d6de + 718f24a commit d5017a1

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/commit.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,14 @@ static void format_header_field(git_buf *out, const char *field, const char *con
878878
git_buf_putc(out, '\n');
879879
}
880880

881+
static const git_oid *commit_parent_from_commit(size_t n, void *payload)
882+
{
883+
const git_commit *commit = (const git_commit *) payload;
884+
885+
return git_array_get(commit->parent_ids, n);
886+
887+
}
888+
881889
int git_commit_create_with_signature(
882890
git_oid *out,
883891
git_repository *repo,
@@ -890,12 +898,26 @@ int git_commit_create_with_signature(
890898
const char *field;
891899
const char *header_end;
892900
git_buf commit = GIT_BUF_INIT;
901+
git_commit *parsed;
902+
git_array_oid_t parents = GIT_ARRAY_INIT;
893903

894-
/* We start by identifying the end of the commit header */
904+
/* The first step is to verify that all the tree and parents exist */
905+
parsed = git__calloc(1, sizeof(git_commit));
906+
GIT_ERROR_CHECK_ALLOC(parsed);
907+
if ((error = commit_parse(parsed, commit_content, strlen(commit_content), 0)) < 0)
908+
goto cleanup;
909+
910+
if ((error = validate_tree_and_parents(&parents, repo, &parsed->tree_id, commit_parent_from_commit, parsed, NULL, true)) < 0)
911+
goto cleanup;
912+
913+
git_array_clear(parents);
914+
915+
/* Then we start appending by identifying the end of the commit header */
895916
header_end = strstr(commit_content, "\n\n");
896917
if (!header_end) {
897918
git_error_set(GIT_ERROR_INVALID, "malformed commit contents");
898-
return -1;
919+
error = -1;
920+
goto cleanup;
899921
}
900922

901923
/* The header ends after the first LF */
@@ -919,6 +941,7 @@ int git_commit_create_with_signature(
919941
goto cleanup;
920942

921943
cleanup:
944+
git_commit__free(parsed);
922945
git_buf_dispose(&commit);
923946
return error;
924947
}

tests/commit/write.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -299,19 +299,43 @@ void test_commit_write__can_validate_objects(void)
299299
cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
300300
}
301301

302-
void test_commit_write__attach_singleline_signature(void)
302+
void test_commit_write__attach_signature_checks_objects(void)
303303
{
304304
const char *sig = "magic word: pretty please";
305+
const char *badtree = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
306+
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
307+
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
308+
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
309+
\n\
310+
a simple commit which does not work\n";
305311

306-
const char *data = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
312+
const char *badparent = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
307313
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
308314
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
309315
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
310316
\n\
317+
a simple commit which does not work\n";
318+
319+
git_oid id;
320+
321+
cl_git_fail_with(-1, git_commit_create_with_signature(&id, g_repo, badtree, sig, "magicsig"));
322+
cl_git_fail_with(-1, git_commit_create_with_signature(&id, g_repo, badparent, sig, "magicsig"));
323+
324+
}
325+
326+
void test_commit_write__attach_singleline_signature(void)
327+
{
328+
const char *sig = "magic word: pretty please";
329+
330+
const char *data = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
331+
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
332+
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
333+
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
334+
\n\
311335
a simple commit which works\n";
312336

313-
const char *complete = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
314-
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
337+
const char *complete = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
338+
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
315339
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
316340
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
317341
magicsig magic word: pretty please\n\
@@ -352,15 +376,15 @@ cpxtDQQMGYFpXK/71stq\n\
352376
=ozeK\n\
353377
-----END PGP SIGNATURE-----";
354378

355-
const char *data = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
356-
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
379+
const char *data = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
380+
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
357381
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
358382
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
359383
\n\
360384
a simple commit which works\n";
361385

362-
const char *complete = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
363-
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
386+
const char *complete = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
387+
parent 8496071c1b46c854b31185ea97743be6a8774479\n\
364388
author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
365389
committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n\
366390
gpgsig -----BEGIN PGP SIGNATURE-----\n\

0 commit comments

Comments
 (0)