Skip to content

Commit 28d0ba0

Browse files
author
Edward Thomson
committed
symbolic ref target validation: fixups
Fixups requested in libgit2#3912.
1 parent 452bf57 commit 28d0ba0

File tree

4 files changed

+19
-41
lines changed

4 files changed

+19
-41
lines changed

include/git2/common.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ typedef enum {
157157
GIT_OPT_SET_SSL_CERT_LOCATIONS,
158158
GIT_OPT_SET_USER_AGENT,
159159
GIT_OPT_ENABLE_STRICT_OBJECT_CREATION,
160-
GIT_OPT_ENABLE_SYMBOLIC_REF_TARGET_VALIDATION,
160+
GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION,
161161
GIT_OPT_SET_SSL_CIPHERS,
162162
GIT_OPT_GET_USER_AGENT,
163163
} git_libgit2_opt_t;
@@ -272,16 +272,14 @@ typedef enum {
272272
* > will be validated when creating a new commit. This defaults
273273
* > to disabled.
274274
*
275-
* * opts(GIT_OPT_ENABLE_SYMBOLIC_REF_TARGET_VALIDATION, int enabled)
275+
* * opts(GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION, int enabled)
276276
*
277-
* > Validate the target of a symbolic ref when creating it.
278-
* > For example, 'foobar' is not a valid ref,
279-
* > therefore 'foobar' is not a valid target
280-
* > for a symbolic ref by default,
281-
* > where as 'refs/heads/foobar' is.
282-
* > Disabling this bypasses validation so that an arbitrary
283-
* > strings such as 'foobar' can be used for a symbolic ref target.
284-
* > This defaults to enabled.
277+
* > Validate the target of a symbolic ref when creating it. For
278+
* > example, `foobar` is not a valid ref, therefore `foobar` is
279+
* > not a valid target for a symbolic ref by default, whereas
280+
* > `refs/heads/foobar` is. Disabling this bypasses validation
281+
* > so that an arbitrary strings such as `foobar` can be used
282+
* > for a symbolic ref target. This defaults to enabled.
285283
*
286284
* * opts(GIT_OPT_SET_SSL_CIPHERS, const char *ciphers)
287285
*

src/refs.c

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ int git_reference_name_to_id(
177177
return 0;
178178
}
179179

180-
static int reference__normalize_for_repo(
180+
static int reference_normalize_for_repo(
181181
git_refname_t out,
182182
git_repository *repo,
183183
const char *name,
@@ -190,29 +190,12 @@ static int reference__normalize_for_repo(
190190
precompose)
191191
flags |= GIT_REF_FORMAT__PRECOMPOSE_UNICODE;
192192

193-
if (!validate) {
194-
flags |= GIT_REF_VALIDATION_DISABLE;
195-
}
193+
if (!validate)
194+
flags |= GIT_REF_FORMAT__VALIDATION_DISABLE;
196195

197196
return git_reference_normalize_name(out, GIT_REFNAME_MAX, name, flags);
198197
}
199198

200-
static int reference_normalize_for_repo(
201-
git_refname_t out,
202-
git_repository *repo,
203-
const char *name)
204-
{
205-
return reference__normalize_for_repo(out, repo, name, true);
206-
}
207-
208-
static int reference_normalize_for_repo_without_validation(
209-
git_refname_t out,
210-
git_repository *repo,
211-
const char *name)
212-
{
213-
return reference__normalize_for_repo(out, repo, name, false);
214-
}
215-
216199
int git_reference_lookup_resolved(
217200
git_reference **ref_out,
218201
git_repository *repo,
@@ -236,7 +219,7 @@ int git_reference_lookup_resolved(
236219

237220
scan_type = GIT_REF_SYMBOLIC;
238221

239-
if ((error = reference_normalize_for_repo(scan_name, repo, name)) < 0)
222+
if ((error = reference_normalize_for_repo(scan_name, repo, name, true)) < 0)
240223
return error;
241224

242225
if ((error = git_repository_refdb__weakptr(&refdb, repo)) < 0)
@@ -406,7 +389,7 @@ static int reference__create(
406389
if (ref_out)
407390
*ref_out = NULL;
408391

409-
error = reference_normalize_for_repo(normalized, repo, name);
392+
error = reference_normalize_for_repo(normalized, repo, name, true);
410393
if (error < 0)
411394
return error;
412395

@@ -427,11 +410,8 @@ static int reference__create(
427410
} else {
428411
git_refname_t normalized_target;
429412

430-
if (git_reference__enable_symbolic_ref_target_validation) {
431-
error = reference_normalize_for_repo(normalized_target, repo, symbolic);
432-
} else {
433-
error = reference_normalize_for_repo_without_validation(normalized_target, repo, symbolic);
434-
}
413+
error = reference_normalize_for_repo(normalized_target, repo,
414+
symbolic, git_reference__enable_symbolic_ref_target_validation);
435415

436416
if (error < 0)
437417
return error;
@@ -612,7 +592,7 @@ static int reference__rename(git_reference **out, git_reference *ref, const char
612592
assert(ref && new_name && signature);
613593

614594
if ((error = reference_normalize_for_repo(
615-
normalized, git_reference_owner(ref), new_name)) < 0)
595+
normalized, git_reference_owner(ref), new_name, true)) < 0)
616596
return error;
617597

618598

@@ -905,7 +885,7 @@ int git_reference__normalize_name(
905885
int segment_len, segments_count = 0, error = GIT_EINVALIDSPEC;
906886
unsigned int process_flags;
907887
bool normalize = (buf != NULL);
908-
bool validate = (flags & GIT_REF_VALIDATION_DISABLE) == 0;
888+
bool validate = (flags & GIT_REF_FORMAT__VALIDATION_DISABLE) == 0;
909889

910890
#ifdef GIT_USE_ICONV
911891
git_path_iconv_t ic = GIT_PATH_ICONV_INIT;

src/refs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extern bool git_reference__enable_symbolic_ref_target_validation;
5555
#define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE
5656

5757
#define GIT_REF_FORMAT__PRECOMPOSE_UNICODE (1u << 16)
58-
#define GIT_REF_VALIDATION_DISABLE (1u << 15)
58+
#define GIT_REF_FORMAT__VALIDATION_DISABLE (1u << 15)
5959

6060
#define GIT_REFNAME_MAX 1024
6161

src/settings.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ int git_libgit2_opts(int key, ...)
192192
git_object__strict_input_validation = (va_arg(ap, int) != 0);
193193
break;
194194

195-
case GIT_OPT_ENABLE_SYMBOLIC_REF_TARGET_VALIDATION:
195+
case GIT_OPT_ENABLE_STRICT_SYMBOLIC_REF_CREATION:
196196
git_reference__enable_symbolic_ref_target_validation = (va_arg(ap, int) != 0);
197197
break;
198198

0 commit comments

Comments
 (0)