Reject invalid network driver options in wslc network create (#40850)
beena352 committed
Jun 18, 2026 at 19:46 UTC
e4c7f827728d13c8096d63a84b99619c87529025
3 files changed
+56
-6
localization/strings/en-US/Resources.resw
+8
@@ -2326,6 +2326,14 @@ For privacy information about this product please visit https://aka.ms/privacy.<
2326
<value>Unsupported network driver: '{}'</value>
2327
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
2328
</data>
2329
+ <data name="MessageWslcInvalidNetworkDriverOption" xml:space="preserve">
2330
+ <value>Unsupported network driver option: '{}'</value>
2331
+ <comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
2332
+ </data>
2333
+ <data name="MessageWslcGatewayRequiresSubnet" xml:space="preserve">
2334
+ <value>Network driver option 'Gateway' requires 'Subnet' to also be specified.</value>
2335
+ <comment>{Locked="Gateway"}{Locked="Subnet"}Command line arguments, file names and string inserts should not be translated</comment>
2336
+ </data>
2337
<data name = "MessageWslcNetworkInUse" xml:space = "preserve" >
2338
<value>Network '{}' has active endpoints.</value>
2339
<comment>{FixedPlaceholder="{}"}Command line arguments, file names and string inserts should not be translated</comment>
src/windows/wslcsession/WSLCSession.cpp
+11
@@ -2363,6 +2363,17 @@ try
2363
auto driverOpts = wslutil::ParseKeyValuePairs(Options->DriverOpts, Options->DriverOptsCount);
2364
auto labels = wslutil::ParseKeyValuePairs(Options->Labels, Options->LabelsCount, WSLCNetworkManagedLabel);
2365
2366
+ static constexpr std::array<std::string_view, 3> c_supportedDriverOpts{"Internal", "Subnet", "Gateway"};
2367
+ for (const auto& [key, _] : driverOpts)
2368
+ {
2369
+ const bool supported = std::any_of(
2370
+ c_supportedDriverOpts.begin(), c_supportedDriverOpts.end(), [&](std::string_view opt) { return key == opt; });
2371
+ THROW_HR_WITH_USER_ERROR_IF(E_INVALIDARG, Localization::MessageWslcInvalidNetworkDriverOption(key), !supported);
2372
+ }
2373
+
2374
+ THROW_HR_WITH_USER_ERROR_IF(
2375
+ E_INVALIDARG, Localization::MessageWslcGatewayRequiresSubnet(), driverOpts.contains("Gateway") && !driverOpts.contains("Subnet"));
2376
+
2377
auto lock = m_lock.lock_shared();
2378
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_dockerClient);
2379
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_INVALID_STATE), !m_virtualMachine);
test/windows/WSLCTests.cpp
+37
-6
@@ -5078,18 +5078,49 @@ class WSLCTests
5078
VERIFY_ARE_EQUAL(networkName, std::string(networks[0].Name));
5079
}
5080
5081
- WSLC_TEST_METHOD(NetworkCreateInvalidDriverTest)
5081
+ WSLC_TEST_METHOD(NetworkCreateInvalidDriverAndOptionTest)
5082
{
5083
WSLCNetworkOptions options{};
5084
- options.Name = "bad-driver-net";
5084
+ options.Name = "bad-network-create-input";
5085
+ options.Driver = "bridge";
5086
options.DriverOpts = nullptr;
5087
options.DriverOptsCount = 0;
5088
5088
- for (const char* driver : {"overlay", "Bridge", ""})
5089
- {
5090
- options.Driver = driver;
5089
+ auto verifyInvalid = [&](PCWSTR expectedMessage) {
5090
VERIFY_ARE_EQUAL(E_INVALIDARG, m_defaultSession->CreateNetwork(&options, nullptr));
5092
- ValidateCOMErrorMessageContains(L"Unsupported network driver:");
5091
+ ValidateCOMErrorMessageContains(expectedMessage);
5092
+ };
5093
+
5094
+ // Invalid drivers (unknown, wrong case, empty)
5095
+ {
5096
+ options.DriverOpts = nullptr;
5097
+ options.DriverOptsCount = 0;
5098
+ for (const char* driver : {"overlay", "Bridge", ""})
5099
+ {
5100
+ options.Driver = driver;
5101
+ verifyInvalid(L"Unsupported network driver:");
5102
+ }
5103
+ }
5104
+
5105
+ // Invalid driver options (wrong case and unknown keys)
5106
+ {
5107
+ options.Driver = "bridge";
5108
+ for (const char* key : {"internal", "subnet", "gateway", "foo"})
5109
+ {
5110
+ WSLCDriverOption opt{key, "true"};
5111
+ options.DriverOpts = &opt;
5112
+ options.DriverOptsCount = 1;
5113
+ verifyInvalid(wsl::shared::string::MultiByteToWide(key).c_str());
5114
+ }
5115
+ }
5116
+
5117
+ // Gateway specified without Subnet
5118
+ {
5119
+ options.Driver = "bridge";
5120
+ WSLCDriverOption opt{"Gateway", "172.44.0.1"};
5121
+ options.DriverOpts = &opt;
5122
+ options.DriverOptsCount = 1;
5123
+ verifyInvalid(L"requires 'Subnet'");
5124
}
5125
}
5126