Skip to content

Support SHA256 hashes in @EXPLICIT files #3866

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

Merged
merged 9 commits into from
Mar 21, 2025
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
19 changes: 17 additions & 2 deletions libmamba/src/specs/package_info.cpp
Copy link
Member

Choose a reason for hiding this comment

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

I think we might want to do something in else cases.

How about raising a warning when hashes are malformed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that sounds sensible. I didn't add them to respect the existing logic (silent ignore). I'm not sure how to add warnings in this codebase, so feel free to commit to the branch directly :)

Copy link
Member

Choose a reason for hiding this comment

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

You are right, and thinking again I am not sure we want to add warnings to spam users if this does not cause fatal behavior and if they cannot do anything about it.

Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,24 @@ namespace mamba::specs
return parse_url(url).transform(
[&](PackageInfo&& pkg) -> PackageInfo
{
if (is_hash(hash))
if (util::starts_with(hash, "sha256:"))
{
pkg.md5 = hash;
hash = hash.substr(7);
if (hash.size() == 64 && is_hash(hash))
{
pkg.sha256 = hash;
}
}
else if (is_hash(hash))
{
if (hash.size() == 32)
{
pkg.md5 = hash;
}
else if (hash.size() == 64)
{
pkg.sha256 = hash;
}
}
return pkg;
}
Expand Down
39 changes: 39 additions & 0 deletions libmamba/tests/src/specs/test_package_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
REQUIRE(pkg.package_url == url);
REQUIRE(pkg.md5 == "");
REQUIRE(pkg.sha256 == "");
REQUIRE(pkg.platform == "linux-64");
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
}
Expand All @@ -48,6 +49,43 @@ namespace
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
REQUIRE(pkg.md5 == url.substr(url.rfind('#') + 1));
REQUIRE(pkg.sha256 == "");
REQUIRE(pkg.platform == "linux-64");
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
}

SECTION("https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0"
)
{
static constexpr std::string_view url = "https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0";

auto pkg = PackageInfo::from_url(url).value();

REQUIRE(pkg.name == "pkg");
REQUIRE(pkg.version == "6.4");
REQUIRE(pkg.build_string == "bld");
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
REQUIRE(pkg.md5 == "");
REQUIRE(pkg.sha256 == url.substr(url.rfind('#') + 1));
REQUIRE(pkg.platform == "linux-64");
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
}

SECTION("https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#sha256:7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0"
)
{
static constexpr std::string_view url = "https://conda.anaconda.org/conda-forge/linux-64/pkg-6.4-bld.conda#sha256:7dbaa197d7ba6032caf7ae7f32c1efa07dbaa197d7ba6032caf7ae7f32c1efa0";

auto pkg = PackageInfo::from_url(url).value();

REQUIRE(pkg.name == "pkg");
REQUIRE(pkg.version == "6.4");
REQUIRE(pkg.build_string == "bld");
REQUIRE(pkg.filename == "pkg-6.4-bld.conda");
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
REQUIRE(pkg.md5 == "");
REQUIRE(pkg.sha256 == url.substr(url.rfind("#sha256:") + 8));
REQUIRE(pkg.platform == "linux-64");
REQUIRE(pkg.channel == "https://conda.anaconda.org/conda-forge");
}
Expand All @@ -65,6 +103,7 @@ namespace
REQUIRE(pkg.filename == "_libgcc_mutex-0.1-conda_forge.tar.bz2");
REQUIRE(pkg.package_url == url.substr(0, url.rfind('#')));
REQUIRE(pkg.md5 == url.substr(url.rfind('#') + 1));
REQUIRE(pkg.sha256 == "");
REQUIRE(pkg.platform == "linux-64");
// Make sure the token is not censored when setting the channel
REQUIRE(
Expand Down
21 changes: 18 additions & 3 deletions libmambapy/tests/test_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,27 @@ def test_PackageInfo():
# str
assert str(pkg) == "pkg-1.0-bld"

# from_url
pkg = PackageInfo.from_url("https://repo.mamba.pm/conda-forge/linux-64/bar-5.1-xld.conda#01234")
# from_url with md5
pkg = PackageInfo.from_url(
"https://repo.mamba.pm/conda-forge/linux-64/bar-5.1-xld.conda"
"#01234012340123401234012340123401"
)
assert pkg.name == "bar"
assert pkg.version == "5.1"
assert pkg.build_string == "xld"
assert pkg.md5 == "01234012340123401234012340123401"
assert pkg.sha256 == ""

# from_url with sha256
pkg = PackageInfo.from_url(
"https://repo.mamba.pm/conda-forge/linux-64/bar-5.1-xld.conda"
"#0123401234012340123401234012340101234012340123401234012340123401"
)
assert pkg.name == "bar"
assert pkg.version == "5.1"
assert pkg.build_string == "xld"
assert pkg.md5 == "01234"
assert pkg.md5 == ""
assert pkg.sha256 == "0123401234012340123401234012340101234012340123401234012340123401"

# getters and setters
pkg.name = "foo"
Expand Down
Loading