mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-10-16 20:44:03 +00:00
Lua: Sandbox by default (whitelisted functions); Disable inline scripts for now; Add more checks for security reasons (#7136)
https://github.com/XTLS/Xray-core/pull/7136#issuecomment-6158632073 https://github.com/XTLS/Xray-core/pull/7136#issuecomment-6170831601 https://github.com/XTLS/Xray-core/pull/7136#issuecomment-6184362904
This commit is contained in:
@@ -17,12 +17,12 @@ import (
|
||||
)
|
||||
|
||||
func checkFile(file, code string) error {
|
||||
r, err := filesystem.OpenAsset(file)
|
||||
r, size, err := filesystem.OpenAsset(file)
|
||||
if err != nil {
|
||||
return errors.New("failed to open ", file).Base(err)
|
||||
}
|
||||
defer r.Close()
|
||||
if _, err := find(r, []byte(code), false); err != nil {
|
||||
if _, err := find(r, []byte(code), false, size); err != nil {
|
||||
return errors.New("failed to check code ", code, " from ", file).Base(err)
|
||||
}
|
||||
return nil
|
||||
@@ -30,12 +30,12 @@ func checkFile(file, code string) error {
|
||||
|
||||
func loadFile(file, code string) ([]byte, error) {
|
||||
runtime.GC() // peak mem
|
||||
r, err := filesystem.OpenAsset(file)
|
||||
r, size, err := filesystem.OpenAsset(file)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to open ", file).Base(err)
|
||||
}
|
||||
defer r.Close()
|
||||
bs, err := find(r, []byte(code), true)
|
||||
bs, err := find(r, []byte(code), true, size)
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to load code ", code, " from ", file).Base(err)
|
||||
}
|
||||
@@ -60,13 +60,13 @@ func loadIP(file, code string) ([]*CIDR, error) {
|
||||
// unmarshalling it into a []*Domain, so value is only valid during fn.
|
||||
func loadSite(file, code, attrs string, fn func(Domain_Type, []byte)) error {
|
||||
runtime.GC() // peak mem
|
||||
r, err := filesystem.OpenAsset(file)
|
||||
r, size, err := filesystem.OpenAsset(file)
|
||||
if err != nil {
|
||||
return errors.New("failed to open ", file).Base(err)
|
||||
}
|
||||
defer r.Close()
|
||||
br := bufio.NewReaderSize(r, 64*1024)
|
||||
n, err := seek(br, []byte(code))
|
||||
n, err := seek(br, []byte(code), size)
|
||||
if err != nil {
|
||||
return errors.New("failed to load code ", code, " from ", file).Base(err)
|
||||
}
|
||||
@@ -114,6 +114,9 @@ func decodeVarint(br *bufio.Reader) (uint64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if shift == 63 && b > 1 {
|
||||
break // Only one value bit fits in the tenth byte.
|
||||
}
|
||||
x |= (uint64(b) & 0x7F) << shift
|
||||
if (b & 0x80) == 0 {
|
||||
return x, nil
|
||||
@@ -123,9 +126,9 @@ func decodeVarint(br *bufio.Reader) (uint64, error) {
|
||||
return 0, errors.New("varint overflow")
|
||||
}
|
||||
|
||||
func find(r io.Reader, code []byte, readBody bool) ([]byte, error) {
|
||||
func find(r io.Reader, code []byte, readBody bool, size int64) ([]byte, error) {
|
||||
br := bufio.NewReaderSize(r, 64*1024)
|
||||
bodyL, err := seek(br, code)
|
||||
bodyL, err := seek(br, code, size)
|
||||
if err != nil || !readBody {
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,7 +140,7 @@ func find(r io.Reader, code []byte, readBody bool) ([]byte, error) {
|
||||
}
|
||||
|
||||
// seek advances br to the body of the entry for code and returns the body length.
|
||||
func seek(br *bufio.Reader, code []byte) (int, error) {
|
||||
func seek(br *bufio.Reader, code []byte, size int64) (int, error) {
|
||||
codeL := len(code)
|
||||
if codeL == 0 {
|
||||
return 0, errors.New("empty code")
|
||||
@@ -145,7 +148,8 @@ func seek(br *bufio.Reader, code []byte) (int, error) {
|
||||
need := 2 + codeL // TODO: if code too long
|
||||
|
||||
for {
|
||||
if _, err := br.ReadByte(); err != nil {
|
||||
tag, err := br.ReadByte()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
@@ -153,10 +157,10 @@ func seek(br *bufio.Reader, code []byte) (int, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
bodyL := int(x)
|
||||
if bodyL <= 0 {
|
||||
return 0, errors.New("invalid body length: ", bodyL)
|
||||
if x == 0 || x > uint64(size) || x > uint64(^uint(0)>>1) {
|
||||
return 0, errors.New("invalid body length: ", x)
|
||||
}
|
||||
bodyL := int(x)
|
||||
|
||||
// Peek no more than the buffer holds: a code longer than the buffer cannot match a single
|
||||
// length byte anyway, so a short peek only skips it, as base find (io.ReadFull) does.
|
||||
@@ -167,7 +171,8 @@ func seek(br *bufio.Reader, code []byte) (int, error) {
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
if bodyL >= need && len(prefix) >= need && int(prefix[1]) == codeL && bytes.Equal(prefix[2:], code) {
|
||||
if tag == 0x0a && bodyL >= need && len(prefix) >= need &&
|
||||
prefix[0] == 0x0a && int(prefix[1]) == codeL && bytes.Equal(prefix[2:], code) {
|
||||
return bodyL, nil
|
||||
}
|
||||
if _, err := br.Discard(bodyL); err != nil {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package geodata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -12,6 +13,58 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func TestFindChecksEntryAndCodeTags(t *testing.T) {
|
||||
for _, payload := range [][]byte{
|
||||
{0x12, 6, 0x0a, 4, 'T', 'E', 'S', 'T'}, // Unknown field posing as entry.
|
||||
{0x08, 6, 0x0a, 4, 'T', 'E', 'S', 'T'}, // Wrong entry wire type.
|
||||
{0x0a, 6, 0x1a, 4, 'T', 'E', 'S', 'T'}, // Unknown field posing as code.
|
||||
{0x0a, 6, 0x08, 4, 'T', 'E', 'S', 'T'}, // Wrong code wire type.
|
||||
} {
|
||||
if _, err := find(bytes.NewReader(payload), []byte("TEST"), true, int64(len(payload))); err == nil {
|
||||
t.Fatalf("matched incorrect entry/code tags in %x", payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileRejectsVarintOverflow(t *testing.T) {
|
||||
payload := []byte{
|
||||
0x0a,
|
||||
0x86, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x02, // 2^64 + 6
|
||||
0x0a, 0x04, 'T', 'E', 'S', 'T',
|
||||
}
|
||||
dir := t.TempDir()
|
||||
t.Setenv("xray.location.asset", dir)
|
||||
if err := os.WriteFile(filepath.Join(dir, "overflow.dat"), payload, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := loadFile("overflow.dat", "TEST"); err == nil || !strings.Contains(err.Error(), "varint overflow") {
|
||||
t.Fatalf("want varint overflow, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFileRejectsOversizedEntry(t *testing.T) {
|
||||
payload := []byte{
|
||||
0x0a, 0x03, 0x0a, 0x01, 'A', // A valid entry before the oversized one.
|
||||
0x0a, 0x80, 0x80, 0x80, 0x80, 0x04, // Entry length: 1 GiB.
|
||||
0x0a, 0x04, 'T', 'E', 'S', 'T',
|
||||
}
|
||||
dir := t.TempDir()
|
||||
t.Setenv("xray.location.asset", dir)
|
||||
if err := os.WriteFile(filepath.Join(dir, "oversized.dat"), payload, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Check without allocating the body first, so a regression fails safely.
|
||||
if err := checkFile("oversized.dat", "TEST"); err == nil {
|
||||
t.Fatal("accepted an entry length larger than the file")
|
||||
}
|
||||
if _, err := loadFile("oversized.dat", "TEST"); err == nil || !strings.Contains(err.Error(), "invalid body length") {
|
||||
t.Fatalf("want invalid body length, got %v", err)
|
||||
}
|
||||
if err := loadSite("oversized.dat", "TEST", "", func(Domain_Type, []byte) {}); err == nil || !strings.Contains(err.Error(), "invalid body length") {
|
||||
t.Fatalf("want invalid body length, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type siteEntry struct {
|
||||
Type Domain_Type
|
||||
Value string
|
||||
|
||||
@@ -44,8 +44,8 @@ func RegisterLua(L *lua.LState) {
|
||||
"Match": luaIPMatch,
|
||||
"AnyMatch": luaIPAnyMatch,
|
||||
"Matches": luaIPMatches,
|
||||
"FilterIPs": newLuaIPFilterIPs(xlua.NewSlicePusher[net.IP](L)),
|
||||
})
|
||||
"FilterIPs": newLuaIPFilterIPs(xlua.NewReadOnlySlicePusher[net.IP](L)),
|
||||
}, "SetReverse", "ToggleReverse")
|
||||
return 1
|
||||
}))
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ local geodata = require("xray.geodata")
|
||||
local matcher = geodata.BuildIPMatcher("127.0.0.0/8")
|
||||
assert(matcher.Match == matcher.match and matcher.AnyMatch == matcher.anyMatch)
|
||||
assert(matcher.Matches == matcher.matches and matcher.FilterIPs == matcher.filterIPs)
|
||||
assert(matcher.Reload == nil and matcher.reload == nil)
|
||||
assert(matcher:match(ip))
|
||||
assert(matcher:anyMatch({ip}) and matcher:matches({ip}))
|
||||
assert(not matcher:AnyMatch(nil))
|
||||
@@ -97,6 +98,11 @@ assert(matcher.missing == nil)
|
||||
|
||||
local domain = geodata.BuildDomainMatcher("full:example.com")
|
||||
assert(domain.Match == domain.match and domain.MatchAny == domain.matchAny)
|
||||
assert(domain.Reload == nil and domain.reload == nil)
|
||||
for _, value in ipairs({matcher, domain}) do
|
||||
assert(not pcall(function() return -value end))
|
||||
assert(not pcall(function() return value ^ {} end))
|
||||
end
|
||||
assert(domain:matchAny("example.com"))
|
||||
assert(#domain:Match("example.com") == 1)
|
||||
assert(domain:match("example.com")[1] == 0)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// DO NOT EDIT. Regression test for app/geodata's luaGuardHeader.
|
||||
|
||||
package lua
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/yuin/gopher-lua/parse"
|
||||
)
|
||||
|
||||
func TestGeodataLuaGuard(t *testing.T) {
|
||||
_, err := parse.Parse(strings.NewReader("\x12\x01\x00function HandleRoute() end"), "geodata")
|
||||
var syntaxErr *parse.Error
|
||||
if !errors.As(err, &syntaxErr) || syntaxErr.Pos.Line != 1 || syntaxErr.Pos.Column != 1 {
|
||||
t.Fatalf("want Lua rejection at the first byte, got %v", err)
|
||||
}
|
||||
}
|
||||
+95
-4
@@ -1,6 +1,9 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
|
||||
glua "github.com/yuin/gopher-lua"
|
||||
luar "layeh.com/gopher-luar"
|
||||
)
|
||||
@@ -9,7 +12,7 @@ import (
|
||||
// The returned function wraps slices without reflection or metatable lookup,
|
||||
// and pushes nil for nil slices. Use it with this state or its coroutines.
|
||||
func NewSlicePusher[T any](L *glua.LState) func(*glua.LState, []T) {
|
||||
metatable := luar.New(L, []T{}).(*glua.LUserData).Metatable
|
||||
metatable := luar.MT(L, []T(nil)).LTable
|
||||
return func(L *glua.LState, values []T) {
|
||||
if values == nil {
|
||||
L.Push(glua.LNil)
|
||||
@@ -22,21 +25,105 @@ func NewSlicePusher[T any](L *glua.LState) func(*glua.LState, []T) {
|
||||
}
|
||||
}
|
||||
|
||||
// NewReadOnlySlicePusher borrows slices without copying and recursively makes
|
||||
// nested slice types read-only through their shared metatables. IP byte views
|
||||
// and masks are also read-only. Other referenced elements retain their mutability.
|
||||
// Element bindings are initialized on first indexing or iteration.
|
||||
func NewReadOnlySlicePusher[T any](L *glua.LState) func(*glua.LState, []T) {
|
||||
sliceMetatable := luar.MT(L, []T{})
|
||||
if sliceMetatable.RawGetString("__readonly") != glua.LTrue {
|
||||
readOnly := L.NewFunction(func(L *glua.LState) int {
|
||||
L.RaiseError("slice is read-only")
|
||||
return 0
|
||||
})
|
||||
setSliceReadOnly[[]T](L, readOnly)
|
||||
}
|
||||
return NewSlicePusher[T](L)
|
||||
}
|
||||
|
||||
func setSliceReadOnly[S ~[]E, E any](L *glua.LState, readOnly *glua.LFunction) {
|
||||
var visit func(reflect.Type)
|
||||
visit = func(typ reflect.Type) {
|
||||
if typ.Kind() != reflect.Slice {
|
||||
return
|
||||
}
|
||||
metatable := luar.MT(L, reflect.Zero(typ).Interface())
|
||||
if metatable.RawGetString("__readonly") == glua.LTrue {
|
||||
return
|
||||
}
|
||||
// Mark before descending because named slice types can be recursive.
|
||||
metatable.RawSetString("__readonly", glua.LTrue)
|
||||
metatable.RawSetString("__newindex", readOnly)
|
||||
// Reject explicitly so Lua cannot fall back to the other operand.
|
||||
metatable.RawSetString("__add", readOnly)
|
||||
if typ == reflect.TypeFor[net.IP]() {
|
||||
// A nil receiver lets AppendText return its argument as a writable []byte.
|
||||
methods := metatable.RawGetString("methods").(*glua.LTable)
|
||||
appendText := methods.RawGetString("AppendText")
|
||||
for key, method := methods.Next(glua.LNil); key != glua.LNil; key, method = methods.Next(key) {
|
||||
if method == appendText {
|
||||
methods.RawSet(key, glua.LNil)
|
||||
}
|
||||
}
|
||||
visit(reflect.TypeFor[net.IPMask]())
|
||||
}
|
||||
if element := typ.Elem(); element.Kind() == reflect.Slice {
|
||||
index := metatable.RawGetString("__index").(*glua.LFunction)
|
||||
call := metatable.RawGetString("__call").(*glua.LFunction)
|
||||
initialize := func() {
|
||||
visit(element)
|
||||
metatable.RawSetString("__index", index)
|
||||
metatable.RawSetString("__call", call)
|
||||
}
|
||||
metatable.RawSetString("__index", L.NewFunction(func(L *glua.LState) int {
|
||||
initialize()
|
||||
return index.GFunction(L)
|
||||
}))
|
||||
metatable.RawSetString("__call", L.NewFunction(func(L *glua.LState) int {
|
||||
initialize()
|
||||
return call.GFunction(L)
|
||||
}))
|
||||
}
|
||||
}
|
||||
visit(reflect.TypeFor[S]())
|
||||
}
|
||||
|
||||
// DirectMethod handles a Lua call without luar's reflected method invocation.
|
||||
// It returns the result count and whether it handled the arguments. On false,
|
||||
// it must leave the stack unchanged for the original luar wrapper.
|
||||
type DirectMethod func(L *glua.LState) (nresults int, handled bool)
|
||||
|
||||
// PushMethods pushes a luar userdata exposing only the named Go methods and
|
||||
// their aliases through reflected calls. The method list is fixed by the first
|
||||
// binding of that Go type in each LState.
|
||||
func PushMethods(L *glua.LState, value any, methods ...string) {
|
||||
PushWithDirectMethods(L, value, nil, methods...)
|
||||
}
|
||||
|
||||
// PushWithDirectMethods pushes a luar userdata with typed Go method bindings.
|
||||
// Handled calls bypass luar's argument conversion and reflect.Call; method lookup
|
||||
// uses the methods table directly instead of luar's reflected __index handler.
|
||||
// value must expose methods only. Bindings and their closures are installed once
|
||||
// per Go type per LState, outside the method-call hot path.
|
||||
func PushWithDirectMethods(L *glua.LState, value any, directMethods map[string]DirectMethod) {
|
||||
// Only directMethods and additionalMethods are exposed, including their aliases.
|
||||
// Bindings and their closures are installed once per Go type per LState, outside
|
||||
// the method-call hot path.
|
||||
func PushWithDirectMethods(L *glua.LState, value any, directMethods map[string]DirectMethod, additionalMethods ...string) {
|
||||
userdata := luar.New(L, value).(*glua.LUserData)
|
||||
metatable := userdata.Metatable.(*glua.LTable)
|
||||
methods := metatable.RawGetString("methods").(*glua.LTable)
|
||||
if metatable.RawGetString("__index") != methods {
|
||||
// Keep all aliases for the explicitly exposed methods.
|
||||
exposed := make(map[glua.LValue]bool, len(directMethods)+len(additionalMethods))
|
||||
for name := range directMethods {
|
||||
exposed[methods.RawGetString(name)] = true
|
||||
}
|
||||
for _, name := range additionalMethods {
|
||||
exposed[methods.RawGetString(name)] = true
|
||||
}
|
||||
for key, method := methods.Next(glua.LNil); key != glua.LNil; key, method = methods.Next(key) {
|
||||
if !exposed[method] {
|
||||
methods.RawSet(key, glua.LNil)
|
||||
}
|
||||
}
|
||||
for name, direct := range directMethods {
|
||||
original := methods.RawGetString(name)
|
||||
fn := L.NewFunction(func(L *glua.LState) int {
|
||||
@@ -52,6 +139,10 @@ func PushWithDirectMethods(L *glua.LState, value any, directMethods map[string]D
|
||||
}
|
||||
}
|
||||
}
|
||||
// Pointer operations would bypass the method-only API.
|
||||
metatable.RawSetString("__newindex", glua.LNil)
|
||||
metatable.RawSetString("__pow", glua.LNil)
|
||||
metatable.RawSetString("__unm", glua.LNil)
|
||||
metatable.RawSetString("__index", methods)
|
||||
}
|
||||
L.Push(userdata)
|
||||
|
||||
@@ -2,6 +2,7 @@ package lua
|
||||
|
||||
import (
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
glua "github.com/yuin/gopher-lua"
|
||||
@@ -47,6 +48,188 @@ assert(ok and result == true)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadOnlySlicePusher(t *testing.T) {
|
||||
L := glua.NewState()
|
||||
defer L.Close()
|
||||
values := []int{3, 5}
|
||||
NewReadOnlySlicePusher[int](L)(L, values)
|
||||
L.SetGlobal("values", L.Get(-1))
|
||||
L.Pop(1)
|
||||
if err := L.DoString(`
|
||||
assert(#values == 2 and values[1] == 3 and values[2] == 5)
|
||||
assert(not pcall(function() values[1] = 7 end))
|
||||
assert(not pcall(function() return values + 7 end))
|
||||
local sum = 0
|
||||
for _, value in values() do sum = sum + value end
|
||||
assert(sum == 8)
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := L.GetGlobal("values").(*glua.LUserData).Value.([]int)
|
||||
if &got[0] != &values[0] || values[0] != 3 || values[1] != 5 {
|
||||
t.Fatal("read-only binding copied or modified the slice")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadOnlyIPSlicePusher(t *testing.T) {
|
||||
L := glua.NewState()
|
||||
defer L.Close()
|
||||
ips := []net.IP{{192, 0, 2, 1}, net.ParseIP("2001:db8::1")}
|
||||
NewReadOnlySlicePusher[net.IP](L)(L, ips)
|
||||
L.SetGlobal("ips", L.Get(-1))
|
||||
L.Pop(1)
|
||||
readOnly := luar.MT(L, ips).RawGetString("__newindex")
|
||||
NewReadOnlySlicePusher[net.IP](L)
|
||||
if luar.MT(L, ips).RawGetString("__newindex") != readOnly {
|
||||
t.Fatal("IP metatables were initialized more than once")
|
||||
}
|
||||
if err := L.DoString(`
|
||||
local function readOnly(value)
|
||||
assert(not pcall(function() value[1] = value[1] end))
|
||||
assert(not pcall(function() return value + value[1] end))
|
||||
end
|
||||
assert(#ips == 2 and ips[1]:String() == "192.0.2.1")
|
||||
readOnly(ips)
|
||||
for _, ip in ips() do
|
||||
readOnly(ip)
|
||||
readOnly(ip:To16())
|
||||
if ip:To4() then
|
||||
readOnly(ip:To4())
|
||||
readOnly(ip:DefaultMask())
|
||||
end
|
||||
assert(ip:Equal(ip:To16()) and type(ip:String()) == "string")
|
||||
assert(ip.AppendText == nil and ip.appendText == nil)
|
||||
local text, err = ip:MarshalText()
|
||||
assert(not err)
|
||||
text[1] = 88 -- Independently owned byte buffers remain writable.
|
||||
assert(not pcall(function() return ips + text end))
|
||||
end
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := L.GetGlobal("ips").(*glua.LUserData).Value.([]net.IP)
|
||||
if &got[0] != &ips[0] || &got[0][0] != &ips[0][0] || &got[1][0] != &ips[1][0] {
|
||||
t.Fatal("read-only binding copied the IP data")
|
||||
}
|
||||
for i, want := range []string{"192.0.2.1", "2001:db8::1"} {
|
||||
if got[i].String() != want {
|
||||
t.Fatalf("IP %d changed to %s; want %s", i, got[i], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadOnlyIPSlicePusherLazy(t *testing.T) {
|
||||
for _, tc := range []struct{ name, access string }{
|
||||
{"index", `local ip = ips[1]`},
|
||||
{"iterate", `local _, ip = ips()()`},
|
||||
{"coroutine", `local co = coroutine.create(function() return ips[1] end); local ok, ip = coroutine.resume(co); assert(ok)`},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
L := glua.NewState()
|
||||
defer L.Close()
|
||||
methods := 0
|
||||
luar.GetConfig(L).MethodNames = func(_ reflect.Type, method reflect.Method) []string {
|
||||
methods++
|
||||
return []string{method.Name}
|
||||
}
|
||||
NewReadOnlySlicePusher[net.IP](L)(L, []net.IP{{192, 0, 2, 1}})
|
||||
L.SetGlobal("ips", L.Get(-1))
|
||||
L.Pop(1)
|
||||
if err := L.DoString(`assert(#ips == 1); assert(not pcall(function() ips[1] = nil end))`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if methods != 0 {
|
||||
t.Fatal("IP methods initialized without reading an element")
|
||||
}
|
||||
source := tc.access + `
|
||||
assert(ip:String() == "192.0.2.1" and ip.AppendText == nil)
|
||||
assert(not pcall(function() ip[1] = 0 end))
|
||||
assert(not pcall(function() ip:To4()[1] = 0 end))
|
||||
assert(not pcall(function() ip:To16()[1] = 0 end))
|
||||
assert(not pcall(function() ip:DefaultMask()[1] = 0 end))
|
||||
`
|
||||
if err := L.DoString(source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
initialized := methods
|
||||
if initialized == 0 {
|
||||
t.Fatal("IP access did not initialize its methods")
|
||||
}
|
||||
if err := L.DoString(source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if methods != initialized {
|
||||
t.Fatal("IP methods initialized more than once")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadOnlyNestedSlicePusher(t *testing.T) {
|
||||
L := glua.NewState()
|
||||
defer L.Close()
|
||||
values := [][][]byte{{{3, 5}}}
|
||||
NewReadOnlySlicePusher[[][]byte](L)(L, values)
|
||||
L.SetGlobal("values", L.Get(-1))
|
||||
L.Pop(1)
|
||||
ips := [][]net.IP{{{192, 0, 2, 1}}}
|
||||
NewReadOnlySlicePusher[[]net.IP](L)(L, ips)
|
||||
L.SetGlobal("ips", L.Get(-1))
|
||||
L.Pop(1)
|
||||
if err := L.DoString(`
|
||||
local function readOnly(value)
|
||||
assert(not pcall(function() value[1] = value[1] end))
|
||||
assert(not pcall(function() return value + value[1] end))
|
||||
end
|
||||
assert(values[1][1][1] == 3 and values[1][1][2] == 5)
|
||||
readOnly(values)
|
||||
for _, group in values() do
|
||||
readOnly(group)
|
||||
for _, bytes in group() do readOnly(bytes) end
|
||||
end
|
||||
readOnly(ips)
|
||||
readOnly(ips[1])
|
||||
local ip = ips[1][1]
|
||||
assert(ip:String() == "192.0.2.1")
|
||||
readOnly(ip)
|
||||
readOnly(ip:To4())
|
||||
readOnly(ip:To16())
|
||||
readOnly(ip:DefaultMask())
|
||||
assert(ip.AppendText == nil and ip.appendText == nil)
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
readOnly := luar.MT(L, ips[0]).RawGetString("__newindex")
|
||||
NewReadOnlySlicePusher[net.IP](L)
|
||||
if luar.MT(L, ips[0]).RawGetString("__newindex") != readOnly {
|
||||
t.Fatal("nested slice metatable was initialized again")
|
||||
}
|
||||
got := L.GetGlobal("values").(*glua.LUserData).Value.([][][]byte)
|
||||
if &got[0][0][0] != &values[0][0][0] || values[0][0][0] != 3 || values[0][0][1] != 5 {
|
||||
t.Fatal("read-only binding copied or modified the nested slice")
|
||||
}
|
||||
}
|
||||
|
||||
type recursiveReadOnlySlice []recursiveReadOnlySlice
|
||||
|
||||
func TestReadOnlyRecursiveSlicePusher(t *testing.T) {
|
||||
L := glua.NewState()
|
||||
defer L.Close()
|
||||
values := make(recursiveReadOnlySlice, 1)
|
||||
values[0] = values
|
||||
NewReadOnlySlicePusher[recursiveReadOnlySlice](L)(L, values)
|
||||
L.SetGlobal("values", L.Get(-1))
|
||||
L.Pop(1)
|
||||
if err := L.DoString(`
|
||||
local nested = values[1][1][1]
|
||||
assert(#nested == 1)
|
||||
assert(not pcall(function() nested[1] = nested end))
|
||||
assert(not pcall(function() return nested + nested end))
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlicePusherMetatablePerState(t *testing.T) {
|
||||
first := glua.NewState()
|
||||
defer first.Close()
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"sync"
|
||||
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
)
|
||||
|
||||
var privilegedKey struct {
|
||||
sync.Once
|
||||
publicKey ed25519.PublicKey
|
||||
}
|
||||
|
||||
// SetPrivilegedPublicKey configures the trusted Ed25519 public key once at
|
||||
// startup, before compiling scripts. An empty value disables authorization.
|
||||
// Nonempty values must be unpadded Base64URL encodings of exactly 32 bytes.
|
||||
func SetPrivilegedPublicKey(value string) error {
|
||||
var publicKey ed25519.PublicKey
|
||||
if value != "" {
|
||||
decoded, err := decodePrivilegedValue(value, ed25519.PublicKeySize)
|
||||
if err != nil {
|
||||
return errors.New("invalid Lua privileged public key").Base(err)
|
||||
}
|
||||
publicKey = ed25519.PublicKey(decoded)
|
||||
}
|
||||
privilegedKey.Do(func() {
|
||||
privilegedKey.publicKey = publicKey
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodePrivilegedValue(value string, size int) ([]byte, error) {
|
||||
encoding := base64.RawURLEncoding.Strict()
|
||||
if len(value) != encoding.EncodedLen(size) {
|
||||
return nil, errors.New("expected an unpadded Base64URL encoding of ", size, " bytes")
|
||||
}
|
||||
decoded, err := encoding.DecodeString(value)
|
||||
if err != nil || len(decoded) != size || encoding.EncodeToString(decoded) != value {
|
||||
return nil, errors.New("expected an unpadded Base64URL encoding of ", size, " bytes")
|
||||
}
|
||||
return decoded, nil
|
||||
}
|
||||
|
||||
func verifyPrivilegedSignature(code []byte) (bool, error) {
|
||||
_, end, value, found := privilegedSignatureLine(code)
|
||||
if !found {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
publicKey := privilegedKey.publicKey
|
||||
if len(publicKey) == 0 {
|
||||
return false, errors.New("Lua privileged signature requires --lua-privileged-pubkey")
|
||||
}
|
||||
signature, err := decodePrivilegedValue(string(bytes.TrimSpace(value)), ed25519.SignatureSize)
|
||||
if err != nil {
|
||||
return false, errors.New("invalid Lua privileged signature").Base(err)
|
||||
}
|
||||
digest := sha256.Sum256(bytes.TrimSpace(code[end:]))
|
||||
if !ed25519.Verify(publicKey, digest[:], signature) {
|
||||
return false, errors.New("Lua privileged signature verification failed")
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// SignPrivilegedScript signs the script body, then prepends or replaces the signature line.
|
||||
func SignPrivilegedScript(code []byte, seed string) ([]byte, error) {
|
||||
decoded, err := decodePrivilegedValue(seed, ed25519.SeedSize)
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid Ed25519 private key seed").Base(err)
|
||||
}
|
||||
|
||||
start, end, _, found := privilegedSignatureLine(code)
|
||||
body := code
|
||||
if found {
|
||||
body = code[end:]
|
||||
}
|
||||
|
||||
digest := sha256.Sum256(bytes.TrimSpace(body))
|
||||
signature := ed25519.Sign(ed25519.NewKeyFromSeed(decoded), digest[:])
|
||||
header := []byte("-- @xray:privileged-signature=" + base64.RawURLEncoding.EncodeToString(signature))
|
||||
|
||||
if !found {
|
||||
if newline := bytes.IndexByte(code, '\n'); newline > 0 && code[newline-1] == '\r' {
|
||||
header = append(header, '\r')
|
||||
}
|
||||
header = append(header, '\n')
|
||||
return append(header, code...), nil
|
||||
}
|
||||
signed := make([]byte, 0, start+len(header)+len(code)-end)
|
||||
signed = append(signed, code[:start]...)
|
||||
signed = append(signed, header...)
|
||||
return append(signed, code[end:]...), nil
|
||||
}
|
||||
|
||||
func privilegedSignatureLine(code []byte) (start, end int, value []byte, found bool) {
|
||||
for rest := code; len(rest) != 0; {
|
||||
start = len(code) - len(rest)
|
||||
var line []byte
|
||||
line, rest, _ = bytes.Cut(rest, []byte{'\n'})
|
||||
line = bytes.TrimSuffix(line, []byte{'\r'})
|
||||
if len(bytes.TrimSpace(line)) == 0 {
|
||||
continue
|
||||
}
|
||||
end = start + len(line)
|
||||
line, found := bytes.CutPrefix(bytes.TrimLeft(line, " \t"), []byte("--"))
|
||||
if !found {
|
||||
return 0, 0, nil, false
|
||||
}
|
||||
value, found = bytes.CutPrefix(bytes.TrimLeft(line, " \t"), []byte("@xray:privileged-signature="))
|
||||
return start, end, value, found
|
||||
}
|
||||
return 0, 0, nil, false
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package lua
|
||||
|
||||
// TODO: Re-enable these tests and their helper when Compile supports privileged signature authorization again.
|
||||
/*
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/ed25519"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func privilegedSeed(t *testing.T) string {
|
||||
t.Helper()
|
||||
seed, err := hex.DecodeString(privilegedTestSeed)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(seed)
|
||||
}
|
||||
|
||||
func TestSignPrivilegedScriptPreservesSource(t *testing.T) {
|
||||
setTestPrivilegedPublicKey(t, privilegedTestPublicKey)
|
||||
seed := privilegedSeed(t)
|
||||
for _, tc := range []struct {
|
||||
name, prefix, header, body, newline string
|
||||
}{
|
||||
{name: "unsigned", body: privilegedFixtureBody, newline: "\n"},
|
||||
{name: "unsigned boundary whitespace", body: " \t\n\n" + privilegedFixtureBody + " \t\n", newline: "\n"},
|
||||
{name: "unsigned CRLF", body: strings.ReplaceAll(privilegedFixtureBody, "\n", "\r\n"), newline: "\r\n"},
|
||||
{name: "replace invalid header", header: privilegedSignatureHeader + "%", body: "\n" + privilegedFixtureBody, newline: "\n"},
|
||||
{name: "replace indented header", prefix: " \t\r\n\n", header: " \t--\t @xray:privileged-signature=% \t", body: "\r\n \t\r\n" + privilegedFixtureBody + "\r\n", newline: "\r\n"},
|
||||
{name: "replace valid header", header: privilegedSignatureHeader + privilegedFixtureSignature, body: "\n" + privilegedFixtureBody, newline: "\n"},
|
||||
{name: "empty script", newline: "\n"},
|
||||
{name: "header without newline", header: privilegedSignatureHeader + "%"},
|
||||
{name: "ordinary comment before marker", body: "-- ordinary comment\n" + privilegedSignatureHeader + "%\nvalue = 1", newline: "\n"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
code := []byte(tc.prefix + tc.header + tc.body)
|
||||
original := bytes.Clone(code)
|
||||
signed, err := SignPrivilegedScript(code, seed)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(code, original) {
|
||||
t.Fatal("signing modified the input buffer")
|
||||
}
|
||||
body := tc.body
|
||||
if tc.header != "" {
|
||||
body = strings.TrimPrefix(body, tc.newline)
|
||||
}
|
||||
header := privilegedSignatureHeader + testSignature(t, body)
|
||||
want := header + tc.newline + tc.body
|
||||
if tc.header != "" {
|
||||
want = tc.prefix + header + tc.body
|
||||
}
|
||||
if string(signed) != want {
|
||||
t.Fatalf("signed source = %q, want %q", signed, want)
|
||||
}
|
||||
program, err := Compile(writeTestScript(t, string(signed)))
|
||||
if err != nil || program == nil || !program.privileged {
|
||||
t.Fatalf("signed source Compile = %v, %v; want privileged program", program, err)
|
||||
}
|
||||
again, err := SignPrivilegedScript(signed, seed)
|
||||
if err != nil || !bytes.Equal(again, signed) {
|
||||
t.Fatalf("resigning changed the source: %q, %v", again, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignPrivilegedScriptRejectsInvalidSeed(t *testing.T) {
|
||||
seed := privilegedSeed(t)
|
||||
for _, value := range []string{
|
||||
"", "%", seed + "=",
|
||||
base64.RawURLEncoding.EncodeToString(make([]byte, ed25519.SeedSize-1)),
|
||||
base64.RawURLEncoding.EncodeToString(make([]byte, ed25519.PrivateKeySize)),
|
||||
} {
|
||||
if signed, err := SignPrivilegedScript([]byte(privilegedFixtureBody), value); signed != nil || err == nil {
|
||||
t.Fatalf("SignPrivilegedScript with invalid seed = %q, %v; want rejection", signed, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
+179
-6
@@ -3,11 +3,14 @@ package lua
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/xtls/xray-core/common/base64"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/platform"
|
||||
glua "github.com/yuin/gopher-lua"
|
||||
"github.com/yuin/gopher-lua/parse"
|
||||
@@ -15,7 +18,8 @@ import (
|
||||
|
||||
// Program holds immutable bytecode that can be run by independent LStates.
|
||||
type Program struct {
|
||||
proto *glua.FunctionProto
|
||||
proto *glua.FunctionProto
|
||||
privileged bool
|
||||
}
|
||||
|
||||
// LStateFactory returns a fully initialized state or nil and an error.
|
||||
@@ -27,10 +31,17 @@ func Compile(script string) (*Program, error) {
|
||||
var code []byte
|
||||
var err error
|
||||
name := "[inline]"
|
||||
privileged := false
|
||||
if strings.HasPrefix(script, base64.GzipPrefix) || strings.HasPrefix(script, base64.Prefix) {
|
||||
// TODO: Restore inline script support and re-enable the related tests.
|
||||
return nil, errors.New("currently unavailable")
|
||||
code, err = base64.Decode(script)
|
||||
} else if name, err = platform.ResolveLuaFile(script); err == nil {
|
||||
code, err = os.ReadFile(name)
|
||||
// TODO: Restore privileged signature authorization and re-enable the related tests and CLI entries.
|
||||
// if err == nil {
|
||||
// privileged, err = verifyPrivilegedSignature(code)
|
||||
// }
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -44,14 +55,26 @@ func Compile(script string) (*Program, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Program{proto: proto}, nil
|
||||
return &Program{proto: proto, privileged: privileged}, nil
|
||||
}
|
||||
|
||||
// NewState creates a state, runs register, executes the program under ctx, and
|
||||
// runs validate. It removes the initialization context before returning a state
|
||||
// owned by the caller.
|
||||
func (p *Program) NewState(ctx context.Context, register func(*glua.LState), validate func(*glua.LState) error) (*glua.LState, error) {
|
||||
L := glua.NewState()
|
||||
// Choose registrySize for the script type's expected stack usage, balancing
|
||||
// per-state memory usage against the number of states retained in a pool.
|
||||
func (p *Program) NewState(ctx context.Context, registrySize int, register func(*glua.LState), validate func(*glua.LState) error) (*glua.LState, error) {
|
||||
options := glua.Options{
|
||||
RegistrySize: registrySize,
|
||||
RegistryMaxSize: glua.RegistrySize,
|
||||
RegistryGrowStep: registrySize,
|
||||
}
|
||||
var L *glua.LState
|
||||
if p.privileged {
|
||||
L = glua.NewState(options)
|
||||
} else {
|
||||
L = newSandboxedState(options)
|
||||
}
|
||||
valid := false
|
||||
defer func() {
|
||||
if !valid {
|
||||
@@ -63,6 +86,10 @@ func (p *Program) NewState(ctx context.Context, register func(*glua.LState), val
|
||||
if register != nil {
|
||||
register(L)
|
||||
}
|
||||
if !p.privileged {
|
||||
// Hide package internals to prevent data snooping and sandbox escape.
|
||||
L.SetGlobal(glua.LoadLibName, glua.LNil)
|
||||
}
|
||||
L.Push(L.NewFunctionFromProto(p.proto))
|
||||
// Execute the Lua script's top level.
|
||||
if err := L.PCall(0, 0, nil); err != nil {
|
||||
@@ -77,11 +104,157 @@ func (p *Program) NewState(ctx context.Context, register func(*glua.LState), val
|
||||
return L, nil
|
||||
}
|
||||
|
||||
func newSandboxedState(options glua.Options) *glua.LState {
|
||||
options.SkipOpenLibs = true
|
||||
L := glua.NewState(options)
|
||||
|
||||
for _, lib := range []struct {
|
||||
name string
|
||||
open glua.LGFunction
|
||||
}{
|
||||
{glua.LoadLibName, glua.OpenPackage},
|
||||
{glua.BaseLibName, glua.OpenBase},
|
||||
{glua.TabLibName, glua.OpenTable},
|
||||
{glua.StringLibName, glua.OpenString},
|
||||
{glua.MathLibName, glua.OpenMath},
|
||||
{glua.OsLibName, glua.OpenOs},
|
||||
} {
|
||||
L.Push(L.NewFunction(lib.open))
|
||||
L.Push(glua.LString(lib.name))
|
||||
L.Call(1, 0)
|
||||
}
|
||||
|
||||
for _, name := range []string{
|
||||
"dofile", "loadfile", "load", "loadstring", "getfenv", "setfenv",
|
||||
"getmetatable", "setmetatable", "rawget", "rawset",
|
||||
"collectgarbage", "module", "newproxy", "print", "_printregs",
|
||||
} {
|
||||
L.SetGlobal(name, glua.LNil)
|
||||
}
|
||||
|
||||
// Prevent printing memory addresses that reveal the memory layout.
|
||||
toString := func(value glua.LValue) glua.LString {
|
||||
result := "<opaque>"
|
||||
switch value.Type() {
|
||||
case glua.LTNil, glua.LTBool, glua.LTNumber, glua.LTString:
|
||||
result = value.String()
|
||||
case glua.LTUserData:
|
||||
switch native := value.(*glua.LUserData).Value.(type) {
|
||||
case error: // Errors can also implement fmt.Stringer; redact them first.
|
||||
result = redactMemoryAddresses(native.Error())
|
||||
case fmt.Stringer:
|
||||
result = native.String()
|
||||
}
|
||||
}
|
||||
return glua.LString(result)
|
||||
}
|
||||
L.SetGlobal("tostring", L.NewFunction(func(L *glua.LState) int {
|
||||
L.Push(toString(L.CheckAny(1)))
|
||||
return 1
|
||||
}))
|
||||
|
||||
// Prevent printing memory addresses that reveal the memory layout.
|
||||
stringLib := L.GetGlobal(glua.StringLibName).(*glua.LTable)
|
||||
stringLib.RawSetString("dump", glua.LNil)
|
||||
format := stringLib.RawGetString("format")
|
||||
stringLib.RawSetString("format", L.NewFunction(func(L *glua.LState) int {
|
||||
nargs := L.GetTop()
|
||||
for i := 2; i <= nargs; i++ {
|
||||
value := L.Get(i)
|
||||
switch value.Type() {
|
||||
case glua.LTBool, glua.LTNumber, glua.LTString:
|
||||
continue
|
||||
}
|
||||
L.Replace(i, toString(value))
|
||||
}
|
||||
L.Insert(format, 1)
|
||||
L.Call(nargs, 1)
|
||||
return 1
|
||||
}))
|
||||
|
||||
// Redact addresses before caught errors become readable by the script.
|
||||
redactError := func(value glua.LValue) glua.LValue {
|
||||
if message, ok := value.(glua.LString); ok {
|
||||
if redacted := redactMemoryAddresses(string(message)); redacted != string(message) {
|
||||
return glua.LString(redacted)
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
for _, name := range []string{"pcall", "xpcall"} {
|
||||
call := L.GetGlobal(name)
|
||||
L.SetGlobal(name, L.NewFunction(func(L *glua.LState) int {
|
||||
nargs := L.GetTop()
|
||||
if name == "xpcall" {
|
||||
L.CheckFunction(1)
|
||||
handler := L.CheckFunction(2)
|
||||
L.Replace(2, L.NewFunction(func(L *glua.LState) int {
|
||||
L.Replace(1, redactError(L.Get(1)))
|
||||
L.Insert(handler, 1)
|
||||
L.Call(1, 1)
|
||||
return 1
|
||||
}))
|
||||
}
|
||||
L.Insert(call, 1)
|
||||
L.Call(nargs, glua.MultRet)
|
||||
if L.Get(1) == glua.LFalse {
|
||||
L.Replace(2, redactError(L.Get(2)))
|
||||
}
|
||||
return L.GetTop()
|
||||
}))
|
||||
}
|
||||
|
||||
// Prevent seed output from compromising cryptographic security.
|
||||
L.GetGlobal(glua.MathLibName).(*glua.LTable).RawSetString("randomseed", glua.LNil)
|
||||
|
||||
// Keep only time functions, including when os is accessed through require.
|
||||
osLib := L.GetGlobal(glua.OsLibName).(*glua.LTable)
|
||||
for key, _ := osLib.Next(glua.LNil); key != glua.LNil; key, _ = osLib.Next(key) {
|
||||
switch key {
|
||||
case glua.LString("clock"), glua.LString("date"), glua.LString("difftime"), glua.LString("time"):
|
||||
default:
|
||||
osLib.RawSet(key, glua.LNil)
|
||||
}
|
||||
}
|
||||
|
||||
// Prevent module injection through the global package table.
|
||||
module := L.GetGlobal(glua.LoadLibName).(*glua.LTable)
|
||||
preload := module.RawGetString("preload").(*glua.LTable)
|
||||
loaders := L.CreateTable(1, 0)
|
||||
loaders.RawSetInt(1, L.NewFunction(func(L *glua.LState) int {
|
||||
name := L.CheckString(1)
|
||||
loader := preload.RawGetString(name)
|
||||
if loader == glua.LNil {
|
||||
loader = glua.LString("no preloaded module: " + name)
|
||||
}
|
||||
L.Push(loader)
|
||||
return 1
|
||||
}))
|
||||
module.RawSetString("loaders", loaders)
|
||||
L.SetField(L.Get(glua.RegistryIndex), "_LOADERS", loaders)
|
||||
for _, name := range []string{"loadlib", "seeall", "path", "cpath", "config"} {
|
||||
module.RawSetString(name, glua.LNil)
|
||||
}
|
||||
|
||||
return L
|
||||
}
|
||||
|
||||
var memoryAddressPattern = regexp.MustCompile(`0[xX][0-9a-fA-F]+`)
|
||||
|
||||
func redactMemoryAddresses(message string) string {
|
||||
if !strings.Contains(message, "0x") && !strings.Contains(message, "0X") {
|
||||
return message
|
||||
}
|
||||
return memoryAddressPattern.ReplaceAllString(message, "<address>")
|
||||
}
|
||||
|
||||
// NewStateFactory returns a factory that gives each state an initialization timeout.
|
||||
func (p *Program) NewStateFactory(initTimeout time.Duration, register func(*glua.LState), validate func(*glua.LState) error) LStateFactory {
|
||||
// Choose registrySize for the script type's expected stack usage, balancing
|
||||
// per-state memory usage against the number of states retained in a pool.
|
||||
func (p *Program) NewStateFactory(initTimeout time.Duration, registrySize int, register func(*glua.LState), validate func(*glua.LState) error) LStateFactory {
|
||||
return func(ctx context.Context) (*glua.LState, error) {
|
||||
initCtx, cancel := context.WithTimeout(ctx, initTimeout)
|
||||
defer cancel()
|
||||
return p.NewState(initCtx, register, validate)
|
||||
return p.NewState(initCtx, registrySize, register, validate)
|
||||
}
|
||||
}
|
||||
|
||||
+1001
-15
File diff suppressed because it is too large
Load Diff
@@ -34,12 +34,13 @@ func ReadAsset(file string) ([]byte, error) {
|
||||
return ReadFile(path)
|
||||
}
|
||||
|
||||
func OpenAsset(file string) (io.ReadCloser, error) {
|
||||
path, _, err := getAssetFileLocation(file)
|
||||
func OpenAsset(file string) (io.ReadCloser, int64, error) {
|
||||
path, info, err := getAssetFileLocation(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, 0, err
|
||||
}
|
||||
return NewFileReader(path)
|
||||
r, err := NewFileReader(path)
|
||||
return r, info.Size(), err
|
||||
}
|
||||
|
||||
func StatAsset(file string) (os.FileInfo, error) {
|
||||
|
||||
@@ -93,7 +93,7 @@ func GetConfDirPath() string {
|
||||
return configPath
|
||||
}
|
||||
|
||||
// ResolveLuaFile finds a local Lua script and returns its absolute path.
|
||||
// ResolveLuaFile finds a local .lua script and returns its resolved absolute path.
|
||||
// Relative paths: XRAY_LOCATION_CONFDIR > XRAY_LOCATION_CONFIG > working dir > executable dir.
|
||||
func ResolveLuaFile(path string) (string, error) {
|
||||
if path == "" {
|
||||
@@ -113,7 +113,22 @@ func ResolveLuaFile(path string) (string, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return resolveFile(paths)
|
||||
path, err := resolveFile(paths)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return resolveLuaTarget(path)
|
||||
}
|
||||
|
||||
func resolveLuaTarget(path string) (string, error) {
|
||||
path, err := filepath.EvalSymlinks(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to resolve Lua file symlinks: %w", err)
|
||||
}
|
||||
if filepath.Ext(path) != ".lua" {
|
||||
return "", errors.New("Lua file extension must be .lua")
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func resolveFile(paths []string) (string, error) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/xtls/xray-core/common"
|
||||
@@ -88,6 +89,10 @@ func TestResolveLuaFile(t *testing.T) {
|
||||
for _, path := range paths[:3] {
|
||||
common.Must(os.WriteFile(path, nil, 0o600))
|
||||
}
|
||||
for i, path := range paths {
|
||||
paths[i], err = filepath.EvalSymlinks(path)
|
||||
common.Must(err)
|
||||
}
|
||||
if got, err := ResolveLuaFile(paths[2]); err != nil || got != paths[2] {
|
||||
t.Fatalf("absolute path = %q, %v; want %q", got, err, paths[2])
|
||||
}
|
||||
@@ -115,3 +120,35 @@ func TestResolveLuaFile(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveLuaFileSymlink(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
link, target string
|
||||
valid bool
|
||||
}{
|
||||
{"alias.lua", "data.dat", false},
|
||||
{"alias.dat", "script.lua", true},
|
||||
} {
|
||||
t.Run(tc.link, func(t *testing.T) {
|
||||
dir, err := filepath.EvalSymlinks(t.TempDir())
|
||||
common.Must(err)
|
||||
target := filepath.Join(dir, tc.target)
|
||||
common.Must(os.WriteFile(target, nil, 0o600))
|
||||
link := filepath.Join(dir, tc.link)
|
||||
if err := os.Symlink(tc.target, link); err != nil {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skipf("cannot create symlinks: %v", err)
|
||||
}
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := ResolveLuaFile(link)
|
||||
if tc.valid {
|
||||
if err != nil || got != target {
|
||||
t.Fatalf("ResolveLuaFile(%q) = %q, %v; want %q", link, got, err, target)
|
||||
}
|
||||
} else if err == nil || got != "" || !strings.Contains(err.Error(), "Lua file extension must be .lua") {
|
||||
t.Fatalf("ResolveLuaFile(%q) = %q, %v; want .lua extension error", link, got, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user