-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from KonferCA/71-be-patch-directory-traversal-…
…in-static-file-serving Fix/71/prevent-directory-traversal
- Loading branch information
Showing
2 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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,69 @@ | ||
package server | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestStaticFileServing(t *testing.T) { | ||
// setup test directory structure | ||
staticDir := "static/dist" | ||
assetsDir := filepath.Join(staticDir, "assets", "img") | ||
err := os.MkdirAll(assetsDir, 0755) | ||
assert.NoError(t, err) | ||
defer os.RemoveAll("static") // cleanup after test | ||
|
||
// create a test file | ||
testFile := filepath.Join(assetsDir, "logo.png") | ||
err = os.WriteFile(testFile, []byte("test content"), 0644) | ||
assert.NoError(t, err) | ||
|
||
// create index.html | ||
err = os.WriteFile(filepath.Join(staticDir, "index.html"), []byte("<html>test</html>"), 0644) | ||
assert.NoError(t, err) | ||
|
||
// setup test server | ||
s, err := New(true) | ||
assert.NoError(t, err) | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
expectedCode int | ||
}{ | ||
{ | ||
name: "normal asset path", | ||
path: "/assets/img/logo.png", | ||
expectedCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "path traversal attempt 1", | ||
path: "/assets/../../../etc/passwd", | ||
expectedCode: http.StatusForbidden, | ||
}, | ||
{ | ||
name: "path traversal attempt 2", | ||
path: "/assets/%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd", | ||
expectedCode: http.StatusForbidden, | ||
}, | ||
{ | ||
name: "path traversal attempt 3", | ||
path: "/assets/..%2f..%2f..%2fetc%2fpasswd", | ||
expectedCode: http.StatusForbidden, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := httptest.NewRequest(http.MethodGet, tt.path, nil) | ||
rec := httptest.NewRecorder() | ||
s.echoInstance.ServeHTTP(rec, req) | ||
assert.Equal(t, tt.expectedCode, rec.Code) | ||
}) | ||
} | ||
} |