Skip to content

Commit

Permalink
Fix: potential vulnerability in http provider (#2680)
Browse files Browse the repository at this point in the history
  • Loading branch information
M4rtin Hsu authored Apr 16, 2023
1 parent 8e05fbf commit df61a58
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion adapter/provider/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
types "github.com/Dreamacro/clash/constant/provider"
)

var errVehicleType = errors.New("unsupport vehicle type")
var (
errVehicleType = errors.New("unsupport vehicle type")
errSubPath = errors.New("path is not subpath of home directory")
)

type healthCheckSchema struct {
Enable bool `provider:"enable"`
Expand Down Expand Up @@ -53,6 +56,9 @@ func ParseProxyProvider(name string, mapping map[string]any) (types.ProxyProvide
case "file":
vehicle = NewFileVehicle(path)
case "http":
if !C.Path.IsSubPath(path) {
return nil, fmt.Errorf("%w: %s", errSubPath, path)
}
vehicle = NewHTTPVehicle(schema.URL, path)
default:
return nil, fmt.Errorf("%w: %s", errVehicleType, schema.Type)
Expand Down
13 changes: 13 additions & 0 deletions constant/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"
P "path"
"path/filepath"
"strings"
)

const Name = "clash"
Expand Down Expand Up @@ -51,6 +52,18 @@ func (p *path) Resolve(path string) string {
return path
}

// IsSubPath return true if path is a subpath of homedir
func (p *path) IsSubPath(path string) bool {
homedir := p.HomeDir()
path = p.Resolve(path)
rel, err := filepath.Rel(homedir, path)
if err != nil {
return false
}

return !strings.Contains(rel, "..")
}

func (p *path) MMDB() string {
return P.Join(p.homeDir, "Country.mmdb")
}
Expand Down

0 comments on commit df61a58

Please sign in to comment.