From b55666deaf3a8bbf85747f0f18b9f774b423af47 Mon Sep 17 00:00:00 2001 From: Clemens Vasters Date: Thu, 29 Aug 2024 15:32:29 +0200 Subject: [PATCH] Addresses issue #8, allowing "." as a separator inside a relay name (#93) --- Microsoft.Azure.Relay.Bridge.sln | 1 + .../Configuration/LocalForward.cs | 2 +- .../Configuration/LocalForwardBinding.cs | 4 +- .../Configuration/RemoteForward.cs | 2 +- .../Configuration/RemoteForwardBinding.cs | 4 +- src/Microsoft.Azure.Relay.Bridge/Constants.cs | 6 ++- .../ConfigTest.cs | 50 +++++++++++++++++++ 7 files changed, 61 insertions(+), 8 deletions(-) diff --git a/Microsoft.Azure.Relay.Bridge.sln b/Microsoft.Azure.Relay.Bridge.sln index 73f40e9..ae1ad0c 100644 --- a/Microsoft.Azure.Relay.Bridge.sln +++ b/Microsoft.Azure.Relay.Bridge.sln @@ -22,6 +22,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution global.json = global.json LICENSE.txt = LICENSE.txt NuGetPackageVerifier.json = NuGetPackageVerifier.json + OVERVIEW.md = OVERVIEW.md package-all.cmd = package-all.cmd package.cmd = package.cmd package.sh = package.sh diff --git a/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForward.cs b/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForward.cs index 7a77338..05bb5c6 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForward.cs +++ b/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForward.cs @@ -167,7 +167,7 @@ public string RelayName { var val = value != null ? value.Trim('\'', '\"') : value; if (val != null && - !new Regex("^[0-9A-Za-z_-]+$").Match(val).Success) + !Constants.RelayNameRegex.Match(val).Success) { throw BridgeEventSource.Log.ArgumentOutOfRange( nameof(RelayName), diff --git a/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForwardBinding.cs b/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForwardBinding.cs index de849f5..f360ecf 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForwardBinding.cs +++ b/src/Microsoft.Azure.Relay.Bridge/Configuration/LocalForwardBinding.cs @@ -160,7 +160,7 @@ public string PortName { var val = value != null ? value.Trim('\'', '\"') : value; if (val != null && - !new Regex("^[0-9A-Za-z_-]+$").Match(val).Success) + !Constants.RelayNameRegex.Match(val).Success) { throw BridgeEventSource.Log.ArgumentOutOfRange( nameof(PortName), @@ -180,7 +180,7 @@ public string BindLocalSocket Uri path; if (val != null && !Uri.TryCreate(val, UriKind.Absolute, out path) && - !new Regex("^[0-9A-Za-z_-]+$").Match(val).Success) + !Constants.RelayNameRegex.Match(val).Success) { throw BridgeEventSource.Log.ArgumentOutOfRange( nameof(BindLocalSocket), diff --git a/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForward.cs b/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForward.cs index bdda35b..373d7f2 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForward.cs +++ b/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForward.cs @@ -57,7 +57,7 @@ public string RelayName { var val = value != null ? value.Trim('\'', '\"') : value; if (val != null && - !new Regex("^[0-9A-Za-z/_-]+$").Match(val).Success) + !Constants.RelayNameRegex.Match(val).Success) { throw BridgeEventSource.Log.ArgumentOutOfRange( nameof(RelayName), diff --git a/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForwardBinding.cs b/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForwardBinding.cs index 54a9ab6..92f38b9 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForwardBinding.cs +++ b/src/Microsoft.Azure.Relay.Bridge/Configuration/RemoteForwardBinding.cs @@ -66,7 +66,7 @@ public string PortName { var val = value != null ? value.Trim('\'', '\"') : value; if (val != null && - !new Regex("^[0-9A-Za-z_-]+$").Match(val).Success) + !Constants.RelayNameRegex.Match(val).Success) { throw BridgeEventSource.Log.ArgumentOutOfRange( nameof(PortName), @@ -86,7 +86,7 @@ public string LocalSocket Uri path; if (val != null && !Uri.TryCreate(val, UriKind.Absolute, out path) && - !new Regex("^[0-9A-Za-z_-]+$").Match(val).Success) + !Constants.RelayNameRegex.Match(val).Success) { throw BridgeEventSource.Log.ArgumentOutOfRange( nameof(LocalSocket), diff --git a/src/Microsoft.Azure.Relay.Bridge/Constants.cs b/src/Microsoft.Azure.Relay.Bridge/Constants.cs index cc738a6..a0d8ae9 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Constants.cs +++ b/src/Microsoft.Azure.Relay.Bridge/Constants.cs @@ -1,10 +1,12 @@ // // Copyright (c) Microsoft. All rights reserved. // // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Text.RegularExpressions; + namespace Microsoft.Azure.Relay.Bridge { - static class Constants + public static class Constants { - + public static Regex RelayNameRegex = new Regex(@"^[0-9A-Za-z/_\-\.]+$", RegexOptions.Compiled); } } \ No newline at end of file diff --git a/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs b/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs index bc33a87..17e8db6 100644 --- a/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs +++ b/test/unit/Microsoft.Azure.Relay.Bridge.Tests/ConfigTest.cs @@ -279,6 +279,56 @@ public void CommandLineBadLocalForwardTest() Assert.Contains("BindAddress", ex2.Message); } + [Fact] + public void CommandLineGoodRemoteForwardTest() + { + CommandLineSettings.Run(new string[] { "-R", "foobar:3000" }, + (settings) => + { + var cfg = Config.LoadConfig(settings); + Assert.Single(cfg.RemoteForward); + Assert.Equal("foobar", cfg.RemoteForward[0].RelayName); + Assert.Equal(3000, cfg.RemoteForward[0].HostPort); + return 0; + }); + CommandLineSettings.Run(new string[] { "-R", "foobar_foobar:3000" }, + (settings) => + { + var cfg = Config.LoadConfig(settings); + Assert.Single(cfg.RemoteForward); + Assert.Equal("foobar_foobar", cfg.RemoteForward[0].RelayName); + Assert.Equal(3000, cfg.RemoteForward[0].HostPort); + return 0; + }); + CommandLineSettings.Run(new string[] { "-R", "foobar/foobar:3000" }, + (settings) => + { + var cfg = Config.LoadConfig(settings); + Assert.Single(cfg.RemoteForward); + Assert.Equal("foobar/foobar", cfg.RemoteForward[0].RelayName); + Assert.Equal(3000, cfg.RemoteForward[0].HostPort); + return 0; + }); + CommandLineSettings.Run(new string[] { "-R", "foobar.foobar:3000" }, + (settings) => + { + var cfg = Config.LoadConfig(settings); + Assert.Single(cfg.RemoteForward); + Assert.Equal("foobar.foobar", cfg.RemoteForward[0].RelayName); + Assert.Equal(3000, cfg.RemoteForward[0].HostPort); + return 0; + }); + CommandLineSettings.Run(new string[] { "-R", "foobar-foobar:3000" }, + (settings) => + { + var cfg = Config.LoadConfig(settings); + Assert.Single(cfg.RemoteForward); + Assert.Equal("foobar-foobar", cfg.RemoteForward[0].RelayName); + Assert.Equal(3000, cfg.RemoteForward[0].HostPort); + return 0; + }); + } + [Fact] public void CommandLineBadRemoteForwardTest() {