-
Notifications
You must be signed in to change notification settings - Fork 1.7k
#2375 #2376 Map non-ASCII header HttpRequestException to 400 Bad Request
#2379
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
base: develop
Are you sure you want to change the base?
Changes from 8 commits
001efdf
7dd44b4
d3d26f6
6f7fd84
16b9211
51efe4e
ee31a53
2411b59
803d06b
d1246cb
6c93d12
cf13333
e2b0001
2e23dd9
43f6aee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using Ocelot.Errors; | ||
|
|
||
| namespace Ocelot.Requester; | ||
|
|
||
| public class BadRequestError : Error | ||
| { | ||
| public BadRequestError(string message) | ||
| : base(message, OcelotErrorCode.BadRequestError, StatusCodes.Status400BadRequest) | ||
| { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,6 +47,14 @@ public Error Map(Exception exception) | |
| return new PayloadTooLargeError(exception); | ||
| } | ||
|
|
||
| // Late Catch: Map the HttpRequestException to a 400 Bad Request. | ||
| // If the header format is invalid, HttpClient throws this exception locally. | ||
| // By catching it here, we ensure Ocelot returns a clean 4xx client error instead of a generic 502 Bad Gateway. | ||
| if (exception is HttpRequestException && exception.Message.Contains("only ASCII characters")) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It’s hard to judge this solution until a valid acceptance test has been written. I believe we can improve this area, since the We might also consider covering more scenarios that return a
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Appreciate the guidance |
||
| { | ||
| return new BadRequestError(exception.Message); | ||
| } | ||
|
|
||
| return new ConnectionToDownstreamServiceError(exception); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Hosting; | ||
| using Microsoft.AspNetCore.Http; | ||
| using System.Net.Sockets; | ||
| using System.Text; | ||
| using Ocelot.Configuration.File; | ||
|
|
||
| namespace Ocelot.AcceptanceTests.Requester; | ||
|
|
||
| [Trait("Bug", "2376")] | ||
| [Trait("PR", "2381")] | ||
| public sealed class InvalidHeaderValueTests : Steps | ||
| { | ||
| private const int BasePortSeed = 20000; | ||
| private const int PortStride = 2; | ||
| private const int RequestTimeoutSeconds = 10; | ||
| private const int ReadBufferSize = 4096; | ||
| private const string GatewayRequestPath = "/ocelot/posts/askdj"; | ||
| private const string DownstreamRequestPath = "/todos/askdj"; | ||
| private const string DownstreamResponseBody = "Hello from Laura"; | ||
| private const string HostHeaderName = "Host"; | ||
| private const string AcceptHeaderName = "Accept"; | ||
| private const string ConnectionHeaderName = "Connection"; | ||
| private const string TestHeaderName = "skull"; | ||
| private const string ExpectedStatusLine = "HTTP/1.1 400 Bad Request"; | ||
|
|
||
| [Theory] | ||
| [InlineData("💀")] | ||
| [InlineData("é")] | ||
| [InlineData("漢")] | ||
| public async Task Should_return_400_bad_request_when_request_contains_non_ascii_header_value(string headerValue) | ||
|
raman-m marked this conversation as resolved.
Outdated
|
||
| { | ||
| var basePort = BasePortSeed + (Environment.ProcessId % 10000) * PortStride; | ||
| var downstreamPort = basePort; | ||
| var gatewayPort = basePort + 1; | ||
|
Majdi-Zlitni marked this conversation as resolved.
Outdated
|
||
| var route = GivenRoute(downstreamPort, "/ocelot/posts/{id}", "/todos/{id}"); | ||
| var configuration = GivenConfiguration(route); | ||
|
|
||
| GivenThereIsAConfiguration(configuration); | ||
| GivenThereIsAServiceRunningOn(downstreamPort, DownstreamRequestPath, context => | ||
| { | ||
| context.Response.StatusCode = (int)HttpStatusCode.OK; | ||
| return context.Response.WriteAsync(DownstreamResponseBody); | ||
| }); | ||
|
Majdi-Zlitni marked this conversation as resolved.
Outdated
|
||
| await GivenOcelotHostIsRunning(null, null, null, builder => builder | ||
| .UseKestrel() | ||
| .ConfigureAppConfiguration(WithBasicConfiguration) | ||
| .ConfigureServices(WithAddOcelot) | ||
| .Configure(WithUseOcelot) | ||
| .UseUrls(DownstreamUrl(gatewayPort)), null, null, null); | ||
|
Majdi-Zlitni marked this conversation as resolved.
Outdated
|
||
|
|
||
| var response = await SendRawRequestAsync(gatewayPort, headerValue); | ||
|
raman-m marked this conversation as resolved.
Outdated
|
||
|
|
||
| response.FirstLine().ShouldBe(ExpectedStatusLine); | ||
| } | ||
|
|
||
| private static async Task<string> SendRawRequestAsync(int port, string headerValue) | ||
| { | ||
| using var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(RequestTimeoutSeconds)); | ||
| using var client = new TcpClient(); | ||
| await client.ConnectAsync(System.Net.IPAddress.Loopback, port).WaitAsync(timeout.Token); | ||
|
|
||
| using var stream = client.GetStream(); | ||
|
raman-m marked this conversation as resolved.
Outdated
|
||
| var request = $"GET {GatewayRequestPath} HTTP/1.1\r\n{HostHeaderName}: localhost:{port}\r\n{AcceptHeaderName}: */*\r\n{TestHeaderName}: {headerValue}\r\n{ConnectionHeaderName}: close\r\n\r\n"; | ||
|
raman-m marked this conversation as resolved.
Outdated
|
||
| var requestBytes = Encoding.UTF8.GetBytes(request); | ||
| await stream.WriteAsync(requestBytes, timeout.Token); | ||
| await stream.FlushAsync(timeout.Token); | ||
|
|
||
| var buffer = new byte[ReadBufferSize]; | ||
| var response = new StringBuilder(); | ||
| int read; | ||
|
|
||
| while ((read = await stream.ReadAsync(buffer, timeout.Token)) > 0) | ||
| { | ||
| response.Append(Encoding.UTF8.GetString(buffer, 0, read)); | ||
| } | ||
|
Comment on lines
+36
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that is funny. However, the original bug #2374 has a different call stack: As a result, we are reproducing the same bug on the I was wrong — the original test version actually used the correct approach to emulate a Tomorrow I will rewrite the test based on the original version. The AI coding agent was right when it suggested how to properly emulate |
||
|
|
||
| return response.ToString(); | ||
| } | ||
| } | ||
|
|
||
| internal static class InvalidHeaderValueTestsExtensions | ||
| { | ||
| public static string FirstLine(this string response) | ||
| => response.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)[0]; | ||
| } | ||
|
raman-m marked this conversation as resolved.
Outdated
|
||
|
raman-m marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using Ocelot.Errors; | ||
| using Ocelot.Requester; | ||
|
|
||
| namespace Ocelot.UnitTests.Requester; | ||
|
|
||
| public class BadRequestErrorTests | ||
| { | ||
| [Fact] | ||
| public void Should_create_bad_request_error() | ||
| { | ||
| // Arrange | ||
| var message = "This is a bad request message."; | ||
|
|
||
| // Act | ||
| var error = new BadRequestError(message); | ||
|
|
||
| // Assert | ||
| error.Message.ShouldBe(message); | ||
| error.Code.ShouldBe(OcelotErrorCode.BadRequestError); | ||
| error.HttpStatusCode.ShouldBe(StatusCodes.Status400BadRequest); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.