Skip to content

Commit

Permalink
Add option to remove credentials in URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoinePrv committed Oct 6, 2023
1 parent a5b03e4 commit df3f8e5
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 39 deletions.
7 changes: 4 additions & 3 deletions libmamba/include/mamba/specs/conda_url.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace mamba::specs
public:

using StripScheme = util::detail::StripScheme;
using HideConfidential = util::detail::HideConfidential;
using Credentials = util::detail::Credentials;
using Encode = util::detail::Encode;
using Decode = util::detail::Decode;

Expand Down Expand Up @@ -207,12 +207,13 @@ namespace mamba::specs
* asset.
* @param strip_scheme If true, remove the scheme and "localhost" on file URI.
* @param rstrip_path If non-null, remove the given charaters at the end of the path.
* @param hide_confidential If true, hide password and tokens in the decoded string.
* @param credentials If true, hide password and tokens in the decoded string.
* @param credentials Decide to keep, remove, or hide passwrd, users, and token.
*/
[[nodiscard]] auto pretty_str(
StripScheme strip_scheme = StripScheme::no,
char rstrip_path = 0,
HideConfidential hide_confidential = HideConfidential::no
Credentials credentials = Credentials::Show
) const -> std::string;


Expand Down
16 changes: 8 additions & 8 deletions libmamba/include/mamba/util/url.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ namespace mamba::util
yes
};

enum class HideConfidential : bool
enum class Credentials
{
no,
yes
Show,
Hide,
Remove,
};

struct Encode
Expand Down Expand Up @@ -59,7 +60,7 @@ namespace mamba::util
public:

using StripScheme = detail::StripScheme;
using HideConfidential = detail::HideConfidential;
using Credentials = detail::Credentials;
using Encode = detail::Encode;
using Decode = detail::Decode;

Expand Down Expand Up @@ -229,16 +230,15 @@ namespace mamba::util
/**
* Return the full decoded url.
*
* Due to decoding, the outcome may not be understood by parser and usable to reach an
* asset.
* Due to decoding, the outcome may not be understood by parser and usable to fetch the URL.
* @param strip_scheme If true, remove the scheme and "localhost" on file URI.
* @param rstrip_path If non-null, remove the given charaters at the end of the path.
* @param hide_confidential If true, hide password in the decoded string.
* @param credentials Decide to keep, remove, or hide credentials.
*/
[[nodiscard]] auto pretty_str(
StripScheme strip_scheme = StripScheme::no,
char rstrip_path = 0,
HideConfidential hide_confidential = HideConfidential::no
Credentials credentials = Credentials::Show
) const -> std::string;

protected:
Expand Down
2 changes: 1 addition & 1 deletion libmamba/src/core/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ namespace mamba
out,
" {:<15} {}\n",
"URL",
url.pretty_str(CondaURL::StripScheme::no, '/', CondaURL::HideConfidential::yes)
url.pretty_str(CondaURL::StripScheme::no, '/', CondaURL::Credentials::Hide)
);

fmt::print(out, fmtstring, "MD5", pkg.md5.empty() ? "Not available" : pkg.md5);
Expand Down
53 changes: 41 additions & 12 deletions libmamba/src/specs/conda_url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,31 +355,60 @@ namespace mamba::specs
}

