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

Skip to content

Commit f2dddf5

Browse files
author
Edward Thomson
committed
turn on strict object validation by default
1 parent 4afe536 commit f2dddf5

File tree

5 files changed

+34
-29
lines changed

5 files changed

+34
-29
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ v0.23 + 1
2929
* Rebases can now be performed purely in-memory, without touching the
3030
repository's workdir.
3131

32+
* When adding objects to the index, or when creating new tree or commit
33+
objects, the inputs are validated to ensure that the dependent objects
34+
exist and are of the correct type. This object validation can be
35+
disabled with the GIT_OPT_ENABLE_STRICT_OBJECT_CREATION option.
36+
3237
### API additions
3338

3439
* `git_config_lock()` has been added, which allow for

src/object.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "blob.h"
1515
#include "tag.h"
1616

17-
bool git_object__strict_input_validation = false;
17+
bool git_object__strict_input_validation = true;
1818

1919
typedef struct {
2020
const char *str; /* type name string */

tests/commit/write.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void test_commit_write__cleanup(void)
3838

3939
cl_git_sandbox_cleanup();
4040

41-
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
41+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
4242
}
4343

4444

@@ -196,10 +196,12 @@ static int create_commit_from_ids(
196196
return ret;
197197
}
198198

199-
void test_commit_write__doesnt_validate_objects_by_default(void)
199+
void test_commit_write__can_write_invalid_objects(void)
200200
{
201201
git_oid expected_id, tree_id, parent_id, commit_id;
202202

203+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
204+
203205
/* this is a valid tree and parent */
204206
git_oid_fromstr(&tree_id, tree_id_str);
205207
git_oid_fromstr(&parent_id, parent_id_str);
@@ -237,8 +239,6 @@ void test_commit_write__can_validate_objects(void)
237239
{
238240
git_oid tree_id, parent_id, commit_id;
239241

240-
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
241-
242242
/* this is a valid tree and parent */
243243
git_oid_fromstr(&tree_id, tree_id_str);
244244
git_oid_fromstr(&parent_id, parent_id_str);

tests/index/add.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void test_index_add__cleanup(void)
2020
cl_git_sandbox_cleanup();
2121
g_repo = NULL;
2222

23-
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
23+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
2424
}
2525

2626
static void test_add_entry(
@@ -42,7 +42,7 @@ static void test_add_entry(
4242
void test_index_add__invalid_entries_succeeds_by_default(void)
4343
{
4444
/*
45-
* Ensure that there is no validation on ids by default
45+
* Ensure that there is validation on object ids by default
4646
*/
4747

4848
/* ensure that we can add some actually good entries */
@@ -51,34 +51,34 @@ void test_index_add__invalid_entries_succeeds_by_default(void)
5151
test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK);
5252

5353
/* test that we fail to add some invalid (missing) blobs and trees */
54-
test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB);
55-
test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
56-
test_add_entry(true, invalid_id, GIT_FILEMODE_LINK);
54+
test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB);
55+
test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
56+
test_add_entry(false, invalid_id, GIT_FILEMODE_LINK);
5757

5858
/* test that we validate the types of objects */
59-
test_add_entry(true, valid_commit_id, GIT_FILEMODE_BLOB);
60-
test_add_entry(true, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
61-
test_add_entry(true, valid_commit_id, GIT_FILEMODE_LINK);
59+
test_add_entry(false, valid_commit_id, GIT_FILEMODE_BLOB);
60+
test_add_entry(false, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
61+
test_add_entry(false, valid_commit_id, GIT_FILEMODE_LINK);
6262

6363
/*
64-
* Ensure that strict object references will fail the `index_add`
64+
* Ensure that there we can disable validation
6565
*/
6666

67-
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
67+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
6868

6969
/* ensure that we can add some actually good entries */
7070
test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB);
7171
test_add_entry(true, valid_blob_id, GIT_FILEMODE_BLOB_EXECUTABLE);
7272
test_add_entry(true, valid_blob_id, GIT_FILEMODE_LINK);
7373

74-
/* test that we fail to add some invalid (missing) blobs and trees */
75-
test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB);
76-
test_add_entry(false, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
77-
test_add_entry(false, invalid_id, GIT_FILEMODE_LINK);
74+
/* test that we can now add some invalid (missing) blobs and trees */
75+
test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB);
76+
test_add_entry(true, invalid_id, GIT_FILEMODE_BLOB_EXECUTABLE);
77+
test_add_entry(true, invalid_id, GIT_FILEMODE_LINK);
7878

79-
/* test that we validate the types of objects */
80-
test_add_entry(false, valid_commit_id, GIT_FILEMODE_BLOB);
81-
test_add_entry(false, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
82-
test_add_entry(false, valid_commit_id, GIT_FILEMODE_LINK);
79+
/* test that we do not validate the types of objects */
80+
test_add_entry(true, valid_commit_id, GIT_FILEMODE_BLOB);
81+
test_add_entry(true, valid_tree_id, GIT_FILEMODE_BLOB_EXECUTABLE);
82+
test_add_entry(true, valid_commit_id, GIT_FILEMODE_LINK);
8383
}
8484

tests/object/tree/write.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void test_object_tree_write__cleanup(void)
1919
{
2020
cl_git_sandbox_cleanup();
2121

22-
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
22+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
2323
}
2424

2525
void test_object_tree_write__from_memory(void)
@@ -492,11 +492,11 @@ static void test_invalid_objects(bool should_allow_invalid)
492492

493493
void test_object_tree_write__object_validity(void)
494494
{
495-
/* Ensure that we can add invalid objects by default */
496-
test_invalid_objects(true);
497-
498-
/* Ensure that we can turn on validation */
499-
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 1));
495+
/* Ensure that we cannot add invalid objects by default */
500496
test_invalid_objects(false);
497+
498+
/* Ensure that we can turn off validation */
499+
cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0));
500+
test_invalid_objects(true);
501501
}
502502

0 commit comments

Comments
 (0)