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 bug creating impossible constraints #6

Merged
merged 1 commit into from
Nov 7, 2022
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
29 changes: 19 additions & 10 deletions src/security_constraints/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,25 @@ def get_safe_version_constraints(

"""
safe_specs: List[str] = []
vulnerable_specs = [p.strip() for p in vulnerability.vulnerable_range.split(",")]
for vulnerable_spec in vulnerable_specs:
if vulnerable_spec.startswith("= "):
safe_specs.append(f"!={vulnerable_spec[2:]}")
elif vulnerable_spec.startswith("<= "):
safe_specs.append(f">{vulnerable_spec[3:]}")
elif vulnerable_spec.startswith("< "):
safe_specs.append(f">={vulnerable_spec[2:]}")
elif vulnerable_spec.startswith(">= "):
safe_specs.append(f"<{vulnerable_spec[3:]}")
vulnerable_spec: str
if "," in vulnerability.vulnerable_range:
# If there is a known min and max affected version, make the constraints
# just specify the minimum safe version, since min and max constraints cannot
# be met at the same time.
vulnerable_spec = [
p.strip() for p in vulnerability.vulnerable_range.split(",")
][-1]
else:
vulnerable_spec = vulnerability.vulnerable_range.strip()

if vulnerable_spec.startswith("= "):
safe_specs.append(f"!={vulnerable_spec[2:]}")
elif vulnerable_spec.startswith("<= "):
safe_specs.append(f">{vulnerable_spec[3:]}")
elif vulnerable_spec.startswith("< "):
safe_specs.append(f">={vulnerable_spec[2:]}")
elif vulnerable_spec.startswith(">= "):
safe_specs.append(f"<{vulnerable_spec[3:]}")
return PackageConstraints(
package=vulnerability.package,
specifiers=safe_specs,
Expand Down
2 changes: 1 addition & 1 deletion test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_get_security_vulnerability_database_apis(monkeypatch) -> None:
package="pystuff",
vulnerable_range=">= 4.3.0, < 4.3.5",
),
PackageConstraints(package="pystuff", specifiers=["<4.3.0", ">=4.3.5"]),
PackageConstraints(package="pystuff", specifiers=[">=4.3.5"]),
),
(
SecurityVulnerability(
Expand Down