Skip to content

Commit 327a2ca

Browse files
Copilotadamsitnik
andauthored
Reject nulls in environment variable values
Co-authored-by: adamsitnik <[email protected]>
1 parent 9b8b6db commit 327a2ca

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

‎src/libraries/System.Diagnostics.Process/src/Resources/Strings.resx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@
318318
<data name="Argument_EncodingConversionOverflowChars" xml:space="preserve">
319319
<value>The output char buffer is too small to contain the decoded characters, encoding '{0}' fallback '{1}'.</value>
320320
</data>
321-
<data name="Argument_EnvironmentVariableNameContainsNull" xml:space="preserve">
322-
<value>Environment variable name cannot contain a null character.</value>
321+
<data name="Argument_NullCharInEnvVar" xml:space="preserve">
322+
<value>Environment variable cannot contain a null character.</value>
323323
</data>
324324
<data name="ArgumentOutOfRange_GetByteCountOverflow" xml:space="preserve">
325325
<value>Too many characters. The resulting number of bytes is larger than what can be returned as an int.</value>

‎src/libraries/System.Diagnostics.Process/src/System/Collections/Specialized/DictionaryWrapper.cs‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public string? this[string key]
2020
set
2121
{
2222
ValidateKey(key);
23+
ValidateValue(value);
2324
_contents[key] = value;
2425
}
2526
}
@@ -88,12 +89,19 @@ public bool Remove(KeyValuePair<string, string?> item)
8889

8990
private static void ValidateKey(string key)
9091
{
91-
ArgumentNullException.ThrowIfNull(key);
92+
if (key is not null && key.Contains('\0'))
93+
{
94+
throw new ArgumentException(SR.Argument_NullCharInEnvVar, nameof(key));
95+
}
96+
}
9297

93-
if (key.Contains('\0'))
98+
private static void ValidateValue(string? value)
99+
{
100+
if (value is not null && value.Contains('\0'))
94101
{
95-
throw new ArgumentException(SR.Argument_EnvironmentVariableNameContainsNull, nameof(key));
102+
throw new ArgumentException(SR.Argument_NullCharInEnvVar, nameof(value));
96103
}
97104
}
105+
98106
}
99107
}

‎src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,25 @@ public void TestEnvironmentProperty()
211211
}
212212

213213
[Fact]
214-
public void EnvironmentVariableNameContainingNull_ThrowsArgumentException()
214+
public void EnvironmentVariableContainingNull_ThrowsArgumentException()
215215
{
216-
const string InvalidName = "Name\0Suffix";
216+
const string InvalidKey = "Name\0Suffix";
217+
const string InvalidValue = "Value\0Suffix";
217218
ProcessStartInfo psi = new ProcessStartInfo();
218219
IDictionary environment = (IDictionary)psi.Environment;
219220
ICollection<KeyValuePair<string, string>> environmentCollection = psi.Environment;
220221

221-
AssertExtensions.Throws<ArgumentException>("key", () => psi.Environment[InvalidName] = "value");
222-
AssertExtensions.Throws<ArgumentException>("key", () => environment[InvalidName] = "value");
223-
AssertExtensions.Throws<ArgumentException>("key", () => psi.Environment.Add(InvalidName, "value"));
224-
AssertExtensions.Throws<ArgumentException>("key", () => environmentCollection.Add(new KeyValuePair<string, string>(InvalidName, "value")));
225-
AssertExtensions.Throws<ArgumentException>("key", () => environment.Add(InvalidName, "value"));
222+
AssertExtensions.Throws<ArgumentException>("key", () => psi.Environment[InvalidKey] = "value");
223+
AssertExtensions.Throws<ArgumentException>("key", () => environment[InvalidKey] = "value");
224+
AssertExtensions.Throws<ArgumentException>("key", () => psi.Environment.Add(InvalidKey, "value"));
225+
AssertExtensions.Throws<ArgumentException>("key", () => environmentCollection.Add(new KeyValuePair<string, string>(InvalidKey, "value")));
226+
AssertExtensions.Throws<ArgumentException>("key", () => environment.Add(InvalidKey, "value"));
227+
228+
AssertExtensions.Throws<ArgumentException>("value", () => psi.Environment["key"] = InvalidValue);
229+
AssertExtensions.Throws<ArgumentException>("value", () => environment["key"] = InvalidValue);
230+
AssertExtensions.Throws<ArgumentException>("value", () => psi.Environment.Add("key", InvalidValue));
231+
AssertExtensions.Throws<ArgumentException>("value", () => environmentCollection.Add(new KeyValuePair<string, string>("key", InvalidValue)));
232+
AssertExtensions.Throws<ArgumentException>("value", () => environment.Add("key", InvalidValue));
226233
}
227234

228235
[ConditionalFact(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]

0 commit comments

Comments
 (0)