Skip to content

📝 [Proposal]: Add silent request termination #3252

@grivera64

Description

@grivera64

Feature Proposal Description

This issue proposes to add a method to fiber.Ctx that allows users to silently terminate HTTP requests in Fiber without sending any response headers nor response body. It should be a new method for the Ctx interface.

This feature will be helpful for:

  • DDOS protection (avoid responding to malicious traffic)
  • Hiding endpoints (stop bots from scraping sensitive endpoints by ignoring unauthenticated requests)

Note: While these methods are achievable through using a reverse proxy (e.g. NGINX), providing a native way
of doing this in Fiber will give more control to users of how to implement the above for their use case (e.g. a user's app is running on a low-resource machine).

This feature will work by closing the underlying context's net.Conn using the fasthttp.(*RequestCtx).Conn().Close() method. Fiber provides access to the underlying fasthttp.(*RequestCtx) through c.Context(). Thank you, @guno1928, for helping me find the simplest way to do this on Fiber's Discord Server! Your help is greatly appreciated.

The method's name is currently undecided, but here are a few ideas:

  • c.End() (look at Alignment with Express API) (Drop() is different from the Express equivalent res.end())
  • c.Close()
  • c.Terminate()
  • c.Stop()
  • c.Drop()

Please feel free to give to an opinion of which of these (or any other recommendations) would be a clear name for the method.

Alignment with Express API

The Express API uses the function response.connection.end() response.connection.destroy() to silently terminate HTTP requests:

app.get("/", (request, response) => {
  response.connection.destroy();
  return;
});

To be similar to Express.js, this feature proposes to use a single method for the same functionality, called Drop().

app.Get("/", func (c fiber.Ctx) error {
 return c.Drop()
})

HTTP RFC Standards Compliance

N/A, this feature will not affect how Fiber complies with the HTTP RFC standards.

API Stability

This feature will not require changing any existing methods in fiber.Ctx, so it should be safe to add. As fasthttp's interface is stable, there should be minimal changes or deprecations in the future.

Feature Examples

package main

import (
	"log"
	"net"

	"github.com/gofiber/fiber/v3"
)

func main() {
	app := fiber.New()
	app.Get("/", func (c fiber.Ctx) error {
		name := c.Query("name")
		// Silently terminate requests without name
		if name == "" {
			return c.Drop()
		}
		return c.SendString("Hello, " + name + "!")
	})
	log.Fatal(app.Listen(":3000"))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions