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

Filter illegal header fields #2842

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
martincostello marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Net.Http.Headers;

namespace Swashbuckle.AspNetCore.SwaggerGen
{
Expand All @@ -21,11 +22,11 @@ public static class ApiParameterDescriptionExtensions
#endif
};

private static readonly List<string> IllegalHeaderParameters = new List<string>
private static readonly HashSet<string> IllegalHeaderParameters = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"accept",
"content-type",
"authorization"
HeaderNames.Accept,
HeaderNames.ContentType,
HeaderNames.Authorization
martincostello marked this conversation as resolved.
Show resolved Hide resolved
};

public static bool IsRequiredParameter(this ApiParameterDescription apiParameter)
Expand Down Expand Up @@ -121,7 +122,9 @@ internal static bool IsFromForm(this ApiParameterDescription apiParameter)

internal static bool IsIllegalHeaderParameter(this ApiParameterDescription apiParameter)
{
return apiParameter.Source == BindingSource.Header && IllegalHeaderParameters.Contains(apiParameter.Name, StringComparer.OrdinalIgnoreCase);
// Certian header parameters are not allowed and should be described using the corresponding OpenAPI keywords
martincostello marked this conversation as resolved.
Show resolved Hide resolved
// https://swagger.io/docs/specification/describing-parameters/#header-parameters
return apiParameter.Source == BindingSource.Header && IllegalHeaderParameters.Contains(apiParameter.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public void ActionWithIntParameterWithRequiredAttribute([Required]int param)
public void ActionWithIntParameterWithSwaggerIgnoreAttribute([SwaggerIgnore] int param)
{ }

public void ActionWithAcceptFromHeaderParameter([FromHeader] string accept)
public void ActionWithAcceptFromHeaderParameter([FromHeader] string accept, string param)
{ }

public void ActionWithContentTypeFromHeaderParameter([FromHeader(Name = "Content-Type")] string contentType)
public void ActionWithContentTypeFromHeaderParameter([FromHeader(Name = "Content-Type")] string contentType, string param)
{ }

public void ActionWithAuthorizationFromHeaderParameter([FromHeader] string authorization)
public void ActionWithAuthorizationFromHeaderParameter([FromHeader] string authorization, string param)
{ }

public void ActionWithObjectParameter(XmlAnnotatedType param)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ public void GetSwagger_IgnoresParameters_IfActionParameterHasSwaggerIgnoreAttrib
[InlineData(nameof(FakeController.ActionWithAuthorizationFromHeaderParameter))]
public void GetSwagger_IgnoresParameters_IfActionParameterIsIllegalHeaderParameter(string action)
{
var parameter = typeof(FakeController).GetMethod(action).GetParameters()[0];
var fromHeaderAttribute = parameter.GetCustomAttribute<FromHeaderAttribute>();
var illegalParameter = typeof(FakeController).GetMethod(action).GetParameters()[0];
var fromHeaderAttribute = illegalParameter.GetCustomAttribute<FromHeaderAttribute>();

var subject = Subject(
new[]
Expand All @@ -525,9 +525,9 @@ public void GetSwagger_IgnoresParameters_IfActionParameterIsIllegalHeaderParamet
{
new ApiParameterDescription
{
Name = fromHeaderAttribute?.Name ?? parameter.Name,
Name = fromHeaderAttribute?.Name ?? illegalParameter.Name,
Source = BindingSource.Header,
ModelMetadata = ModelMetadataFactory.CreateForParameter(parameter)
ModelMetadata = ModelMetadataFactory.CreateForParameter(illegalParameter)
}
}
)
Expand All @@ -537,7 +537,8 @@ public void GetSwagger_IgnoresParameters_IfActionParameterIsIllegalHeaderParamet
var document = subject.GetSwagger("v1");

var operation = document.Paths["/resource"].Operations[OperationType.Get];
Assert.Empty(operation.Parameters);
var parameter = Assert.Single(operation.Parameters);
Assert.Equal("param", parameter.Name);
}

[Theory]
Expand All @@ -546,8 +547,9 @@ public void GetSwagger_IgnoresParameters_IfActionParameterIsIllegalHeaderParamet
[InlineData(nameof(FakeController.ActionWithAuthorizationFromHeaderParameter))]
public void GetSwagger_GenerateParametersSchemas_IfActionParameterIsIllegalHeaderParameterWithProvidedOpenApiOperation(string action)
{
var parameter = typeof(FakeController).GetMethod(action).GetParameters()[0];
var fromHeaderAttribute = parameter.GetCustomAttribute<FromHeaderAttribute>();
var illegalParameter = typeof(FakeController).GetMethod(action).GetParameters()[0];
var fromHeaderAttribute = illegalParameter.GetCustomAttribute<FromHeaderAttribute>();
var illegalParameterName = fromHeaderAttribute?.Name ?? illegalParameter.Name;
var methodInfo = typeof(FakeController).GetMethod(action);
var actionDescriptor = new ActionDescriptor
{
Expand All @@ -560,7 +562,7 @@ public void GetSwagger_GenerateParametersSchemas_IfActionParameterIsIllegalHeade
{
new OpenApiParameter
{
Name = fromHeaderAttribute?.Name ?? parameter.Name,
Name = illegalParameterName,
}
}
}
Expand All @@ -583,9 +585,9 @@ public void GetSwagger_GenerateParametersSchemas_IfActionParameterIsIllegalHeade
{
new ApiParameterDescription
{
Name = fromHeaderAttribute?.Name ?? parameter.Name,
Name = illegalParameterName,
Source = BindingSource.Header,
ModelMetadata = ModelMetadataFactory.CreateForParameter(parameter)
ModelMetadata = ModelMetadataFactory.CreateForParameter(illegalParameter)
}
}),
}
Expand All @@ -594,7 +596,8 @@ public void GetSwagger_GenerateParametersSchemas_IfActionParameterIsIllegalHeade
var document = subject.GetSwagger("v1");

var operation = document.Paths["/resource"].Operations[OperationType.Get];
Assert.Null(operation.Parameters[0].Schema);
Assert.Null(operation.Parameters.Single(p => p.Name == illegalParameterName).Schema);
Assert.NotNull(operation.Parameters.Single(p => p.Name == "param").Schema);
}

[Fact]
Expand Down