Skip to content

Commit 50cbafc

Browse files
committed
Add more test and remove password also from error logs
1 parent f7968d1 commit 50cbafc

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

‎git/cmd.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ def pump_stream(cmdline, name, stream, is_decode, handler):
8282
line=line.decode(defenc)
8383
handler(line)
8484
exceptExceptionasex:
85-
log.error("Pumping %r of cmd(%s) failed due to: %r", name, cmdline, ex)
86-
raiseCommandError(['<%s-pump>'%name] +cmdline, ex) fromex
85+
log.error("Pumping %r of cmd(%s) failed due to: %r", name, remove_password_if_present(cmdline), ex)
86+
raiseCommandError(['<%s-pump>'%name] +remove_password_if_present(cmdline), ex) fromex
8787
finally:
8888
stream.close()
8989

‎git/util.py‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,13 @@ def expand_path(p, expand_vars=True):
343343
defremove_password_if_present(cmdline):
344344
"""
345345
Parse any command line argument and if on of the element is an URL with a
346-
password, replace it by stars. If nothing found just returns a copy of the
347-
command line as-is.
346+
password, replace it by stars (in-place).
347+
348+
If nothing found just returns the command line as-is.
348349
349350
This should be used for every log line that print a command line.
350351
"""
351-
redacted_cmdline= []
352-
forto_parseincmdline:
352+
forindex, to_parseinenumerate(cmdline):
353353
try:
354354
url=urlsplit(to_parse)
355355
# Remove password from the URL if present
@@ -358,12 +358,11 @@ def remove_password_if_present(cmdline):
358358

359359
edited_url=url._replace(
360360
netloc=url.netloc.replace(url.password, "*****"))
361-
redacted_cmdline.append(urlunsplit(edited_url))
361+
cmdline[index] =urlunsplit(edited_url)
362362
exceptValueError:
363-
redacted_cmdline.append(to_parse)
364363
# This is not a valid URL
365364
pass
366-
returnredacted_cmdline
365+
returncmdline
367366

368367

369368
#} END utilities

‎test/test_util.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
Actor,
3131
IterableList,
3232
cygpath,
33-
decygpath
33+
decygpath,
34+
remove_password_if_present,
3435
)
3536

3637

@@ -322,3 +323,17 @@ def test_pickle_tzoffset(self):
322323
t2=pickle.loads(pickle.dumps(t1))
323324
self.assertEqual(t1._offset, t2._offset)
324325
self.assertEqual(t1._name, t2._name)
326+
327+
deftest_remove_password_from_command_line(self):
328+
"""Check that the password is not printed on the logs"""
329+
password="fakepassword1234"
330+
url_with_pass="https://fakeuser:{}@fakerepo.example.com/testrepo".format(password)
331+
url_without_pass="https://fakerepo.example.com/testrepo"
332+
333+
cmd_1= ["git", "clone", "-v", url_with_pass]
334+
cmd_2= ["git", "clone", "-v", url_without_pass]
335+
cmd_3= ["no", "url", "in", "this", "one"]
336+
337+
assertpasswordnotinremove_password_if_present(cmd_1)
338+
assertcmd_2==remove_password_if_present(cmd_2)
339+
assertcmd_3==remove_password_if_present(cmd_3)

0 commit comments

Comments
(0)