From 8d049035084d7fb3201ef3b652c8439c5b459614 Mon Sep 17 00:00:00 2001 From: Gavin Barron Date: Mon, 16 Nov 2026 16:08:18 -0800 Subject: [PATCH 1/4] Reuse user request paths for current-user requests Add default Graph URI replacement and batch-step remapping for the shared Me request-builder sentinel. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- README.md | 14 ++++ src/Microsoft.Graph.Core/CoreConstants.cs | 21 +++++ .../Models/BatchRequestStep.cs | 1 + .../Requests/GraphClientFactory.cs | 1 + .../GraphUriReplacementHandlerOption.cs | 57 +++++++++++++ .../Content/BatchRequestContentTests.cs | 46 ++++++++++ .../Requests/GraphClientFactoryTests.cs | 6 +- .../GraphUriReplacementHandlerTests.cs | 83 +++++++++++++++++++ 8 files changed, 227 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs create mode 100644 tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs diff --git a/README.md b/README.md index 27f91f4fd..361800e94 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,20 @@ if (response.IsSuccessStatusCode) } ``` +## Current-user request builders + +The SDK can reuse `/users/{user-id}` request builders for `Me` by setting the user ID +to `CoreConstants.ReplacementConstants.UserIdTokenToReplace`. The default +`GraphClientFactory` pipeline rewrites this reserved `/users/TokenToReplace` path +to `/me` before sending the request; it does not resolve or fetch a user ID. +Batch request steps apply the same replacement before serialization. + +If you supply your own `HttpClient` or middleware pipeline, include +`UriReplacementHandler` configured with +`new GraphUriReplacementHandlerOption()` to retain this behavior. Custom request +adapters must provide equivalent rewriting. Ordinary user IDs and query values +are not replaced. + ## Documentation and resources * [Microsoft Graph API](https://graph.microsoft.com) diff --git a/src/Microsoft.Graph.Core/CoreConstants.cs b/src/Microsoft.Graph.Core/CoreConstants.cs index e4e8771a8..5ddb0f918 100644 --- a/src/Microsoft.Graph.Core/CoreConstants.cs +++ b/src/Microsoft.Graph.Core/CoreConstants.cs @@ -14,6 +14,27 @@ public static class CoreConstants /// public const int PollingIntervalInMs = 5000; + /// + /// Constants used to reuse user request builders for the current user. + /// + public static class ReplacementConstants + { + /// + /// Reserved user ID used by current-user request builders. + /// + public const string UserIdTokenToReplace = "TokenToReplace"; + + /// + /// Synthetic users endpoint replaced before sending a request. + /// + public const string UsersEndpointWithReplaceToken = "/users/" + UserIdTokenToReplace; + + /// + /// Current-user endpoint. + /// + public const string MeEndpoint = "/me"; + } + /// /// Header constants. /// diff --git a/src/Microsoft.Graph.Core/Models/BatchRequestStep.cs b/src/Microsoft.Graph.Core/Models/BatchRequestStep.cs index e9abb0444..3a6836b38 100644 --- a/src/Microsoft.Graph.Core/Models/BatchRequestStep.cs +++ b/src/Microsoft.Graph.Core/Models/BatchRequestStep.cs @@ -48,6 +48,7 @@ public BatchRequestStep(string requestId, HttpRequestMessage httpRequestMessage, RequestId = (!string.IsNullOrEmpty(requestId)) ? requestId : throw new ArgumentNullException(nameof(requestId)); Request = httpRequestMessage ?? throw new ArgumentNullException(nameof(httpRequestMessage)); + Request.RequestUri = new GraphUriReplacementHandlerOption().Replace(Request.RequestUri); DependsOn = dependsOn; } diff --git a/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs b/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs index c1445ac52..78c60575d 100644 --- a/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs +++ b/src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs @@ -171,6 +171,7 @@ public static HttpClient Create( public static IList CreateDefaultHandlers(GraphClientOptions graphClientOptions = null) { var handlers = KiotaClientFactory.CreateDefaultHandlers(); + handlers.Insert(0, new UriReplacementHandler(new GraphUriReplacementHandlerOption())); handlers.Add(new GraphTelemetryHandler(graphClientOptions));// add the telemetry handler last. return handlers; diff --git a/src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs b/src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs new file mode 100644 index 000000000..ccd65cdcc --- /dev/null +++ b/src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs @@ -0,0 +1,57 @@ +// ------------------------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +// ------------------------------------------------------------------------------ + +namespace Microsoft.Graph +{ + using System; + using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; + + /// + /// Replaces the reserved current-user path with the Microsoft Graph /me endpoint. + /// + public class GraphUriReplacementHandlerOption : IUriReplacementHandlerOption + { + private readonly bool enabled; + + /// + /// Creates current-user URI replacement options. + /// + /// Whether current-user replacement is enabled. + public GraphUriReplacementHandlerOption(bool enabled = true) + { + this.enabled = enabled; + } + + /// + public bool IsEnabled() => enabled; + + /// + public Uri Replace(Uri original) + { + if (!enabled || original == null || !original.IsAbsoluteUri) + { + return original; + } + + var path = original.AbsolutePath; + var token = CoreConstants.ReplacementConstants.UsersEndpointWithReplaceToken; + var index = path.IndexOf(token, StringComparison.Ordinal); + while (index >= 0) + { + var end = index + token.Length; + if (end == path.Length || path[end] == '/') + { + return new UriBuilder(original) + { + Path = path.Substring(0, index) + CoreConstants.ReplacementConstants.MeEndpoint + path.Substring(end) + }.Uri; + } + + index = path.IndexOf(token, end, StringComparison.Ordinal); + } + + return original; + } + } +} diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs index b68c33978..8500c9031 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Content/BatchRequestContentTests.cs @@ -48,6 +48,52 @@ public void BatchRequestContent_DefaultInitialize() Assert.True(batchRequestContent.BatchRequestSteps.Count.Equals(0)); } + [Theory] + [InlineData(false, false)] + [InlineData(true, false)] + [InlineData(false, true)] + public async Task BatchRequestContent_ReplacesCurrentUserBeforeSerializationAsync(bool useRequestInformation, bool usePrebuiltStep) + { + const string url = "https://graph.microsoft.com/v1.0/users/TokenToReplace/messages?$select=subject"; + using var content = new BatchRequestContent(client); + if (useRequestInformation) + { + var requestInfo = new RequestInformation + { + HttpMethod = Method.POST, + URI = new Uri(url) + }; + requestInfo.Headers.Add("ConsistencyLevel", "eventual"); + requestInfo.SetStreamContent(new MemoryStream(Encoding.UTF8.GetBytes("{\"subject\":\"test\"}")), "application/json"); + await content.AddBatchRequestStepAsync(requestInfo, "current-user"); + } + else + { + var request = new HttpRequestMessage(HttpMethod.Post, url) + { + Content = new StringContent("{\"subject\":\"test\"}", Encoding.UTF8, "application/json") + }; + request.Headers.Add("ConsistencyLevel", "eventual"); + if (usePrebuiltStep) + { + content.AddBatchRequestStep(new BatchRequestStep("current-user", request)); + } + else + { + content.AddBatchRequestStep(request); + } + } + + using var stream = await content.GetBatchRequestContentAsync(); + using var document = await JsonDocument.ParseAsync(stream); + var step = document.RootElement.GetProperty("requests")[0]; + Assert.Equal("/me/messages?$select=subject", step.GetProperty("url").GetString()); + Assert.Equal("POST", step.GetProperty("method").GetString()); + Assert.Equal("test", step.GetProperty("body").GetProperty("subject").GetString()); + Assert.Equal("eventual", step.GetProperty("headers").GetProperty("ConsistencyLevel").GetString()); + Assert.Equal("https://graph.microsoft.com/v1.0/me/messages?$select=subject", content.BatchRequestSteps.Values.Single().Request.RequestUri.AbsoluteUri); + } + [Fact] public void BatchRequestContent_InitializeWithSerializer() { diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/GraphClientFactoryTests.cs b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/GraphClientFactoryTests.cs index d4d9c7f12..53969923b 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/GraphClientFactoryTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/GraphClientFactoryTests.cs @@ -71,7 +71,8 @@ public void Should_CreatePipeline_Without_CompressionHandler() [Fact] public void Should_CreatePipeline_Without_HttpMessageHandlerInput() { - using UriReplacementHandler uriReplacementHandler = (UriReplacementHandler)GraphClientFactory.CreatePipeline(handlers, new MockRedirectHandler()); + using UriReplacementHandler graphUriReplacementHandler = (UriReplacementHandler)GraphClientFactory.CreatePipeline(handlers, new MockRedirectHandler()); + using UriReplacementHandler uriReplacementHandler = (UriReplacementHandler)graphUriReplacementHandler.InnerHandler; using RetryHandler retryHandler = (RetryHandler)uriReplacementHandler.InnerHandler; using RedirectHandler redirectHandler = (RedirectHandler)retryHandler.InnerHandler; using ParametersNameDecodingHandler odataQueryHandler = (ParametersNameDecodingHandler)redirectHandler.InnerHandler; @@ -101,7 +102,8 @@ public void Should_CreatePipeline_Without_HttpMessageHandlerInput() [Fact] public void CreatePipelineWithHttpMessageHandlerInput() { - using UriReplacementHandler uriReplacementHandler = (UriReplacementHandler)GraphClientFactory.CreatePipeline(handlers, new MockRedirectHandler()); + using UriReplacementHandler graphUriReplacementHandler = (UriReplacementHandler)GraphClientFactory.CreatePipeline(handlers, new MockRedirectHandler()); + using UriReplacementHandler uriReplacementHandler = (UriReplacementHandler)graphUriReplacementHandler.InnerHandler; using RetryHandler retryHandler = (RetryHandler)uriReplacementHandler.InnerHandler; using RedirectHandler redirectHandler = (RedirectHandler)retryHandler.InnerHandler; using ParametersNameDecodingHandler odataQueryHandler = (ParametersNameDecodingHandler)redirectHandler.InnerHandler; diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs new file mode 100644 index 000000000..030874160 --- /dev/null +++ b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs @@ -0,0 +1,83 @@ +// ------------------------------------------------------------------------------ +// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. +// ------------------------------------------------------------------------------ + +namespace Microsoft.Graph.DotnetCore.Core.Test.Requests.Middleware +{ + using System; + using System.Collections.Generic; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; + using Microsoft.Kiota.Abstractions; + using Microsoft.Kiota.Abstractions.Authentication; + using Microsoft.Kiota.Http.HttpClientLibrary.Middleware.Options; + using Xunit; + + public class GraphUriReplacementHandlerTests + { + [Theory] + [InlineData("https://graph.microsoft.com/v1.0/users/TokenToReplace", "https://graph.microsoft.com/v1.0/me")] + [InlineData("https://graph.microsoft.us/beta/users/TokenToReplace/messages?$filter=subject%20eq%20%27a%2Fb%27", "https://graph.microsoft.us/beta/me/messages?$filter=subject%20eq%20%27a%2Fb%27")] + [InlineData("https://localhost:8443/custom/v1.0/users/TokenToReplace/photo/$value#fragment", "https://localhost:8443/custom/v1.0/me/photo/$value#fragment")] + [InlineData("https://graph.microsoft.com/v1.0/users/TokenToReplace/messages/a%2Fb?value=%252F", "https://graph.microsoft.com/v1.0/me/messages/a%2Fb?value=%252F")] + [InlineData("https://graph.microsoft.com/v1.0/users/actual-id/messages", "https://graph.microsoft.com/v1.0/users/actual-id/messages")] + [InlineData("https://graph.microsoft.com/v1.0/users/TokenToReplaceExtra", "https://graph.microsoft.com/v1.0/users/TokenToReplaceExtra")] + [InlineData("https://graph.microsoft.com/v1.0/me?next=/users/TokenToReplace", "https://graph.microsoft.com/v1.0/me?next=/users/TokenToReplace")] + [InlineData("https://graph.microsoft.com/v1.0/users/tokentoreplace", "https://graph.microsoft.com/v1.0/users/tokentoreplace")] + public async Task DefaultPipelineReplacesOnlyReservedUserPathAsync(string url, string expected) + { + using var httpClient = GraphClientFactory.Create(finalHandler: new FakeSuccessHandler()); + using var response = await httpClient.GetAsync(url); + + Assert.Equal(expected, response.RequestMessage.RequestUri.AbsoluteUri); + } + + [Fact] + public async Task RequestOptionCanDisableReplacementAsync() + { + using var httpClient = GraphClientFactory.Create(finalHandler: new FakeSuccessHandler()); + using var adapter = new BaseGraphRequestAdapter(new AnonymousAuthenticationProvider(), httpClient: httpClient); + var requestInfo = new RequestInformation + { + HttpMethod = Method.GET, + URI = new Uri("https://graph.microsoft.com/v1.0/users/TokenToReplace") + }; + requestInfo.AddRequestOptions(new[] { new GraphUriReplacementHandlerOption(false) }); + using var request = await adapter.ConvertToNativeRequestAsync(requestInfo); + using var response = await httpClient.SendAsync(request); + + Assert.Equal(requestInfo.URI, response.RequestMessage.RequestUri); + } + + [Fact] + public async Task KiotaRequestOptionStillAppliesAsync() + { + using var httpClient = GraphClientFactory.Create(finalHandler: new FakeSuccessHandler()); + using var adapter = new BaseGraphRequestAdapter(new AnonymousAuthenticationProvider(), httpClient: httpClient); + var requestInfo = new RequestInformation + { + HttpMethod = Method.GET, + URI = new Uri("https://graph.microsoft.com/v1.0/users/TokenToReplace/messages") + }; + requestInfo.AddRequestOptions(new[] + { + new UriReplacementHandlerOption(true, new[] { new KeyValuePair("/messages", "/events") }) + }); + using var request = await adapter.ConvertToNativeRequestAsync(requestInfo); + using var response = await httpClient.SendAsync(request); + + Assert.Equal("https://graph.microsoft.com/v1.0/me/events", response.RequestMessage.RequestUri.AbsoluteUri); + } + + [Fact] + public async Task ExplicitCustomPipelineIsNotReplacedAsync() + { + using var httpClient = GraphClientFactory.Create(Array.Empty(), finalHandler: new FakeSuccessHandler()); + const string url = "https://graph.microsoft.com/v1.0/users/TokenToReplace"; + using var response = await httpClient.GetAsync(url, CancellationToken.None); + + Assert.Equal(url, response.RequestMessage.RequestUri.AbsoluteUri); + } + } +} From daa8217add94c44a3ff055933fcc8343d7e66b1c Mon Sep 17 00:00:00 2001 From: Gavin Barron Date: Mon, 16 Nov 2026 16:54:41 -0800 Subject: [PATCH 2/4] fix: use repository-required UTF-8 BOM encoding Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../Middleware/Options/GraphUriReplacementHandlerOption.cs | 2 +- .../Requests/Middleware/GraphUriReplacementHandlerTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs b/src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs index ccd65cdcc..b0364f6e6 100644 --- a/src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs +++ b/src/Microsoft.Graph.Core/Requests/Middleware/Options/GraphUriReplacementHandlerOption.cs @@ -1,4 +1,4 @@ -// ------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs index 030874160..61d3fcd4c 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs +++ b/tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/Middleware/GraphUriReplacementHandlerTests.cs @@ -1,4 +1,4 @@ -// ------------------------------------------------------------------------------ +// ------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. // ------------------------------------------------------------------------------ From 13abd1154b1ce991e5b05ff9de26b94e27ee6178 Mon Sep 17 00:00:00 2001 From: Gavin Barron Date: Mon, 16 Nov 2026 16:58:41 -0800 Subject: [PATCH 3/4] fix: use patched .NET SDK for NativeAOT validation Update the pinned .NET SDK to 10.0.404 so trimming CI uses the patched 10.0.15 NativeAOT compiler instead of vulnerable 10.0.8 tooling. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index afedd64bf..ad9e0c101 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "10.0.202", + "version": "10.0.404", "rollForward": "major" } } \ No newline at end of file From 114ea072ad66f794d68fae5d56558e416081b339 Mon Sep 17 00:00:00 2001 From: Gavin Barron Date: Mon, 16 Nov 2026 17:10:04 -0800 Subject: [PATCH 4/4] fix: align nested SDK pins with patched build tooling The trimming job runs from its project directory, where a nested global.json overrode the root SDK pin and selected the vulnerable 10.0.8 NativeAOT compiler on CI. Update both nested pins to 10.0.404 as well. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- src/Microsoft.Graph.Core/global.json | 2 +- tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Graph.Core/global.json b/src/Microsoft.Graph.Core/global.json index afedd64bf..ad9e0c101 100644 --- a/src/Microsoft.Graph.Core/global.json +++ b/src/Microsoft.Graph.Core/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "10.0.202", + "version": "10.0.404", "rollForward": "major" } } \ No newline at end of file diff --git a/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json b/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json index 9ebe63cbc..5ff965535 100644 --- a/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json +++ b/tests/Microsoft.Graph.DotnetCore.Core.Trimming/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "10.0.202", + "version": "10.0.404", "rollForward": "major" } }