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

serving a compress enabled public folder without +w permissions results in a 404 #1766

Closed
rhabichl opened this issue Apr 23, 2024 · 1 comment

Comments

@rhabichl
Copy link
Contributor

Original bug gofiber/fiber#2940

The problem is in fs.go in the following function: openIndexFile()

func (h *fsHandler) openIndexFile(ctx *RequestCtx, dirPath string, mustCompress bool, fileEncoding string) (*fsFile, error) {
	for _, indexName := range h.indexNames {
		indexFilePath := dirPath + "/" + indexName
		ff, err := h.openFSFile(indexFilePath, mustCompress, fileEncoding)
		if err == nil {
			return ff, nil
		}
		if !errors.Is(err, fs.ErrNotExist) {
			return nil, fmt.Errorf("cannot open file %q: %w", indexFilePath, err)
		}
	}

	if !h.generateIndexPages {
		return nil, fmt.Errorf("cannot access directory without index page. Directory %q", dirPath)
	}

	return h.createDirIndex(ctx, dirPath, mustCompress, fileEncoding)
}

Because nowhere is checked if h.openFSFile() returns an error because the compressed file can't be written to the filesystem. The solution would be to check for the error like in handleRequest() and retry without compression:

func (h *fsHandler) handleRequest(ctx *RequestCtx) {
...
                var err error
		ff, err = h.openFSFile(filePath, mustCompress, fileEncoding)
		if mustCompress && err == errNoCreatePermission {
			ctx.Logger().Printf("insufficient permissions for saving compressed file for %q. Serving uncompressed file. "+
				"Allow write access to the directory with this file in order to improve fasthttp performance", filePath)
			mustCompress = false
			ff, err = h.openFSFile(filePath, mustCompress, fileEncoding)
		}
...
}
erikdubbelboer pushed a commit that referenced this issue Apr 29, 2024
* Implemented what was described by me in issue#1766.

* fixed linting isssues in fs.go with gofmt -e -d -s

---------

Co-authored-by: Raphael Habichler <raphael.habichler@bmd.at>
@erikdubbelboer
Copy link
Collaborator

Fixed in #1767

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

No branches or pull requests

2 participants