From 670895b7b29deb2d51af11bb35b0ab16cf1ac196 Mon Sep 17 00:00:00 2001 From: Nico Clack <16543008+hedgehog125@users.noreply.github.com> Date: Sun, 23 Nov 2025 00:44:36 +0000 Subject: [PATCH 1/2] Fix router.StaticFS --- routergroup.go | 4 ++++ routes_test.go | 23 +++++++++++++++++++ testdata/embed/index.html | 2 ++ .../embed/tutorials/making-gin/index.html | 2 ++ testdata/embed/tutorials/making-gin/main.js | 1 + 5 files changed, 32 insertions(+) create mode 100644 testdata/embed/index.html create mode 100644 testdata/embed/tutorials/making-gin/index.html create mode 100644 testdata/embed/tutorials/making-gin/main.js diff --git a/routergroup.go b/routergroup.go index b2540ec1..0fb2c365 100644 --- a/routergroup.go +++ b/routergroup.go @@ -223,6 +223,10 @@ func (group *RouterGroup) createStaticHandler(relativePath string, fs http.FileS } file := c.Param("filepath") + file = path.Clean("/" + file)[1:] + if file == "" { + file = "." + } // Check if file exists and/or if we have permission to access it f, err := fs.Open(file) if err != nil { diff --git a/routes_test.go b/routes_test.go index 1cae3fce..3339fe0f 100644 --- a/routes_test.go +++ b/routes_test.go @@ -5,7 +5,9 @@ package gin import ( + "embed" "fmt" + "io/fs" "net/http" "net/http/httptest" "os" @@ -646,6 +648,27 @@ func TestRouterStaticFSNotFound(t *testing.T) { assert.Equal(t, "non existent", w.Body.String()) } +//go:embed testdata/embed +var embeddedFolder embed.FS + +const embeddedPath = "testdata/embed" + +func TestRouteStaticFSCleansPath(t *testing.T) { + router := New() + subFS, err := fs.Sub(embeddedFolder, embeddedPath) + require.NoError(t, err) + fs := &OnlyFilesFS{ + FileSystem: http.FS(subFS), + } + router.StaticFS("/", fs) + router.NoRoute(func(c *Context) { + c.String(http.StatusNotFound, "non existent") + }) + + w := PerformRequest(router, http.MethodGet, "/tutorials/making-gin/") + assert.Contains(t, w.Body.String(), "This is another simple embedded page.") +} + func TestRouterStaticFSFileNotFound(t *testing.T) { router := New() diff --git a/testdata/embed/index.html b/testdata/embed/index.html new file mode 100644 index 00000000..cacfde8a --- /dev/null +++ b/testdata/embed/index.html @@ -0,0 +1,2 @@ + +Hello embedded world! diff --git a/testdata/embed/tutorials/making-gin/index.html b/testdata/embed/tutorials/making-gin/index.html new file mode 100644 index 00000000..53490fb3 --- /dev/null +++ b/testdata/embed/tutorials/making-gin/index.html @@ -0,0 +1,2 @@ + +This is another simple embedded page. diff --git a/testdata/embed/tutorials/making-gin/main.js b/testdata/embed/tutorials/making-gin/main.js new file mode 100644 index 00000000..5780c658 --- /dev/null +++ b/testdata/embed/tutorials/making-gin/main.js @@ -0,0 +1 @@ +console.log("This is a simple embedded JavaScript file."); From 8ef5a662945eb0549cb9d920a14b2be05935028a Mon Sep 17 00:00:00 2001 From: Nico Clack <16543008+hedgehog125@users.noreply.github.com> Date: Sun, 23 Nov 2025 14:01:27 +0000 Subject: [PATCH 2/2] Add test for nested JavaScript file --- routes_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/routes_test.go b/routes_test.go index 3339fe0f..b4a1c085 100644 --- a/routes_test.go +++ b/routes_test.go @@ -669,6 +669,22 @@ func TestRouteStaticFSCleansPath(t *testing.T) { assert.Contains(t, w.Body.String(), "This is another simple embedded page.") } +func TestRouteStaticFSNestedJsFile(t *testing.T) { + router := New() + subFS, err := fs.Sub(embeddedFolder, embeddedPath) + require.NoError(t, err) + fs := &OnlyFilesFS{ + FileSystem: http.FS(subFS), + } + router.StaticFS("/", fs) + router.NoRoute(func(c *Context) { + c.String(http.StatusNotFound, "non existent") + }) + + w := PerformRequest(router, http.MethodGet, "/tutorials/making-gin/main.js") + assert.Contains(t, w.Body.String(), "console.log(\"This is a simple embedded JavaScript file.\");") +} + func TestRouterStaticFSFileNotFound(t *testing.T) { router := New()