From a084ea24b36125ea8b919511ff6e1c7d8e93a2c1 Mon Sep 17 00:00:00 2001 From: Chris <60838650+chrisharris333@users.noreply.github.com> Date: Fri, 16 Jul 2021 06:38:26 -0500 Subject: [PATCH] HTTPS by default, HTTP only if specified (#12394) * HTTPS by default, HTTP only if specified * Added/Updated unit tests;Added FTPS --- .../UriHelper/ExtendedUriParserTests.cs | 38 ++++++++++++------- .../UriHelper/ExtendedUriParser.cs | 8 ++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Uri.UnitTests/UriHelper/ExtendedUriParserTests.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Uri.UnitTests/UriHelper/ExtendedUriParserTests.cs index 84edf307d8..2843634bc0 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Uri.UnitTests/UriHelper/ExtendedUriParserTests.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Uri.UnitTests/UriHelper/ExtendedUriParserTests.cs @@ -11,20 +11,28 @@ namespace Microsoft.Plugin.Uri.UnitTests.UriHelper [TestFixture] public class ExtendedUriParserTests { - [TestCase("google.com", true, "http://google.com/")] - [TestCase("localhost", true, "http://localhost/")] - [TestCase("127.0.0.1", true, "http://127.0.0.1/")] - [TestCase("127.0.0.1:80", true, "http://127.0.0.1/")] + [TestCase("google.com", true, "https://google.com/")] + [TestCase("http://google.com", true, "http://google.com/")] + [TestCase("localhost", true, "https://localhost/")] + [TestCase("http://localhost", true, "http://localhost/")] + [TestCase("127.0.0.1", true, "https://127.0.0.1/")] + [TestCase("http://127.0.0.1", true, "http://127.0.0.1/")] + [TestCase("http://127.0.0.1:80", true, "http://127.0.0.1/")] [TestCase("127", false, null)] [TestCase("", false, null)] [TestCase("https://google.com", true, "https://google.com/")] [TestCase("ftps://google.com", true, "ftps://google.com/")] [TestCase(null, false, null)] - [TestCase("bing.com/search?q=gmx", true, "http://bing.com/search?q=gmx")] - [TestCase("h", true, "http://h/")] - [TestCase("ht", true, "http://ht/")] - [TestCase("htt", true, "http://htt/")] - [TestCase("http", true, "http://http/")] + [TestCase("bing.com/search?q=gmx", true, "https://bing.com/search?q=gmx")] + [TestCase("http://bing.com/search?q=gmx", true, "http://bing.com/search?q=gmx")] + [TestCase("h", true, "https://h/")] + [TestCase("http://h", true, "http://h/")] + [TestCase("ht", true, "https://ht/")] + [TestCase("http://ht", true, "http://ht/")] + [TestCase("htt", true, "https://htt/")] + [TestCase("http://htt", true, "http://htt/")] + [TestCase("http", true, "https://http/")] + [TestCase("http://http", true, "http://http/")] [TestCase("http:", false, null)] [TestCase("http:/", false, null)] [TestCase("http://", false, null)] @@ -36,10 +44,14 @@ namespace Microsoft.Plugin.Uri.UnitTests.UriHelper [TestCase("http://test.c", true, "http://test.c/")] [TestCase("http://test.co", true, "http://test.co/")] [TestCase("http://test.com", true, "http://test.com/")] - [TestCase("http:3", true, "http://http:3/")] - [TestCase("[::]", true, "http://[::]/")] - [TestCase("[2001:0DB8::1]", true, "http://[2001:db8::1]/")] - [TestCase("[2001:0DB8::1]:80", true, "http://[2001:db8::1]/")] + [TestCase("http:3", true, "https://http:3/")] + [TestCase("http://http:3", true, "http://http:3/")] + [TestCase("[::]", true, "https://[::]/")] + [TestCase("http://[::]", true, "http://[::]/")] + [TestCase("[2001:0DB8::1]", true, "https://[2001:db8::1]/")] + [TestCase("http://[2001:0DB8::1]", true, "http://[2001:db8::1]/")] + [TestCase("[2001:0DB8::1]:80", true, "https://[2001:db8::1]/")] + [TestCase("http://[2001:0DB8::1]:80", true, "http://[2001:db8::1]/")] public void TryParseCanParseHostName(string query, bool expectedSuccess, string expectedResult) { // Arrange diff --git a/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/UriHelper/ExtendedUriParser.cs b/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/UriHelper/ExtendedUriParser.cs index 7605ccfc65..ea56595d0d 100644 --- a/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/UriHelper/ExtendedUriParser.cs +++ b/src/modules/launcher/Plugins/Microsoft.Plugin.Uri/UriHelper/ExtendedUriParser.cs @@ -32,6 +32,14 @@ namespace Microsoft.Plugin.Uri.UriHelper try { var urlBuilder = new UriBuilder(input); + var hadDefaultPort = urlBuilder.Uri.IsDefaultPort; + urlBuilder.Port = hadDefaultPort ? -1 : urlBuilder.Port; + + if (!input.Contains("HTTP://", StringComparison.OrdinalIgnoreCase) && + !input.Contains("FTPS://", StringComparison.OrdinalIgnoreCase)) + { + urlBuilder.Scheme = System.Uri.UriSchemeHttps; + } result = urlBuilder.Uri; return true;