Skip to content

Commit d42fd4a

Browse files
M293/backport 5852 (#2406)
* set env in ProcessInvoker sanitized (#2280) * set env in ProcessInvoker sanitized * Update release notes and runnerversion --------- Co-authored-by: Stefan Ruvceski <96768603+ruvceskistefan@users.noreply.github.com>
1 parent a78e6e0 commit d42fd4a

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

releaseNote.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## Features
22
## Bugs
33
- Fixed an issue where container environment variables names or values could escape the docker command (#2108)
4+
- Sanitize Windows ENVs (#2280)
45

56
## Misc
67

src/Runner.Sdk/ProcessInvoker.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,17 @@ public async Task<int> ExecuteAsync(
264264
{
265265
foreach (KeyValuePair<string, string> kvp in environment)
266266
{
267+
#if OS_WINDOWS
268+
string tempKey = String.IsNullOrWhiteSpace(kvp.Key) ? kvp.Key : kvp.Key.Split('\0')[0];
269+
string tempValue = String.IsNullOrWhiteSpace(kvp.Value) ? kvp.Value : kvp.Value.Split('\0')[0];
270+
if(!String.IsNullOrWhiteSpace(tempKey))
271+
{
272+
_proc.StartInfo.Environment[tempKey] = tempValue;
273+
}
274+
#else
267275
_proc.StartInfo.Environment[kvp.Key] = kvp.Value;
276+
277+
#endif
268278
}
269279
}
270280

src/Test/L0/ProcessInvokerL0.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,76 @@ public async Task SetCIEnv()
129129
}
130130
}
131131
}
132+
#if OS_WINDOWS
133+
[Fact]
134+
[Trait("Level", "L0")]
135+
[Trait("Category", "Common")]
136+
public async Task SetTestEnvWithNullInKey()
137+
{
138+
using (TestHostContext hc = new(this))
139+
{
140+
Tracing trace = hc.GetTrace();
141+
142+
Int32 exitCode = -1;
143+
var processInvoker = new ProcessInvokerWrapper();
144+
processInvoker.Initialize(hc);
145+
var stdout = new List<string>();
146+
var stderr = new List<string>();
147+
processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
148+
{
149+
trace.Info(e.Data);
150+
stdout.Add(e.Data);
151+
};
152+
processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
153+
{
154+
trace.Info(e.Data);
155+
stderr.Add(e.Data);
156+
};
157+
158+
exitCode = await processInvoker.ExecuteAsync("", "cmd.exe", "/c \"echo %TEST%\"", new Dictionary<string, string>() { { "TEST\0second", "first" } }, CancellationToken.None);
159+
160+
161+
trace.Info("Exit Code: {0}", exitCode);
162+
Assert.Equal(0, exitCode);
163+
Assert.Equal("first", stdout.First(x => !string.IsNullOrWhiteSpace(x)));
164+
165+
}
166+
}
132167

168+
[Fact]
169+
[Trait("Level", "L0")]
170+
[Trait("Category", "Common")]
171+
public async Task SetTestEnvWithNullInValue()
172+
{
173+
using (TestHostContext hc = new(this))
174+
{
175+
Tracing trace = hc.GetTrace();
176+
177+
Int32 exitCode = -1;
178+
var processInvoker = new ProcessInvokerWrapper();
179+
processInvoker.Initialize(hc);
180+
var stdout = new List<string>();
181+
var stderr = new List<string>();
182+
processInvoker.OutputDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
183+
{
184+
trace.Info(e.Data);
185+
stdout.Add(e.Data);
186+
};
187+
processInvoker.ErrorDataReceived += (object sender, ProcessDataReceivedEventArgs e) =>
188+
{
189+
trace.Info(e.Data);
190+
stderr.Add(e.Data);
191+
};
192+
193+
exitCode = await processInvoker.ExecuteAsync("", "cmd.exe", "/c \"echo %TEST%\"", new Dictionary<string, string>() { { "TEST", "first\0second" } }, CancellationToken.None);
194+
195+
trace.Info("Exit Code: {0}", exitCode);
196+
Assert.Equal(0, exitCode);
197+
Assert.Equal("first", stdout.First(x => !string.IsNullOrWhiteSpace(x)));
198+
199+
}
200+
}
201+
#endif
133202
[Fact]
134203
[Trait("Level", "L0")]
135204
[Trait("Category", "Common")]

src/runnerversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.293.1
1+
2.293.2

0 commit comments

Comments
 (0)