Skip to content

Commit bb6aacf

Browse files
ggarzia-MSFTCopilot
andcommitted
Reject terminal stdin for wslc image build
Co-authored-by: Copilot <[email protected]>
1 parent 5b71754 commit bb6aacf

4 files changed

Lines changed: 30 additions & 80 deletions

File tree

‎localization/strings/en-US/Resources.resw‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,10 @@ On first run, creates the file with all settings commented out at their defaults
38603860
<data name="WSLCCLI_ImageSaveStdoutIsTerminalError" xml:space="preserve">
38613861
<value>Cannot write image to terminal. Use the -o option or redirect stdout.</value>
38623862
</data>
3863+
<data name="WSLCCLI_BuildStdinIsTerminalError" xml:space="preserve">
3864+
<value>Cannot read build input from terminal. Pipe a Dockerfile or tar archive to stdin.</value>
3865+
<comment>{Locked="Dockerfile"}{Locked="stdin"}Command line arguments, file names and string inserts should not be translated</comment>
3866+
</data>
38633867
<data name="WSLCCLI_ContainerExportStdoutIsTerminalError" xml:space="preserve">
38643868
<value>Cannot export container to terminal. Use the -o option or redirect stdout.</value>
38653869
</data>

‎src/windows/service/inc/wslc.idl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,9 @@ typedef struct _WSLCBuildImageOptions
616616
[unique, string] LPCWSTR OutputMountPath; // When set, the server mounts this Windows directory read-write into the VM and points the exporter's dest at it (plus OutputMountFile). Used for single-file exporters with a real destination; mutually exclusive with OutputHandle.
617617
[unique, string] LPCWSTR OutputMountFile; // Leaf filename within OutputMountPath that a single-file exporter writes to.
618618
[unique, string] LPCWSTR IidFilePath; // Absolute path of the client's --iidfile destination. The server mounts its parent directory read-write into the VM and passes --iidfile pointing at it, so buildx writes the image ID straight to the destination.
619-
WSLCHandle ContextHandle; // When Type != WSLCHandleTypeUnknown, the server relays this stream to docker's stdin as the build context ("-"). The stream is either a tar archive (optionally compressed) or a Dockerfile built with an empty context.
620-
[unique, string] LPCSTR DockerfilePath; // Dockerfile path inside the streamed context, passed as --file. Only valid with ContextHandle.
621-
} WSLCBuildImageOptions;
619+
WSLCHandle ContextHandle; // When Type != WSLCHandleTypeUnknown, the server relays this stream to docker's stdin as the build context ("-"). The stream is either a tar archive (optionally compressed) or a Dockerfile built with an empty context.
620+
[unique, string] LPCSTR DockerfilePath; // Dockerfile path inside the streamed context, passed as --file. Only valid with ContextHandle.
621+
} WSLCBuildImageOptions;
622622

623623
typedef struct _WSLCTagImageOptions
624624
{

‎src/windows/wslc/services/ImageService.cpp‎

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -116,78 +116,24 @@ struct BuildContextInput
116116
HANDLE ContextHandle = nullptr;
117117
HANDLE DockerfileHandle = nullptr;
118118
wil::unique_hfile Dockerfile;
119-
std::optional<wsl::windows::common::filesystem::TempFile> ConsoleInput;
119+
std::optional<wsl::windows::common::filesystem::TempFile> SpooledStdin;
120120
std::string DockerfilePath;
121121
};
122122

