Skip to content

[🐛 Bug]: install_addon() is removing the first letter of all the files of an extension when the path to an extension dir ends with a '/' #13685

@jkbzh

Description

@jkbzh

What happened?

The firefox webdriver provides an install_addon() call that lets you install a browser extension. You can call this extension by giving the path to either the extensions xpi file or the path to where the source code for the extension is found.

For the second case, install_addon() will go walk the directory where the extension is and create an xpi file. However, the function doesn't control that the path that is given ends with a '/'. The function works correctly when the path doesn't end with a '/'. But when you do give it a path that does end with a '/', install_addon() is over-compensating the trailing slash of the patch, which results in that all the files that it is adding to the zip file are missing their first letter.

https://github.com/SeleniumHQ/selenium/blob/83083c63593f245ed82cfd9e43a17654d5f73a43/py/selenium/webdriver/firefox/webdriver.py#L126C1-L134C68

L126 always return true if parameter "path" is a path, independently of it having a trailing end '/'
if os.path.isdir(path):

L128 is already compensating for the trailing slash that may be added by os_walk_path() in line 30... but note that no extra slash is added when the path itself ends with a slash.

        path_root = len(path) + 1  # account for trailing slash

L133 compensates again for the trailing slash as it doesn't take into account that the index for the first char in a string starts from 0, not 1
zipped.write(filename, filename[path_root:])

One solution would be to strip any trailing '/' that may in the parameter value:

      path_root = len(path.rstrip(os.sep)) + 1  # account for trailing slash

I let you adjust this to what is more pythonic and portable to you.

How can we reproduce the issue?

You can clone from https://github.com/mdn/webextensions-examples/ and call
with webdriver.Firefox(options=options) as driver:
    # install and init the extension
    addon_id = driver.install_addon(extension_path, temporary=True)

with extension_path pointing to the dir where the source code (and manifest for the extension exists).  For example: 

/webextensions-examples/tree/main/borderify/

If you pass the extension_path without a trailing '/', it works.

If you pass the extension_path with a trailing '/', the first character of each file will be missing in the generated xpi file.

Relevant log output

File "my_home/scripts/selenium_xpi_request.py", line 88, in <module>
    addon_id = driver.install_addon(extension_path, temporary=True)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "my_home/.virtualenvs/w3cpy3/lib/python3.11/site-packages/selenium/webdriver/firefox/webdriver.py", line 140, in install_addon
    return self.execute("INSTALL_ADDON", payload)["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "my_home/.virtualenvs/w3cpy3/lib/python3.11/site-packages/selenium/webdriver/remote/webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "my_home/.virtualenvs/w3cpy3/lib/python3.11/site-packages/selenium/webdriver/remote/errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Could not install add-on: /tmp/addon-9c494ff3-9c7e-4420-ac77-feb5ddb7e349.xpi: File /tmp/addon-9c494ff3-9c7e-4420-ac77-feb5ddb7e349.xpi does not contain a valid manifest
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:191:5
UnknownError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:800:5
install@chrome://remote/content/marionette/addon.sys.mjs:88:13

Operating System

Debian 12.5

Selenium version

Python 3.11.2

What are the browser(s) and version(s) where you see this issue?

Mozilla Firefox 123.0.1

What are the browser driver(s) and version(s) where you see this issue?

geckodriver 0.34.0 (c44f0d09630a 2024-01-02 15:36 +0000)

Are you using Selenium Grid?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-needs-triagingA Selenium member will evaluate this soon!I-defectSomething is not working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions