Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow GetType and compare ignoring case #9972

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Build.UnitTests/Evaluation/Expander_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
@$"<Project>
<PropertyGroup>
<foo>aa</foo>
<typeval>$(foo.{methodName}().FullName)</typeval>
</PropertyGroup>
</Project>");
var exception = Should.Throw<InvalidProjectFileException>(() =>
{
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",
@$"<Project>
<PropertyGroup>
<foo>aa</foo>
<typeval>$(foo.{methodName}().FullName)</typeval>
</PropertyGroup>
</Project>");
Should.NotThrow(() =>
{
new ProjectInstance(projectFile.Path);
});
}
}

/// <summary>
/// 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
Expand Down
2 changes: 1 addition & 1 deletion src/Build/Evaluation/Expander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down