Skip to content

Commit

Permalink
feat: add alias name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Nov 24, 2024
1 parent 8b95094 commit 02dffe1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/dotnet-exec/Commands/AliasCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ internal sealed class AliasCommand : Command
{
internal static readonly Argument<string> AliasNameArg = new("aliasName", "Alias Name");
internal static readonly Argument<string> AliasValueArg = new("aliasValue", "Alias Value");

public AliasCommand() : base("alias", "Alias management")
{
AddCommand(new AliasListCommand());
Expand Down
15 changes: 15 additions & 0 deletions src/dotnet-exec/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using WeihanLi.Common.Models;

namespace Exec;
Expand All @@ -39,6 +40,13 @@ public static class Helper

private const string EnableDebugEnvName = "DOTNET_EXEC_DEBUG_ENABLED";
public const string EnableWebReferenceEnvName = "DOTNET_EXEC_WEB_REF_ENABLED";

private static readonly Regex AliasNameRegex = new(@"^\w+[\w\-:\.]*$");
public static bool IsValidAliasName(string aliasName)
{
return aliasName is { Length: > 0 and <= 64 } && AliasNameRegex.IsMatch(aliasName);
}

public static bool DebugModelEnabled(string[] args)
{
if (args.Contains("--debug"))
Expand Down Expand Up @@ -226,6 +234,13 @@ public static void Initialize(this Command command, IServiceProvider serviceProv
"set" => async (InvocationContext context) =>
{
var aliasName = context.ParseResult.GetValueForArgument(AliasCommand.AliasNameArg);

if (IsValidAliasName(aliasName))
{
Console.WriteLine("Invalid alias name, alias name max length is 64 and only allow characters,numbers and `-`/`_`/`:`/`.` ");
return;
}

var aliasValue = context.ParseResult.GetValueForArgument(AliasCommand.AliasValueArg);
if (!appConfiguration.Aliases.TryGetValue(aliasName, out var currentValue) || currentValue == aliasValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ internal sealed class AliasOptionsPreConfigureMiddleware
{
public Task InvokeAsync(ExecOptions context, Func<ExecOptions, Task> next)
{
if (appConfiguration.Aliases.TryGetValue(context.Script, out var aliasValue))
if (Helper.IsValidAliasName(context.Script)
&& appConfiguration.Aliases.TryGetValue(context.Script, out var aliasValue)
)
{
context.Script = aliasValue;
logger.LogDebug("Script replaced to alias value : {AliasValue}", aliasValue);
Expand Down
28 changes: 28 additions & 0 deletions tests/UnitTest/HelperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2022-2024 Weihan Li. All rights reserved.
// Licensed under the Apache license version 2.0 http://www.apache.org/licenses/LICENSE-2.0

namespace UnitTest;
public class HelperTest
{
[Theory]
[InlineData("new-guid")]
[InlineData("new_guid")]
[InlineData("now")]
[InlineData("date")]
[InlineData("test:123")]
[InlineData("test.test123")]
public void AliasNameValidTest(string alias)
{
Assert.True(Helper.IsValidAliasName(alias));
}

[Theory]
[InlineData("")]
[InlineData("https://google.com")]
[InlineData("https://github.com/WeihanLi/dotnet-exec/blob/main/tests/IntegrationTest/CodeSamples/ConfigurationManagerSample.cs")]
[InlineData("dotnet-exec_blob_main_tests_IntegrationTest_CodeSamples_ConfigurationManagerSample.cs")]
public void AliasNameRegexInvalidTest(string alias)
{
Assert.False(Helper.IsValidAliasName(alias));
}
}

0 comments on commit 02dffe1

Please sign in to comment.