From c3f59f8b128e146ee8e9e79042ad592d5c938612 Mon Sep 17 00:00:00 2001 From: Blue Date: Fri, 13 Jun 2025 17:21:28 -0700 Subject: [PATCH] Don't try to create invalid file names if the distribution download url contains parameters (#13109) --- src/windows/common/Distribution.cpp | 3 ++- src/windows/common/WslInstall.cpp | 2 +- src/windows/common/wslutil.h | 2 +- test/windows/UnitTests.cpp | 40 +++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/windows/common/Distribution.cpp b/src/windows/common/Distribution.cpp index 368dd18..bb57973 100644 --- a/src/windows/common/Distribution.cpp +++ b/src/windows/common/Distribution.cpp @@ -285,7 +285,8 @@ void wsl::windows::common::distribution::LegacyInstallViaGithub(const Distributi wslutil::PrintMessage(Localization::MessageDownloading(distro.FriendlyName.c_str()), stdout); - const auto downloadPath = wslutil::DownloadFile(*downloadUrl); + // Note: The appx extensions is required for the installation to succeed. + const auto downloadPath = wslutil::DownloadFile(*downloadUrl, distro.Name + L".appx"); auto deleteFile = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&] { THROW_IF_WIN32_BOOL_FALSE(DeleteFileW(downloadPath.c_str())); }); diff --git a/src/windows/common/WslInstall.cpp b/src/windows/common/WslInstall.cpp index bfe9e5e..e4d426a 100644 --- a/src/windows/common/WslInstall.cpp +++ b/src/windows/common/WslInstall.cpp @@ -300,7 +300,7 @@ std::pair WslInstall::InstallModernDistribution( else { PrintMessage(Localization::MessageDownloading(distribution.FriendlyName.c_str()), stdout); - installPath = DownloadFile(downloadInfo->Url); + installPath = DownloadFile(downloadInfo->Url, distribution.Name + L".wsl"); fileDownloaded = true; } diff --git a/src/windows/common/wslutil.h b/src/windows/common/wslutil.h index 5898974..b1ac821 100644 --- a/src/windows/common/wslutil.h +++ b/src/windows/common/wslutil.h @@ -82,7 +82,7 @@ std::wstring ConstructPipePath(_In_ std::wstring_view PipeName); GUID CreateV5Uuid(const GUID& namespaceGuid, const std::span name); -std::wstring DownloadFile(std::wstring_view Url, std::wstring Filename = L""); +std::wstring DownloadFile(std::wstring_view Url, std::wstring Filename); [[nodiscard]] HANDLE DuplicateHandleFromCallingProcess(_In_ HANDLE handleInTarget); diff --git a/test/windows/UnitTests.cpp b/test/windows/UnitTests.cpp index fe6cbe6..f4e2c4a 100644 --- a/test/windows/UnitTests.cpp +++ b/test/windows/UnitTests.cpp @@ -5269,6 +5269,46 @@ Error code: Wsl/InstallDistro/E_UNEXPECTED\r\n", Error code: Wsl/InstallDistro/WSL_E_INVALID_JSON\r\n", L""); } + + // Validate that url parameters are correctly handled. + { + constexpr auto tarEndpoint = L"http://127.0.0.1:6667/"; + + UniqueWebServer fileServer(tarEndpoint, std::filesystem::path(g_testDistroPath)); + + wil::unique_handle tarHandle{CreateFile(g_testDistroPath.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr)}; + VERIFY_IS_TRUE(!!tarHandle); + + auto manifest = std::format( + R"({{ + "ModernDistributions": {{ + "test": [ + {{ + "Name": "test-url-download", + "FriendlyName": "FriendlyName", + "Default": true, + "Amd64Url": {{ + "Url": "{}/distro.tar?foo=bar&key=value", + "Sha256": "{}" + }} + }} + ] + }}}})", + tarEndpoint, + tarHash); + + auto restore = SetManifest(manifest); + + auto cleanup = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, []() { UnregisterDistribution(L"test-url-download"); }); + + auto [output, error] = LxsstuLaunchWslAndCaptureOutput(L"--install --no-launch test-url-download"); + VERIFY_ARE_EQUAL( + output, + L"Downloading: FriendlyName\r\nInstalling: FriendlyName\r\nDistribution successfully installed. It can be " + L"launched via 'wsl.exe -d test-url-download'\r\n"); + + VERIFY_ARE_EQUAL(error, L""); + } } TEST_METHOD(ModernInstallEndToEnd)