-
Notifications
You must be signed in to change notification settings - Fork 865
/
lua_oslib_safe.go
193 lines (174 loc) · 4.79 KB
/
lua_oslib_safe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package lifted
// This code is directly lifted from the argo-cd codebase in order to avoid relying on the lua package.
// For reference:
// https://github.com/argoproj/argo-cd/blob/master/util/lua/oslib_safe.go
// oslib_safe contains a subset of the lua OS library. For security reasons, we do not expose
// the entirety of lua OS library to custom actions, such as ones which can exit, read files, etc.
// Only the safe functions like os.time(), os.date() are exposed. Implementation was copied from
// github.com/yuin/gopher-lua.
import (
"fmt"
"strings"
"time"
lua "github.com/yuin/gopher-lua"
)
// OpenSafeOs open safe os
func OpenSafeOs(L *lua.LState) int {
tabmod := L.RegisterModule(lua.TabLibName, osFuncs)
L.Push(tabmod)
return 1
}
// SafeOsLoader sofe laoder
func SafeOsLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), osFuncs)
L.Push(mod)
return 1
}
var osFuncs = map[string]lua.LGFunction{
"time": osTime,
"date": osDate,
}
func osTime(L *lua.LState) int {
if L.GetTop() == 0 {
L.Push(lua.LNumber(time.Now().Unix()))
} else {
tbl := L.CheckTable(1)
sec := getIntField(tbl, "sec", 0)
min := getIntField(tbl, "min", 0)
hour := getIntField(tbl, "hour", 12)
day := getIntField(tbl, "day", -1)
month := getIntField(tbl, "month", -1)
year := getIntField(tbl, "year", -1)
isdst := getBoolField(tbl, "isdst", false)
t := time.Date(year, time.Month(month), day, hour, min, sec, 0, time.Local)
// TODO dst
if false {
print(isdst)
}
L.Push(lua.LNumber(t.Unix()))
}
return 1
}
func getIntField(tb *lua.LTable, key string, v int) int {
ret := tb.RawGetString(key)
if ln, ok := ret.(lua.LNumber); ok {
return int(ln)
}
return v
}
func getBoolField(tb *lua.LTable, key string, v bool) bool {
ret := tb.RawGetString(key)
if lb, ok := ret.(lua.LBool); ok {
return bool(lb)
}
return v
}
func osDate(L *lua.LState) int {
t := time.Now()
cfmt := "%c"
if L.GetTop() >= 1 {
cfmt = L.CheckString(1)
if strings.HasPrefix(cfmt, "!") {
t = time.Now().UTC()
cfmt = strings.TrimLeft(cfmt, "!")
}
if L.GetTop() >= 2 {
t = time.Unix(L.CheckInt64(2), 0)
}
if strings.HasPrefix(cfmt, "*t") {
ret := L.NewTable()
ret.RawSetString("year", lua.LNumber(t.Year()))
ret.RawSetString("month", lua.LNumber(t.Month()))
ret.RawSetString("day", lua.LNumber(t.Day()))
ret.RawSetString("hour", lua.LNumber(t.Hour()))
ret.RawSetString("min", lua.LNumber(t.Minute()))
ret.RawSetString("sec", lua.LNumber(t.Second()))
ret.RawSetString("wday", lua.LNumber(t.Weekday()+1))
// TODO yday & dst
ret.RawSetString("yday", lua.LNumber(0))
ret.RawSetString("isdst", lua.LFalse)
L.Push(ret)
return 1
}
}
L.Push(lua.LString(strftime(t, cfmt)))
return 1
}
var cDateFlagToGo = map[byte]string{
'a': "mon", 'A': "Monday", 'b': "Jan", 'B': "January", 'c': "02 Jan 06 15:04 MST", 'd': "02",
'F': "2006-01-02", 'H': "15", 'I': "03", 'm': "01", 'M': "04", 'p': "PM", 'P': "pm", 'S': "05",
'x': "15/04/05", 'X': "15:04:05", 'y': "06", 'Y': "2006", 'z': "-0700", 'Z': "MST"}
func strftime(t time.Time, cfmt string) string {
sc := newFlagScanner('%', "", "", cfmt)
for c, eos := sc.Next(); !eos; c, eos = sc.Next() {
if !sc.ChangeFlag {
if sc.HasFlag {
if v, ok := cDateFlagToGo[c]; ok {
sc.AppendString(t.Format(v))
} else {
switch c {
case 'w':
sc.AppendString(fmt.Sprint(int(t.Weekday())))
default:
sc.AppendChar('%')
sc.AppendChar(c)
}
}
sc.HasFlag = false
} else {
sc.AppendChar(c)
}
}
}
return sc.String()
}
type flagScanner struct {
flag byte
start string
end string
buf []byte
str string
Length int
Pos int
HasFlag bool
ChangeFlag bool
}
func newFlagScanner(flag byte, start, end, str string) *flagScanner {
return &flagScanner{flag, start, end, make([]byte, 0, len(str)), str, len(str), 0, false, false}
}
// AppendString append string to fs.buf
func (fs *flagScanner) AppendString(str string) { fs.buf = append(fs.buf, str...) }
// AppendChar append char to fs.buf
func (fs *flagScanner) AppendChar(ch byte) { fs.buf = append(fs.buf, ch) }
// String return the fs.buf of string
func (fs *flagScanner) String() string { return string(fs.buf) }
// Next iterate fs
func (fs *flagScanner) Next() (byte, bool) {
c := byte('\000')
fs.ChangeFlag = false
if fs.Pos == fs.Length {
if fs.HasFlag {
fs.AppendString(fs.end)
}
return c, true
} else {
c = fs.str[fs.Pos]
if c == fs.flag {
if fs.Pos < (fs.Length-1) && fs.str[fs.Pos+1] == fs.flag {
fs.HasFlag = false
fs.AppendChar(fs.flag)
fs.Pos += 2
return fs.Next()
} else if fs.Pos != fs.Length-1 {
if fs.HasFlag {
fs.AppendString(fs.end)
}
fs.AppendString(fs.start)
fs.ChangeFlag = true
fs.HasFlag = true
}
}
}
fs.Pos++
return c, false
}