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

Remove regex use in DataProtection #30855

Merged
merged 5 commits into from
Mar 16, 2021
Merged
Changes from 3 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
25 changes: 22 additions & 3 deletions src/DataProtection/DataProtection/src/TypeForwardingActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

Expand All @@ -13,7 +12,6 @@ internal class TypeForwardingActivator : SimpleActivator
private const string OldNamespace = "Microsoft.AspNet.DataProtection";
private const string CurrentNamespace = "Microsoft.AspNetCore.DataProtection";
private readonly ILogger _logger;
private static readonly Regex _versionPattern = new Regex(@",\s?Version=[0-9]+(\.[0-9]+){0,3}", RegexOptions.Compiled, TimeSpan.FromSeconds(2));

public TypeForwardingActivator(IServiceProvider services)
: this(services, NullLoggerFactory.Instance)
Expand Down Expand Up @@ -64,6 +62,27 @@ internal object CreateInstance(Type expectedBaseType, string originalTypeName, o
}

protected string RemoveVersionFromAssemblyName(string forwardedTypeName)
=> _versionPattern.Replace(forwardedTypeName, "");
{
// Type, Assembly, Version={Version}, Culture={Culture}, PublicKeyToken={Token}

var versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal);
while (versionStartIndex != -1)
pranavkm marked this conversation as resolved.
Show resolved Hide resolved
{
var versionEndIndex = forwardedTypeName.IndexOf(',', versionStartIndex + "Version=".Length);

if (versionEndIndex == -1)
{
// No end index?
return forwardedTypeName;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this return forwardedTypeName.Substring(0, versionStartIndex); or something like that. e.g. Foo, MyAssembly, Verison=3.1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we have any tests or we'd ever expect to hit this condition, should we just throw instead? Its basically invalid data from what expect which is the normal full type string with culture and public key

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Silently falling upwards feels weird

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to throw InvalidOperationException

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I guess I was wrong, that case does exist and has tests:

            var name = "Microsoft.AspNet.DataProtection.TypeForwardingActivatorTests+ClassWithParameterlessCtor, Microsoft.AspNet.DataProtection.Tests, Version=1.0.0.0";

So I guess i'm going to revert this back to the original return, and remove the comment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay tweaked the code to look for the ", Version=", and if no comma is found, it just drops everything after.

}

forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex + 2);
versionStartIndex = forwardedTypeName.IndexOf("Version=", StringComparison.Ordinal);
}

// No version left
return forwardedTypeName;

}
}
}