diff --git a/src/Build.UnitTests/Evaluation/Expander_Tests.cs b/src/Build.UnitTests/Evaluation/Expander_Tests.cs index d52827d46e3..4af483bd8e8 100644 --- a/src/Build.UnitTests/Evaluation/Expander_Tests.cs +++ b/src/Build.UnitTests/Evaluation/Expander_Tests.cs @@ -4978,6 +4978,72 @@ public void ExpandItem_ConvertToStringUsingInvariantCultureForNumberData_Respect } } + [Theory] + [InlineData("getType")] + [InlineData("GetType")] + [InlineData("gettype")] + public void GetTypeMethod_ShouldNotBeAllowed(string methodName) + { + var currentThread = Thread.CurrentThread; + var originalCulture = currentThread.CurrentCulture; + var originalUICulture = currentThread.CurrentUICulture; + var enCultureInfo = new CultureInfo("en"); + + try + { + currentThread.CurrentCulture = enCultureInfo; + currentThread.CurrentUICulture = enCultureInfo; + + using (var env = TestEnvironment.Create()) + { + var root = env.CreateFolder(); + + var projectFile = env.CreateFile(root, ".proj", + @$" + + aa + $(foo.{methodName}().FullName) + + "); + var exception = Should.Throw(() => + { + new ProjectInstance(projectFile.Path); + }); + exception.BaseMessage.ShouldContain($"The function \"{methodName}\" on type \"System.String\" is not available for execution as an MSBuild property function."); + } + } + finally + { + currentThread.CurrentCulture = originalCulture; + currentThread.CurrentUICulture = originalUICulture; + } + } + + [Theory] + [InlineData("getType")] + [InlineData("GetType")] + [InlineData("gettype")] + public void GetTypeMethod_ShouldBeAllowed_EnabledByEnvVariable(string methodName) + { + using (var env = TestEnvironment.Create()) + { + env.SetEnvironmentVariable("MSBUILDENABLEALLPROPERTYFUNCTIONS", "1"); + var root = env.CreateFolder(); + + var projectFile = env.CreateFile(root, ".proj", + @$" + + aa + $(foo.{methodName}().FullName) + +"); + Should.NotThrow(() => + { + new ProjectInstance(projectFile.Path); + }); + } + } + /// /// Determines if ICU mode is enabled. /// Copied from: https://learn.microsoft.com/en-us/dotnet/core/extensions/globalization-icu#determine-if-your-app-is-using-icu diff --git a/src/Build/Evaluation/Expander.cs b/src/Build/Evaluation/Expander.cs index aa897a90926..c8c3d32f072 100644 --- a/src/Build/Evaluation/Expander.cs +++ b/src/Build/Evaluation/Expander.cs @@ -5308,7 +5308,7 @@ private static bool IsInstanceMethodAvailable(string methodName) } // This could be expanded to an allow / deny list. - return methodName != "GetType"; + return !string.Equals("GetType", methodName, StringComparison.OrdinalIgnoreCase); } ///