Skip to content

Commit

Permalink
Fix MapPath and UsePathBase's use of implicit PathStrings #41168 (#41215
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Tratcher committed Apr 17, 2022
1 parent 1b02b85 commit 02e7e14
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Http/Http.Abstractions/src/Extensions/MapMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Task Invoke(HttpContext context)
return _next(context);
}

private async Task InvokeCore(HttpContext context, string matchedPath, string remainingPath)
private async Task InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
{
var path = context.Request.Path;
var pathBase = context.Request.PathBase;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Builder.Extensions;
Expand All @@ -25,7 +25,7 @@ public static IApplicationBuilder UsePathBase(this IApplicationBuilder app, Path
}

// Strip trailing slashes
pathBase = pathBase.Value?.TrimEnd('/');
pathBase = new PathString(pathBase.Value?.TrimEnd('/'));
if (!pathBase.HasValue)
{
return app;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Task Invoke(HttpContext context)
return _next(context);
}

private async Task InvokeCore(HttpContext context, string matchedPath, string remainingPath)
private async Task InvokeCore(HttpContext context, PathString matchedPath, PathString remainingPath)
{
var originalPath = context.Request.Path;
var originalPathBase = context.Request.PathBase;
Expand Down
21 changes: 12 additions & 9 deletions src/Http/Http.Abstractions/test/MapPathMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,27 @@ public void NullArguments_ArgumentNullException()
}

[Theory]
[InlineData("/foo", "", "/foo")]
[InlineData("/foo", "", "/foo/")]
[InlineData("/foo", "/Bar", "/foo")]
[InlineData("/foo", "/Bar", "/foo/cho")]
[InlineData("/foo", "/Bar", "/foo/cho/")]
[InlineData("/foo/cho", "/Bar", "/foo/cho")]
[InlineData("/foo/cho", "/Bar", "/foo/cho/do")]
public async Task PathMatchFunc_BranchTaken(string matchPath, string basePath, string requestPath)
[InlineData("/foo", "", "/foo", "/foo", "")]
[InlineData("/foo", "", "/foo/", "/foo", "/")]
[InlineData("/foo", "/Bar", "/foo", "/Bar/foo", "")]
[InlineData("/foo", "/Bar", "/foo/cho", "/Bar/foo", "/cho")]
[InlineData("/foo", "/Bar", "/foo/cho/", "/Bar/foo", "/cho/")]
[InlineData("/foo/cho", "/Bar", "/foo/cho", "/Bar/foo/cho", "")]
[InlineData("/foo/cho", "/Bar", "/foo/cho/do", "/Bar/foo/cho", "/do")]
[InlineData("/foo%42/cho", "/Bar%42", "/foo%42/cho/do%42", "/Bar%42/foo%42/cho", "/do%42")]
public async Task PathMatchFunc_BranchTaken(string matchPath, string basePath, string requestPath, string expectedPathBase, string expectedPath)
{
HttpContext context = CreateRequest(basePath, requestPath);
var builder = new ApplicationBuilder(serviceProvider: null!);
builder.Map(matchPath, UseSuccess);
builder.Map(new PathString(matchPath), UseSuccess);
var app = builder.Build();
await app.Invoke(context);

Assert.Equal(200, context.Response.StatusCode);
Assert.Equal(basePath, context.Request.PathBase.Value);
Assert.Equal(requestPath, context.Request.Path.Value);
Assert.Equal(expectedPathBase, (string)context.Items["test.PathBase"]!);
Assert.Equal(expectedPath, context.Items["test.Path"]);
}

[Theory]
Expand Down
14 changes: 13 additions & 1 deletion src/Http/Http.Abstractions/test/UsePathBaseExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,23 @@ public Task PathBaseCanHaveUnicodeCharacters(string registeredPathBase, string p
return TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath);
}

[Theory]
[InlineData("/b%42", "", "/b%42/something%42", "/b%42", "/something%42")]
[InlineData("/b%42", "", "/B%42/something%42", "/B%42", "/something%42")]
[InlineData("/b%42", "", "/b%42/Something%42", "/b%42", "/Something%42")]
[InlineData("/b%42", "/oldb%42", "/b%42/something%42", "/oldb%42/b%42", "/something%42")]
[InlineData("/b%42", "/oldb%42", "/b%42/Something%42", "/oldb%42/b%42", "/Something%42")]
[InlineData("/b%42", "/oldb%42", "/B%42/something%42", "/oldb%42/B%42", "/something%42")]
public Task PathBaseCanHavePercentCharacters(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath)
{
return TestPathBase(registeredPathBase, pathBase, requestPath, expectedPathBase, expectedPath);
}

private static async Task TestPathBase(string registeredPathBase, string pathBase, string requestPath, string expectedPathBase, string expectedPath)
{
HttpContext requestContext = CreateRequest(pathBase, requestPath);
var builder = CreateBuilder()
.UsePathBase(registeredPathBase);
.UsePathBase(new PathString(registeredPathBase));
builder.Run(context =>
{
context.Items["test.Path"] = context.Request.Path;
Expand Down

0 comments on commit 02e7e14

Please sign in to comment.