diff options
author | Tamas Zakor <ztamas@inf.u-szeged.hu> | 2020-05-05 12:25:58 +0200 |
---|---|---|
committer | Tamas Zakor <ztamas@inf.u-szeged.hu> | 2020-05-20 12:33:37 +0200 |
commit | 7b6d15cdf271772695d28bf48f1f41a29dc29e28 (patch) | |
tree | d4b86c7119d3fe231c6025da392933e0b8147c73 /src/core | |
parent | 1fa77712fb241e9c87bf97263df57a5f6f905702 (diff) |
Fix path validation in FilePickerController::accepted()
In some cases QUrl::toLocalFile() does wrong path conversion on
Windows. Therefore we have to convert the file URL to absolute path
in another way and use FilePath::IsAbsolute() for validating that.
Also stabilize WebEngineViewSingleFileUpload tests. They didn't wait
for html title change when the expected value was the same as the
default title.
Change-Id: Ica7798a299f9b28657afeeccccfba3fdecc515e2
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/file_picker_controller.cpp | 72 |
1 files changed, 54 insertions, 18 deletions
diff --git a/src/core/file_picker_controller.cpp b/src/core/file_picker_controller.cpp index 3c81a977c..01a6d0746 100644 --- a/src/core/file_picker_controller.cpp +++ b/src/core/file_picker_controller.cpp @@ -39,9 +39,8 @@ #include "file_picker_controller.h" #include "type_conversion.h" -#if defined(OS_WIN) + #include "base/files/file_path.h" -#endif #include "content/browser/renderer_host/render_view_host_impl.h" #include "content/browser/web_contents/web_contents_impl.h" #include "content/public/browser/file_select_listener.h" @@ -72,27 +71,64 @@ void FilePickerController::accepted(const QStringList &files) for (const QString &urlString : files) { // We accept strings on both absolute-path and file-URL form: - if (QDir::isAbsolutePath(urlString)) { - QString absolutePath = QDir::fromNativeSeparators(urlString); -#if defined(OS_WIN) - if (absolutePath.at(0).isLetter() && absolutePath.at(1) == QLatin1Char(':') && !base::FilePath::IsSeparator(absolutePath.at(2).toLatin1())) - qWarning("Ignoring invalid item in FilePickerController::accepted(QStringList): %s", qPrintable(urlString)); - else + if (toFilePath(urlString).IsAbsolute()) { + stringList.append(urlString); + continue; + } + + if (urlString.startsWith("file:")) { + base::FilePath filePath = toFilePath(urlString).NormalizePathSeparators(); + std::vector<base::FilePath::StringType> pathComponents; + // Splits the file URL into host name, path and file name. + filePath.GetComponents(&pathComponents); + + QString absolutePath; +#if !defined(OS_WIN) + absolutePath = "/"; #endif - stringList.append(absolutePath); - } else { - QUrl url(urlString, QUrl::StrictMode); - if (url.isLocalFile() && QDir::isAbsolutePath(url.toLocalFile())) { - QString absolutePath = url.toLocalFile(); + + QString scheme = toQt(pathComponents[0]); + if (scheme.size() > 5) { #if defined(OS_WIN) - if (absolutePath.at(0).isLetter() && absolutePath.at(1) == QLatin1Char(':') && !base::FilePath::IsSeparator(absolutePath.at(2).toLatin1())) + // There is no slash at the end of the file scheme and it is valid on Windows: file:C:/ + if (scheme.at(5).isLetter() && scheme.at(6) != ':') { + absolutePath += scheme.at(5) + ":/"; + } else { +#endif qWarning("Ignoring invalid item in FilePickerController::accepted(QStringList): %s", qPrintable(urlString)); - else + continue; +#if defined(OS_WIN) + } +#endif + } + + // Non-local file and UNC Path validation: file://path/file + if (base::FilePath::IsSeparator(urlString.at(5).toLatin1()) + && base::FilePath::IsSeparator(urlString.at(6).toLatin1()) + && !base::FilePath::IsSeparator(urlString.at(7).toLatin1())) { +#if defined(OS_WIN) + if (urlString.at(8) != ':' && pathComponents.size() > 2) { + absolutePath += "//"; +#else + if (pathComponents.size() > 2) { + absolutePath += "/"; #endif - stringList.append(absolutePath); - } else - qWarning("Ignoring invalid item in FilePickerController::accepted(QStringList): %s", qPrintable(urlString)); + } else { + qWarning("Ignoring invalid item in FilePickerController::accepted(QStringList): %s", qPrintable(urlString)); + continue; + } + } + + // Build absolute path from file URI componenets. + for (int j = 1; j < pathComponents.size(); j++) + absolutePath += toQt(pathComponents[j]) + (j != pathComponents.size()-1 ? "/" : ""); + + if (toFilePath(absolutePath).IsAbsolute()) { + stringList.append(absolutePath); + continue; + } } + qWarning("Ignoring invalid item in FilePickerController::accepted(QStringList): %s", qPrintable(urlString)); } FilePickerController::filesSelectedInChooser(stringList); |