| 1 | #!/bin/bash |
| 2 | set -oue pipefail |
| 3 | |
| 4 | INFO='\033[0;36m' # Cyan for general information |
| 5 | SUCCESS='\033[0;32m' # Green for success messages |
| 6 | WARN='\033[0;33m' # Yellow for warnings |
| 7 | ERROR='\033[0;31m' # Red for errors |
| 8 | NC='\033[0m' # No Color |
| 9 | |
| 10 | echo "This script will help you set up 1Password in a Flatpak browser." |
| 11 | echo -e "${WARN}Note: It will make it possible for any Flatpak application to integrate, not just some. Consider if you find this worth the risk.${NC}" |
| 12 | echo |
| 13 | |
| 14 | PACKAGE_LIST=$(flatpak list --app --columns=application) |
| 15 | |
| 16 | ALLOWED_EXTENSIONS_FIREFOX='"allowed_extensions": [ |
| 17 | "{0a75d802-9aed-41e7-8daa-24c067386e82}", |
| 18 | "{25fc87fa-4d31-4fee-b5c1-c32a7844c063}", |
| 19 | "{d634138d-c276-4fc8-924b-40a0ea21d284}" |
| 20 | ]' |
| 21 | ALLOWED_EXTENSIONS_CHROMIUM='"allowed_origins": [ |
| 22 | "chrome-extension://hjlinigoblmkhjejkmbegnoaljkphmgo/", |
| 23 | "chrome-extension://gejiddohjgogedgjnonbofjigllpkmbf/", |
| 24 | "chrome-extension://khgocmkkpikpnmmkgmdnfckapcdkgfaf/", |
| 25 | "chrome-extension://aeblfdkhhhdcdjpifhhbdiojplfjncoa/", |
| 26 | "chrome-extension://dppgmdbiimibapkepcbdbmkaabgiofem/" |
| 27 | ]' |
| 28 | |
| 29 | is_firefox_dir() { |
| 30 | local dir="$1" |
| 31 | |
| 32 | # Skip if not a directory or if it's . or .. or .cache |
| 33 | if [[ ! -d "$dir" ]] || [[ "$(basename "$dir")" = "." ]] || [[ "$(basename "$dir")" = ".." ]] || [[ "$(basename "$dir")" = "cache" ]] || [[ "$(basename "$dir")" = ".cache" ]]; then |
| 34 | return 1 |
| 35 | elif [[ -f "$dir/profiles.ini" ]]; then |
| 36 | return 0 |
| 37 | else |
| 38 | return 1 |
| 39 | fi |
| 40 | } |
| 41 | |
| 42 | list_flatpak_browsers() { |
| 43 | BROWSER_ID_LIST=("$@") |
| 44 | |
| 45 | for BROWSER_ID in "${BROWSER_ID_LIST[@]}"; do |
| 46 | if echo "$PACKAGE_LIST" | grep -q "$BROWSER_ID"; then |
| 47 | MATCHING_BROWSERS=$(echo "$PACKAGE_LIST" | grep "$BROWSER_ID") |
| 48 | while IFS= read -r BROWSER || [[ -n $BROWSER ]]; do |
| 49 | echo -e "${INFO} - $BROWSER${NC}" |
| 50 | done < <(printf '%s' "$MATCHING_BROWSERS") # https://superuser.com/questions/284187/how-to-iterate-over-lines-in-a-variable-in-bash |
| 51 | fi |
| 52 | done |
| 53 | } |
| 54 | |
| 55 | |
| 56 | # Getting what browser to install for |
| 57 | echo -e "${INFO}Detected Chromium-based browsers (incomplete list):${NC}" |
| 58 | CHROMIUM_BROWSER_ID_LIST=("com.google.Chrome" "com.brave.Browser" "com.vivaldi.Vivaldi" "com.opera.Opera" "com.microsoft.Edge" "ru.yandex.Browser" "org.chromium.Chromium" "io.github.ungoogled_software.ungoogled_chromium") |
| 59 | list_flatpak_browsers "${CHROMIUM_BROWSER_ID_LIST[@]}" |
| 60 | |
| 61 | echo -e "${INFO}Detected Firefox-based browsers (incomplete list):${NC}" |
| 62 | FIREFOX_BROWSER_ID_LIST=("org.mozilla.firefox" "one.ablaze.floorp" "io.gitlab.librewolf-community" "org.torproject.torbrowser-launcher" "app.zen_browser.zen" "org.garudalinux.firedragon" "net.mullvad.MullvadBrowser" "net.waterfox.waterfox") |
| 63 | list_flatpak_browsers "${FIREFOX_BROWSER_ID_LIST[@]}" |
| 64 | |
| 65 | echo |
| 66 | |
| 67 | echo -n "Enter the name of your browser's Flatpak application ID (e.g. com.google.Chrome): " |
| 68 | read -r FLATPAK_ID |
| 69 | if ! echo "$PACKAGE_LIST" | grep -q "$FLATPAK_ID"; then |
| 70 | echo -e "${ERROR}ERROR: Could not find the specified browser${NC}" |
| 71 | exit 1 |
| 72 | fi |
| 73 | if [[ " ${FIREFOX_BROWSER_ID_LIST[*]} " =~ [[:space:]]${FLATPAK_ID}[[:space:]] ]]; then |
| 74 | BROWSER_TYPE="firefox" |
| 75 | elif [[ " ${CHROMIUM_BROWSER_ID_LIST[*]} " =~ [[:space:]]${FLATPAK_ID}[[:space:]] ]]; then |
| 76 | BROWSER_TYPE="chromium" |
| 77 | else |
| 78 | echo "Could not determine browser type. Is your browser based on Chromium or Firefox?" |
| 79 | echo -n "Enter 'chromium' or 'firefox': " |
| 80 | read -r BROWSER_TYPE |
| 81 | fi |
| 82 | if [[ "$BROWSER_TYPE" != "chromium" ]] && [[ "$BROWSER_TYPE" != "firefox" ]]; then |
| 83 | echo -e "${ERROR}ERROR: Invalid browser type \"$BROWSER_TYPE\"; expected either chromium or firefox${NC}" |
| 84 | exit 1 |
| 85 | fi |
| 86 | |
| 87 | echo |
| 88 | |
| 89 | |
| 90 | # Giving the browser permission to bypass the sandbox |
| 91 | echo -e "${INFO}Giving your browser permission to run programs outside the sandbox${NC}" |
| 92 | flatpak override --user --talk-name=org.freedesktop.Flatpak "$FLATPAK_ID" |
| 93 | |
| 94 | |
| 95 | # Creating a wrapper script for 1Password in the browser's directory |
| 96 | echo -e "${INFO}Creating a wrapper script for 1Password${NC}" |
| 97 | mkdir -p "$HOME/.var/app/$FLATPAK_ID/data/bin" |
| 98 | cat <<EOF > "$HOME/.var/app/$FLATPAK_ID/data/bin/1password-wrapper.sh" |
| 99 | #!/bin/bash |
| 100 | if [ "\${container-}" = flatpak ]; then |
| 101 | flatpak-spawn --host /opt/1Password/1Password-BrowserSupport "\$@" |
| 102 | else |
| 103 | exec /opt/1Password/1Password-BrowserSupport "\$@" |
| 104 | fi |
| 105 | EOF |
| 106 | chmod +x "$HOME/.var/app/$FLATPAK_ID/data/bin/1password-wrapper.sh" |
| 107 | |
| 108 | |
| 109 | # Creating a Native Messaging Hosts file |
| 110 | echo -e "${INFO}Creating a Native Messaging Hosts file for the 1Password extension to tell the browser to use the wrapper script${NC}" |
| 111 | |
| 112 | # Find the Native Messaging Hosts directory |
| 113 | if [[ "$BROWSER_TYPE" = "chromium" ]]; then |
| 114 | NATIVE_MESSAGING_HOSTS_DIR=$(find "$HOME/.var/app/$FLATPAK_ID/config" -maxdepth 3 -type d -name "NativeMessagingHosts" 2>/dev/null) |
| 115 | elif [[ "$BROWSER_TYPE" = "firefox" ]]; then |
| 116 | shopt -s dotglob |
| 117 | for dir in "$HOME/.var/app/$FLATPAK_ID"/*; do |
| 118 | if is_firefox_dir "$dir"; then |
| 119 | NATIVE_MESSAGING_HOSTS_DIR="$dir"/native-messaging-hosts |
| 120 | break |
| 121 | fi |
| 122 | |
| 123 | for subdir in "$dir"/*; do |
| 124 | if is_firefox_dir "$subdir"; then |
| 125 | # Firefox, for example, puts profiles in .mozilla/firefox, but it puts native-messaging-hosts in .mozilla. |
| 126 | NATIVE_MESSAGING_HOSTS_DIR="$dir"/native-messaging-hosts |
| 127 | break |
| 128 | fi |
| 129 | done |
| 130 | done |
| 131 | fi |
| 132 | |
| 133 | if [[ ! -v NATIVE_MESSAGING_HOSTS_DIR ]] || [[ -z "$NATIVE_MESSAGING_HOSTS_DIR" ]]; then |
| 134 | echo -e "${ERROR}ERROR: Could not find Native Messaging Hosts directory${NC}" |
| 135 | exit 1 |
| 136 | fi |
| 137 | |
| 138 | add_native_messaging_host() { |
| 139 | local WRAPPER_PATH="$1" |
| 140 | local ALLOWED_EXTENSIONS="$2" |
| 141 | local NATIVE_MESSAGING_HOSTS_DIR="$3" |
| 142 | |
| 143 | # Create the file |
| 144 | if [[ ! -d $NATIVE_MESSAGING_HOSTS_DIR ]]; then |
| 145 | echo -e "${INFO}Creating Native Messaging Hosts directory at $NATIVE_MESSAGING_HOSTS_DIR${NC}" |
| 146 | mkdir -p "$NATIVE_MESSAGING_HOSTS_DIR" |
| 147 | fi |
| 148 | |
| 149 | cat <<EOF > "$NATIVE_MESSAGING_HOSTS_DIR/com.1password.1password.json" |
| 150 | { |
| 151 | "name": "com.1password.1password", |
| 152 | "description": "1Password BrowserSupport", |
| 153 | "path": "$WRAPPER_PATH", |
| 154 | "type": "stdio", |
| 155 | $ALLOWED_EXTENSIONS |
| 156 | } |
| 157 | EOF |
| 158 | } |
| 159 | |
| 160 | PUT_FILES_IN_MOZILLA_DIR=false |
| 161 | WRAPPER_PATH="$HOME/.var/app/$FLATPAK_ID/data/bin/1password-wrapper.sh" |
| 162 | if [[ "$BROWSER_TYPE" = "chromium" ]]; then |
| 163 | add_native_messaging_host "$WRAPPER_PATH" "$ALLOWED_EXTENSIONS_CHROMIUM" "$NATIVE_MESSAGING_HOSTS_DIR" |
| 164 | elif [[ "$BROWSER_TYPE" = "firefox" ]]; then |
| 165 | add_native_messaging_host "$WRAPPER_PATH" "$ALLOWED_EXTENSIONS_FIREFOX" "$NATIVE_MESSAGING_HOSTS_DIR" |
| 166 | BROWSERS_NOT_USING_MOZILLA=("org.mozilla.firefox" "io.gitlab.librewolf-community" "net.waterfox.waterfox") |
| 167 | if [[ " ${BROWSERS_NOT_USING_MOZILLA[*]} " =~ [[:space:]]${FLATPAK_ID}[[:space:]] ]]; then |
| 168 | echo -e "${INFO}Skipping adding to $HOME/.mozilla/native-messaging-hosts/com.1password.1password.json${NC}" |
| 169 | else |
| 170 | echo "Some browsers, like Floorp and Zen, need the file in ~/.mozilla instead of in their own sandbox. This requires replacing the existing file $HOME/.mozilla/native-messaging-hosts/com.1password.1password.json with a custom one. Then, to prevent 1Password overwriting it, the file needs to be marked as read-only using chattr +i on it." |
| 171 | echo -n "Do you want to continue? This will require sudo privileges. (Y/n) " |
| 172 | read -r CONTINUE |
| 173 | if [[ "$CONTINUE" = "N" ]] || [[ "$CONTINUE" = "n" ]]; then |
| 174 | echo -e "${INFO}Skipping${NC}" |
| 175 | else |
| 176 | PUT_FILES_IN_MOZILLA_DIR=true |
| 177 | cp "$HOME/.var/app/$FLATPAK_ID/data/bin/1password-wrapper.sh" "$HOME/.mozilla/native-messaging-hosts/1password-wrapper.sh" |
| 178 | flatpak override --user --filesystem="$HOME/.mozilla/native-messaging-hosts" "$FLATPAK_ID" |
| 179 | |
| 180 | WRAPPER_PATH="$HOME/.mozilla/native-messaging-hosts/1password-wrapper.sh" # For browsers that need the file in ~/.mozilla, like Zen and Floorp |
| 181 | NATIVE_MESSAGING_HOSTS_DIR="$HOME/.mozilla/native-messaging-hosts" |
| 182 | sudo chattr -i "$NATIVE_MESSAGING_HOSTS_DIR/com.1password.1password.json" # Remove read-only flag if it exists |
| 183 | add_native_messaging_host "$WRAPPER_PATH" "$ALLOWED_EXTENSIONS_FIREFOX" "$NATIVE_MESSAGING_HOSTS_DIR" |
| 184 | |
| 185 | echo -e "${INFO}Marking $HOME/.mozilla/native-messaging-hosts/com.1password.1password.json as read-only using chattr +i. To undo, run this command:${NC}" |
| 186 | echo -e "${INFO}sudo chattr -i $HOME/.mozilla/native-messaging-hosts/com.1password.1password.json${NC}" |
| 187 | sudo chattr +i "$HOME/.mozilla/native-messaging-hosts/com.1password.1password.json" # Prevent 1Password from overwriting the file |
| 188 | fi |
| 189 | fi |
| 190 | fi |
| 191 | |
| 192 | echo |
| 193 | |
| 194 | |
| 195 | # Add flatpak-session-helper to custom_allowed_browsers list (needs root) |
| 196 | echo -e "${INFO}Adding Flatpaks to the list of supported browsers in 1Password${NC}" |
| 197 | echo "Note: This requires sudo permissions. If this doesn't work, append flatpak-session-helper to the file /etc/1password/custom_allowed_browsers" |
| 198 | if [[ ! -d /etc/1password ]]; then |
| 199 | echo -e "${INFO}Creating directory /etc/1password${NC}" |
| 200 | sudo mkdir /etc/1password |
| 201 | fi |
| 202 | if grep -q 'flatpak-session-helper' /etc/1password/custom_allowed_browsers; then |
| 203 | echo -e "${WARN}Already added to allowed browsers${NC}" |
| 204 | else |
| 205 | echo -e "${INFO}Adding to allowed browsers${NC}" |
| 206 | echo -e 'flatpak-session-helper' | sudo tee -a /etc/1password/custom_allowed_browsers >/dev/null |
| 207 | fi |
| 208 | |
| 209 | |
| 210 | # Done |
| 211 | if grep -q 'flatpak-session-helper' /etc/1password/custom_allowed_browsers; then |
| 212 | echo -e "${SUCCESS}Success! 1Password should now work in your Flatpak browser.${NC}" |
| 213 | echo "Now, restart both your browser and 1Password." |
| 214 | else |
| 215 | echo -e "${ERROR}ERROR: Could not add to allowed browsers${NC}" |
| 216 | exit 1 |
| 217 | fi |
This script will automatically add support for 1Password to integrate with Flatpak web browsers on Linux. I haven't tested every browser, so add a comment if it doesn't work for your browser.
Note: The 1Password app itself needs to be installed as a native package.
To use this script, follow these steps:
- Download the script (right-click on "Raw" and click "Save Link As").
- Open your terminal in the directory that you downloaded the script to. For example, if it's in your Downloads folder, run
cd Downloadsafter opening your terminal. - Mark the script as executable using the command
chmod +x 1password-flatpak-browser-integration.sh. - Run the script using the command
./1password-flatpak-browser-integration.sh. - When it asks, enter the Flatpak application ID of your browser, then press Enter. If it's not listed, the easiest way to find it is to run
flatpak list --app --columns=application | grep -i <browser name>, replacing<browser name>with the name of your browser. - Restart both 1Password and your browser.
This is generally made following this guide, but I'll add some further explanation as to how it works here.
Native Messaging Hosts
Web browsers communicate with native applications using something called "Native Messaging" (Firefox, Chrome). This has three components:
- The extension specifies what native application it wants to communicate with. For example, the 1Password extension says that it supports the com.1password.1password native host.
- The application places a JSON file in a specific directory of the browser (
~/.config/google-chrome/NativeMessagingHostsfor a native installation of Chrome and~/.mozilla/native-messaging-hostsfor native Firefox). This JSON file tells the browser what to do when the extension calls that native host, and it specifies a list of extension IDs so that only certain extensions can use it. This is what it looks like for 1Password:
Chrome
{
"name": "com.1password.1password",
"description": "1Password BrowserSupport",
"path": "/usr/lib/opt/1Password/1Password-BrowserSupport",
"type": "stdio",
"allowed_origins": [
"chrome-extension://hjlinigoblmkhjejkmbegnoaljkphmgo/",
"chrome-extension://gejiddohjgogedgjnonbofjigllpkmbf/",
"chrome-extension://khgocmkkpikpnmmkgmdnfckapcdkgfaf/",
"chrome-extension://aeblfdkhhhdcdjpifhhbdiojplfjncoa/",
"chrome-extension://dppgmdbiimibapkepcbdbmkaabgiofem/"
]
}
Firefox
{
"name": "com.1password.1password",
"description": "1Password BrowserSupport",
"path": "/usr/lib/opt/1Password/1Password-BrowserSupport",
"type": "stdio",
"allowed_extensions": [
"{0a75d802-9aed-41e7-8daa-24c067386e82}",
"{25fc87fa-4d31-4fee-b5c1-c32a7844c063}",
"{d634138d-c276-4fc8-924b-40a0ea21d284}"
]
}
This normally works well. The problem is that, since Flatpaks are sandboxed, they can't access either the JSON file or the executable on the host. To resolve that, we need to bypass the sandbox.
Bypassing the Flatpak Sandbox
First, we need to allow the browser to access the JSON file. The easiest way is to just put the file inside the sandbox rather than allowing the browsers to bypass the sandbox, so that's what this script does in most cases (see Complications for when we don't). This just involves putting a JSON file in ~/.var/app/<browser ID>/config/<browser name>/NativeMessagingHosts on Chromium and ~/.var/app/<browser ID>/.<browser name>/native-messaging-hosts on Firefox (though every fork does things differently; see Complications).
Next, we need to allow the browser to run the host command. This has three parts:
- To allow the app to run commands, we need to grant it permission to talk on the
org.freedesktop.Flatpakbus using either Flatseal or this command:flatpak override --user --talk-name=org.freedesktop.Flatpak <browser ID> - Now, it can run terminal commands by prepending
flatpak spawn --hostto them. However, the JSON file that 1Password creates doesn't have that; it just runs/opt/1Password/1Password-BrowserSupport. To fix this, we need to create a custom shell script inside the Flatpak sandbox with these contents:
#!/bin/bash
flatpak-spawn --host /opt/1Password/1Password-BrowserSupport "$@"
- We then need to tell the JSON file to run this script instead of the default 1Password-BrowserSupport binary. To do this, we just replace
/usr/lib/opt/1Password/1Password-BrowserSupportwith<home directory>/.var/app/<browser ID>/data/bin/1password-wrapper.sh(or wherever you put the script) in the JSON file.
Complications
Firefox forks have several different places where the native-messaging-hosts directory needs to be located:
- In the same directory where browser profiles are stored, such as
~/.var/app/io.gitlab.librewolf-community/.librewolf - In the parent directory to the directory where profiles are stored, such as
~/.var/app/org.mozilla.firefox/.mozilla(and profiles are stored in~/.var/app/org.mozilla.firefox/.mozilla/firefox) - In the
~/.mozilladirectory of the host
The first two places are somewhat annoying to find but not too difficult, but the third causes problems. Since it's on the host, the Flatpak doesn't naturally have access to it, so we need to give it permission to access the ~/.mozilla/native-messaging-hosts directory. Then, since this would also be used by native installations of browsers like Firefox, the script needs to be reconfigured to detect if it's being run from a Flatpak container and choose whether or not to run flatpak-spawn --host accordingly. Finally, the JSON file would usually be overwritten by 1Password every time it starts, so we need to mark it as read-only using chattr +i.
Another difficulty is finding the directory where the native messaging host JSON should go. Chromium-based browsers put it in ~/.var/app/<browser ID>/config/<browser name>/NativeMessagingHosts or ~/.var/app/<browser ID>/config/<company name>/<browser name>/NativeMessagingHosts. Luckily, the NativeMessagingHosts directory is already created, so we just have to run a find command to locate it. Firefox-based browsers don't create their native-messaging-hosts folder automatically, so we need to locate the profiles.ini file and derive the location from there.
Then, on browsers like Floorp and Zen, which require the files be put in ~/.mozilla, this custom JSON file would interere with the native version of Firefox (or Floorp or Zen) because the browser would be attempting to run flatpak-spawn --host without being in a Flatpak, which fails. To get around this, we need a slightly more complex script that runs flatpak-spawn --host if it's being run from a Flatpak and just runs the command normally otherwise. However, that normal command is being run by bash, not by the browser, which is a security risk, so I'm running it through exec instead.
1Password only lets certain apps integrate with it, as described below. If this were to run directly through Bash, that would mean that we'd need to allow 1Password to integrate with everything run by Bash, which is a lot. Basically everything running on your system would then have access to your 1Password vault. To get around this, we run exec /opt/1Password/1Password-BrowserSupport "$@" instead of just /opt/1Password/1Password-BrowserSupport "$@". The exec command basically replaces the Bash shell with whatever command you ran, which means that 1Password-BrowserSupport's parent process is now your browser, not Bash.
Telling 1Password that it can connect
On Linux, 1Password will only accept integration requests from processes in its internal list or in /etc/1password/custom_allowed_browsers. Flatpak browsers are not in the internal list, so we need to manually add them to custom_allowed_browsers. Luckily, every command run from a Flatpak is actually run by flatpak-session-helper, so just adding that to the list allows every Flatpak to integrate with 1Password. This does make it easier for a malicious Flatpak to integrate with it, however, so be mindful of that.