Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
428 stars 182 forks source link

HttpTrigger gives exception The header name format is invalid (when using HTTPS) #494

Closed ak37165 closed 3 years ago

ak37165 commented 3 years ago

Create a simple function "func init, func new" add an HttpTrigger and in the function code try accessing some header:

var didSucceed = req.Headers.TryGetValues("User-Agent", out IEnumerable<string> values);

Now run the function with HTTPS using:

func host start -port 7071 --useHttps --pause-on-error --cert CERTIFICATEFILENAME.pfx --password PASSWORD

where certificate has been generated per these instructions for Azure functions, using PowerShell (see this and when you try that method, the resulting error message will provide the following commands):

$cert = New-SelfSignedCertificate -Subject localhost -DnsName localhost -FriendlyName "Functions Development" -KeyUsage DigitalSignature -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1")
Export-PfxCertificate -Cert $cert -FilePath CERTIFICATEFILENAME.pfx -Password (ConvertTo-SecureString -String <PASSWORD> -Force -AsPlainText)

Call the function from PostMan - works fine (PostMan does not add problematic headers). Call the function using a browser (Chrome, Edge, or FireFox) - and you will see the exception:

One or more errors occurred. (The header name format is invalid.) [2021-06-10T12:50:23.837Z] ---> System.FormatException: The header name format is invalid. [2021-06-10T12:50:23.838Z] at System.Net.Http.Headers.HttpHeaders.GetHeaderDescriptor(String name) [2021-06-10T12:50:23.841Z] at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)

All 3 browsers tried above add a header ":authority:" when sending a HTTPS request. This is problematic for https://github.com/Azure/azure-functions-dotnet-worker/blob/72b9d17a485eda1e6e3626a9472948be1152ab7d/src/DotNetWorker.Core/Http/HttpHeadersCollection.cs#L39-L45

Related: issue. Even though the cause and symptom are different for that issue, I believe the fix will be the same.

brettsam commented 3 years ago

Duplicate of #386