mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-20 17:10:08 +08:00
chore: allow setting addition safePaths by environment variable SAFE_PATHS
Some checks failed
Test / test (1.20, ubuntu-latest) (push) Failing after 1s
Test / test (1.21, ubuntu-latest) (push) Failing after 1s
Test / test (1.22, ubuntu-latest) (push) Failing after 1s
Test / test (1.23, ubuntu-latest) (push) Failing after 1s
Test / test (1.24, ubuntu-latest) (push) Failing after 1s
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 1s
Test / test (1.20, macos-13) (push) Has been cancelled
Test / test (1.20, macos-latest) (push) Has been cancelled
Test / test (1.20, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.20, windows-latest) (push) Has been cancelled
Test / test (1.21, macos-13) (push) Has been cancelled
Test / test (1.21, macos-latest) (push) Has been cancelled
Test / test (1.21, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.21, windows-latest) (push) Has been cancelled
Test / test (1.22, macos-13) (push) Has been cancelled
Test / test (1.22, macos-latest) (push) Has been cancelled
Test / test (1.22, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.22, windows-latest) (push) Has been cancelled
Test / test (1.23, macos-13) (push) Has been cancelled
Test / test (1.23, macos-latest) (push) Has been cancelled
Test / test (1.23, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.23, windows-latest) (push) Has been cancelled
Test / test (1.24, macos-13) (push) Has been cancelled
Test / test (1.24, macos-latest) (push) Has been cancelled
Test / test (1.24, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.24, windows-latest) (push) Has been cancelled
Some checks failed
Test / test (1.20, ubuntu-latest) (push) Failing after 1s
Test / test (1.21, ubuntu-latest) (push) Failing after 1s
Test / test (1.22, ubuntu-latest) (push) Failing after 1s
Test / test (1.23, ubuntu-latest) (push) Failing after 1s
Test / test (1.24, ubuntu-latest) (push) Failing after 1s
Trigger CMFA Update / trigger-CMFA-update (push) Failing after 1s
Test / test (1.20, macos-13) (push) Has been cancelled
Test / test (1.20, macos-latest) (push) Has been cancelled
Test / test (1.20, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.20, windows-latest) (push) Has been cancelled
Test / test (1.21, macos-13) (push) Has been cancelled
Test / test (1.21, macos-latest) (push) Has been cancelled
Test / test (1.21, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.21, windows-latest) (push) Has been cancelled
Test / test (1.22, macos-13) (push) Has been cancelled
Test / test (1.22, macos-latest) (push) Has been cancelled
Test / test (1.22, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.22, windows-latest) (push) Has been cancelled
Test / test (1.23, macos-13) (push) Has been cancelled
Test / test (1.23, macos-latest) (push) Has been cancelled
Test / test (1.23, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.23, windows-latest) (push) Has been cancelled
Test / test (1.24, macos-13) (push) Has been cancelled
Test / test (1.24, macos-latest) (push) Has been cancelled
Test / test (1.24, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.24, windows-latest) (push) Has been cancelled
package managers can allow for pre-defined safe paths without disabling the entire security check feature for https://github.com/MetaCubeX/mihomo/issues/2004
This commit is contained in:
parent
7e7016b567
commit
791ea5e568
@ -37,13 +37,23 @@ var Path = func() *path {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &path{homeDir: homeDir, configFile: "config.yaml", allowUnsafePath: allowUnsafePath}
|
var safePaths []string
|
||||||
|
for _, safePath := range strings.Split(os.Getenv("SAFE_PATHS"), ":") {
|
||||||
|
safePath = strings.TrimSpace(safePath)
|
||||||
|
if len(safePath) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
safePaths = append(safePaths, safePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &path{homeDir: homeDir, configFile: "config.yaml", allowUnsafePath: allowUnsafePath, safePaths: safePaths}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
type path struct {
|
type path struct {
|
||||||
homeDir string
|
homeDir string
|
||||||
configFile string
|
configFile string
|
||||||
allowUnsafePath bool
|
allowUnsafePath bool
|
||||||
|
safePaths []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHomeDir is used to set the configuration path
|
// SetHomeDir is used to set the configuration path
|
||||||
@ -72,19 +82,22 @@ func (p *path) Resolve(path string) string {
|
|||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsSafePath return true if path is a subpath of homedir
|
// IsSafePath return true if path is a subpath of homedir (or in the SAFE_PATHS environment variable)
|
||||||
func (p *path) IsSafePath(path string) bool {
|
func (p *path) IsSafePath(path string) bool {
|
||||||
if p.allowUnsafePath || features.CMFA {
|
if p.allowUnsafePath || features.CMFA {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
homedir := p.HomeDir()
|
homedir := p.HomeDir()
|
||||||
path = p.Resolve(path)
|
path = p.Resolve(path)
|
||||||
rel, err := filepath.Rel(homedir, path)
|
safePaths := append([]string{homedir}, p.safePaths...) // add homedir to safePaths
|
||||||
if err != nil {
|
for _, safePath := range safePaths {
|
||||||
return false
|
if rel, err := filepath.Rel(safePath, path); err == nil {
|
||||||
|
if filepath.IsLocal(rel) {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return filepath.IsLocal(rel)
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *path) GetPathByHash(prefix, name string) string {
|
func (p *path) GetPathByHash(prefix, name string) string {
|
||||||
|
|||||||
41
constant/path_test.go
Normal file
41
constant/path_test.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package constant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPath(t *testing.T) {
|
||||||
|
assert.False(t, (&path{}).IsSafePath("/usr/share/metacubexd/"))
|
||||||
|
assert.True(t, (&path{
|
||||||
|
safePaths: []string{"/usr/share/metacubexd"},
|
||||||
|
}).IsSafePath("/usr/share/metacubexd/"))
|
||||||
|
|
||||||
|
assert.False(t, (&path{}).IsSafePath("../metacubexd/"))
|
||||||
|
assert.True(t, (&path{
|
||||||
|
homeDir: "/usr/share/mihomo",
|
||||||
|
safePaths: []string{"/usr/share/metacubexd"},
|
||||||
|
}).IsSafePath("../metacubexd/"))
|
||||||
|
assert.False(t, (&path{
|
||||||
|
homeDir: "/usr/share/mihomo",
|
||||||
|
safePaths: []string{"/usr/share/ycad"},
|
||||||
|
}).IsSafePath("../metacubexd/"))
|
||||||
|
|
||||||
|
assert.False(t, (&path{}).IsSafePath("/opt/mykeys/key1.key"))
|
||||||
|
assert.True(t, (&path{
|
||||||
|
safePaths: []string{"/opt/mykeys"},
|
||||||
|
}).IsSafePath("/opt/mykeys/key1.key"))
|
||||||
|
assert.True(t, (&path{
|
||||||
|
safePaths: []string{"/opt/mykeys/"},
|
||||||
|
}).IsSafePath("/opt/mykeys/key1.key"))
|
||||||
|
assert.True(t, (&path{
|
||||||
|
safePaths: []string{"/opt/mykeys/key1.key"},
|
||||||
|
}).IsSafePath("/opt/mykeys/key1.key"))
|
||||||
|
|
||||||
|
assert.True(t, (&path{}).IsSafePath("key1.key"))
|
||||||
|
assert.True(t, (&path{}).IsSafePath("./key1.key"))
|
||||||
|
assert.True(t, (&path{}).IsSafePath("./mykey/key1.key"))
|
||||||
|
assert.True(t, (&path{}).IsSafePath("./mykey/../key1.key"))
|
||||||
|
assert.False(t, (&path{}).IsSafePath("./mykey/../../key1.key"))
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user