Skip to content
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

Added better errors for MultiPart requests. #6829

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/HotChocolate/AspNetCore/src/AspNetCore/ErrorHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HotChocolate.AspNetCore.Properties;
using static HotChocolate.AspNetCore.Properties.AspNetCoreResources;

namespace HotChocolate.AspNetCore;

Expand All @@ -9,32 +9,32 @@ internal static class ErrorHelper
{
public static IError InvalidRequest()
=> ErrorBuilder.New()
.SetMessage(AspNetCoreResources.ErrorHelper_InvalidRequest)
.SetMessage(ErrorHelper_InvalidRequest)
.SetCode(ErrorCodes.Server.RequestInvalid)
.Build();

public static IError RequestHasNoElements()
=> ErrorBuilder.New()
.SetMessage(AspNetCoreResources.ErrorHelper_RequestHasNoElements)
.SetMessage(ErrorHelper_RequestHasNoElements)
.SetCode(ErrorCodes.Server.RequestInvalid)
.Build();

public static IError NoSupportedAcceptMediaType()
=> ErrorBuilder.New()
.SetMessage(AspNetCoreResources.ErrorHelper_NoSupportedAcceptMediaType)
.SetMessage(ErrorHelper_NoSupportedAcceptMediaType)
.SetCode(ErrorCodes.Server.NoSupportedAcceptMediaType)
.Build();

public static IQueryResult TypeNameIsEmpty()
=> QueryResultBuilder.CreateError(
new Error(
AspNetCoreResources.ErrorHelper_TypeNameIsEmpty,
ErrorHelper_TypeNameIsEmpty,
code: ErrorCodes.Server.TypeParameterIsEmpty));

public static IQueryResult InvalidTypeName(string typeName)
=> QueryResultBuilder.CreateError(
new Error(
AspNetCoreResources.ErrorHelper_InvalidTypeName,
ErrorHelper_InvalidTypeName,
code: ErrorCodes.Server.InvalidTypeName,
extensions: new Dictionary<string, object?>
{
Expand All @@ -44,7 +44,7 @@ public static IQueryResult InvalidTypeName(string typeName)
public static IQueryResult TypeNotFound(string typeName)
=> QueryResultBuilder.CreateError(
new Error(
string.Format(AspNetCoreResources.ErrorHelper_TypeNotFound, typeName),
string.Format(ErrorHelper_TypeNotFound, typeName),
code: ErrorCodes.Server.TypeDoesNotExist,
extensions: new Dictionary<string, object?>
{
Expand All @@ -54,10 +54,16 @@ public static IQueryResult TypeNotFound(string typeName)
public static IQueryResult InvalidAcceptMediaType(string headerValue)
=> QueryResultBuilder.CreateError(
new Error(
string.Format(AspNetCoreResources.ErrorHelper_InvalidAcceptMediaType, headerValue),
string.Format(ErrorHelper_InvalidAcceptMediaType, headerValue),
code: ErrorCodes.Server.InvalidAcceptHeaderValue,
extensions: new Dictionary<string, object?>
{
{ nameof(headerValue), headerValue }
}));

public static IQueryResult MultiPartRequestPreflightRequired()
=> QueryResultBuilder.CreateError(
new Error(
ErrorHelper_MultiPartRequestPreflightRequired,
code: ErrorCodes.Server.MultiPartPreflightRequired));
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using HotChocolate.AspNetCore.Serialization;
using HotChocolate.Language;
using HotChocolate.Utilities;
using static System.Net.HttpStatusCode;
using static HotChocolate.AspNetCore.ErrorHelper;
using static HotChocolate.AspNetCore.Properties.AspNetCoreResources;
using HttpRequestDelegate = Microsoft.AspNetCore.Http.RequestDelegate;

Expand All @@ -16,6 +18,7 @@ public sealed class HttpMultipartMiddleware : HttpPostMiddlewareBase
private const string _operations = "operations";
private const string _map = "map";
private readonly FormOptions _formOptions;
private readonly IQueryResult _multipartRequestError = MultiPartRequestPreflightRequired();

public HttpMultipartMiddleware(
HttpRequestDelegate next,
Expand All @@ -40,10 +43,16 @@ public override async Task InvokeAsync(HttpContext context)
{
if (HttpMethods.IsPost(context.Request.Method) &&
GetOptions(context).EnableMultipartRequests &&
ParseContentType(context) == RequestContentType.Form &&
(context.Request.Headers.ContainsKey(HttpHeaderKeys.Preflight) ||
!GetOptions(context).EnforceMultipartRequestsPreflightHeader))
ParseContentType(context) == RequestContentType.Form)
{
if (!context.Request.Headers.ContainsKey(HttpHeaderKeys.Preflight) &&
GetOptions(context).EnforceMultipartRequestsPreflightHeader)
{
var headerResult = HeaderUtilities.GetAcceptHeader(context.Request);
await WriteResultAsync(context, _multipartRequestError, headerResult.AcceptMediaTypes, BadRequest);
return;
}

if (!IsDefaultSchema)
{
context.Items[WellKnownContextData.SchemaName] = SchemaName;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,7 @@
<data name="ErrorHelper_TypeNameIsEmpty" xml:space="preserve">
<value>The specified types argument is empty.</value>
</data>
<data name="ErrorHelper_MultiPartRequestPreflightRequired" xml:space="preserve">
<value>Multi-part requests must include a GraphQL preflight header.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"ContentType": null,
"StatusCode": "NotFound",
"ContentType": "application/graphql-response+json; charset=utf-8",
"StatusCode": "BadRequest",
"Data": null,
"Errors": null,
"Errors": [
{
"message": "Multi-part requests must include a GraphQL preflight header.",
"extensions": {
"code": "HC0077"
}
}
],
"Extensions": null
}
5 changes: 5 additions & 0 deletions src/HotChocolate/Core/src/Abstractions/ErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ public static class Server
/// The request did not specify any supported accept media type.
/// </summary>
public const string InvalidAcceptHeaderValue = "HC0064";

/// <summary>
/// Multi-part requests must include a GraphQL preflight header.
/// </summary>
public const string MultiPartPreflightRequired = "HC0077";
}

public static class Schema
Expand Down
Loading