Procházet zdrojové kódy

allow user@host (aka: no password) in URI parsing

If the URI had no password the username was ignored
David Kalnischkies před 10 roky
rodič
revize
a1f3ac8aba
2 změnil soubory, kde provedl 21 přidání a 7 odebrání
  1. 9 7
      apt-pkg/contrib/strutl.cc
  2. 12 0
      test/libapt/uri_test.cc

+ 9 - 7
apt-pkg/contrib/strutl.cc

@@ -1639,13 +1639,15 @@ void URI::CopyFrom(const string &U)
    I = FirstColon + 1;
    if (I > SingleSlash)
       I = SingleSlash;
-   for (; I < SingleSlash && *I != ':'; ++I);
-   string::const_iterator SecondColon = I;
-   
-   // Search for the @ after the colon
-   for (; I < SingleSlash && *I != '@'; ++I);
-   string::const_iterator At = I;
-   
+
+   // Search for the @ separating user:pass from host
+   auto const RevAt = std::find(
+	 std::string::const_reverse_iterator(SingleSlash),
+	 std::string::const_reverse_iterator(I), '@');
+   string::const_iterator const At = RevAt.base() == I ? SingleSlash : std::prev(RevAt.base());
+   // and then look for the colon between user and pass
+   string::const_iterator const SecondColon = std::find(I, At, ':');
+
    // Now write the host and user/pass
    if (At == SingleSlash)
    {

+ 12 - 0
test/libapt/uri_test.cc

@@ -28,6 +28,18 @@ TEST(URITest, BasicHTTP)
    EXPECT_EQ("http://ualberta.ca", URI::SiteOnly(U));
    EXPECT_EQ("http://ualberta.ca/blah", URI::ArchiveOnly(U));
    EXPECT_EQ("http://ualberta.ca/blah", URI::NoUserPassword(U));
+   // just a user
+   U = URI("https://apt@example.org/blah");
+   EXPECT_EQ("https", U.Access);
+   EXPECT_EQ("apt", U.User);
+   EXPECT_EQ("", U.Password);
+   EXPECT_EQ(0, U.Port);
+   EXPECT_EQ("example.org", U.Host);
+   EXPECT_EQ("/blah", U.Path);
+   EXPECT_EQ("https://apt@example.org/blah", (std::string)U);
+   EXPECT_EQ("https://example.org", URI::SiteOnly(U));
+   EXPECT_EQ("https://example.org/blah", URI::ArchiveOnly(U));
+   EXPECT_EQ("https://example.org/blah", URI::NoUserPassword(U));
 }
 TEST(URITest, SingeSlashFile)
 {