Skip to content

bitwarden-desktop: fix system authentication - #339384

Merged
Aleksanaa merged 1 commit into
NixOS:masterfrom
bvngee:bitwarden-fix-system-auth
Nov 13, 2024
Merged

Aleksanaa merged 1 commit into
NixOS:masterfrom
bvngee:bitwarden-fix-system-auth

Conversation

@bvngee

@bvngee bvngee commented Sep 4, 2024 •

Copy link
Copy Markdown
Contributor

Description of changes

This does 3 things:

  • installs the com.bitwarden.Bitwarden.policy polkit action file (required for system authentication)
  • patches bitwarden to not try to install it manually into /usr/share/polkit-1/actions/
  • patches bitwarden to return the path to our wrapper script when it looks for its executable instead of returning the path to electron (see this comment). This is required for Native Messaging to work (part of system auth)

now we can use bitwarden-desktop to authenticate with biometrics in the web extension via polkit and native messaging :)

Relevant:

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@NixOSInfra NixOSInfra added the 12.first-time contribution This PR is the author's first one; please be gentle! label Sep 4, 2024
@bvngee

bvngee commented Sep 4, 2024

Copy link
Copy Markdown
Contributor Author

Oh and I assume I should @ maintainers:

@amarshall

@ofborg
ofborg Bot requested a review from amarshall September 4, 2024 02:57
@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. labels Sep 4, 2024
@bvngee

bvngee commented Sep 6, 2024

Copy link
Copy Markdown
Contributor Author

I agree with both your suggestions regarding extracting the polkit file automatically - unfortunately I can't type very much right now due to wrist pain so I will have to get back to it soon.

@bvngee

bvngee commented Sep 11, 2024 •

Copy link
Copy Markdown
Contributor Author

@amarshall @Aleksanaa
I tried for a long while to extract the polkit action file automatically with ts-node but for the life of me I could not get it to work. On top of having to patch in export, I also had to patch package.json with {"type": "module"} to get ESM to work and even with every combination of cli flags I could think of I still had errors with resolving imports. So I gave up and commited a solution using awk which altho maybe slightly more fragile is honestly simpler anyways. If one of you can get the ts-node solution to work though I'm all for it. (I'm also not a js/ts guy so I might have missed something.)

@bvngee
bvngee force-pushed the bitwarden-fix-system-auth branch from 878c002 to 4a3920e Compare September 12, 2024 00:15
@bvngee
bvngee requested a review from amarshall September 12, 2024 00:19
@bvngee

bvngee commented Sep 18, 2024

Copy link
Copy Markdown
Contributor Author

@amarshall Sorry to bother - any chance you could review this in the near future? Let me know if you'd like any changes.

@Aleksanaa

Copy link
Copy Markdown
Member

Hi I'm sorry I can't give much time to review this at the moment. Have you verified the new command you use to extract com.bitwarden.Bitwarden.policy results in the same file as before?

@bvngee

bvngee commented Sep 18, 2024

Copy link
Copy Markdown
Contributor Author

Hi I'm sorry I can't give much time to review this at the moment.

No worries at all!

Have you verified the new command you use to extract com.bitwarden.Bitwarden.policy results in the same file as before?

haha I have now! It missed the first line before, now its identical. Thanks for making me double check. Should be ready to merge anytime

@alexyao2015

Copy link
Copy Markdown
Contributor

I wish I had seen this before! I created #344073 after several days of manually digging through each thing. Would you also be able to fix the startup app path issue as well in here?

@amarshall

Copy link
Copy Markdown
Member

@alexyao2015 Are you able to test this PR? I don’t use this functionality and don’t have biometrics hardware to test it.

@jamesbirtles

Copy link
Copy Markdown

I've been using this for a few days and it works well (after I figured out that I needed to move bitwarden from being installed as a user package via home manager to a regular system package instead).

@bvngee

bvngee commented Sep 24, 2024 •

Copy link
Copy Markdown
Contributor Author

