Repository navigation
Dotnet: DevUI improve local request checks #9575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
westey (westey-m)
merged 3 commits into
microsoft:main
from
westey-m:dotnet-devui-local
Oct 22, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
dotnet/src/Microsoft.Agents.AI.DevUI/DevUIAccessControlMiddleware.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| namespace Microsoft.Agents.AI.DevUI; | ||
|
|
||
| /// <summary> | ||
| /// Applies DevUI access validation to the standard OpenAI Responses and Conversations routes. | ||
| /// </summary> | ||
| internal sealed class DevUIAccessControlMiddleware | ||
| { | ||
| private static readonly PathString s_responsesPath = new("/v1/responses"); | ||
| private static readonly PathString s_conversationsPath = new("/v1/conversations"); | ||
|
|
||
| private readonly RequestDelegate _next; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="DevUIAccessControlMiddleware"/> class. | ||
| /// </summary> | ||
| /// <param name="next">The next middleware in the request pipeline.</param> | ||
| public DevUIAccessControlMiddleware(RequestDelegate next) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(next); | ||
| this._next = next; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates protected DevUI backend routes and continues allowed requests. | ||
| /// </summary> | ||
| /// <param name="context">The current HTTP context.</param> | ||
| /// <param name="validator">The shared DevUI request validator.</param> | ||
| /// <returns>A task that represents processing the request.</returns> | ||
| public async Task InvokeAsync(HttpContext context, DevUIRequestValidator validator) | ||
| { | ||
| var requestPath = context.Request.PathBase.Add(context.Request.Path); | ||
| if (IsProtectedPath(requestPath)) | ||
| { | ||
| var failure = validator.ValidateLocalRequest(context); | ||
| if (failure is not null) | ||
| { | ||
| await failure.ExecuteAsync(context).ConfigureAwait(false); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| await this._next(context).ConfigureAwait(false); | ||
| } | ||
|
|
||
| private static bool IsProtectedPath(PathString path) | ||
| => ContainsPathSegment(path, s_responsesPath) || ContainsPathSegment(path, s_conversationsPath); | ||
|
|
||
| private static bool ContainsPathSegment(PathString path, PathString routePath) | ||
| { | ||
| var pathValue = path.Value; | ||
| var routeValue = routePath.Value; | ||
| if (pathValue is null || routeValue is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| var matchIndex = pathValue.IndexOf(routeValue, StringComparison.OrdinalIgnoreCase); | ||
| while (matchIndex >= 0) | ||
| { | ||
| var matchEnd = matchIndex + routeValue.Length; | ||
| if (matchEnd == pathValue.Length || pathValue[matchEnd] == '/') | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| matchIndex = pathValue.IndexOf(routeValue, matchEnd, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
dotnet/src/Microsoft.Agents.AI.DevUI/DevUIAccessControlStartupFilter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| namespace Microsoft.Agents.AI.DevUI; | ||
|
|
||
| /// <summary> | ||
| /// Adds <see cref="DevUIAccessControlMiddleware"/> to the application request pipeline. | ||
| /// </summary> | ||
| internal sealed class DevUIAccessControlStartupFilter : IStartupFilter | ||
| { | ||
| /// <inheritdoc /> | ||
| public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(next); | ||
|
|
||
| return app => | ||
| { | ||
| app.UseMiddleware<DevUIAccessControlMiddleware>(); | ||
| next(app); | ||
| }; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.