Add files via upload

This commit is contained in:
風雪 authored and GitHub committed 2026-08-16 15:58:35 +08:00
1 parent 15f0a1725d
commit 7f7873e9d8
1 file changed
+37 -9
+37 -9
View File
@@ -50,21 +50,49 @@ func NewProcessManager(binary, config string) *ProcessManager {
}
}
// isAllowedBinaryPath restricts where the Mihomo binary can reside.
// This prevents an attacker from tricking 3m-ui into executing an arbitrary
// file by manipulating the configuration path.
func isAllowedBinaryPath(path string) bool {
allowedPrefixes := []string{
"/usr/local/bin/",
"/usr/bin/",
"/opt/",
// productionAllowedBinaryPrefixes is the hard-coded allowlist used in
// production. It prevents an attacker from tricking 3m-ui into executing an
// arbitrary file by manipulating the configured binary path.
var productionAllowedBinaryPrefixes = []string{
"/usr/local/bin/",
"/usr/bin/",
"/opt/",
}
// extraAllowedBinaryPrefixes is consulted by isAllowedBinaryPath in addition
// to the production list. Production code never sets this; unit tests may
// temporarily allow paths under t.TempDir() via AllowBinaryPathPrefixForTesting.
var extraAllowedBinaryPrefixes []string
// AllowBinaryPathPrefixForTesting appends a directory prefix that
// isAllowedBinaryPath will accept. Intended only for unit tests that need a
// fake mihomo binary under t.TempDir(). Callers should restore the previous
// state with the returned function (typically via t.Cleanup).
func AllowBinaryPathPrefixForTesting(prefix string) (restore func()) {
prev := append([]string(nil), extraAllowedBinaryPrefixes...)
clean := filepath.Clean(prefix)
if clean != "" && !strings.HasSuffix(clean, string(filepath.Separator)) {
clean += string(filepath.Separator)
}
extraAllowedBinaryPrefixes = append(extraAllowedBinaryPrefixes, clean)
return func() {
extraAllowedBinaryPrefixes = prev
}
}
// isAllowedBinaryPath restricts where the Mihomo binary can reside.
func isAllowedBinaryPath(path string) bool {
clean := filepath.Clean(path)
for _, prefix := range allowedPrefixes {
for _, prefix := range productionAllowedBinaryPrefixes {
if strings.HasPrefix(clean, prefix) {
return true
}
}
for _, prefix := range extraAllowedBinaryPrefixes {
if prefix != "" && strings.HasPrefix(clean, prefix) {
return true
}
}
return false
}