I wish I had seen this before! I created #344073 after several days of manually digging through each thing. Would you also be able to fix the startup app path issue as well in here?

@alexyao2015 Hi! I'm not exactly sure what you mean by "the startup app path issue"; I think my fix here accounts for what you're talking about but if I missed something I will definitely add it to this PR.

Also, for anyone wanting to test this PR, note that you don't need biometrics hardware; all this feature does is integrate with polkit, which can be configured to authenticate in many different ways (by changing it's pam.d configuration to use eg. fprintd, yubikey, or just standard password prompts via polkit agents like polkit-gnome-authentication-agent-1). And yeah, as @jamesbirtles mentioned, you have to use NixOS's environment.systemPackages and not home manager's home.packages due to home manager not installing polkit files properly.

@alexyao2015

Copy link
Copy Markdown
Contributor

Hey by "startup app path issue", take a look at the linked issue under #3. The autostart entry faces a similar problem to what was fixed in the ts file.

I'm thinking instead of replacing the return statement, create a git patch to replace the two instances of app.getPath("exe") with a string that can be substituted with the correct static path instead. I think using git to construct the patch allows us to better home in on the exact location we want to patch.

Alternatively, I'm thinking, in this case, we could even do a find replace of all code in the repo for app.getPath("exe") and replace that with the static path. This would fix future uses as well.

@alexyao2015

Copy link
Copy Markdown
Contributor

you have to use NixOS's environment.systemPackages

This is probably an undesirable result, forcing people to use systemPackages now. Is there some other way to add a configuration flag in the polkit package to allow people to manually enable to action there?

@bvngee

bvngee commented Sep 24, 2024

Copy link
Copy Markdown
Contributor Author

Hey by "startup app path issue", take a look at the linked issue under #3. The autostart entry faces a similar problem to what was fixed in the ts file.

Ah, you're right! I've never used autostart before so I never noticed.

I'm thinking instead of replacing the return statement, create a git patch to replace the two instances of app.getPath("exe") with a string that can be substituted with the correct static path instead. I think using git to construct the patch allows us to better home in on the exact location we want to patch.

Alternatively, I'm thinking, in this case, we could even do a find replace of all code in the repo for app.getPath("exe") and replace that with the static path. This would fix future uses as well.

Both approaches seem good; I'll start working on whichever one seems nicer now.

you have to use NixOS's environment.systemPackages

This is probably an undesirable result, forcing people to use systemPackages now. Is there some other way to add a configuration flag in the polkit package to allow people to manually enable to action there?

I agree, but sadly not that I know of! Maybe there should be though.

@amarshall

Copy link
Copy Markdown
Member

It doesn’t force anyone to use systemPackages. That’s only necessary if one needs the polkit features. It’s nothing unique to Bitwarden, either, but applicable to any package requiring polkit or other similar privileged access.

@alexyao2015

Copy link
Copy Markdown
Contributor

I see something like this being done in the pam module. Perhaps something similar can be done in the polkit module.

@bvngee

bvngee commented Sep 24, 2024

Copy link
Copy Markdown
Contributor Author

Alright @alexyao2015 @amarshall I've changed # 3 to replace all instances of app.getPath("exe") with the wrapper script path instead of just the one instance of return this.exePath, as suggested by alexyao. Now both the native messaging manifest file and the autostart desktop entry are correct. I used a global find/replaced in src/, but I'd be happy to do a more manual approach instead; in that case I'd make a patch that replaces two current app.getPath("exe") usages with @out@/bin/bitwarden and uses a substituteAllInPlace on them. LMK if you'd prefer that instead.
@jamesbirtles if you could test this again too that would be great, though I'm (fairly) confident this is done correctly :P

@alexyao2015

Copy link
Copy Markdown
Contributor

