Skip to content

[3.11] gh-143930: Reject leading dashes in webbrowser URLs (GH-143931)#146364

Open
tomcruiseqi wants to merge 1 commit intopython:3.11from
tomcruiseqi:backport-82a24a4-3.11
Open

[3.11] gh-143930: Reject leading dashes in webbrowser URLs (GH-143931)#146364
tomcruiseqi wants to merge 1 commit intopython:3.11from
tomcruiseqi:backport-82a24a4-3.11

Conversation

@tomcruiseqi
Copy link

@tomcruiseqi tomcruiseqi commented Mar 24, 2026

Copilot AI review requested due to automatic review settings March 24, 2026 09:40
@tomcruiseqi tomcruiseqi changed the title gh-143930: Reject leading dashes in webbrowser URLs [3.11] gh-143930: Reject leading dashes in webbrowser URLs (GH-143931) Mar 24, 2026
@bedevere-app bedevere-app bot added the type-security A security issue label Mar 24, 2026
@tomcruiseqi tomcruiseqi changed the title [3.11] gh-143930: Reject leading dashes in webbrowser URLs (GH-143931) [3.11] gh-143930: Reject leading dashes in webbrowser URLs Mar 24, 2026
@tomcruiseqi tomcruiseqi changed the title [3.11] gh-143930: Reject leading dashes in webbrowser URLs [3.11] gh-143930: Reject leading dashes in webbrowser URLs (GH-143931) Mar 24, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Security hardening for webbrowser to prevent option-injection style issues when URLs are forwarded to external browser commands.

Changes:

  • Add URL validation that rejects inputs whose first non-whitespace character is -.
  • Invoke this validation across built-in browser controller implementations before launching/dispatching.
  • Add a regression test for the new rejection behavior and a Security NEWS entry.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
Misc/NEWS.d/next/Security/2026-01-16-12-04-49.gh-issue-143930.zYC5x3.rst Documents the security behavior change for webbrowser.open().
Lib/webbrowser.py Introduces _check_url() and applies it to multiple controllers before executing browser actions.
Lib/test/test_webbrowser.py Adds a regression test ensuring dash-prefixed inputs are rejected.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +158 to +163
@staticmethod
def _check_url(url):
"""Ensures that the URL is safe to pass to subprocesses as a parameter"""
if url and url.lstrip().startswith("-"):
raise ValueError(f"Invalid URL: {url}")

Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

Because register() can return controllers that don’t inherit from BaseBrowser (and therefore won’t call _check_url()), invalid dash-prefixed URLs can still reach custom backends via webbrowser.open(). To make the guarantee match the NEWS entry/title, consider validating once at the public API boundary (module-level open() / open_new*()) before dispatching to any controller.

Copilot uses AI. Check for mistakes.
Comment on lines +62 to +66
def test_reject_dash_prefixes(self):
browser = self.browser_class(name=CMD_NAME)
with self.assertRaises(ValueError):
browser.open(f"--key=val {URL}")

Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

This adds coverage for GenericBrowser, but the new URL validation is also invoked by BackgroundBrowser, UnixBrowser subclasses, and platform-specific controllers. Consider factoring this into CommandTestMixin (or adding equivalent tests per class) so the rejection behavior is enforced across the other command-based browser implementations too.

Copilot uses AI. Check for mistakes.
def _check_url(url):
"""Ensures that the URL is safe to pass to subprocesses as a parameter"""
if url and url.lstrip().startswith("-"):
raise ValueError(f"Invalid URL: {url}")
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

The ValueError message includes the raw URL; using repr (or otherwise escaping/quoting) would make the exception unambiguous and avoid embedding control characters/newlines in error output. Consider formatting the URL with !r and/or using a more specific message (e.g., that leading dashes are rejected).

Suggested change
raise ValueError(f"Invalid URL: {url}")
raise ValueError(f"Invalid URL (leading dashes are not allowed): {url!r}")

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants