Skip to content

Commit 569fa8e

Browse files
committed
fix environment variable name validation
1 parent e569784 commit 569fa8e

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
@@ -24,7 +24,10 @@ public static class ContainerHelpers
2424

2525
public const string HostObjectPass = "SDK_CONTAINER_REGISTRY_PWORD";
2626

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

2932
/// <summary>
3033
/// 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)