Skip to content

Commit 43a1d69

Browse files
authored
Merge pull request #291 from dotnet/fix-env-diagnostics
Fix environment variable name validation
2 parents 36295f2 + 569fa8e commit 43a1d69

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Microsoft.NET.Build.Containers/ContainerHelpers.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public static class ContainerHelpers
2525

2626
public const string HostObjectPass = "SDK_CONTAINER_REGISTRY_PWORD";
2727

28-
private static Regex envVarRegex = new Regex(@"^[a-zA-Z_]+$");
28+
/// <summary>
29+
/// Matches an environment variable name - must start with a letter or underscore, and can only contain letters, numbers, and underscores.
30+
/// </summary>
31+
private static Regex envVarRegex = new Regex(@"^[a-zA-Z_]{1,}[a-zA-Z0-9_]*$");
2932

3033
/// <summary>
3134
/// DefaultRegistry is the canonical representation of something that lives in the local docker daemon. It's used as the inferred registry for repositories

Test.Microsoft.NET.Build.Containers/ContainerHelpersTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,18 @@ public void CanParsePort(string input, bool shouldParse, int? expectedPortNumber
9797
Assert.AreEqual(expectedError, errors);
9898
}
9999
}
100+
101+
[TestMethod]
102+
[DataRow("FOO", true)]
103+
[DataRow("foo_bar", true)]
104+
[DataRow("foo-bar", false)]
105+
[DataRow("foo.bar", false)]
106+
[DataRow("foo bar", false)]
107+
[DataRow("1_NAME", false)]
108+
[DataRow("ASPNETCORE_URLS", true)]
109+
[DataRow("ASPNETCORE_URLS2", true)]
110+
public void CanRecognizeEnvironmentVariableNames(string envVarName, bool isValid) {
111+
var success = ContainerHelpers.IsValidEnvironmentVariable(envVarName);
112+
Assert.AreEqual(isValid, success, $"Expected {envVarName} to be {(isValid ? "valid" : "invalid")}");
113+
}
100114
}

0 commit comments

Comments
 (0)