123-
// Reads typed console input until EOF (Ctrl+Z at the start of a line) and spools it as UTF-8 to a
124-
// temporary file, since a console handle cannot be relayed to the service.
125-
wsl::windows::common::filesystem::TempFile SpoolConsoleInput(HANDLE console)
126-
{
127-
std::wstring text;
128-
std::array<wchar_t, 1024> buffer{};
129-
for (;;)
130-
{
131-
DWORD read{};
132-
THROW_IF_WIN32_BOOL_FALSE(ReadConsoleW(console, buffer.data(), static_cast<DWORD>(buffer.size()), &read, nullptr));
133-
if (read == 0)
134-
{
135-
break;
136-
}
137-
138-
DWORD length = read;
139-
for (DWORD i = 0; i < read; ++i)
140-
{
141-
const bool atLineStart = i == 0 ? text.empty() || text.back() == L'\n' : buffer[i - 1] == L'\n';
142-
if (atLineStart && buffer[i] == L'\x1a')
143-
{
144-
length = i;
145-
break;
146-
}
147-
}
148-
149-
text.append(buffer.data(), length);
150-
if (length != read)
151-
{
152-
break;
153-
}
154-
}
155-
156-
std::erase(text, L'\r');
157-
const auto utf8 = wsl::windows::common::string::WideToMultiByte(text);
158-
159-
wsl::windows::common::filesystem::TempFile file(
160-
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS, wsl::windows::common::filesystem::TempFileFlags::DeleteOnClose);
161-
162-
size_t offset = 0;
163-
while (offset < utf8.size())
164-
{
165-
DWORD written{};
166-
const auto length = static_cast<DWORD>(std::min(utf8.size() - offset, static_cast<size_t>(MAXDWORD)));
167-
THROW_IF_WIN32_BOOL_FALSE(WriteFile(file.Handle.get(), utf8.data() + offset, length, &written, nullptr));
168-
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_WRITE_FAULT), written == 0);
169-
offset += written;
170-
}
171-
THROW_LAST_ERROR_IF(SetFilePointer(file.Handle.get(), 0, nullptr, FILE_BEGIN) == INVALID_SET_FILE_POINTER);
172-
return file;
173-
}
174-
175123
// Returns a relayable stdin handle. Character devices need a file because COM cannot relay their handles.
176124
HANDLE OpenStdin(BuildContextInput& input)
177125
{
178126
auto handle = GetStdHandle(STD_INPUT_HANDLE);
179-
if (IsConsoleHandle(handle))
180-
{
181-
input.ConsoleInput.emplace(SpoolConsoleInput(handle));
182-
return input.ConsoleInput->Handle.get();
183-
}
127+
128+
// Intentional divergence from docker, which accepts a Dockerfile typed into an interactive terminal.
129+
THROW_HR_WITH_USER_ERROR_IF(E_INVALIDARG, Localization::WSLCCLI_BuildStdinIsTerminalError(), IsConsoleHandle(handle));
184130

185131
if (GetFileType(handle) != FILE_TYPE_CHAR)
186132
{
187133
return handle;
188134
}
189135

190-
input.ConsoleInput.emplace(
136+
input.SpooledStdin.emplace(
191137
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS, wsl::windows::common::filesystem::TempFileFlags::DeleteOnClose);
192138
std::array<char, 4096> buffer{};
193139
for (;;)
@@ -211,12 +157,12 @@ HANDLE OpenStdin(BuildContextInput& input)
211157
}
212158

213159
DWORD written{};
214-
THROW_IF_WIN32_BOOL_FALSE(WriteFile(input.ConsoleInput->Handle.get(), buffer.data(), read, &written, nullptr));
160+
THROW_IF_WIN32_BOOL_FALSE(WriteFile(input.SpooledStdin->Handle.get(), buffer.data(), read, &written, nullptr));
215161
THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_WRITE_FAULT), written != read);
216162
}
217163

218-
THROW_LAST_ERROR_IF(SetFilePointer(input.ConsoleInput->Handle.get(), 0, nullptr, FILE_BEGIN) == INVALID_SET_FILE_POINTER);
219-
return input.ConsoleInput->Handle.get();
164+
THROW_LAST_ERROR_IF(SetFilePointer(input.SpooledStdin->Handle.get(), 0, nullptr, FILE_BEGIN) == INVALID_SET_FILE_POINTER);
165+
return input.SpooledStdin->Handle.get();
220166
}
221167

222168
BuildContextInput OpenBuildContext(const std::wstring& contextPath, const std::wstring& dockerfilePath)