I tested it and it seems to work well. On this particular version (2024.8.x) with a new Bitwarden extension (2024.9.x), I noticed that the system unlock didn't function correctly. Updating the package to the latest version (2024.9.0) fixed the issue and I was able to unlock the extension once again. You might want to rebase your change on master again to pull in the change that updates Bitwarden to 2024.9.0.

I created a separate branch with a few cherry picked commits to test this here.

@bvngee
bvngee force-pushed the bitwarden-fix-system-auth branch from a873810 to cdb96a2 Compare September 25, 2024 06:18
@bvngee

bvngee commented Sep 25, 2024

Copy link
Copy Markdown
Contributor Author

You might want to rebase your change on master again to pull in the change that updates Bitwarden to 2024.9.0.

Done. (Sorry about the force push; I'm still kinda unsure about how to handle this situation properly in git)

@jamesbirtles

Copy link
Copy Markdown

Just updated to the latest commit, still working well, and the startup checkbox is now working properly too 👍

@amarshall amarshall Sep 25, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This feels brittle.

I wonder, if you apply the below patch, is this still needed? Reason being that I think this makes the --inherit-argv0 actually have an effect, and I suspect may mean getPath('exe') will be the wrapper instead of the electron.

diff --git a/pkgs/by-name/bi/bitwarden-desktop/package.nix b/pkgs/by-name/bi/bitwarden-desktop/package.nix
index 104abfb85505..11373f398978 100644
--- a/pkgs/by-name/bi/bitwarden-desktop/package.nix
+++ b/pkgs/by-name/bi/bitwarden-desktop/package.nix
@@ -10,8 +10,8 @@
 , gtk3
 , jq
 , libsecret
+, makeBinaryWrapper
 , makeDesktopItem
-, makeWrapper
 , napi-rs-cli
 , nix-update-script
 , nodejs_20
@@ -78,7 +78,7 @@ in buildNpmPackage rec {
     cargo
     copyDesktopItems
     jq
-    makeWrapper
+    makeBinaryWrapper
     napi-rs-cli
     pkg-config
     (python3.withPackages (ps: with ps; [ setuptools ]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, I think the docs are outdated and the shell makeWrapper does support it (though maybe there’s still some difference). Question is then: how is Electron getting the wrong exe path?

@amarshall amarshall Sep 25, 2024 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking into the source, Electron defers to Chromium which, on Linux, gets the path by resolving /proc/self/exe, so argv0 I guess won’t have an effect on it.

I may try to hack on something a bit later or tomorrow…

@bvngee bvngee Sep 26, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This feels brittle.

Understood, that's fair - would you be OK with the manual git patch and substituteAllInPlace approach (as previously described) instead of the current global find/replace?

I wonder, if you apply the below patch, is this still needed? Reason being that I think this makes the --inherit-argv0 actually have an effect, and I suspect may mean getPath('exe') will be the wrapper instead of the electron.
Question is then: how is Electron getting the wrong exe path?
Looking into the source, Electron defers to Chromium which, on Linux, gets the path by resolving /proc/self/exe, so argv0 I guess won’t have an effect on it.

I had a similar thought initially (aside from the /proc/self/exe part which I never discovered) and I believe, even if electron did read from argv0, this solution would still also require a change to electron packaging in nixpkgs, because while electron apps' wrapper scripts do set the electron package's argv0 to their wrapper's names, that electron package itself is a wrapper which does not set the unwrapped electron binary's argv0 to the one that script inherited (see cat $(nix eval --raw nixpkgs#electron.outPath)/bin/electron, no exec -a). That's what I initially noticed and why I went for patching bitwarden instead, as any change that would make it work would be far out of the scope of this PR. But if I'm reading the scripts wrong or you come up with something, let us know!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I came up with a simpler alternative to this—check out 597b42f#diff-abc99a217fe3c23c6ad190d6c205e836b702d4f921e90bacf7f9a4bfdffdb893

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

awesome, that's super clean. I'll add this in rn

@huantianad

huantianad commented Oct 2, 2024 •

Copy link
Copy Markdown
Contributor

Instead of fixing bitwarden's native messaging host installer, maybe we should use nix's firefox packages' nativeMessagingHosts argument?

I hate to keep bikeshedding this, but this seems like the method that other packages, like plasma-browser-integration and other password managers use for this, and could simplify the discussion above.

@bvngee

bvngee commented Oct 5, 2024 •

Copy link
Copy Markdown
Contributor Author

Instead of fixing bitwarden's native messaging host installer, maybe we should use nix's firefox packages' nativeMessagingHosts argument?
this seems like the method that other packages, like plasma-browser-integration and other password managers use for this, and could simplify the discussion above.

Thanks, I was not aware of this, however I still don't feel that that's a good enough solution because:

  • It would not work for those who don't use / don't want to use the firefox nixos module
  • It would not work for other supported browsers (like edge), some of which don't have nixos modules
  • The autostart setting within bitwarden-desktop would still be broken (the fix applies to both).

I'm still willing to be convinced but I'd argue that a solution like the current one is still optimal.

(we could consider also copying the native messaging file to $out/lib/mozilla/native-messaging-hosts for the firefox module to work as well; this would likely just require 1 more awk command)

@amarshall I'd like to get this merged when possible, what are your thoughts? Are you ok enough with the current find/replace or rather switch to a git patch and substituteAllInPlace, or @huantianad's idea, or something else completely?

@bvngee

bvngee commented Oct 6, 2024

Copy link
Copy Markdown
Contributor Author

Thanks @amarshall, I switched to what you came up with.

@amarshall

Copy link
Copy Markdown
Member

I believe all the subsequent commits are just updates to the first one, and are semantically one commit, as such, please squash them and then I think this is good-to-go.

@ofborg
ofborg Bot requested a review from amarshall October 6, 2024 05:03
@bvngee
bvngee force-pushed the bitwarden-fix-system-auth branch from 1f20405 to 4cc4fd6 Compare October 6, 2024 05:07
@bvngee

bvngee commented Oct 6, 2024

Copy link
Copy Markdown
Contributor Author

I believe all the subsequent commits are just updates to the first one, and are semantically one commit, as such, please squash them and then I think this is good-to-go.

done!

@huantianad

huantianad commented Oct 6, 2024 •

Copy link
Copy Markdown
Contributor

(we could consider also copying the native messaging file to $out/lib/mozilla/native-messaging-hosts for the firefox module to work as well; this would likely just require 1 more awk command)

Maybe we could do this so this derivation is consistent with the others?

Also this native messaging hosts is done on the derivation level and not on the module level.

I'm also not sure if there is something that the Firefox maintainers would prefer, not sure if it's worth asking tho.

I think we can discuss this further in a separate issue/PR if more convenient and get this merged first.

@bvngee

bvngee commented Oct 6, 2024 •

Copy link
Copy Markdown
Contributor Author

Maybe we could do this so this derivation is consistent with the others?

I'm happy to do that in a separate issue/PR (whatever works) where you can ping relevant FF maintainers if need be

@bvngee

bvngee commented Oct 8, 2024 •

Copy link
Copy Markdown
Contributor Author

@amarshall this should be ready to merge anytime (correct me if not)

@bvngee

bvngee commented Oct 22, 2024

Copy link
Copy Markdown
Contributor Author

@amarshall Sorry to be annoying about this - any chance this could be merged soon? Thanks.

@Aleksanaa
Aleksanaa self-requested a review October 27, 2024 04:00

@Aleksanaa Aleksanaa left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Merging as is.

@Aleksanaa
Aleksanaa merged commit 6b1d3ea into NixOS:master Nov 13, 2024
@gepbird gepbird mentioned this pull request Nov 13, 2024
3 of 13 tasks
@bvngee
bvngee deleted the bitwarden-fix-system-auth branch July 12, 2025 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 10.rebuild-linux: 1 This PR causes 1 package to rebuild on Linux. 12.first-time contribution This PR is the author's first one; please be gentle!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants