Skip to content

Commit

Permalink
Enabled security while serving static files
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <vr@labstack.com>
  • Loading branch information
vishr committed Feb 22, 2017
1 parent 91d2727 commit d259f88
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 3 additions & 1 deletion echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"net"
"net/http"
"path"
"path/filepath"
"reflect"
"runtime"
"sync"
Expand Down Expand Up @@ -403,7 +404,8 @@ func (e *Echo) Static(prefix, root string) {

func static(i i, prefix, root string) {
h := func(c Context) error {
return c.File(path.Join(root, c.Param("*")))
name := filepath.Join(root, path.Clean("/"+c.Param("*"))) // `/` for security
return c.File(name)
}
i.GET(prefix, h)
if prefix == "/" {
Expand Down
7 changes: 4 additions & 3 deletions middleware/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -66,11 +67,11 @@ func StaticWithConfig(config StaticConfig) echo.MiddlewareFunc {

return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
path := c.Request().URL.Path
p := c.Request().URL.Path
if strings.HasSuffix(c.Path(), "*") { // When serving from a group, e.g. `/static*`.
path = c.Param("*")
p = c.Param("*")
}
name := filepath.Join(config.Root, path)
name := filepath.Join(config.Root, path.Clean("/"+p)) // "/"+ for security

fi, err := os.Stat(name)
if err != nil {
Expand Down

0 comments on commit d259f88

Please sign in to comment.