Skip to content

native messaging example: port disconnects without an error #560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
epragalakis opened this issue May 4, 2024 · 18 comments · Fixed by #564
Closed

native messaging example: port disconnects without an error #560

epragalakis opened this issue May 4, 2024 · 18 comments · Fixed by #564
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior. effort: small This task is a small effort. example New or updated example p2 We want to address this but may have other higher priority items.

Comments

@epragalakis
Copy link

What information was incorrect, unhelpful, or incomplete?

the code example does not run on two different OS' and the documentation does not explain why.

What did you expect to see?

more complete and descriptive errors

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

I'm trying to run this web-extension example: https://github.com/mdn/webextensions-examples/tree/main/native-messaging on Linux (latest firefox) and macOs (latest firefox dev) and on calling browser.runtime.connectNative('ping_pong'); I get a disconnect event with a null error object.

@epragalakis epragalakis added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label May 4, 2024
Copy link

github-actions bot commented May 4, 2024

It looks like this is your first issue. Welcome! 👋 One of the project maintainers will be with you as soon as possible. We appreciate your patience. To safeguard the health of the project, please take a moment to read our code of conduct.

@github-actions github-actions bot added the idle Issues and pull requests with no activity for three months. label Jun 3, 2024
@lmmx
Copy link

lmmx commented Jul 10, 2024

Came here to raise the same issue, this native messaging ping pong hello world demo is broken on Linux for me 👎

@github-actions github-actions bot removed the idle Issues and pull requests with no activity for three months. label Jul 11, 2024
@sirthias
Copy link

Broken on macOS Sonoma 14.5 and Firefox 128.0 as well.
After the browser.runtime.connectNative(...) call the port is immediately disconnected without an error.
The app script is not even started once.

@dotproto dotproto self-assigned this Jul 13, 2024
@dotproto dotproto added bug Indicates an unexpected problem or unintended behavior. effort: small This task is a small effort. p2 We want to address this but may have other higher priority items. example New or updated example and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Jul 13, 2024
dotproto added a commit to dotproto/webextensions-examples that referenced this issue Jul 13, 2024
dotproto added a commit to dotproto/webextensions-examples that referenced this issue Jul 13, 2024
dotproto added a commit to dotproto/webextensions-examples that referenced this issue Jul 13, 2024
@willdurand
Copy link
Member

Is this broken as in "first time I tried and it didn't work" or "was working before and now it doesn't work anymore"?

@sirthias
Copy link