auto
CondaURL::pretty_str(StripScheme strip_scheme, char rstrip_path, HideConfidential hide_confifential) const
CondaURL::pretty_str(StripScheme strip_scheme, char rstrip_path, Credentials credentials) const
-> std::string
{
std::string computed_path = pretty_str_path(strip_scheme, rstrip_path);

if (hide_confifential == HideConfidential::yes)
std::string l_user = {};
std::string l_password = {};
std::string l_path = {};
switch (credentials)
{
const auto len = token_and_prefix_len(computed_path);
if (len > 0)
case (Credentials::Show):
{
l_user = user(Decode::yes);
l_password = password(Decode::yes);
l_path = pretty_str_path(strip_scheme, rstrip_path);
break;
}
case (Credentials::Hide):
{
set_token_no_check_input_impl(computed_path, 0, len, "*****");
l_user = user(Decode::yes);
l_password = "*****";
if (token().empty())
{
l_path = pretty_str_path(strip_scheme, rstrip_path);
}
else
{
l_path = util::concat("/t/*****", path_without_token());
}
break;
}
case (Credentials::Remove):
{
if (token().empty())
{
l_path = pretty_str_path(strip_scheme, rstrip_path);
}
else
{
l_path = path_without_token();
}
break;
}
}

return util::concat(
(strip_scheme == StripScheme::no) ? scheme() : "",
(strip_scheme == StripScheme::no) ? "://" : "",
user(Decode::yes),
password(Decode::no).empty() ? "" : ":",
(hide_confifential == HideConfidential::no) ? password(Decode::yes) : "*****",
user(Decode::no).empty() ? "" : "@",
l_user,
(l_password.empty() || l_user.empty()) ? "" : ":",
(l_password.empty() || l_user.empty()) ? "" : l_password,
l_user.empty() ? "" : "@",
host(Decode::yes),
port().empty() ? "" : ":",
port(),
computed_path,
l_path,
query().empty() ? "" : "?",
query(),
fragment().empty() ? "" : "#",
Expand Down
32 changes: 26 additions & 6 deletions libmamba/src/util/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,17 +516,37 @@ namespace mamba::util
return computed_path;
}

auto
URL::pretty_str(StripScheme strip_scheme, char rstrip_path, HideConfidential hide_confidential) const
auto URL::pretty_str(StripScheme strip_scheme, char rstrip_path, Credentials credentials) const
-> std::string
{
std::string l_user = {};
std::string l_password = {};
switch (credentials)
{
case (Credentials::Show):
{
l_user = user(Decode::yes);
l_password = password(Decode::yes);
break;
}
case (Credentials::Hide):
{
l_user = user(Decode::yes);
l_password = "*****";
break;
}
case (Credentials::Remove):
{
break;
}
}
return util::concat(
(strip_scheme == StripScheme::no) ? scheme() : "",
(strip_scheme == StripScheme::no) ? "://" : "",
user(Decode::yes),
m_password.empty() ? "" : ":",
(hide_confidential == HideConfidential::no) ? password(Decode::yes) : "*****",
m_user.empty() ? "" : "@",
l_user,
(l_password.empty() || l_user.empty()) ? "" : ":",
(l_password.empty() || l_user.empty()) ? "" : l_password,
l_user.empty() ? "" : "@",
host(Decode::yes),
m_port.empty() ? "" : ":",
m_port,
Expand Down
19 changes: 13 additions & 6 deletions libmamba/tests/src/specs/test_conda_url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,30 +329,37 @@ TEST_SUITE("specs::CondaURL")
CHECK_EQ(url.pretty_str(CondaURL::StripScheme::no, '/'), "https://mamba.org/page");
}

SUBCASE("Hide confidential option")
SUBCASE("Credentail option")
{
CondaURL url = {};
url.set_user("user");
url.set_password("pass");
CHECK_EQ(
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::HideConfidential::no),
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::Credentials::Show),
"https://user:pass@localhost/"
);
CHECK_EQ(
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::HideConfidential::yes),
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::Credentials::Hide),
"https://user:*****@localhost/"
);
CHECK_EQ(
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::Credentials::Remove),
"https://localhost/"
);

url.set_path("/t/abcd1234/linux-64");
CHECK_EQ(
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::HideConfidential::no),
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::Credentials::Show),
"https://user:pass@localhost/t/abcd1234/linux-64"
);

CHECK_EQ(
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::HideConfidential::yes),
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::Credentials::Hide),
"https://user:*****@localhost/t/*****/linux-64"
);
CHECK_EQ(
url.pretty_str(CondaURL::StripScheme::no, 0, CondaURL::Credentials::Remove),
"https://localhost/linux-64"
);
}

SUBCASE("https://user:password@mamba.org:8080/folder/file.html?param=value#fragment")
Expand Down
10 changes: 7 additions & 3 deletions libmamba/tests/src/util/test_url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,19 +408,23 @@ TEST_SUITE("util::URL")
CHECK_EQ(url.pretty_str(URL::StripScheme::no, '/'), "https://mamba.org/page");
}

SUBCASE("Hide password option")
SUBCASE("Credential option")
{
URL url = {};
url.set_user("user");
url.set_password("pass");
CHECK_EQ(
url.pretty_str(URL::StripScheme::no, 0, URL::HideConfidential::no),
url.pretty_str(URL::StripScheme::no, 0, URL::Credentials::Show),
"https://user:pass@localhost/"
);
CHECK_EQ(
url.pretty_str(URL::StripScheme::no, 0, URL::HideConfidential::yes),
url.pretty_str(URL::StripScheme::no, 0, URL::Credentials::Hide),
"https://user:*****@localhost/"
);
CHECK_EQ(
url.pretty_str(URL::StripScheme::no, 0, URL::Credentials::Remove),
"https://localhost/"
);
}
}

Expand Down

0 comments on commit df3f8e5

Please sign in to comment.