Skip to content

Commit 269efa0

Browse files
committed
fix: ensure access-token matches are complete
1 parent a9f4d73 commit 269efa0

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/libfetchers-tests/access-tokens.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,36 @@ class AccessKeysTest : public ::testing::Test
2020
void TearDown() override { }
2121
};
2222

23-
TEST_F(AccessKeysTest, singleGitHub)
23+
TEST_F(AccessKeysTest, singleOrgGitHub)
2424
{
2525
fetchers::Settings fetchSettings = fetchers::Settings{};
26-
fetchSettings.accessTokens.get().insert({"github.com","token"});
26+
fetchSettings.accessTokens.get().insert({"github.com/a","token"});
2727
auto i = Input::fromURL(fetchSettings, "github:a/b");
2828

2929
auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/a/b");
3030
ASSERT_EQ(token,"token");
3131
}
3232

33+
TEST_F(AccessKeysTest, nonMatches)
34+
{
35+
fetchers::Settings fetchSettings = fetchers::Settings{};
36+
fetchSettings.accessTokens.get().insert({"github.com","token"});
37+
auto i = Input::fromURL(fetchSettings, "gitlab:github.com/evil");
38+
39+
auto token = i.scheme->getAccessToken(fetchSettings, "gitlab.com", "gitlab.com/github.com/evil");
40+
ASSERT_EQ(token,std::nullopt);
41+
}
42+
43+
TEST_F(AccessKeysTest, noPartialMatches)
44+
{
45+
fetchers::Settings fetchSettings = fetchers::Settings{};
46+
fetchSettings.accessTokens.get().insert({"github.com/partial","token"});
47+
auto i = Input::fromURL(fetchSettings, "github:partial-match/repo");
48+
49+
auto token = i.scheme->getAccessToken(fetchSettings, "github.com", "github.com/partial-match");
50+
ASSERT_EQ(token,std::nullopt);
51+
}
52+
3353
TEST_F(AccessKeysTest, repoGitHub)
3454
{
3555
fetchers::Settings fetchSettings = fetchers::Settings{};

src/libfetchers/github.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,15 @@ struct GitArchiveInputScheme : InputScheme
179179
size_t answer_match_len = 0;
180180
if(! url.empty()) {
181181
for (auto & token : tokens) {
182-
auto match_len = url.find(token.first);
183-
if (match_len != std::string::npos && token.first.length() > answer_match_len) {
182+
auto first = url.find(token.first);
183+
if (
184+
first != std::string::npos
185+
&& token.first.length() > answer_match_len
186+
&& first == 0
187+
&& url.substr(0,token.first.length()) == token.first
188+
&& (url.length() == token.first.length() || url[token.first.length()] == '/')
189+
)
190+
{
184191
answer = token.second;
185192
answer_match_len = token.first.length();
186193
}

0 commit comments

Comments
 (0)