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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 [Bug]: serving a compress enabled public folder without +w permissions results in a 404 #2940

Open
3 tasks done
luisgarciaalanis opened this issue Mar 26, 2024 · 3 comments

Comments

@luisgarciaalanis
Copy link

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.
Copy link

welcome bot commented Mar 26, 2024

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

@ReneWerner87
Copy link
Member

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

@luisgarciaalanis
Copy link
Author

luisgarciaalanis commented Mar 26, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants