Skip to content

Add support for snap#11

Open
rvxtrm wants to merge 1 commit intohargoniX:mainfrom
rvxtrm:main
Open

Add support for snap#11
rvxtrm wants to merge 1 commit intohargoniX:mainfrom
rvxtrm:main

Conversation

@rvxtrm
Copy link
Copy Markdown

@rvxtrm rvxtrm commented Sep 14, 2023

Snap is a popular software packaging and distribution system. When keepassxc was distributed by snap, the proxy can not read the /tmp directory because it is sandboxed (as for flatpak packages).
The socket can be read in the $SNAP_COMMON directory mentioned in this documentation : https://snapcraft.io/docs/data-locations

This PR aims at adding support for snap packages keepassxc by checking the existence of this directory before returning the default location for the socket (the same way for flatpak)

@hargoniX
Copy link
Copy Markdown
Owner

Very nice, thank you! As this socket location seems to be a common place for change. Could you maybe convert the code into a for loop that goes over all paths that we can think of, checks if they exist and returns them in that case and if it doesn't find anything throws an exception?

Copy link
Copy Markdown
Owner

@hargoniX hargoniX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

@rvxtrm
Copy link
Copy Markdown
Author

rvxtrm commented Sep 18, 2023

I tried to find something simple and generic, yet I had to preserve the if statements if I want to avoid errors on getting non set environment variables.
Looks like this :

    def get_socket_path():
        socket_name = "org.keepassxc.KeePassXC.BrowserServer"

        if platform.system() == "Windows":
            socket_search_paths = [socket_name + "_" + getpass.getuser()]
        else:
            socket_search_paths = [os.path.join("/tmp", socket_name)]
            if "TMPDIR" in os.environ:
                socket_search_paths.append(os.path.join(os.environ["TMPDIR"], socket_name))
            if "XDG_RUNTIME_DIR" in os.environ:
                socket_search_paths.append(os.path.join(os.environ["XDG_RUNTIME_DIR"], "app/org.keepassxc.KeePassXC", socket_name))
            if "HOME" in os.environ:
                socket_search_paths.append(os.path.join(os.environ["HOME"], "snap/keepassxc/common", socket_name))

        for socket_path in socket_search_paths:
            if os.path.exists(socket_path):
                return socket_path
        raise Exception("Error: Could not find any keepassxc socket in the following locations : ${socket_search_paths}")

I will try to get something more compact and readable but I cant figure out now

@kgraefe
Copy link
Copy Markdown

kgraefe commented Jan 24, 2024

proposal:

for var, path in [
    ("TMPDIR", ""),
    ("XDG_RUNTIME_DIR", "app/org.keepassxc.KeePassXC"),
    ("HOME", "snap/keepassxc/common"),
]:
    if var in os.environ:
        socket_path = os.path.join(os.environ[var], path, socket_name)
        if os.path.exists(socket_path):
            return socket_path

raise Exception("Error: Could not find any keepassxc socket")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants