Skip to content

Commit 24c3e7f

Browse files
amustaque97Corey Daley
andauthored
Fix Single Page Application example in README.md file (#678)
Fixes #588 **Summary of Changes** 1. Add test case to validate proposed fix (both negative and positive test case). 2. Update `README.md` file. PS: If you want to verify how I'm able to reproduce the issue and tried my fix. Here is a link of my local PR where I have configured `Github Action` to execute same test cases on all the platforms i.e. `ubuntu`, `macos` and `windows`. PR link: amustaque97#1 > PS: Make sure your PR includes/updates tests! If you need help with this part, just ask! --------- Co-authored-by: Corey Daley <[email protected]>
1 parent 546dd0c commit 24c3e7f

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,20 +247,11 @@ type spaHandler struct {
247247
// file located at the index path on the SPA handler will be served. This
248248
// is suitable behavior for serving an SPA (single page application).
249249
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
250-
// get the absolute path to prevent directory traversal
251-
path, err := filepath.Abs(r.URL.Path)
252-
if err != nil {
253-
// if we failed to get the absolute path respond with a 400 bad request
254-
// and stop
255-
http.Error(w, err.Error(), http.StatusBadRequest)
256-
return
257-
}
258-
259-
// prepend the path with the path to the static directory
260-
path = filepath.Join(h.staticPath, path)
250+
// Join internally call path.Clean to prevent directory traversal
251+
path := filepath.Join(h.staticPath, path)
261252

262253
// check whether a file exists at the given path
263-
_, err = os.Stat(path)
254+
_, err := os.Stat(path)
264255
if os.IsNotExist(err) {
265256
// file does not exist, serve index.html
266257
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))

0 commit comments

Comments
 (0)