Skip to content

Commit fdde2e7

Browse files
committed
Reject empty strings in Maven, Nuget, Rubygems and Conan versions
MavenVersion, NugetVersion, RubygemsVersion and ConanVersion accepted the empty string because their is_valid() overrides only tried to parse the value, and the underlying parsers accept empty input. An accepted empty version then sorted below every real version (Maven, Rubygems), raised TypeError on comparison (Nuget), or both (Conan), silently corrupting downstream range logic instead of failing. Make the overrides honor the base Version.is_valid() contract, which already documents that the empty string is invalid, by checking super().is_valid() first. DebianVersion gets the same guard for consistency although its parser already rejected empty input. Fixes #204 Signed-off-by: Manoj Gowda <manojgowdabs18@gmail.com>
1 parent 92b1a24 commit fdde2e7

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

src/univers/versions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ def build_value(cls, string):
248248

249249
@classmethod
250250
def is_valid(cls, string):
251+
if not super().is_valid(string):
252+
return False
251253
try:
252254
cls.build_value(string)
253255
return True
@@ -325,7 +327,7 @@ def build_value(cls, string):
325327

326328
@classmethod
327329
def is_valid(cls, string):
328-
return gem.GemVersion.is_correct(string)
330+
return super().is_valid(string) and gem.GemVersion.is_correct(string)
329331

330332

331333
class ArchLinuxVersion(Version):
@@ -375,6 +377,8 @@ def build_value(cls, string):
375377

376378
@classmethod
377379
def is_valid(cls, string):
380+
if not super().is_valid(string):
381+
return False
378382
try:
379383
cls.build_value(string)
380384
return True
@@ -392,6 +396,8 @@ def build_value(cls, string):
392396

393397
@classmethod
394398
def is_valid(cls, string):
399+
if not super().is_valid(string):
400+
return False
395401
try:
396402
cls.build_value(string)
397403
return True
@@ -664,6 +670,8 @@ def build_value(cls, string):
664670

665671
@classmethod
666672
def is_valid(cls, string):
673+
if not super().is_valid(string):
674+
return False
667675
try:
668676
cls.build_value(string)
669677
return True

tests/test_versions.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,26 @@ def test_lexicographic_version():
253253
assert LexicographicVersion("Abc") < LexicographicVersion(None)
254254
assert LexicographicVersion("123") < LexicographicVersion("bbc")
255255
assert LexicographicVersion("2.3.4") > LexicographicVersion("1.2.3")
256+
257+
258+
def test_version_with_empty_string_is_invalid():
259+
# https://github.com/aboutcode-org/univers/issues/204
260+
# These schemes used to accept the empty string and then sort it as the
261+
# minimum version (Maven, Rubygems), raise on comparison (Nuget), or
262+
# both (Conan), silently corrupting downstream range logic.
263+
import pytest
264+
265+
from univers.versions import ConanVersion
266+
from univers.versions import InvalidVersion
267+
268+
for version_class in (
269+
Version,
270+
MavenVersion,
271+
NugetVersion,
272+
RubygemsVersion,
273+
ConanVersion,
274+
SemverVersion,
275+
PypiVersion,
276+
):
277+
with pytest.raises(InvalidVersion):
278+
version_class("")

0 commit comments

Comments
 (0)