|
| 1 | +package echo |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + "testing/fstest" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func adminSecretFS() fstest.MapFS { |
| 13 | + return fstest.MapFS{ |
| 14 | + "admin/secret.txt": {Data: []byte("TOP-SECRET")}, |
| 15 | + "public/ok.txt": {Data: []byte("public")}, |
| 16 | + "index.html": {Data: []byte("index")}, |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +// Regression for GHSA-3pmx-cf9f-34xr: encoded/decoded ".." in a static wildcard must |
| 21 | +// not resolve a file across a route-level middleware guard. With unescaping disabled |
| 22 | +// by default the encoded form never decodes, and the dot-dot guard rejects any ".." |
| 23 | +// that the router (e.g. UseEscapedPathForMatching) decoded itself. |
| 24 | +func TestStaticFS_EncodedDotDotDoesNotBypassRoute(t *testing.T) { |
| 25 | + run := func(t *testing.T, e *Echo) { |
| 26 | + g := e.Group("/admin", func(next HandlerFunc) HandlerFunc { |
| 27 | + return func(c *Context) error { return c.String(http.StatusForbidden, "denied") } |
| 28 | + }) |
| 29 | + g.GET("/*", func(c *Context) error { return c.String(http.StatusOK, "reached-protected-handler") }) |
| 30 | + e.StaticFS("/", adminSecretFS()) |
| 31 | + |
| 32 | + cases := []struct { |
| 33 | + target string |
| 34 | + wantCode int |
| 35 | + }{ |
| 36 | + {"/admin/secret.txt", http.StatusForbidden}, // protected route fires |
| 37 | + {"/public/%2E%2E/admin/secret.txt", http.StatusNotFound}, // high-sev: encoded dot-dot |
| 38 | + {"/public/%2e%2e/admin/secret.txt", http.StatusNotFound}, // lower-case hex |
| 39 | + {"/public%2F..%2Fadmin%2Fsecret.txt", http.StatusNotFound}, // encoded-slash variant |
| 40 | + {"/public/../admin/secret.txt", http.StatusNotFound}, // literal dot-dot |
| 41 | + {"/public/ok.txt", http.StatusOK}, // legitimate static file |
| 42 | + {"/index.html", http.StatusOK}, // legitimate static file |
| 43 | + } |
| 44 | + for _, tc := range cases { |
| 45 | + req := httptest.NewRequest(http.MethodGet, tc.target, nil) |
| 46 | + rec := httptest.NewRecorder() |
| 47 | + e.ServeHTTP(rec, req) |
| 48 | + assert.Equal(t, tc.wantCode, rec.Code, "GET %s", tc.target) |
| 49 | + assert.NotContains(t, rec.Body.String(), "TOP-SECRET", "GET %s leaked protected file", tc.target) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + t.Run("default router", func(t *testing.T) { run(t, New()) }) |
| 54 | + t.Run("UseEscapedPathForMatching", func(t *testing.T) { |
| 55 | + run(t, NewWithConfig(Config{Router: NewRouter(RouterConfig{UseEscapedPathForMatching: true})})) |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +// With EnablePathUnescapingStaticFiles the wildcard is unescaped (so encoded file |
| 60 | +// names work), but encoded separators and ".." segments are still rejected. |
| 61 | +func TestStaticFS_EnablePathUnescapingStaticFiles(t *testing.T) { |
| 62 | + fsys := fstest.MapFS{ |
| 63 | + "hello world.txt": {Data: []byte("spaced")}, |
| 64 | + "admin/secret.txt": {Data: []byte("TOP-SECRET")}, |
| 65 | + } |
| 66 | + e := NewWithConfig(Config{EnablePathUnescapingStaticFiles: true}) |
| 67 | + g := e.Group("/admin", func(next HandlerFunc) HandlerFunc { |
| 68 | + return func(c *Context) error { return c.String(http.StatusForbidden, "denied") } |
| 69 | + }) |
| 70 | + g.GET("/*", func(c *Context) error { return c.String(http.StatusOK, "reached") }) |
| 71 | + e.StaticFS("/", fsys) |
| 72 | + |
| 73 | + cases := []struct { |
| 74 | + target string |
| 75 | + wantCode int |
| 76 | + wantBody string |
| 77 | + }{ |
| 78 | + {"/hello%20world.txt", http.StatusOK, "spaced"}, // encoded space now decoded and served |
| 79 | + {"/admin%2Fsecret.txt", http.StatusNotFound, ""}, // encoded slash still rejected |
| 80 | + {"/public/%2E%2E/admin/secret.txt", http.StatusNotFound, ""}, // encoded dot-dot still rejected |
| 81 | + } |
| 82 | + for _, tc := range cases { |
| 83 | + req := httptest.NewRequest(http.MethodGet, tc.target, nil) |
| 84 | + rec := httptest.NewRecorder() |
| 85 | + e.ServeHTTP(rec, req) |
| 86 | + assert.Equal(t, tc.wantCode, rec.Code, "GET %s", tc.target) |
| 87 | + if tc.wantBody != "" { |
| 88 | + assert.Equal(t, tc.wantBody, rec.Body.String(), "GET %s", tc.target) |
| 89 | + } |
| 90 | + assert.NotContains(t, rec.Body.String(), "TOP-SECRET", "GET %s leaked protected file", tc.target) |
| 91 | + } |
| 92 | +} |
0 commit comments