Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ef63bab

Browse files
author
Edward Thomson
committed
git_commit: validate tree and parent ids
When `GIT_OPT_ENABLE_STRICT_OBJECT_CREATION` is turned on, validate the tree and parent ids given to commit creation functions.
1 parent 7565dc6 commit ef63bab

File tree

2 files changed

+143
-16
lines changed

2 files changed

+143
-16
lines changed

src/commit.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "signature.h"
1818
#include "message.h"
1919
#include "refs.h"
20+
#include "object.h"
2021

2122
void git_commit__free(void *_commit)
2223
{
@@ -36,7 +37,7 @@ void git_commit__free(void *_commit)
3637
git__free(commit);
3738
}
3839

39-
int git_commit_create_from_callback(
40+
static int git_commit__create_internal(
4041
git_oid *id,
4142
git_repository *repo,
4243
const char *update_ref,
@@ -46,7 +47,8 @@ int git_commit_create_from_callback(
4647
const char *message,
4748
const git_oid *tree,
4849
git_commit_parent_callback parent_cb,
49-
void *parent_payload)
50+
void *parent_payload,
51+
bool validate)
5052
{
5153
git_reference *ref = NULL;
5254
int error = 0, matched_parent = 0;
@@ -58,6 +60,9 @@ int git_commit_create_from_callback(
5860

5961
assert(id && repo && tree && parent_cb);
6062

63+
if (validate && !git_object__is_valid(repo, tree, GIT_OBJ_TREE))
64+
return -1;
65+
6166
if (update_ref) {
6267
error = git_reference_lookup_resolved(&ref, repo, update_ref, 10);
6368
if (error < 0 && error != GIT_ENOTFOUND)
@@ -71,6 +76,11 @@ int git_commit_create_from_callback(
7176
git_oid__writebuf(&commit, "tree ", tree);
7277

7378
while ((parent = parent_cb(i, parent_payload)) != NULL) {
79+
if (validate && !git_object__is_valid(repo, parent, GIT_OBJ_COMMIT)) {
80+
error = -1;
81+
goto on_error;
82+
}
83+
7484
git_oid__writebuf(&commit, "parent ", parent);
7585
if (i == 0 && current_id && git_oid_equal(current_id, parent))
7686
matched_parent = 1;
@@ -114,10 +124,26 @@ int git_commit_create_from_callback(
114124

115125
on_error:
116126
git_buf_free(&commit);
117-
giterr_set(GITERR_OBJECT, "Failed to create commit.");
118127
return -1;
119128
}
120129

130+
int git_commit_create_from_callback(
131+
git_oid *id,
132+
git_repository *repo,
133+
const char *update_ref,
134+
const git_signature *author,
135+
const git_signature *committer,
136+
const char *message_encoding,
137+
const char *message,
138+
const git_oid *tree,
139+
git_commit_parent_callback parent_cb,
140+
void *parent_payload)
141+
{
142+
return git_commit__create_internal(
143+
id, repo, update_ref, author, committer, message_encoding, message,
144+
tree, parent_cb, parent_payload, true);
145+
}
146+
121147
typedef struct {
122148
size_t total;
123149
va_list args;
@@ -153,10 +179,10 @@ int git_commit_create_v(
153179
data.total = parent_count;
154180
va_start(data.args, parent_count);
155181

156-
error = git_commit_create_from_callback(
182+
error = git_commit__create_internal(
157183
id, repo, update_ref, author, committer,
158184
message_encoding, message, git_tree_id(tree),
159-
commit_parent_from_varargs, &data);
185+
commit_parent_from_varargs, &data, false);
160186

161187
va_end(data.args);
162188
return error;
@@ -187,10 +213,10 @@ int git_commit_create_from_ids(
187213
{
188214
commit_parent_oids data = { parent_count, parents };
189215

190-
return git_commit_create_from_callback(
216+
return git_commit__create_internal(
191217
id, repo, update_ref, author, committer,
192218
message_encoding, message, tree,
193-
commit_parent_from_ids, &data);
219+
commit_parent_from_ids, &data, true);
194220
}
195221

196222
typedef struct {
@@ -227,10 +253,10 @@ int git_commit_create(
227253

228254
assert(tree && git_tree_owner(tree) == repo);
229255

230-
return git_commit_create_from_callback(
256+
return git_commit__create_internal(
231257
id, repo, update_ref, author, committer,
232258
message_encoding, message, git_tree_id(tree),
233-
commit_parent_from_array, &data);
259+
commit_parent_from_array, &data, false);
234260
}
235261

236262
static const git_oid *commit_parent_for_amend(size_t curr, void *payload)
@@ -290,9 +316,9 @@ int git_commit_amend(
290316
}
291317
}
292318

293-
error = git_commit_create_from_callback(
319+
error = git_commit__create_internal(
294320
id, repo, NULL, author, committer, message_encoding, message,
295-
&tree_id, commit_parent_for_amend, (void *)commit_to_amend);
321+
&tree_id, commit_parent_for_amend, (void *)commit_to_amend, false);
296322

297323
if (!error && update_ref) {
298324
error = git_reference__update_for_commit(

tests/commit/write.c

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include "clar_libgit2.h"
2+
#include "git2/sys/commit.h"
23

34
static const char *committer_name = "Vicent Marti";
45
static const char *committer_email = "[email protected]";
56
static const char *commit_message = "This commit has been created in memory\n\
67
This is a commit created in memory and it will be written back to disk\n";
7-
static const char *tree_oid = "1810dff58d8a660512d4832e740f692884338ccd";
8+
static const char *tree_id_str = "1810dff58d8a660512d4832e740f692884338ccd";
9+
static const char *parent_id_str = "8496071c1b46c854b31185ea97743be6a8774479";
810
static const char *root_commit_message = "This is a root commit\n\
911
This is a root commit and should be the only one in this branch\n";
1012
static const char *root_reflog_message = "commit (initial): This is a root commit \
@@ -35,6 +37,8 @@ void test_commit_write__cleanup(void)
3537
head_old = NULL;
3638

3739
cl_git_sandbox_cleanup();
40+
41+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
3842
}
3943

4044

@@ -46,12 +50,11 @@ void test_commit_write__from_memory(void)
4650
const git_signature *author1, *committer1;
4751
git_commit *parent;
4852
git_tree *tree;
49-
const char *commit_id_str = "8496071c1b46c854b31185ea97743be6a8774479";
5053

51-
git_oid_fromstr(&tree_id, tree_oid);
54+
git_oid_fromstr(&tree_id, tree_id_str);
5255
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
5356

54-
git_oid_fromstr(&parent_id, commit_id_str);
57+
git_oid_fromstr(&parent_id, parent_id_str);
5558
cl_git_pass(git_commit_lookup(&parent, g_repo, &parent_id));
5659

5760
/* create signatures */
@@ -106,7 +109,7 @@ void test_commit_write__root(void)
106109
git_reflog *log;
107110
const git_reflog_entry *entry;
108111

109-
git_oid_fromstr(&tree_id, tree_oid);
112+
git_oid_fromstr(&tree_id, tree_id_str);
110113
cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id));
111114

112115
/* create signatures */
@@ -158,3 +161,101 @@ void test_commit_write__root(void)
158161
git_signature_free(committer);
159162
git_reflog_free(log);
160163
}
164+
165+
static int create_commit_from_ids(
166+
git_oid *result,
167+
const git_oid *tree_id,
168+
const git_oid *parent_id)
169+
{
170+
git_signature *author, *committer;
171+
const git_oid *parent_ids[1];
172+
int ret;
173+
174+
cl_git_pass(git_signature_new(
175+
&committer, committer_name, committer_email, 123456789, 60));
176+
cl_git_pass(git_signature_new(
177+
&author, committer_name, committer_email, 987654321, 90));
178+
179+
parent_ids[0] = parent_id;
180+
181+
ret = git_commit_create_from_ids(
182+
result,
183+
g_repo,
184+
NULL,
185+
author,
186+
committer,
187+
NULL,
188+
root_commit_message,
189+
tree_id,
190+
1,
191+
parent_ids);
192+
193+
git_signature_free(committer);
194+
git_signature_free(author);
195+
196+
return ret;
197+
}
198+
199+
void test_commit_write__doesnt_validate_objects_by_default(void)
200+
{
201+
git_oid expected_id, tree_id, parent_id, commit_id;
202+
203+
/* this is a valid tree and parent */
204+
git_oid_fromstr(&tree_id, tree_id_str);
205+
git_oid_fromstr(&parent_id, parent_id_str);
206+
207+
git_oid_fromstr(&expected_id, "c8571bbec3a72c4bcad31648902e5a453f1adece");
208+
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
209+
cl_assert_equal_oid(&expected_id, &commit_id);
210+
211+
/* this is a wholly invented tree id */
212+
git_oid_fromstr(&tree_id, "1234567890123456789012345678901234567890");
213+
git_oid_fromstr(&parent_id, parent_id_str);
214+
215+
git_oid_fromstr(&expected_id, "996008340b8e68d69bf3c28d7c57fb7ec3c8e202");
216+
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
217+
cl_assert_equal_oid(&expected_id, &commit_id);
218+
219+
/* this is a wholly invented parent id */
220+
git_oid_fromstr(&tree_id, tree_id_str);
221+
git_oid_fromstr(&parent_id, "1234567890123456789012345678901234567890");
222+
223+
git_oid_fromstr(&expected_id, "d78f660cab89d9791ca6714b57978bf2a7e709fd");
224+
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
225+
cl_assert_equal_oid(&expected_id, &commit_id);
226+
227+
/* these are legitimate objects, but of the wrong type */
228+
git_oid_fromstr(&tree_id, parent_id_str);
229+
git_oid_fromstr(&parent_id, tree_id_str);
230+
231+
git_oid_fromstr(&expected_id, "5d80c07414e3f18792949699dfcacadf7748f361");
232+
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
233+
cl_assert_equal_oid(&expected_id, &commit_id);
234+
}
235+
236+
void test_commit_write__can_validate_objects(void)
237+
{
238+
git_oid tree_id, parent_id, commit_id;
239+
240+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
241+
242+
/* this is a valid tree and parent */
243+
git_oid_fromstr(&tree_id, tree_id_str);
244+
git_oid_fromstr(&parent_id, parent_id_str);
245+
cl_git_pass(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
246+
247+
/* this is a wholly invented tree id */
248+
git_oid_fromstr(&tree_id, "1234567890123456789012345678901234567890");
249+
git_oid_fromstr(&parent_id, parent_id_str);
250+
cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
251+
252+
/* this is a wholly invented parent id */
253+
git_oid_fromstr(&tree_id, tree_id_str);
254+
git_oid_fromstr(&parent_id, "1234567890123456789012345678901234567890");
255+
cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
256+
257+
/* these are legitimate objects, but of the wrong type */
258+
git_oid_fromstr(&tree_id, parent_id_str);
259+
git_oid_fromstr(&parent_id, tree_id_str);
260+
cl_git_fail(create_commit_from_ids(&commit_id, &tree_id, &parent_id));
261+
}

0 commit comments

Comments
 (0)