Fix handling of IPv6 addresses in QUrl::fromUserInput
IPv6 addresses can start with ":", for which QDir::isAbsolute() would
always return true (QResourceFileEngine::isRelativePath() returns
constant false) and would trip the calculation for local files.
Similarly, IPv6 addresses can start with strings that look like Windows
drives: "a:", "b:", "c:", "d:", "e:" and "f:" (though not today, as
those address blocks are unassigned). Since a valid IPv6 address will
definitely require at least one more colon and Windows file names cannot
contain ':', there's no ambiguity: a valid IPv6 address is never a valid
file on Windows.
This resolves the ambiguity in favor of IPv6 for Unix filenames (which
can contain a colon) and in case of an URL containing scheme, relative
path and no authority ("dead:beef::" for example could have been parsed
as scheme() == "dead" and path() == "beef::").
Task-number: QTBUG-41089
Change-Id: Id9119af1acf8a75a786519af3b48b4ca3dbf3719
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
parent
10a0f93c86
commit
0c32af08a6
|
|
@ -4079,6 +4079,11 @@ static QUrl adjustFtpPath(QUrl url)
|
|||
return url;
|
||||
}
|
||||
|
||||
static bool isIp6(const QString &text)
|
||||
{
|
||||
QIPAddressUtils::IPv6Address address;
|
||||
return !text.isEmpty() && QIPAddressUtils::parseIp6(address, text.begin(), text.end()) == 0;
|
||||
}
|
||||
|
||||
// The following code has the following copyright:
|
||||
/*
|
||||
|
|
@ -4135,8 +4140,18 @@ QUrl QUrl::fromUserInput(const QString &userInput, const QString &workingDirecto
|
|||
if (trimmedString.isEmpty())
|
||||
return QUrl();
|
||||
|
||||
// Check both QUrl::isRelative (to detect full URLs) and QDir::isAbsolutePath (since on Windows drive letters can be interpreted as schemes)
|
||||
|
||||
// Check for IPv6 addresses, since a path starting with ":" is absolute (a resource)
|
||||
// and IPv6 addresses can start with "c:" too
|
||||
if (isIp6(trimmedString)) {
|
||||
QUrl url;
|
||||
url.setHost(trimmedString);
|
||||
url.setScheme(QStringLiteral("http"));
|
||||
return url;
|
||||
}
|
||||
|
||||
QUrl url = QUrl(trimmedString, QUrl::TolerantMode);
|
||||
// Check both QUrl::isRelative (to detect full URLs) and QDir::isAbsolutePath (since on Windows drive letters can be interpreted as schemes)
|
||||
if (url.isRelative() && !QDir::isAbsolutePath(trimmedString)) {
|
||||
QFileInfo fileInfo(QDir(workingDirectory), trimmedString);
|
||||
if ((options & AssumeLocalFile) || fileInfo.exists())
|
||||
|
|
@ -4181,6 +4196,15 @@ QUrl QUrl::fromUserInput(const QString &userInput)
|
|||
{
|
||||
QString trimmedString = userInput.trimmed();
|
||||
|
||||
// Check for IPv6 addresses, since a path starting with ":" is absolute (a resource)
|
||||
// and IPv6 addresses can start with "c:" too
|
||||
if (isIp6(trimmedString)) {
|
||||
QUrl url;
|
||||
url.setHost(trimmedString);
|
||||
url.setScheme(QStringLiteral("http"));
|
||||
return url;
|
||||
}
|
||||
|
||||
// Check first for files, since on Windows drive letters can be interpretted as schemes
|
||||
if (QDir::isAbsolutePath(trimmedString))
|
||||
return QUrl::fromLocalFile(trimmedString);
|
||||
|
|
|
|||
|
|
@ -2871,6 +2871,13 @@ void tst_QUrl::fromUserInput_data()
|
|||
QTest::newRow("add scheme-1") << "www.example.org" << QUrl("http://www.example.org");
|
||||
QTest::newRow("add scheme-2") << "ftp.example.org" << QUrl("ftp://ftp.example.org");
|
||||
QTest::newRow("add scheme-3") << "hostname" << QUrl("http://hostname");
|
||||
QTest::newRow("ipv4-1") << "127.0.0.1" << QUrl("http://127.0.0.1");
|
||||
QTest::newRow("ipv6-0") << "::" << QUrl("http://[::]");
|
||||
QTest::newRow("ipv6-1") << "::1" << QUrl("http://[::1]");
|
||||
QTest::newRow("ipv6-2") << "1::1" << QUrl("http://[1::1]");
|
||||
QTest::newRow("ipv6-3") << "1::" << QUrl("http://[1::]");
|
||||
QTest::newRow("ipv6-4") << "c::" << QUrl("http://[c::]");
|
||||
QTest::newRow("ipv6-5") << "c:f00:ba4::" << QUrl("http://[c:f00:ba4::]");
|
||||
|
||||
// no host
|
||||
QTest::newRow("nohost-1") << "http://" << QUrl("http://");
|
||||
|
|
@ -2949,6 +2956,16 @@ void tst_QUrl::fromUserInputWithCwd_data()
|
|||
#ifdef Q_OS_WIN
|
||||
QTest::newRow("windows-absolute") << "c:/doesnotexist.txt" << QDir::currentPath() << QUrl("file:///c:/doesnotexist.txt") << QUrl("file:///c:/doesnotexist.txt");
|
||||
#endif
|
||||
|
||||
// IPv4 & IPv6
|
||||
// same as fromUserInput, but needs retesting
|
||||
QTest::newRow("ipv4-1") << "127.0.0.1" << QDir::currentPath() << QUrl("http://127.0.0.1") << QUrl::fromLocalFile(QDir::currentPath() + "/127.0.0.1");
|
||||
QTest::newRow("ipv6-0") << "::" << QDir::currentPath() << QUrl("http://[::]") << QUrl("http://[::]");
|
||||
QTest::newRow("ipv6-1") << "::1" << QDir::currentPath() << QUrl("http://[::1]") << QUrl("http://[::1]");
|
||||
QTest::newRow("ipv6-2") << "1::1" << QDir::currentPath() << QUrl("http://[1::1]") << QUrl("http://[1::1]");
|
||||
QTest::newRow("ipv6-3") << "1::" << QDir::currentPath() << QUrl("http://[1::]") << QUrl("http://[1::]");
|
||||
QTest::newRow("ipv6-4") << "c::" << QDir::currentPath() << QUrl("http://[c::]") << QUrl("http://[c::]");
|
||||
QTest::newRow("ipv6-5") << "c:f00:ba4::" << QDir::currentPath() << QUrl("http://[c:f00:ba4::]") << QUrl("http://[c:f00:ba4::]");
|
||||
}
|
||||
|
||||
void tst_QUrl::fromUserInputWithCwd()
|
||||
|
|
|
|||
Loading…
Reference in New Issue