‎test/windows/wslc/e2e/WSLCE2EImageBuildTests.cpp‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,22 +1562,23 @@ class WSLCE2EImageBuildTests
15621562
VERIFY_ARE_EQUAL(BuiltImageStdinDockerfile.NameAndTag(), wsl::shared::string::MultiByteToWide(inspectData.RepoTags.value()[0]));
15631563
}
15641564

1565-
WSLC_TEST_METHOD(WSLCE2E_Image_Build_StdinDockerfile_PseudoConsole_Success)
1565+
WSLC_TEST_METHOD(WSLCE2E_Image_Build_StdinContext_Terminal_Fails)
15661566
{
1567-
auto imageCleanup = DeleteImageOnExit(BuiltImageStdinConsole);
1568-
auto session = RunWslcInteractive(
1569-
std::format(L"build - -t {} --progress=quiet", BuiltImageStdinConsole.NameAndTag()), ElevationType::Elevated, PseudoConsole{120, 30});
1570-
session.Write("FROM debian:latest\r\nLABEL typed=\"caf\xc3\xa9\"\r\n\x1a\r\n");
1571-
const auto exitCode = session.Wait();
1572-
VERIFY_ARE_EQUAL(0, exitCode, string::MultiByteToWide(session.GetStdoutData()).c_str());
1567+
// A pseudo console gives wslc a console stdin, which build must reject.
1568+
auto session = RunWslcInteractive(L"build - --progress=quiet", ElevationType::Elevated, PseudoConsole{200, 50});
1569+
WaitForPseudoConsoleOutput(session, string::WideToMultiByte(Localization::WSLCCLI_BuildStdinIsTerminalError()));
1570+
VERIFY_ARE_EQUAL(1, session.Wait());
1571+
}
15731572

1574-
auto inspectData = InspectImage(BuiltImageStdinConsole.NameAndTag());
1575-
VERIFY_IS_TRUE(inspectData.Config.has_value());
1576-
VERIFY_IS_TRUE(inspectData.Config.value().Labels.has_value());
1577-
const auto& labels = inspectData.Config.value().Labels.value();
1578-
const auto label = labels.find("typed");
1579-
VERIFY_IS_TRUE(label != labels.end());
1580-
VERIFY_ARE_EQUAL(std::string("caf\xc3\xa9"), label->second);
1573+
WSLC_TEST_METHOD(WSLCE2E_Image_Build_StdinDockerfile_Terminal_Fails)
1574+
{
1575+
auto testRoot = std::filesystem::current_path() / L"wslc-e2e-build-stdin-dockerfile-terminal";
1576+
auto cleanup = SetupTestDirectory(testRoot);
1577+
1578+
auto session = RunWslcInteractive(
1579+
std::format(L"build \"{}\" -f - --progress=quiet", testRoot.wstring()), ElevationType::Elevated, PseudoConsole{200, 50});
1580+
WaitForPseudoConsoleOutput(session, string::WideToMultiByte(Localization::WSLCCLI_BuildStdinIsTerminalError()));
1581+
VERIFY_ARE_EQUAL(1, session.Wait());
15811582
}
15821583

15831584
WSLC_TEST_METHOD(WSLCE2E_Image_Build_StdinNul_ReachesBuilder)
@@ -1637,7 +1638,6 @@ class WSLCE2EImageBuildTests
16371638
const TestImage BuiltImageStdinGzip{L"wslc-e2e-build-stdin-gzip", L"latest", L""};
16381639
const TestImage BuiltImageStdinTarFile{L"wslc-e2e-build-stdin-tar-file", L"latest", L""};
16391640
const TestImage BuiltImageStdinDockerfile{L"wslc-e2e-build-stdin-dockerfile", L"latest", L""};
1640-
const TestImage BuiltImageStdinConsole{L"wslc-e2e-build-stdin-console", L"latest", L""};
16411641

16421642
// Archives a context holding a Dockerfile (at dockerfileInContext) and a marker file, streams it to
16431643
// `wslc build -` over stdin, and verifies the build could COPY the marker out of the streamed context.

0 commit comments

Comments
 (0)