| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ | |||
| 8 | 8 | from gitdb.exc import BadName # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 | |
| 9 | 9 | from gitdb.exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 | |
| 10 | 10 | from git.compat import safe_decode | |
| 11 | + from git.util import remove_password_if_present | ||
| 11 | 12 | ||
| 12 | 13 | # typing ---------------------------------------------------- | |
| 13 | 14 | ||
@@ -54,7 +55,7 @@ def __init__(self, command: Union[List[str], Tuple[str, ...], str], | |||
| 54 | 55 | stdout: Union[bytes, str, None] = None) -> None: | |
| 55 | 56 | if not isinstance(command, (tuple, list)): | |
| 56 | 57 | command = command.split() | |
| 57 | - self.command = command | ||
| 58 | + self.command = remove_password_if_present(command) | ||
| 58 | 59 | self.status = status | |
| 59 | 60 | if status: | |
| 60 | 61 | if isinstance(status, Exception): | |
@@ -66,8 +67,8 @@ def __init__(self, command: Union[List[str], Tuple[str, ...], str], | |||
| 66 | 67 | s = safe_decode(str(status)) | |
| 67 | 68 | status = "'%s'" % s if isinstance(status, str) else s | |
| 68 | 69 | ||
| 69 | - self._cmd = safe_decode(command[0]) | ||
| 70 | - self._cmdline = ' '.join(safe_decode(i) for i in command) | ||
| 70 | + self._cmd = safe_decode(self.command[0]) | ||
| 71 | + self._cmdline = ' '.join(safe_decode(i) for i in self.command) | ||
| 71 | 72 | self._cause = status and " due to: %s" % status or "!" | |
| 72 | 73 | stdout_decode = safe_decode(stdout) | |
| 73 | 74 | stderr_decode = safe_decode(stderr) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,7 +5,6 @@ | |||
| 5 | 5 | # the BSD License: http://www.opensource.org/licenses/bsd-license.php | |
| 6 | 6 | ||
| 7 | 7 | from abc import abstractmethod | |
| 8 | - from .exc import InvalidGitRepositoryError | ||
| 9 | 8 | import os.path as osp | |
| 10 | 9 | from .compat import is_win | |
| 11 | 10 | import contextlib | |
@@ -94,6 +93,8 @@ def unbare_repo(func: Callable[..., T]) -> Callable[..., T]: | |||
| 94 | 93 | """Methods with this decorator raise InvalidGitRepositoryError if they | |
| 95 | 94 | encounter a bare repository""" | |
| 96 | 95 | ||
| 96 | + from .exc import InvalidGitRepositoryError | ||
| 97 | + | ||
| 97 | 98 | @wraps(func) | |
| 98 | 99 | def wrapper(self: 'Remote', *args: Any, **kwargs: Any) -> T: | |
| 99 | 100 | if self.repo.bare: | |
@@ -412,24 +413,29 @@ def expand_path(p: Union[None, PathLike], expand_vars: bool = True) -> Optional[ | |||
| 412 | 413 | def remove_password_if_present(cmdline: Sequence[str]) -> List[str]: | |
| 413 | 414 | """ | |
| 414 | 415 | Parse any command line argument and if on of the element is an URL with a | |
| 415 | - password, replace it by stars (in-place). | ||
| 416 | + username and/or password, replace them by stars (in-place). | ||
| 416 | 417 | ||
| 417 | 418 | If nothing found just returns the command line as-is. | |
| 418 | 419 | ||
| 419 | - This should be used for every log line that print a command line. | ||
| 420 | + This should be used for every log line that print a command line, as well as | ||
| 421 | + exception messages. | ||
| 420 | 422 | """ | |
| 421 | 423 | new_cmdline = [] | |
| 422 | 424 | for index, to_parse in enumerate(cmdline): | |
| 423 | 425 | new_cmdline.append(to_parse) | |
| 424 | 426 | try: | |
| 425 | 427 | url = urlsplit(to_parse) | |
| 426 | 428 | # Remove password from the URL if present | |
| 427 | - if url.password is None: | ||
| 429 | + if url.password is None and url.username is None: | ||
| 428 | 430 | continue | |
| 429 | 431 | ||
| 430 | - edited_url = url._replace( | ||
| 431 | - netloc=url.netloc.replace(url.password, "*****")) | ||
| 432 | - new_cmdline[index] = urlunsplit(edited_url) | ||
| 432 | + if url.password is not None: | ||
| 433 | + url = url._replace( | ||
| 434 | + netloc=url.netloc.replace(url.password, "*****")) | ||
| 435 | + if url.username is not None: | ||
| 436 | + url = url._replace( | ||
| 437 | + netloc=url.netloc.replace(url.username, "*****")) | ||
| 438 | + new_cmdline[index] = urlunsplit(url) | ||
| 433 | 439 | except ValueError: | |
| 434 | 440 | # This is not a valid URL | |
| 435 | 441 | continue | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | HookExecutionError, | |
| 23 | 23 | RepositoryDirtyError, | |
| 24 | 24 | ) | |
| 25 | + from git.util import remove_password_if_present | ||
| 25 | 26 | from test.lib import TestBase | |
| 26 | 27 | ||
| 27 | 28 | import itertools as itt | |
@@ -34,6 +35,7 @@ | |||
| 34 | 35 | ('cmd', 'ελληνικα', 'args'), | |
| 35 | 36 | ('θνιψοδε', 'κι', 'αλλα', 'strange', 'args'), | |
| 36 | 37 | ('θνιψοδε', 'κι', 'αλλα', 'non-unicode', 'args'), | |
| 38 | + ('git', 'clone', '-v', 'https://fakeuser:fakepassword1234@fakerepo.example.com/testrepo'), | ||
| 37 | 39 | ) | |
| 38 | 40 | _causes_n_substrings = ( | |
| 39 | 41 | (None, None), # noqa: E241 @IgnorePep8 | |
@@ -81,7 +83,7 @@ def test_CommandError_unicode(self, case): | |||
| 81 | 83 | self.assertIsNotNone(c._msg) | |
| 82 | 84 | self.assertIn(' cmdline: ', s) | |
| 83 | 85 | ||
| 84 | - for a in argv: | ||
| 86 | + for a in remove_password_if_present(argv): | ||
| 85 | 87 | self.assertIn(a, s) | |
| 86 | 88 | ||
| 87 | 89 | if not cause: | |
@@ -137,14 +139,15 @@ def test_GitCommandNotFound(self, init_args): | |||
| 137 | 139 | @ddt.data( | |
| 138 | 140 | (['cmd1'], None), | |
| 139 | 141 | (['cmd1'], "some cause"), | |
| 140 | - (['cmd1'], Exception()), | ||
| 142 | + (['cmd1', 'https://fakeuser@fakerepo.example.com/testrepo'], Exception()), | ||
| 141 | 143 | ) | |
| 142 | 144 | def test_GitCommandError(self, init_args): | |
| 143 | 145 | argv, cause = init_args | |
| 144 | 146 | c = GitCommandError(argv, cause) | |
| 145 | 147 | s = str(c) | |
| 146 | 148 | ||
| 147 | - self.assertIn(argv[0], s) | ||
| 149 | + for arg in remove_password_if_present(argv): | ||
| 150 | + self.assertIn(arg, s) | ||
| 148 | 151 | if cause: | |
| 149 | 152 | self.assertIn(' failed due to: ', s) | |
| 150 | 153 | self.assertIn(str(cause), s) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -343,18 +343,34 @@ def test_pickle_tzoffset(self): | |||
| 343 | 343 | self.assertEqual(t1._name, t2._name) | |
| 344 | 344 | ||
| 345 | 345 | def test_remove_password_from_command_line(self): | |
| 346 | + username = "fakeuser" | ||
| 346 | 347 | password = "fakepassword1234" | |
| 347 | - url_with_pass = "https://fakeuser:{}@fakerepo.example.com/testrepo".format(password) | ||
| 348 | - url_without_pass = "https://fakerepo.example.com/testrepo" | ||
| 348 | + url_with_user_and_pass = "https://{}:{}@fakerepo.example.com/testrepo".format(username, password) | ||
| 349 | + url_with_user = "https://{}@fakerepo.example.com/testrepo".format(username) | ||
| 350 | + url_with_pass = "https://:{}@fakerepo.example.com/testrepo".format(password) | ||
| 351 | + url_without_user_or_pass = "https://fakerepo.example.com/testrepo" | ||
| 349 | 352 | ||
| 350 | - cmd_1 = ["git", "clone", "-v", url_with_pass] | ||
| 351 | - cmd_2 = ["git", "clone", "-v", url_without_pass] | ||
| 352 | - cmd_3 = ["no", "url", "in", "this", "one"] | ||
| 353 | + cmd_1 = ["git", "clone", "-v", url_with_user_and_pass] | ||
| 354 | + cmd_2 = ["git", "clone", "-v", url_with_user] | ||
| 355 | + cmd_3 = ["git", "clone", "-v", url_with_pass] | ||
| 356 | + cmd_4 = ["git", "clone", "-v", url_without_user_or_pass] | ||
| 357 | + cmd_5 = ["no", "url", "in", "this", "one"] | ||
| 353 | 358 | ||
| 354 | 359 | redacted_cmd_1 = remove_password_if_present(cmd_1) | |
| 360 | + assert username not in " ".join(redacted_cmd_1) | ||
| 355 | 361 | assert password not in " ".join(redacted_cmd_1) | |
| 356 | 362 | # Check that we use a copy | |
| 357 | 363 | assert cmd_1 is not redacted_cmd_1 | |
| 364 | + assert username in " ".join(cmd_1) | ||
| 358 | 365 | assert password in " ".join(cmd_1) | |
| 359 | - assert cmd_2 == remove_password_if_present(cmd_2) | ||
| 360 | - assert cmd_3 == remove_password_if_present(cmd_3) | ||
| 366 | + | ||
| 367 | + redacted_cmd_2 = remove_password_if_present(cmd_2) | ||
| 368 | + assert username not in " ".join(redacted_cmd_2) | ||
| 369 | + assert password not in " ".join(redacted_cmd_2) | ||
| 370 | + | ||
| 371 | + redacted_cmd_3 = remove_password_if_present(cmd_3) | ||
| 372 | + assert username not in " ".join(redacted_cmd_3) | ||
| 373 | + assert password not in " ".join(redacted_cmd_3) | ||
| 374 | + | ||
| 375 | + assert cmd_4 == remove_password_if_present(cmd_4) | ||
| 376 | + assert cmd_5 == remove_password_if_present(cmd_5) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments