From e837e86f248e3012fae3c81ca20821a8e47bd32c Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa Date: Thu, 18 Apr 2024 15:54:14 +0300 Subject: [PATCH 1/4] Added check for uris with query parameters to prevent double encoding of the http request. --- .../Authentication/Cmdlets/InvokeMgGraphRequest.cs | 3 +-- src/Authentication/Authentication/Helpers/EncodeUriPath.cs | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs index 58042510625..86ebdad20c4 100644 --- a/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs +++ b/src/Authentication/Authentication/Cmdlets/InvokeMgGraphRequest.cs @@ -402,8 +402,7 @@ private Uri PrepareUri(HttpClient httpClient, Uri uri) // set body to null to prevent later FillRequestStream Body = null; } - // TODO: Review the fix made in https://github.com/microsoftgraph/msgraph-sdk-powershell/pull/2455. - return uriBuilder.Uri; + return uriBuilder.Uri.EscapeDataStrings(); } private void ThrowIfError(ErrorRecord error) diff --git a/src/Authentication/Authentication/Helpers/EncodeUriPath.cs b/src/Authentication/Authentication/Helpers/EncodeUriPath.cs index 72d8790a197..7879e2fc409 100644 --- a/src/Authentication/Authentication/Helpers/EncodeUriPath.cs +++ b/src/Authentication/Authentication/Helpers/EncodeUriPath.cs @@ -15,6 +15,8 @@ public static class EncodeUriPaths { public static Uri EscapeDataStrings(this Uri uri) { + if(uri.Query.Length > 0) + return uri; int counter = 0; var pathSegments = uri.OriginalString.Split('/'); StringBuilder sb = new StringBuilder(); From 351f646366ebb02466985b1e194fe5bdb1c3e1b9 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa Date: Thu, 18 Apr 2024 16:45:24 +0300 Subject: [PATCH 2/4] Updated Identity broker package reference --- .../Microsoft.Graph.Authentication.Core.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Authentication/Authentication.Core/Microsoft.Graph.Authentication.Core.csproj b/src/Authentication/Authentication.Core/Microsoft.Graph.Authentication.Core.csproj index b187cfffcdc..c385a4f63ae 100644 --- a/src/Authentication/Authentication.Core/Microsoft.Graph.Authentication.Core.csproj +++ b/src/Authentication/Authentication.Core/Microsoft.Graph.Authentication.Core.csproj @@ -4,7 +4,7 @@ 9.0 netstandard2.0;net6.0;net472 Microsoft.Graph.PowerShell.Authentication.Core - 2.6.1 + 2.18.0 true @@ -12,7 +12,7 @@ - + From 7b2c3ba4d631c81532d223098b7d6086be0bd5f2 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa Date: Fri, 19 Apr 2024 10:15:53 +0300 Subject: [PATCH 3/4] Made corrections as per comment from reviewer --- .../Authentication/Helpers/EncodeUriPath.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Authentication/Authentication/Helpers/EncodeUriPath.cs b/src/Authentication/Authentication/Helpers/EncodeUriPath.cs index 7879e2fc409..ff779f4ee3e 100644 --- a/src/Authentication/Authentication/Helpers/EncodeUriPath.cs +++ b/src/Authentication/Authentication/Helpers/EncodeUriPath.cs @@ -15,17 +15,24 @@ public static class EncodeUriPaths { public static Uri EscapeDataStrings(this Uri uri) { - if(uri.Query.Length > 0) - return uri; int counter = 0; var pathSegments = uri.OriginalString.Split('/'); StringBuilder sb = new StringBuilder(); - foreach (var segment in pathSegments) + foreach (var s in pathSegments) { + var segment = s; //Skips the left part of the uri i.e https://graph.microsoft.com if (counter > 2) { sb.Append('/'); + if(s.Contains("?")) + { + var queryStringIndex = segment.IndexOf("?"); + string queryString = segment.Substring(queryStringIndex); + segment = s.Substring(0, queryStringIndex); + sb.Append(Uri.EscapeDataString(segment)); + sb.Append(queryString); + } sb.Append(Uri.EscapeDataString(segment)); } counter++; From b12715caee78fc385efd2d20e56206c1f6428ed0 Mon Sep 17 00:00:00 2001 From: Timothy Wamalwa Date: Fri, 19 Apr 2024 10:27:24 +0300 Subject: [PATCH 4/4] Added else block --- src/Authentication/Authentication/Helpers/EncodeUriPath.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Authentication/Authentication/Helpers/EncodeUriPath.cs b/src/Authentication/Authentication/Helpers/EncodeUriPath.cs index ff779f4ee3e..f16020f6b7f 100644 --- a/src/Authentication/Authentication/Helpers/EncodeUriPath.cs +++ b/src/Authentication/Authentication/Helpers/EncodeUriPath.cs @@ -33,7 +33,10 @@ public static Uri EscapeDataStrings(this Uri uri) sb.Append(Uri.EscapeDataString(segment)); sb.Append(queryString); } - sb.Append(Uri.EscapeDataString(segment)); + else + { + sb.Append(Uri.EscapeDataString(segment)); + } } counter++; }