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

v3: Improve path validation in Static Middleware #3105

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions middleware/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@
path = append([]byte("/"), path...)
}

// Perform explicit path validation
absRoot, err := filepath.Abs(root)
if err != nil {
fctx.Response.SetStatusCode(fiber.StatusInternalServerError)
return nil

Check warning on line 109 in middleware/static/static.go

View check run for this annotation

Codecov / codecov/patch

middleware/static/static.go#L108-L109

Added lines #L108 - L109 were not covered by tests
}

absPath, err := filepath.Abs(filepath.Join(absRoot, string(path)))
if err != nil || !strings.HasPrefix(absPath, absRoot) {
fctx.Response.SetStatusCode(fiber.StatusForbidden)
return nil

Check warning on line 115 in middleware/static/static.go

View check run for this annotation

Codecov / codecov/patch

middleware/static/static.go#L114-L115

Added lines #L114 - L115 were not covered by tests
}

return path
}

Expand Down
Loading