usb-eject: support non-alphanumeric volume names

until now, volumes with whitespace and such would fail to unmount

also adds a sanchk that the directory to unmount is still below the
expected parent after absreal; the path was already passed to gio in
a safe manner (assuming gio doesn't have any vulns) but why risk it
This commit is contained in:
ed 2025-07-07 08:35:41 +00:00
parent d162502c38
commit ed908b9868

View file

@ -4,6 +4,7 @@ import os
import stat import stat
import subprocess as sp import subprocess as sp
import sys import sys
from urllib.parse import unquote_to_bytes as unquote
""" """
@ -28,14 +29,17 @@ which does the following respectively,
""" """
MOUNT_BASE = b"/run/media/egon/"
def main(): def main():
try: try:
label = sys.argv[1].split(":usb-eject:")[1].split(":")[0] label = sys.argv[1].split(":usb-eject:")[1].split(":")[0]
mp = "/run/media/egon/" + label mp = MOUNT_BASE + unquote(label)
# print("ejecting [%s]... " % (mp,), end="") # print("ejecting [%s]... " % (mp,), end="")
mp = os.path.abspath(os.path.realpath(mp.encode("utf-8"))) mp = os.path.abspath(os.path.realpath(mp))
st = os.lstat(mp) st = os.lstat(mp)
if not stat.S_ISDIR(st.st_mode): if not stat.S_ISDIR(st.st_mode) or not mp.startswith(MOUNT_BASE):
raise Exception("not a regular directory") raise Exception("not a regular directory")
# if you're running copyparty as root (thx for the faith) # if you're running copyparty as root (thx for the faith)