Skip to content

Commit 0e71bcf

Browse files
authored
Do not write passwords to the command log (#3291)
1 parent 16a84ba commit 0e71bcf

2 files changed

Lines changed: 29 additions & 21 deletions

File tree

archinstall/lib/general.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,7 @@ def execute(self) -> bool:
307307

308308
# https://stackoverflow.com/questions/4022600/python-pty-fork-how-does-it-work
309309
if not self.pid:
310-
history_logfile = Path(f"{storage['LOG_PATH']}/cmd_history.txt")
311-
312-
change_perm = False
313-
if history_logfile.exists() is False:
314-
change_perm = True
315-
316-
try:
317-
with history_logfile.open("a") as cmd_log:
318-
cmd_log.write(f"{time.time()} {self.cmd}\n")
319-
320-
if change_perm:
321-
history_logfile.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
322-
except (PermissionError, FileNotFoundError):
323-
# If history_logfile does not exist, ignore the error
324-
pass
310+
_log_cmd(self.cmd)
325311

326312
try:
327313
os.execve(self.cmd[0], list(self.cmd), {**os.environ, **self.environment_vars})
@@ -456,6 +442,29 @@ def trace_log(self) -> bytes | None:
456442
return None
457443

458444

445+
def _log_cmd(cmd: list[str]) -> None:
446+
history_logfile = Path(f"{storage['LOG_PATH']}/cmd_history.txt")
447+
448+
change_perm = False
449+
if history_logfile.exists() is False:
450+
change_perm = True
451+
452+
try:
453+
with history_logfile.open("a") as cmd_log:
454+
cmd_log.write(f"{time.time()} {cmd}\n")
455+
456+
if change_perm:
457+
history_logfile.chmod(stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
458+
except (PermissionError, FileNotFoundError):
459+
# If history_logfile does not exist, ignore the error
460+
pass
461+
462+
463+
def run(cmd: list[str], input_data: bytes | None = None) -> None:
464+
_log_cmd(cmd)
465+
subprocess.run(cmd, input=input_data, check=True)
466+
467+
459468
def _pid_exists(pid: int) -> bool:
460469
try:
461470
return any(subprocess.check_output(['ps', '--no-headers', '-o', 'pid', '-p', str(pid)]).strip())

archinstall/lib/installer.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import time
99
from collections.abc import Callable
1010
from pathlib import Path
11+
from subprocess import CalledProcessError
1112
from types import TracebackType
1213
from typing import TYPE_CHECKING, Any
1314

@@ -31,7 +32,7 @@
3132

3233
from .args import arch_config_handler
3334
from .exceptions import DiskError, HardwareIncompatibilityError, RequirementError, ServiceException, SysCallError
34-
from .general import SysCommand
35+
from .general import SysCommand, run
3536
from .hardware import SysInfo
3637
from .locale.utils import verify_keyboard_layout, verify_x11_keyboard_layout
3738
from .luks import Luks2
@@ -1605,14 +1606,12 @@ def user_set_pw(self, user: str, password: str) -> bool:
16051606
# This means the root account isn't locked/disabled with * in /etc/passwd
16061607
self.helper_flags['user'] = True
16071608

1608-
combo = f'{user}:{password}'
1609-
echo = shlex.join(['echo', combo])
1610-
sh = shlex.join(['sh', '-c', echo])
1609+
cmd = ['arch-chroot', str(self.target), 'chpasswd']
16111610

16121611
try:
1613-
SysCommand(f"arch-chroot {self.target} " + sh[:-1] + " | chpasswd'")
1612+
run(cmd, input_data=f'{user}:{password}'.encode())
16141613
return True
1615-
except SysCallError:
1614+
except CalledProcessError:
16161615
return False
16171616

16181617
def user_set_shell(self, user: str, shell: str) -> bool:

0 commit comments

Comments
 (0)