Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite98f57b

Browse files
authored
Merge pull request#1672 from trail-of-forks/robust-refname-checks
Add more checks for the validity of refnames
2 parents1774f1e +46d3d05 commite98f57b

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

‎git/refs/symbolic.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,61 @@ def dereference_recursive(cls, repo: "Repo", ref_path: Union[PathLike, None]) ->
161161
returnhexsha
162162
# END recursive dereferencing
163163

164+
@staticmethod
165+
def_check_ref_name_valid(ref_path:PathLike)->None:
166+
# Based on the rules described in https://git-scm.com/docs/git-check-ref-format/#_description
167+
previous:Union[str,None]=None
168+
one_before_previous:Union[str,None]=None
169+
forcinstr(ref_path):
170+
ifcin" ~^:?*[\\":
171+
raiseValueError(
172+
f"Invalid reference '{ref_path}': references cannot contain spaces, tildes (~), carets (^),"
173+
f" colons (:), question marks (?), asterisks (*), open brackets ([) or backslashes (\\)"
174+
)
175+
elifc==".":
176+
ifpreviousisNoneorprevious=="/":
177+
raiseValueError(
178+
f"Invalid reference '{ref_path}': references cannot start with a period (.) or contain '/.'"
179+
)
180+
elifprevious==".":
181+
raiseValueError(f"Invalid reference '{ref_path}': references cannot contain '..'")
182+
elifc=="/":
183+
ifprevious=="/":
184+
raiseValueError(f"Invalid reference '{ref_path}': references cannot contain '//'")
185+
elifpreviousisNone:
186+
raiseValueError(
187+
f"Invalid reference '{ref_path}': references cannot start with forward slashes '/'"
188+
)
189+
elifc=="{"andprevious=="@":
190+
raiseValueError(f"Invalid reference '{ref_path}': references cannot contain '@{{'")
191+
eliford(c)<32orord(c)==127:
192+
raiseValueError(f"Invalid reference '{ref_path}': references cannot contain ASCII control characters")
193+
194+
one_before_previous=previous
195+
previous=c
196+
197+
ifprevious==".":
198+
raiseValueError(f"Invalid reference '{ref_path}': references cannot end with a period (.)")
199+
elifprevious=="/":
200+
raiseValueError(f"Invalid reference '{ref_path}': references cannot end with a forward slash (/)")
201+
elifprevious=="@"andone_before_previousisNone:
202+
raiseValueError(f"Invalid reference '{ref_path}': references cannot be '@'")
203+
elifany([component.endswith(".lock")forcomponentinstr(ref_path).split("/")]):
204+
raiseValueError(
205+
f"Invalid reference '{ref_path}': references cannot have slash-separated components that end with"
206+
f" '.lock'"
207+
)
208+
164209
@classmethod
165210
def_get_ref_info_helper(
166211
cls,repo:"Repo",ref_path:Union[PathLike,None]
167212
)->Union[Tuple[str,None],Tuple[None,str]]:
168213
"""Return: (str(sha), str(target_ref_path)) if available, the sha the file at
169214
rela_path points to, or None. target_ref_path is the reference we
170215
point to, or None"""
171-
if".."instr(ref_path):
172-
raiseValueError(f"Invalid reference '{ref_path}'")
216+
ifref_path:
217+
cls._check_ref_name_valid(ref_path)
218+
173219
tokens:Union[None,List[str],Tuple[str,str]]=None
174220
repodir=_git_dir(repo,ref_path)
175221
try:

‎test/test_refs.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,39 @@ def test_refs_outside_repo(self):
631631
ref_file.flush()
632632
ref_file_name=Path(ref_file.name).name
633633
self.assertRaises(BadName,self.rorepo.commit,f"../../{ref_file_name}")
634+
635+
deftest_validity_ref_names(self):
636+
check_ref=SymbolicReference._check_ref_name_valid
637+
# Based on the rules specified in https://git-scm.com/docs/git-check-ref-format/#_description
638+
# Rule 1
639+
self.assertRaises(ValueError,check_ref,".ref/begins/with/dot")
640+
self.assertRaises(ValueError,check_ref,"ref/component/.begins/with/dot")
641+
self.assertRaises(ValueError,check_ref,"ref/ends/with/a.lock")
642+
self.assertRaises(ValueError,check_ref,"ref/component/ends.lock/with/period_lock")
643+
# Rule 2
644+
check_ref("valid_one_level_refname")
645+
# Rule 3
646+
self.assertRaises(ValueError,check_ref,"ref/contains/../double/period")
647+
# Rule 4
648+
forcin" ~^:":
649+
self.assertRaises(ValueError,check_ref,f"ref/contains/invalid{c}/character")
650+
forcodeinrange(0,32):
651+
self.assertRaises(ValueError,check_ref,f"ref/contains/invalid{chr(code)}/ASCII/control_character")
652+
self.assertRaises(ValueError,check_ref,f"ref/contains/invalid{chr(127)}/ASCII/control_character")
653+
# Rule 5
654+
forcin"*?[":
655+
self.assertRaises(ValueError,check_ref,f"ref/contains/invalid{c}/character")
656+
# Rule 6
657+
self.assertRaises(ValueError,check_ref,"/ref/begins/with/slash")
658+
self.assertRaises(ValueError,check_ref,"ref/ends/with/slash/")
659+
self.assertRaises(ValueError,check_ref,"ref/contains//double/slash/")
660+
# Rule 7
661+
self.assertRaises(ValueError,check_ref,"ref/ends/with/dot.")
662+
# Rule 8
663+
self.assertRaises(ValueError,check_ref,"ref/contains@{/at_brace")
664+
# Rule 9
665+
self.assertRaises(ValueError,check_ref,"@")
666+
# Rule 10
667+
self.assertRaises(ValueError,check_ref,"ref/contain\\s/backslash")
668+
# Valid reference name should not raise
669+
check_ref("valid/ref/name")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp