test: fix passwd_sanitize()

If any of the variables is empty the output is completely messed up,
because replace("", "FOO") puts "FOO" before every single character.

I don't have my full name configured, and this is what I get:

  USER_FULL_NAME=USER_FULL_NAME=USER_FULL_NAME USER_FULL_NAMEsUSER_FULL_NAMEtUSER_FULL_NAMEdUSER_FULL_NAMEoUSER_FULL_NAMEuUSER_FULL_NAMEtUSER_FULL_NAME USER_FULL_NAME=USER_FULL_NAME=USER_FULL_NAME

Let's check for empty strings before doing any replace.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
This commit is contained in:
Felipe Contreras 2021-05-01 06:54:10 -05:00 committed by David Bremner
parent 6a20478abb
commit ff5f141bf2

View file

@ -711,7 +711,12 @@ name = pw.pw_gecos.partition(",")[0]
fqdn = socket.getfqdn()
for l in sys.stdin:
l = l.replace(user, "USERNAME").replace(fqdn, "FQDN").replace(".(none)","").replace(name, "USER_FULL_NAME")
if user:
l = l.replace(user, "USERNAME")
if fqdn:
l = l.replace(fqdn, "FQDN").replace(".(none)","")
if name:
l = l.replace(name, "USER_FULL_NAME")
sys.stdout.write(l)
'
}