I reactivated an internal dev-plugin that I used about two years ago (https://github.com/sirthias/triggered-reload), which was working fine back then but doesn't work anymore with the latest FF.
I assumed some API changed and re-tried the native messaging example, which was my initial starting point at the time.
It was definitely working correctly two years ago.

So the problem does look like a regression to me...
Unfortunately I don't have any more info on which FF version exactly was the one that introduced the breaking change.

@sirthias
Copy link

Ah, one more detail:
I do have the KeypassXC extension running as well, which also relies on stdio native messaging, and that one is working fine, so it's not that native messaging is broken per se.
I tried drilling into the issue to figure out what sets that addon apart from the example but had to stop due to time.

The only hunch that I currently have:
The KeypassXC extension runs an actual native binary as the target app whereas the example (and my own plugin) run (executable) scripts. Maybe sth changed in the call of the external app that now prevents scripts from running but still allows native binaries?

@Rob--W
Copy link
Member

Rob--W commented Jul 15, 2024

@sirthias
Questions:

@sirthias
Copy link

What Apple device are you using? (Intel or Apple sillicon)

Apple Silicon

OS version? (macOS Sanoma 14.5 according to https://github.com/sirthias)

Yes, the latest Sonoma 14.5

Do you see any error messages in the Browser Console? https://firefox-source-docs.mozilla.org/devtools-user/browser_console/index.html

Nope, no errors or other clues.
As @epragalakis wrote above, the browser.runtime.connectNative(...) goes through normally but the onDisconnect event handler get's called pretty much immediately after registration, with a null error.

If you launch Firefox with a new profile, do you see the same issue? For example web-ext run -v (--verbose forwards stderr from the native messaging binary to stderr of web-ext).

Didn't know about web-ext yet, thanks for the pointer!
-v shows the verbose log output but nothing from the external script.
I don't think the external script isn't even started at all. So there is no stdout to be seen.

@Rob--W
Copy link
Member

Rob--W commented Jul 16, 2024

Do you see anything interesting when you step through the debugger while debugging Firefox yourself?

To do so:

  1. Open the Browser Toolbox: https://firefox-source-docs.mozilla.org/devtools-user/browser_toolbox/index.html
  2. Search for file NativeMessaging.sys.mjs
  3. Put a breakpoint at the NativeManifests.lookupManifest call at https://searchfox.org/mozilla-central/rev/f3e4b33a6122ce63bf81ae8c30cc5ac37458864b/toolkit/components/extensions/NativeMessaging.sys.mjs#71
  4. Activate the part of the extension that calls connectNative
  5. Switch back to the Browser Toolbox and step through.

If needed I am willing to have a Zoom meeting where you share your screen share to debug this issue together with you.

@sirthias
Copy link

Thank you, @Rob--W!
With your help and a bit of drilling deeper I was able to locate the problem now.
The issue is a permissions problem.
When FF starts the external script macOS steps in to check whether the interpreter actually has the rights to access the script file, which it didn't in my case.

When I give /bin/bash Full-Disk-Access my addon starts working as expected.

Since the native messaging example runs a python script the python binary needs Full-Disk-Access as well in order to work. It's a bummer that there is no more fine-grained way to allow access only to the part of the file-system, that is actually required. At least AFAICS.

Note: Giving FF itself Full-Disk-Access is not sufficient. I guess FF starts the external binary detached from its own process tree and the external binary thus doesn't inherit FF's permissions.

@dotproto
Copy link
Contributor

dotproto commented Jul 17, 2024

In my testing I was able to get the revised native messaging demo to work as expected as long as the Python script was not located in ~/Desktop, ~/Documents, or ~/Downloads. I have not granted any applications full disk access, but Terminal does have Documents Folder and Downloads Folder access in System Settings > Privacy & Security > Files and Folders.

@sirthias, where was your native messaging host located?

@sirthias
Copy link

My external script lives somewhere deep below ~/Documents and probably it'd suffice to simply give /bin/bash the right to access to ~/Documents, good point!
macOS Sonoma doesn't appear to offer a GUI option to manually give /bin/bash "Files and Folders" permissions, but I'm sure there is way to get this set up via the CLI. I'll look into it.

@epragalakis
Copy link
Author

epragalakis commented Jul 18, 2024

It does not work for me if I run the script from Downloads (where my terminal has access)
and neither from the home directory, which is not Desktop, Documents or Downloads as @dotproto mentioned above (my terminal does not have elevated access to this dir).

(this is on macOS sonoma 14.5 and firefox dev 129.0b2 - I havent tried it on linux)

Edit: Ill give it a go later to see if Im getting the same error and also test it on regular firefox

@sirthias
Copy link

@epragalakis Have tried giving the python interpreter binary Full-Disk-Access, at least temporarily?

@github-actions github-actions bot added the idle Issues and pull requests with no activity for three months. label Aug 18, 2024
@lmmx
Copy link

lmmx commented Sep 9, 2024

I was reporting the bug from Linux for the record

@github-actions github-actions bot removed the idle Issues and pull requests with no activity for three months. label Sep 10, 2024
@github-actions github-actions bot added the idle Issues and pull requests with no activity for three months. label Oct 10, 2024
@willdurand
Copy link
Member

@epragalakis I just ran into this on macOS and personally that's because the ping_pong.py python script was using an unknown interpreter for my system... it wants python, which is not defined because I use python3:

diff --git a/native-messaging/app/ping_pong.py b/native-messaging/app/ping_pong.py
index e934380..2cf8779 100755
--- a/native-messaging/app/ping_pong.py
+++ b/native-messaging/app/ping_pong.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3

@github-actions github-actions bot removed the idle Issues and pull requests with no activity for three months. label Oct 18, 2024
@epragalakis
Copy link
Author

epragalakis commented Oct 19, 2024

I havent tried this since I reported it but from what I remember I also attempted to run the test with a javascript file.

Since I have a new setup, I will need to give this another go.

edit: same error on linux. @willdurand both the python and python3 commands point to python 3.

@epragalakis
Copy link
Author

it works now on linux :)

(worth noting that storing the manifest in the /usr dir instead of the per-user visibility .mozilla dir was throwing an error)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior. effort: small This task is a small effort. example New or updated example p2 We want to address this but may have other higher priority items.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants