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

[BUG] NullReferenceException When Parsing a HttpResponse without content-type #5928

Closed
gamaSantos opened this issue Aug 23, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@gamaSantos
Copy link
Contributor

gamaSantos commented Aug 23, 2024

Description

When using the Default HttpRequestMessage activity, if the response contains a body but no content-type the first parser that attempts to read the content-type will throw a NullReferenceException.

Steps to Reproduce

To help us identify the issue more quickly, please follow these guidelines:

  1. Detailed Steps: I used the following workflow in the sample web server (src/apps/Elsa.Server.Web/) to test my solution.
    You just need to invoke it to observe bug.
using System.Net;
using System.Net.Mime;
using Elsa.Http;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;

namespace Elsa.Server.Web.Activities;

public class WeatherForecastWorkflow : WorkflowBase
{
    protected override void Build(IWorkflowBuilder builder)
    {
        var uri = 
        builder.Root = new Sequence
        {
            Activities =
            {
                new HttpEndpoint
                {
                    Path = new("/poc"),
                    SupportedMethods = new(new[] { HttpMethods.Get }),
                    CanStartWorkflow = true
                },

                new SendHttpRequest
                {
                    //I'll provide a sample project later
                    Url = new(new Uri("http://localhost:5100")), 
                    Method = new(HttpMethods.Put)
                },

                // Write back the weather forecast.
                new WriteHttpResponse
                {
                    ContentType = new(MediaTypeNames.Text.Html),
                    StatusCode = new(HttpStatusCode.OK),
                    Content = new(context =>
                    {
                        return "Hello";
                    })
                }
            }
        };
    }
}

  1. Code Snippets: no other relevant snippet.

  2. Attachments:

    • Sample Project: You can reproduce the error with the following simple c# server
using System.Net;
using System.Net.Sockets;
using System.Text;

var response = Encoding.UTF8.GetBytes(@"HTTP/1.1 403
Date: Sat, 24 Aug 2024 13:02:23 GMT
Content-Length: 43
Connection: keep-alive
Server: openresty
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS,HEAD
Access-Control-Allow-Headers: Content-Type, gumgaToken, Connection, userRecognition

{""response"":""some_info"",""operation"":""general""}");

var lookback = IPAddress.Parse("127.0.0.1");
var ipAddress = new IPEndPoint(lookback,5100);
var listener = new TcpListener(ipAddress);
Console.WriteLine($"Listening at: {ipAddress} ");
try
{
	listener.Start();
	while(true)
	{
		using var handler = listener.AcceptTcpClient();
		using var stream = handler.GetStream();
		stream.Write(response);
	}
}
finally 
{
	listener.Stop();
}
  1. Reproduction Rate: every time

  2. Video/Screenshots: I don't believe it's necessary, but, I can provide one if needed

  3. Additional Configuration: I don't believe it's necessary.

Expected Behavior

The workflow should continue without a exception.

Actual Behavior

The api returns a 500 exception and the workflow is suspended.

Screenshots

If possible, add screenshots or screen recordings to help explain the problem.

Environment

  • Elsa Package Version: Elsa 3.1.3 and latest commit from main.
  • Operating System: Fedora 40 (6.10.4)
  • Browser and Version: Not applicable

Log Output

System.NullReferenceException Object reference not set to an instance of an object.
at Elsa.Extensions.HttpActivityExecutionContextExtensions.<>c__DisplayClass0_0.b__1(IHttpContentParser x)
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable1 source, Func2 predicate, Boolean& found)
at Elsa.Extensions.HttpActivityExecutionContextExtensions.ParseContentAsync(ActivityExecutionContext context, Stream content, String contentType, Type returnType, CancellationToken cancellationToken)
at Elsa.Http.SendHttpRequestBase.ParseContentAsync(ActivityExecutionContext context, HttpContent httpContent)
at Elsa.Http.SendHttpRequestBase.TrySendAsync(ActivityExecutionContext context)
at Elsa.Http.SendHttpRequestBase.ExecuteAsync(ActivityExecutionContext context)
at Elsa.Workflows.Activity.Elsa.Workflows.Contracts.IActivity.ExecuteAsync(ActivityExecutionContext context)
at Elsa.Workflows.Middleware.Activities.DefaultActivityInvokerMiddleware.ExecuteActivityAsync(ActivityExecutionContext context)
at Elsa.Workflows.Runtime.Middleware.Activities.BackgroundActivityInvokerMiddleware.ExecuteActivityAsync(ActivityExecutionContext context)
at Elsa.Workflows.Middleware.Activities.DefaultActivityInvokerMiddleware.InvokeAsync(ActivityExecutionContext context)
at Elsa.Workflows.Middleware.Activities.NotificationPublishingMiddleware.InvokeAsync(ActivityExecutionContext context)
at Elsa.Workflows.Middleware.Activities.ExecutionLogMiddleware.InvokeAsync(ActivityExecutionContext context)

Troubleshooting Attempts

I could not create a workaround using the workflow studio, but, I could fix it changing the SendHttpRequestBase class, in the ParseContentAsync method, instead of using the null-forgiving operator when creating the contentType variable adding a default value of application/octet-stream
After this change it was necessary to modify the FileHttpContentParser to use the TryGetValue method instead of accessing the Header dictionary directly.

Additional Context

Add any other context about the problem here. This could include the frequency of the issue (e.g., intermittent, every time), conditions under which the issue appears, etc.

Related Issues

Link to any related issues here.

@sfmskywalker
Copy link
Member

Thanks for the detailed bug report + PR that fixes it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants