GoogleCloudPlatform / functions-framework-java

FaaS (Function as a service) framework for writing portable Java functions
Apache License 2.0
133 stars 63 forks source link

Requesting `/favicon.ico` or `/robots.txt` invokes function, although it shouldn't according to doc #225

Closed alsoba13 closed 11 months ago

alsoba13 commented 1 year ago

Expected behaviour

According to Functions framework documentation, requests to /favicon.ico and /robots.txt should return a 404 status without calling the function:

Note that the framework must listen to all inbound paths (.*) and route these requests to the function, with the following exceptions:

  • /robots.txt - must respond with 404 without invoking function
  • /favicon.ico - must response with 404 without invoking function

Actual behaviour

When requesting a /favicon.ico or /robots.txt, a 404 status is returned, but the function is actually invoked, with all its possible side effects.

Reproduction

  1. Take the Hello World example from this repository, but write directly to stdout:
    
    package com.example;

import com.google.cloud.functions.HttpFunction; import com.google.cloud.functions.HttpRequest; import com.google.cloud.functions.HttpResponse;

public class HelloWorld implements HttpFunction { @Override public void service(HttpRequest request, HttpResponse response) throws Exception { System.out.println("Hello World! Path: " + request.getPath()); } }

2. Run function:
```bash
./gradlew runFunction -Prun.functionTarget=com.example.HelloWorld
  1. Curl to /favicon.ico:
    curl localhost:8080/favicon.ico
  2. See the result in the console to check that the function is executed with /favicon.ico:
    Hello World! Path: /favicon.ico

Possible fix

I believe that the bug is at the NotFoundHandler class, that executes the super after writing the 404 error. I am not familiar with Jetty's handlers API, but we may see that other jetty handlers (InetAccessHandler.java) do return right after generating the sendError.

PR for this: https://github.com/GoogleCloudPlatform/functions-framework-java/pull/226

Related

HKWinterhalter commented 11 months ago

Thanks for finding and reporting on this! I have approved PR #226