Blog | G5 Cyber Security

Custom Browser Protocols

TL;DR

Want your browser to open specific apps or perform actions when you click a special link? This guide shows you how to set up custom protocol handlers in Chrome, Firefox, and Edge. It’s useful for things like opening email clients, messaging apps, or even running local scripts.

Setting Up Custom Protocol Handlers

  1. Understand Protocol Handlers: A protocol handler tells your browser what to do when it encounters a link starting with something other than http:// or https://. For example, mailto: opens your email client. We’ll create our own.
  2. Choose Your Protocol: Pick a unique prefix for your protocol (e.g., my-app://, customprotocol://). Avoid common names to prevent conflicts.
  3. Create the Handler Application/Script: This is what will actually *do* something when the protocol is called. It could be a desktop application, a Python script, or anything else that can handle command-line arguments.
    • Example (Python): A simple Python script to display a message:
      import sys
      
      if __name__ == '__main__':
        if len(sys.argv) > 1:
          message = ' '.join(sys.argv[1:])
          print(f'Handling custom protocol with message: {message}')
        else:
          print('No message provided.')
  4. Register the Protocol Handler (Chrome/Edge): Chrome and Edge use a similar system. You can do this through the registry or via command line.
    • Registry Method (Windows): This is more permanent but requires editing the Windows Registry (be careful!).
      1. Open Registry Editor (regedit).
      2. Navigate to HKEY_CURRENT_USERSoftwareClasses.
      3. Create a new key with your protocol name (e.g., my-app).
      4. Within that key, create a subkey named shell.
      5. Within shell, create a subkey named open.
      6. Within open, create a subkey named command.
      7. Set the default value of the command key to the path of your application/script, including any arguments. For example:
        "C:Python39python.exe" "C:pathtoyourscript.py" "%1"
    • Command Line Method (Chrome/Edge): This is temporary and useful for testing.
      chrome://flags/#enable-command-line-protocol-handler

      Enable this flag, then restart Chrome. You can also use the command line when launching chrome:

      chrome.exe --register-protocol-handler=my-app,YourHandlerName,C:pathtoyourscript.py
  5. Register the Protocol Handler (Firefox): Firefox uses a different approach.
    • Create a .desktop file in your Firefox profile directory’s extensions folder. The path is typically something like:
      ~/.mozilla/firefox/{profile_id}/extensions/your-extension-name/protocol-handler.desktop
    • The contents of the .desktop file should look similar to this (replace placeholders):
      [Desktop Entry]
      Type=Application
      Name=Your Handler Name
      Exec="C:Python39python.exe" "C:pathtoyourscript.py" %u
      MimeType=x-scheme-handler/my-app;name=My App Protocol
  6. Test Your Handler: Open a new tab and type your protocol URL (e.g., my-app://hello world) into the address bar and press Enter. If everything is set up correctly, your application/script should run.
  7. Troubleshooting:
    • Permissions: Make sure your script has execute permissions.
    • Pathing: Double-check that the path to your application/script in the registry or .desktop file is correct.
    • Quotes: Use quotes around paths with spaces.
    • Restart Browser: Always restart your browser after making changes.
Exit mobile version