1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-25 19:01:16 +02:00

fix: ensure access-token matches are complete

This commit is contained in:
Thomas Bereknyei 2025-02-13 12:45:37 -05:00
parent a9f4d73d3e
commit 269efa01b3
2 changed files with 31 additions and 4 deletions

View file

@ -20,16 +20,36 @@ public:
void TearDown() override { } void TearDown() override { }
}; };
TEST_F(AccessKeysTest, singleGitHub) TEST_F(AccessKeysTest, singleOrgGitHub)
{ {
fetchers::Settings fetchSettings = fetchers::Settings{}; fetchers::Settings fetchSettings = fetchers::Settings{};
fetchSettings.accessTokens.get().insert({"github.com","token"}); fetchSettings.accessTokens.get().insert({"github.com/a","token"});
auto i = Input::fromURL(fetchSettings, "github:a/b"); auto i = Input::fromURL(fetchSettings, "github:a/b");
auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/b"); auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/b");
ASSERT_EQ(token,"token"); ASSERT_EQ(token,"token");
} }
TEST_F(AccessKeysTest, nonMatches)
{
fetchers::Settings fetchSettings = fetchers::Settings{};
fetchSettings.accessTokens.get().insert({"github.com","token"});
auto i = Input::fromURL(fetchSettings, "gitlab:github.com/evil");
auto token = i.scheme->getAccessToken(fetchSettings, "gitlab.com", "gitlab.com/github.com/evil");
ASSERT_EQ(token,std::nullopt);
}
TEST_F(AccessKeysTest, noPartialMatches)
{
fetchers::Settings fetchSettings = fetchers::Settings{};
fetchSettings.accessTokens.get().insert({"github.com/partial","token"});
auto i = Input::fromURL(fetchSettings, "github:partial-match/repo");
auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/partial-match");
ASSERT_EQ(token,std::nullopt);
}
TEST_F(AccessKeysTest, repoGitHub) TEST_F(AccessKeysTest, repoGitHub)
{ {
fetchers::Settings fetchSettings = fetchers::Settings{}; fetchers::Settings fetchSettings = fetchers::Settings{};

View file

@ -179,8 +179,15 @@ struct GitArchiveInputScheme : InputScheme
size_t answer_match_len = 0; size_t answer_match_len = 0;
if(! url.empty()) { if(! url.empty()) {
for (auto & token : tokens) { for (auto & token : tokens) {
auto match_len = url.find(token.first); auto first = url.find(token.first);
if (match_len != std::string::npos && token.first.length() > answer_match_len) { if (
first != std::string::npos
&& token.first.length() > answer_match_len
&& first == 0
&& url.substr(0,token.first.length()) == token.first
&& (url.length() == token.first.length() || url[token.first.length()] == '/')
)
{
answer = token.second; answer = token.second;
answer_match_len = token.first.length(); answer_match_len = token.first.length();
} }