Last active 2 months ago

https://gist.github.com/LinuxSBC/7c39374130d2d443871ddde64cba18a3

1password-flatpak-browser-integration.sh Raw
1#!/bin/bash
2set -oue pipefail
3
4INFO='\033[0;36m' # Cyan for general information
5SUCCESS='\033[0;32m' # Green for success messages
6WARN='\033[0;33m' # Yellow for warnings
7ERROR='\033[0;31m' # Red for errors
8NC='\033[0m' # No Color
9
10echo "This script will help you set up 1Password in a Flatpak browser."
11echo -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}"
12echo
13
14PACKAGE_LIST=$(flatpak list --app --columns=application)
15
16ALLOWED_EXTENSIONS_FIREFOX='"allowed_extensions": [
17 "{0a75d802-9aed-41e7-8daa-24c067386e82}",
18 "{25fc87fa-4d31-4fee-b5c1-c32a7844c063}",
19 "{d634138d-c276-4fc8-924b-40a0ea21d284}"
20 ]'
21ALLOWED_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
29is_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
42list_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
57echo -e "${INFO}Detected Chromium-based browsers (incomplete list):${NC}"
58CHROMIUM_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")
59list_flatpak_browsers "${CHROMIUM_BROWSER_ID_LIST[@]}"
60
61echo -e "${INFO}Detected Firefox-based browsers (incomplete list):${NC}"
62FIREFOX_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")
63list_flatpak_browsers "${FIREFOX_BROWSER_ID_LIST[@]}"
64
65echo
66
67echo -n "Enter the name of your browser's Flatpak application ID (e.g. com.google.Chrome): "
68read -r FLATPAK_ID
69if ! echo "$PACKAGE_LIST" | grep -q "$FLATPAK_ID"; then
70 echo -e "${ERROR}ERROR: Could not find the specified browser${NC}"
71 exit 1
72fi
73if [[ " ${FIREFOX_BROWSER_ID_LIST[*]} " =~ [[:space:]]${FLATPAK_ID}[[:space:]] ]]; then
74 BROWSER_TYPE="firefox"
75elif [[ " ${CHROMIUM_BROWSER_ID_LIST[*]} " =~ [[:space:]]${FLATPAK_ID}[[:space:]] ]]; then
76 BROWSER_TYPE="chromium"
77else
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
81fi
82if [[ "$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
85fi
86
87echo
88
89
90# Giving the browser permission to bypass the sandbox
91echo -e "${INFO}Giving your browser permission to run programs outside the sandbox${NC}"
92flatpak override --user --talk-name=org.freedesktop.Flatpak "$FLATPAK_ID"
93
94
95# Creating a wrapper script for 1Password in the browser's directory
96echo -e "${INFO}Creating a wrapper script for 1Password${NC}"
97mkdir -p "$HOME/.var/app/$FLATPAK_ID/data/bin"
98cat <<EOF > "$HOME/.var/app/$FLATPAK_ID/data/bin/1password-wrapper.sh"
99#!/bin/bash
100if [ "\${container-}" = flatpak ]; then
101 flatpak-spawn --host /opt/1Password/1Password-BrowserSupport "\$@"
102else
103 exec /opt/1Password/1Password-BrowserSupport "\$@"
104fi
105EOF
106chmod +x "$HOME/.var/app/$FLATPAK_ID/data/bin/1password-wrapper.sh"
107
108
109# Creating a Native Messaging Hosts file
110echo -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
113if [[ "$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)
115elif [[ "$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
131fi
132
133if [[ ! -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
136fi
137
138add_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}
157EOF
158}
159
160PUT_FILES_IN_MOZILLA_DIR=false
161WRAPPER_PATH="$HOME/.var/app/$FLATPAK_ID/data/bin/1password-wrapper.sh"
162if [[ "$BROWSER_TYPE" = "chromium" ]]; then
163 add_native_messaging_host "$WRAPPER_PATH" "$ALLOWED_EXTENSIONS_CHROMIUM" "$NATIVE_MESSAGING_HOSTS_DIR"
164elif [[ "$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
190fi
191
192echo
193
194
195# Add flatpak-session-helper to custom_allowed_browsers list (needs root)
196echo -e "${INFO}Adding Flatpaks to the list of supported browsers in 1Password${NC}"
197echo "Note: This requires sudo permissions. If this doesn't work, append flatpak-session-helper to the file /etc/1password/custom_allowed_browsers"
198if [[ ! -d /etc/1password ]]; then
199 echo -e "${INFO}Creating directory /etc/1password${NC}"
200 sudo mkdir /etc/1password
201fi
202if grep -q 'flatpak-session-helper' /etc/1password/custom_allowed_browsers; then
203 echo -e "${WARN}Already added to allowed browsers${NC}"
204else
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
207fi
208
209
210# Done
211if 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."
214else
215 echo -e "${ERROR}ERROR: Could not add to allowed browsers${NC}"
216 exit 1
217fi
README.md Raw

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:

  1. Download the script (right-click on "Raw" and click "Save Link As").
  2. Open your terminal in the directory that you downloaded the script to. For example, if it's in your Downloads folder, run cd Downloads after opening your terminal.
  3. Mark the script as executable using the command chmod +x 1password-flatpak-browser-integration.sh.
  4. Run the script using the command ./1password-flatpak-browser-integration.sh.
  5. 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.
  6. 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:

  1. 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.
  2. The application places a JSON file in a specific directory of the browser (~/.config/google-chrome/NativeMessagingHosts for a native installation of Chrome and ~/.mozilla/native-messaging-hosts for 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:

  1. To allow the app to run commands, we need to grant it permission to talk on the org.freedesktop.Flatpak bus using either Flatseal or this command: flatpak override --user --talk-name=org.freedesktop.Flatpak <browser ID>
  2. Now, it can run terminal commands by prepending flatpak spawn --host to 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 "$@"
  1. 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-BrowserSupport with <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:

  1. In the same directory where browser profiles are stored, such as ~/.var/app/io.gitlab.librewolf-community/.librewolf
  2. 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)
  3. In the ~/.mozilla directory 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.