mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-27 05:27:00 +00:00
limit the file ext for downloads
This commit is contained in:
@@ -2,6 +2,8 @@ package conf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"github.com/xtls/xray-core/app/geodata"
|
"github.com/xtls/xray-core/app/geodata"
|
||||||
@@ -19,6 +21,9 @@ func (c *GeodataAssetConfig) Build() (*geodata.Asset, error) {
|
|||||||
if err := validateHTTPS(c.URL); err != nil {
|
if err := validateHTTPS(c.URL); err != nil {
|
||||||
return nil, errors.New("invalid geodata asset url: ", c.URL).Base(err)
|
return nil, errors.New("invalid geodata asset url: ", c.URL).Base(err)
|
||||||
}
|
}
|
||||||
|
if !strings.EqualFold(filepath.Ext(c.File), ".dat") {
|
||||||
|
return nil, errors.New("geodata asset file extension must be .dat")
|
||||||
|
}
|
||||||
if _, err := filesystem.StatAsset(c.File); err != nil {
|
if _, err := filesystem.StatAsset(c.File); err != nil {
|
||||||
return nil, errors.New("invalid geodata asset file: ", c.File).Base(err)
|
return nil, errors.New("invalid geodata asset file: ", c.File).Base(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package conf_test
|
package conf_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/xtls/xray-core/app/geodata"
|
"github.com/xtls/xray-core/app/geodata"
|
||||||
@@ -73,3 +75,34 @@ func TestGeodataAssetConfigInvalidURL(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGeodataAssetConfigFileExtension(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
t.Setenv("xray.location.asset", dir)
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
valid bool
|
||||||
|
}{
|
||||||
|
{"custom.dat", true},
|
||||||
|
{"custom.DAT", true},
|
||||||
|
{"launcher.cmd", false},
|
||||||
|
{"script.lua", false},
|
||||||
|
{"config.json", false},
|
||||||
|
{"asset", false},
|
||||||
|
} {
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, tc.name), nil, 0o600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_, err := (&GeodataAssetConfig{
|
||||||
|
URL: "https://example.com/download",
|
||||||
|
File: tc.name,
|
||||||
|
}).Build()
|
||||||
|
if tc.valid {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Build(%q): %v", tc.name, err)
|
||||||
|
}
|
||||||
|
} else if err == nil || !strings.Contains(err.Error(), "extension must be .dat") {
|
||||||
|
t.Fatalf("Build(%q) = %v; want .dat extension error", tc.name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user