Skip to content
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

fix: validate whether a package is an URL correctly #1356

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1355.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Validate whether a package is an URL correctly.
19 changes: 13 additions & 6 deletions src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def get_venv_args(parsed_args: Dict[str, str]) -> List[str]:
return venv_args


def package_is_url(package: str, raise_error: bool = True) -> bool:
url_parse_package = urllib.parse.urlparse(package)
if url_parse_package.scheme and url_parse_package.netloc:
if not raise_error:
return True
raise PipxError("Package cannot be a URL. A valid package name should be passed instead.")
return False


def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.ArgumentParser]) -> ExitCode: # noqa: C901
verbose = args.verbose if "verbose" in args else False
if not constants.WINDOWS and args.is_global:
Expand All @@ -185,12 +194,10 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar

if "package" in args:
package = args.package
url_parse_package = urllib.parse.urlparse(package)
if url_parse_package.scheme and url_parse_package.netloc:
raise PipxError("Package cannot be a url")
package_is_url(package)

if "spec" in args and args.spec is not None:
if urllib.parse.urlparse(args.spec).scheme:
if package_is_url(args.spec, raise_error=False):
if "#egg=" not in args.spec:
args.spec = args.spec + f"#egg={package}"

Expand All @@ -212,8 +219,8 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
logger.info(f"Virtual Environment location is {venv_dir}")

if "packages" in args:
if any(urllib.parse.urlparse(package).scheme for package in args.packages):
raise PipxError("Package cannot be a url. Package name should be passed instead.")
for package in args.packages:
package_is_url(package)
venv_dirs = {package: venv_container.get_venv_dir(package) for package in args.packages}
venv_dirs_msg = "\n".join(f"- {key} : {value}" for key, value in venv_dirs.items())
logger.info(f"Virtual Environment locations are:\n{venv_dirs_msg}")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,9 @@ def test_upgrade_multiple(pipx_temp_env, capsys):
captured = capsys.readouterr()
assert f"upgraded package {name} from {initial_version} to" in captured.out
assert "pycowsay is already at latest version" in captured.out


def test_upgrade_absolute_path(pipx_temp_env, capsys, root):
assert run_pipx_cli(["upgrade", "--verbose", str((root / "testdata" / "empty_project").resolve())])
captured = capsys.readouterr()
assert "Package cannot be a URL" not in captured.err