Skip to content

Commit 1b2a143

Browse files
committed
Add file extension check to service.
1 parent 545eea5 commit 1b2a143

File tree

2 files changed

+39
-29
lines changed

2 files changed

+39
-29
lines changed

src/windows/common/WslClient.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -363,17 +363,6 @@ int ImportDistribution(_In_ std::wstring_view commandLine)
363363
}
364364
else
365365
{
366-
if (WI_IsFlagClear(flags, LXSS_IMPORT_DISTRO_FLAGS_VHD))
367-
{
368-
// Fail if expecting a tar, but the file name has the .vhd or .vhdx extension.
369-
if (wsl::windows::common::string::IsPathComponentEqual(filePath.extension().native(), wsl::windows::common::wslutil::c_vhdFileExtension) ||
370-
wsl::windows::common::string::IsPathComponentEqual(filePath.extension().native(), wsl::windows::common::wslutil::c_vhdxFileExtension))
371-
{
372-
wsl::windows::common::wslutil::PrintMessage(wsl::shared::Localization::MessagePassVhdFlag());
373-
return -1;
374-
}
375-
}
376-
377366
file.reset(CreateFileW(
378367
filePath.c_str(), GENERIC_READ, (FILE_SHARE_READ | FILE_SHARE_DELETE), nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
379368

src/windows/service/exe/LxssUserSession.cpp

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,15 +1081,18 @@ HRESULT LxssUserSessionImpl::ExportDistribution(_In_opt_ LPCGUID DistroGuid, _In
10811081
auto runAsUser = wil::impersonate_token(userToken.get());
10821082

10831083
// Ensure the target file has the correct file extension.
1084-
std::wstring exportPath;
1085-
THROW_IF_FAILED(wil::GetFinalPathNameByHandleW(FileHandle, exportPath));
1086-
1087-
const auto sourceFileExtension = configuration.VhdFilePath.extension().native();
1088-
const auto targetFileExtension = std::filesystem::path(std::move(exportPath)).extension().native();
1089-
if (!wsl::windows::common::string::IsPathComponentEqual(sourceFileExtension, targetFileExtension))
1084+
if (GetFileType(FileHandle) == FILE_TYPE_DISK)
10901085
{
1091-
THROW_HR_WITH_USER_ERROR(
1092-
WSL_E_EXPORT_FAILED, wsl::shared::Localization::MessageRequiresFileExtension(sourceFileExtension.c_str()));
1086+
std::wstring exportPath;
1087+
THROW_IF_FAILED(wil::GetFinalPathNameByHandleW(FileHandle, exportPath));
1088+
1089+
const auto sourceFileExtension = configuration.VhdFilePath.extension().native();
1090+
const auto targetFileExtension = std::filesystem::path(std::move(exportPath)).extension().native();
1091+
if (!wsl::windows::common::string::IsPathComponentEqual(sourceFileExtension, targetFileExtension))
1092+
{
1093+
THROW_HR_WITH_USER_ERROR(
1094+
WSL_E_EXPORT_FAILED, wsl::shared::Localization::MessageRequiresFileExtension(sourceFileExtension.c_str()));
1095+
}
10931096
}
10941097

10951098
const wil::unique_hfile vhdFile(CreateFileW(
@@ -1461,24 +1464,42 @@ HRESULT LxssUserSessionImpl::RegisterDistribution(
14611464
wil::CreateDirectoryDeep(distributionPath.c_str());
14621465
}
14631466

1464-
// If importing a vhd, determine if it is a .vhd or .vhdx.
14651467
std::wstring vhdName{LXSS_VM_MODE_VHD_NAME};
1466-
if (WI_IsFlagSet(Flags, LXSS_IMPORT_DISTRO_FLAGS_VHD))
1468+
if (GetFileType(FileHandle) == FILE_TYPE_DISK)
14671469
{
14681470
std::wstring pathBuffer;
14691471
THROW_IF_FAILED(wil::GetFinalPathNameByHandleW(FileHandle, pathBuffer));
14701472

1471-
std::filesystem::path vhdPath{std::move(pathBuffer)};
1473+
std::filesystem::path filePath{std::move(pathBuffer)};
14721474

1473-
using namespace wsl::windows::common::wslutil;
1474-
if (!wsl::windows::common::string::IsPathComponentEqual(vhdPath.extension().native(), c_vhdFileExtension) &&
1475-
!wsl::windows::common::string::IsPathComponentEqual(vhdPath.extension().native(), c_vhdxFileExtension))
1475+
// If importing a vhd, determine if it is a .vhd or .vhdx.
1476+
if (WI_IsFlagSet(Flags, LXSS_IMPORT_DISTRO_FLAGS_VHD))
14761477
{
1477-
THROW_HR_WITH_USER_ERROR(
1478-
WSL_E_IMPORT_FAILED, wsl::shared::Localization::MessageRequiresFileExtensions(c_vhdFileExtension, c_vhdxFileExtension));
1479-
}
1478+
std::wstring pathBuffer;
1479+
THROW_IF_FAILED(wil::GetFinalPathNameByHandleW(FileHandle, pathBuffer));
1480+
1481+
std::filesystem::path vhdPath{std::move(pathBuffer)};
1482+
1483+
using namespace wsl::windows::common::wslutil;
1484+
if (!wsl::windows::common::string::IsPathComponentEqual(filePath.extension().native(), c_vhdFileExtension) &&
1485+
!wsl::windows::common::string::IsPathComponentEqual(filePath.extension().native(), c_vhdxFileExtension))
1486+
{
1487+
THROW_HR_WITH_USER_ERROR(
1488+
WSL_E_IMPORT_FAILED,
1489+
wsl::shared::Localization::MessageRequiresFileExtensions(c_vhdFileExtension, c_vhdxFileExtension));
1490+
}
14801491

1481-
vhdName = vhdPath.filename();
1492+
vhdName = filePath.filename();
1493+
}
1494+
else
1495+
{
1496+
// Fail if expecting a tar, but the file name has the .vhd or .vhdx extension.
1497+
if (wsl::windows::common::string::IsPathComponentEqual(filePath.extension().native(), wsl::windows::common::wslutil::c_vhdFileExtension) ||
1498+
wsl::windows::common::string::IsPathComponentEqual(filePath.extension().native(), wsl::windows::common::wslutil::c_vhdxFileExtension))
1499+
{
1500+
THROW_HR_WITH_USER_ERROR(WSL_E_IMPORT_FAILED, wsl::shared::Localization::MessagePassVhdFlag());
1501+
}
1502+
}
14821503
}
14831504

14841505
registration = DistributionRegistration::Create(

0 commit comments

Comments
 (0)