gofiber / fiber

⚑️ Express inspired web framework written in Go

Home Page:https://gofiber.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

πŸ› [Bug]: serving a compress enabled public folder without +w permissions results in a 404

luisgarciaalanis opened this issue Β· comments

Bug Description

Serving a Static public folder when the user has no permissions to write makes the server requests return 404.

I was testing it on a docker container, and trying to repro this locally outside docker, it only failed in the container where the permissions to write were disabled.

However if I build the docker image after testing it locally outside the container and it happens to copy the public folder that already contains the .fiber.gz files then it works fine.

It was hard to realize this was the problem because from my perspective it worked sometimes and there was no propper error message suggesting what the problem was.

How to Reproduce

  1. Create a sample index.html inside a public folder.
  2. Remove write permissions
    chmod -R ug-w ./public
  3. Run the CODE:
package main

import (
	"encoding/json"
	"log"
	"time"

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

func main() {
	var app = fiber.New(fiber.Config{
		JSONEncoder: json.Marshal,
	})

	app.Static("/", "./public", fiber.Static{
		Compress:      true,
		CacheDuration: 87600 * time.Hour, // refresh in 10 years.
	})

	app.Static("/*", "./public", fiber.Static{
		Compress:      true,
		CacheDuration: 87600 * time.Hour, // refresh in 10 years.
	})

	if err := app.Listen(":3000"); err != nil {
		log.Fatal(err)
	}
}
  1. Try to connect to http://localhost:3000

BUG: it will fail with a 404

  1. Add write permissions:
    chmod -R u+w ./public

  2. Run the code again
    RESULT: It will pass now and generate all the .fiber.gz files inside public

  3. Remove the permissions again
    chmod -R u-w ./public

  4. Run the code again
    BUG: it now passes even when it can't write.

Expected Behavior

Fail with an error the tells the user that it could not write to the directory?? to the console or back to the browser with 500 and a message?

Fiber Version

3.0.0

Code Snippet (optional)

package main

import (
	"encoding/json"
	"log"
	"time"

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


func main() {
	var app = fiber.New(fiber.Config{
		JSONEncoder: json.Marshal,
	})

	app.Static("/", "./public", fiber.Static{
		Compress:      true,
		CacheDuration: 87600 * time.Hour, // refresh in 10 years.
	})

	app.Static("/*", "./public", fiber.Static{
		Compress:      true,
		CacheDuration: 87600 * time.Hour, // refresh in 10 years.
	})

	if err := app.Listen(":3000"); err != nil {
		log.Fatal(err)
	}
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.

Thanks for opening your first issue here! πŸŽ‰ Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

commented

thanks for the report, probably the error is in the functionality we use from fasthttp

can you try the whole thing directly with the fasthttp fs handler and see if it occurs there too, then we can pass it on to the core framework
https://github.com/gofiber/fiber/blob/main/router.go#L421-L450

Hello @ReneWerner87,

I am trying this:

package main

import (
	"log"
	"time"

	"github.com/valyala/fasthttp"
)

func main() {
	fs := &fasthttp.FS{
		Root:                 "./public",
		IndexNames:           []string{"index.html"},
		AllowEmptyRoot:       true,
		Compress:             true,
		CacheDuration:        30000 * time.Minute,
		CompressedFileSuffix: fasthttp.FSCompressedFileSuffix,
		SkipCache:            false,
		PathNotFound: func(fctx *fasthttp.RequestCtx) {
			fctx.Response.SetStatusCode(404)
		},
	}

	fsHandler := fs.NewRequestHandler()

	if err := fasthttp.ListenAndServe(":3000", fsHandler); err != nil {
		log.Fatalf("error in ListenAndServe: %v", err)
	}
}

but is not generating the gz files, do you know if I am missing something to trigger the generation?