-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(static): disable static path unescaping by default to prevent ACL bypass (GHSA-3pmx-cf9f-34xr) #3015
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
Closed
Closed
fix(static): disable static path unescaping by default to prevent ACL bypass (GHSA-3pmx-cf9f-34xr) #3015
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package echo | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "testing/fstest" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func adminSecretFS() fstest.MapFS { | ||
| return fstest.MapFS{ | ||
| "admin/secret.txt": {Data: []byte("TOP-SECRET")}, | ||
| "public/ok.txt": {Data: []byte("public")}, | ||
| "index.html": {Data: []byte("index")}, | ||
| } | ||
| } | ||
|
|
||
| // Regression for GHSA-3pmx-cf9f-34xr: encoded/decoded ".." in a static wildcard must | ||
| // not resolve a file across a route-level middleware guard. With unescaping disabled | ||
| // by default the encoded form never decodes, and the dot-dot guard rejects any ".." | ||
| // that the router (e.g. UseEscapedPathForMatching) decoded itself. | ||
| func TestStaticFS_EncodedDotDotDoesNotBypassRoute(t *testing.T) { | ||
| run := func(t *testing.T, e *Echo) { | ||
| g := e.Group("/admin", func(next HandlerFunc) HandlerFunc { | ||
| return func(c *Context) error { return c.String(http.StatusForbidden, "denied") } | ||
| }) | ||
| g.GET("/*", func(c *Context) error { return c.String(http.StatusOK, "reached-protected-handler") }) | ||
| e.StaticFS("/", adminSecretFS()) | ||
|
|
||
| cases := []struct { | ||
| target string | ||
| wantCode int | ||
| }{ | ||
| {"/admin/secret.txt", http.StatusForbidden}, // protected route fires | ||
| {"/public/%2E%2E/admin/secret.txt", http.StatusNotFound}, // high-sev: encoded dot-dot | ||
| {"/public/%2e%2e/admin/secret.txt", http.StatusNotFound}, // lower-case hex | ||
| {"/public%2F..%2Fadmin%2Fsecret.txt", http.StatusNotFound}, // encoded-slash variant | ||
| {"/public/../admin/secret.txt", http.StatusNotFound}, // literal dot-dot | ||
| {"/public/ok.txt", http.StatusOK}, // legitimate static file | ||
| {"/index.html", http.StatusOK}, // legitimate static file | ||
| } | ||
| for _, tc := range cases { | ||
| req := httptest.NewRequest(http.MethodGet, tc.target, nil) | ||
| rec := httptest.NewRecorder() | ||
| e.ServeHTTP(rec, req) | ||
| assert.Equal(t, tc.wantCode, rec.Code, "GET %s", tc.target) | ||
| assert.NotContains(t, rec.Body.String(), "TOP-SECRET", "GET %s leaked protected file", tc.target) | ||
| } | ||
| } | ||
|
|
||
| t.Run("default router", func(t *testing.T) { run(t, New()) }) | ||
| t.Run("UseEscapedPathForMatching", func(t *testing.T) { | ||
| run(t, NewWithConfig(Config{Router: NewRouter(RouterConfig{UseEscapedPathForMatching: true})})) | ||
| }) | ||
| } | ||
|
|
||
| // With EnablePathUnescapingStaticFiles the wildcard is unescaped (so encoded file | ||
| // names work), but encoded separators and ".." segments are still rejected. | ||
| func TestStaticFS_EnablePathUnescapingStaticFiles(t *testing.T) { | ||
| fsys := fstest.MapFS{ | ||
| "hello world.txt": {Data: []byte("spaced")}, | ||
| "admin/secret.txt": {Data: []byte("TOP-SECRET")}, | ||
| } | ||
| e := NewWithConfig(Config{EnablePathUnescapingStaticFiles: true}) | ||
| g := e.Group("/admin", func(next HandlerFunc) HandlerFunc { | ||
| return func(c *Context) error { return c.String(http.StatusForbidden, "denied") } | ||
| }) | ||
| g.GET("/*", func(c *Context) error { return c.String(http.StatusOK, "reached") }) | ||
| e.StaticFS("/", fsys) | ||
|
|
||
| cases := []struct { | ||
| target string | ||
| wantCode int | ||
| wantBody string | ||
| }{ | ||
| {"/hello%20world.txt", http.StatusOK, "spaced"}, // encoded space now decoded and served | ||
| {"/admin%2Fsecret.txt", http.StatusNotFound, ""}, // encoded slash still rejected | ||
| {"/public/%2E%2E/admin/secret.txt", http.StatusNotFound, ""}, // encoded dot-dot still rejected | ||
| } | ||
| for _, tc := range cases { | ||
| req := httptest.NewRequest(http.MethodGet, tc.target, nil) | ||
| rec := httptest.NewRecorder() | ||
| e.ServeHTTP(rec, req) | ||
| assert.Equal(t, tc.wantCode, rec.Code, "GET %s", tc.target) | ||
| if tc.wantBody != "" { | ||
| assert.Equal(t, tc.wantBody, rec.Body.String(), "GET %s", tc.target) | ||
| } | ||
| assert.NotContains(t, rec.Body.String(), "TOP-SECRET", "GET %s leaked protected file", tc.target) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vishr , I do not think this
HasDotDotSegmentand any ofpathutilis a maintainable solution. LLM can generate always another patch for leaking solution and complexity only grows.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Root problem here is that router and Static file serving methods and Static middleware are using path inconsistently. Creating functions to check path is just a fix for the symptom not the cause.