diff --git a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs index b65b29a071..b8d68c85d9 100644 --- a/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs +++ b/framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder.cs @@ -93,7 +93,7 @@ public class ClientProxyUrlBuilder : ITransientDependency } else if (pathParameter.DefaultValue != null) { - urlBuilder = urlBuilder.Replace($"{{{pathParameter.Name}}}", EncodePathValue(await ConvertValueToStringAsync(pathParameter.DefaultValue))); + urlBuilder = urlBuilder.Replace($"{{{pathParameter.Name}}}", EncodePathValue(pathParameter, await ConvertValueToStringAsync(pathParameter.DefaultValue))); } else { @@ -124,15 +124,24 @@ public class ClientProxyUrlBuilder : ITransientDependency } } - urlBuilder = urlBuilder.Replace($"{{{pathParameter.Name}}}", EncodePathValue(await ConvertValueToStringAsync(value))); + urlBuilder = urlBuilder.Replace($"{{{pathParameter.Name}}}", EncodePathValue(pathParameter, await ConvertValueToStringAsync(value))); } } } - protected virtual string EncodePathValue(string value) + protected virtual string EncodePathValue(ParameterApiDescriptionModel pathParameter, string value) { // Keep "/" unencoded so catch-all route parameters still receive multiple segments. - return string.Join("/", value.Split('/').Select(Uri.EscapeDataString)); + return string.Join("/", value.Split('/').Select(segment => + { + // Dot segments are removed by URI normalization even when percent-encoded, so the request would go to another endpoint. + if (segment is "." or "..") + { + throw new AbpException($"Path parameter value for {pathParameter.Name} ({pathParameter.NameOnMethod}) cannot contain \".\" or \"..\" segments."); + } + + return Uri.EscapeDataString(segment); + })); } protected virtual async Task AddQueryStringParametersAsync(StringBuilder urlBuilder, ActionApiDescriptionModel action, IReadOnlyDictionary methodArguments, ApiVersionInfo apiVersion) diff --git a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder_PathParameter_Tests.cs b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder_PathParameter_Tests.cs index ee29221a39..25dc5ce008 100644 --- a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder_PathParameter_Tests.cs +++ b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/ClientProxyUrlBuilder_PathParameter_Tests.cs @@ -31,6 +31,10 @@ public class ClientProxyUrlBuilder_PathParameter_Tests [InlineData("a/b#c", "api/files/a/b%23c")] [InlineData("rvA-a&b#", "api/files/rvA-a%26b%23%3Cq%3E")] [InlineData("รค๐Ÿ˜€", "api/files/%C3%A4%F0%9F%98%80")] + [InlineData("...", "api/files/...")] + [InlineData("a..b", "api/files/a..b")] + [InlineData(".hidden", "api/files/.hidden")] + [InlineData("%2E%2E", "api/files/%252E%252E")] public async Task Should_Encode_Path_Value(string value, string expectedUrl) { var action = BuildAction(PathParam("name")); @@ -40,6 +44,28 @@ public class ClientProxyUrlBuilder_PathParameter_Tests url.ShouldBe(expectedUrl); } + [Theory] + [InlineData(".")] + [InlineData("..")] + [InlineData("../admin")] + [InlineData("a/../b")] + [InlineData("a/./b")] + [InlineData("a/..")] + public async Task Should_Reject_Dot_Segments(string value) + { + var exception = await Should.ThrowAsync(() => GenerateUrlAsync(value)); + + exception.Message.ShouldContain("name"); + } + + [Fact] + public async Task Should_Reject_Dot_Segments_In_Default_Path_Value() + { + var action = BuildAction(PathParam("name", defaultValue: "../admin")); + + await Should.ThrowAsync(() => _builder.GenerateUrlWithParametersAsync(action, new Dictionary(), PathApiVersion)); + } + [Fact] public async Task Should_Keep_Format_Of_Non_String_Path_Values() { diff --git a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/RegularTestControllerStaticClientProxy_Tests.cs b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/RegularTestControllerStaticClientProxy_Tests.cs index d6ed4f6c88..7d16a73b5c 100644 --- a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/RegularTestControllerStaticClientProxy_Tests.cs +++ b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/Client/ClientProxying/RegularTestControllerStaticClientProxy_Tests.cs @@ -23,11 +23,22 @@ public class RegularTestControllerStaticClientProxy_Tests : AbpHttpClientTestBas [InlineData("a%23b")] [InlineData("rvA-a&b#")] [InlineData("รค๐Ÿ˜€")] + [InlineData("...")] + [InlineData(".hidden")] public async Task GetWithStringPathAsync(string name) { (await _proxy.GetWithStringPathAsync(name)).ShouldBe(name); } + [Theory] + [InlineData("../increment")] + [InlineData("..")] + public async Task GetWithStringPathAsync_Should_Not_Send_Dot_Segments(string name) + { + var exception = await Should.ThrowAsync(() => _proxy.GetWithStringPathAsync(name)); + exception.ShouldBeOfType(); + } + [Theory] [InlineData("a")] [InlineData("a/b")] diff --git a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs index 1985351881..477f34e785 100644 --- a/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs +++ b/framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs @@ -166,12 +166,30 @@ public class RegularTestControllerClientProxy_Tests : AbpHttpClientTestBase [InlineData("a%23b")] [InlineData("rvA-a&b#")] [InlineData("รค๐Ÿ˜€")] + [InlineData("...")] + [InlineData(".hidden")] public async Task GetWithStringPathAsync(string name) { var result = await _controller.GetWithStringPathAsync(name); result.ShouldBe(name); } + [Theory] + [InlineData("../increment")] + [InlineData("..")] + public async Task GetWithStringPathAsync_Should_Not_Send_Dot_Segments(string name) + { + var exception = await Should.ThrowAsync(() => _controller.GetWithStringPathAsync(name)); + exception.ShouldBeOfType(); + } + + [Fact] + public async Task GetWithCatchAllPathAsync_Should_Not_Send_Dot_Segments() + { + var exception = await Should.ThrowAsync(() => _controller.GetWithCatchAllPathAsync("a/../../increment")); + exception.ShouldBeOfType(); + } + [Theory] [InlineData("a")] [InlineData("a/b")]