-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathapi_exec.go
More file actions
380 lines (338 loc) · 8.68 KB
/
api_exec.go
File metadata and controls
380 lines (338 loc) · 8.68 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package luaplugin
import (
"bufio"
"context"
"errors"
"io"
"os"
"os/exec"
"sync"
"sync/atomic"
"time"
lua "github.com/yuin/gopher-lua"
)
// Default binaries plugins may invoke via cliamp.exec.run(). Users can widen
// this via [plugins] allowed_binaries = "yt-dlp,ffmpeg,ffprobe" in config.toml.
var defaultAllowedBinaries = []string{"yt-dlp", "ffmpeg"}
// Per-process output cap (stdout+stderr). Once exceeded, further output is
// dropped and the process is still allowed to run to completion. Prevents a
// chatty subprocess from OOMing the player.
const execMaxOutputBytes = 4 << 20 // 4 MiB
// Per-plugin concurrency cap. Runaway plugins can't fork-bomb past this.
const execMaxPerPlugin = 4
// Hard timeout cap. Plugins may pass a smaller value; larger values clamp.
const execMaxTimeout = 30 * time.Minute
// execEntry tracks a single running subprocess.
type execEntry struct {
id int64
plugin *Plugin
cmd *exec.Cmd
cancel context.CancelFunc
done chan struct{}
}
// execManager owns all running plugin subprocesses.
type execManager struct {
mu sync.Mutex
entries map[int64]*execEntry
nextID atomic.Int64
perPlug map[*Plugin]int
allowed map[string]struct{}
allowMu sync.RWMutex
}
func newExecManager(allowed []string) *execManager {
em := &execManager{
entries: make(map[int64]*execEntry),
perPlug: make(map[*Plugin]int),
allowed: make(map[string]struct{}),
}
em.setAllowed(allowed)
return em
}
func (em *execManager) setAllowed(names []string) {
em.allowMu.Lock()
defer em.allowMu.Unlock()
em.allowed = make(map[string]struct{}, len(names))
for _, n := range names {
em.allowed[n] = struct{}{}
}
}
func (em *execManager) isAllowed(name string) bool {
em.allowMu.RLock()
defer em.allowMu.RUnlock()
_, ok := em.allowed[name]
return ok
}
func (em *execManager) add(e *execEntry) {
em.mu.Lock()
em.entries[e.id] = e
em.perPlug[e.plugin]++
em.mu.Unlock()
}
func (em *execManager) remove(e *execEntry) {
em.mu.Lock()
if _, ok := em.entries[e.id]; ok {
delete(em.entries, e.id)
em.perPlug[e.plugin]--
if em.perPlug[e.plugin] <= 0 {
delete(em.perPlug, e.plugin)
}
}
em.mu.Unlock()
}
// canStart returns true if the plugin is under its concurrency cap.
func (em *execManager) canStart(p *Plugin) bool {
em.mu.Lock()
defer em.mu.Unlock()
return em.perPlug[p] < execMaxPerPlugin
}
// stopPlugin cancels every process owned by the given plugin and blocks
// until each one has exited. Called from Manager.cleanupPlugin.
func (em *execManager) stopPlugin(p *Plugin) {
em.mu.Lock()
var victims []*execEntry
for _, e := range em.entries {
if e.plugin == p {
victims = append(victims, e)
}
}
em.mu.Unlock()
for _, e := range victims {
e.cancel()
}
for _, e := range victims {
<-e.done
}
}
// stopAll cancels every process. Called from Manager.Close.
func (em *execManager) stopAll() {
em.mu.Lock()
var all []*execEntry
for _, e := range em.entries {
all = append(all, e)
}
em.mu.Unlock()
for _, e := range all {
e.cancel()
}
for _, e := range all {
<-e.done
}
}
// registerExecAPI adds cliamp.exec.run(binary, args, opts?) -> handle, err.
// The exec API is only functional for plugins declaring permissions = {"exec"}.
// Without the permission, cliamp.exec is a no-op table that logs once.
func registerExecAPI(L *lua.LState, cliamp *lua.LTable, em *execManager, p *Plugin, logger *pluginLogger) {
tbl := L.NewTable()
warned := false
guard := func() bool {
if p.perms[PermExec] {
return true
}
if !warned {
logger.log(p.Name, "warn", "cliamp.exec requires permissions = {\"exec\"} — further warnings suppressed")
warned = true
}
return false
}
L.SetField(tbl, "run", L.NewFunction(func(L *lua.LState) int {
if !guard() {
L.Push(lua.LNil)
L.Push(lua.LString("exec permission required"))
return 2
}
binary := L.CheckString(1)
argsTbl := L.CheckTable(2)
optsTbl := L.OptTable(3, nil)
if !em.isAllowed(binary) {
L.Push(lua.LNil)
L.Push(lua.LString("binary not in allowlist: " + binary))
return 2
}
path, err := exec.LookPath(binary)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString("binary not found on PATH: " + binary))
return 2
}
// Flatten argv. Every entry must be a string; reject non-strings rather
// than coercing, so plugins can't sneak nested tables into argv.
var argv []string
var argErr error
argsTbl.ForEach(func(_, v lua.LValue) {
if argErr != nil {
return
}
if v.Type() != lua.LTString {
argErr = errors.New("args must all be strings")
return
}
argv = append(argv, v.String())
})
if argErr != nil {
L.Push(lua.LNil)
L.Push(lua.LString(argErr.Error()))
return 2
}
var onStdout, onStderr, onExit *lua.LFunction
cwd := ""
timeout := execMaxTimeout
if optsTbl != nil {
if fn, ok := optsTbl.RawGetString("on_stdout").(*lua.LFunction); ok {
onStdout = fn
}
if fn, ok := optsTbl.RawGetString("on_stderr").(*lua.LFunction); ok {
onStderr = fn
}
if fn, ok := optsTbl.RawGetString("on_exit").(*lua.LFunction); ok {
onExit = fn
}
if s, ok := optsTbl.RawGetString("cwd").(lua.LString); ok {
cwd = string(s)
}
if n, ok := optsTbl.RawGetString("timeout").(lua.LNumber); ok && float64(n) > 0 {
t := time.Duration(float64(n) * float64(time.Second))
if t < timeout {
timeout = t
}
}
}
if cwd != "" && !isWriteAllowed(cwd) {
L.Push(lua.LNil)
L.Push(lua.LString("cwd not in write allowlist"))
return 2
}
if !em.canStart(p) {
L.Push(lua.LNil)
L.Push(lua.LString("per-plugin exec concurrency cap reached"))
return 2
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
cmd := exec.CommandContext(ctx, path, argv...)
if cwd != "" {
cmd.Dir = cwd
}
// Empty env by default — plugins should not inherit secrets like
// AWS_*, SSH_*, etc. yt-dlp and ffmpeg both run fine with a minimal env.
cmd.Env = minimalExecEnv()
stdout, err := cmd.StdoutPipe()
if err != nil {
cancel()
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
stderr, err := cmd.StderrPipe()
if err != nil {
cancel()
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
if err := cmd.Start(); err != nil {
cancel()
L.Push(lua.LNil)
L.Push(lua.LString(err.Error()))
return 2
}
id := em.nextID.Add(1)
entry := &execEntry{
id: id,
plugin: p,
cmd: cmd,
cancel: cancel,
done: make(chan struct{}),
}
em.add(entry)
// Shared output budget across stdout+stderr.
var outUsed atomic.Int64
pipeStream := func(r io.Reader, fn *lua.LFunction) {
scanner := bufio.NewScanner(r)
// Allow longer lines than default 64KiB for noisy tools like ffmpeg.
scanner.Buffer(make([]byte, 64*1024), 1<<20)
for scanner.Scan() {
line := scanner.Text()
if outUsed.Add(int64(len(line)+1)) > execMaxOutputBytes {
// Budget exhausted — drain silently.
for scanner.Scan() {
}
return
}
if fn == nil {
continue
}
p.mu.Lock()
_ = p.L.CallByParam(lua.P{
Fn: fn,
NRet: 0,
Protect: true,
}, lua.LString(line))
p.mu.Unlock()
}
}
var wg sync.WaitGroup
wg.Add(2)
go func() { defer wg.Done(); pipeStream(stdout, onStdout) }()
go func() { defer wg.Done(); pipeStream(stderr, onStderr) }()
go func() {
wg.Wait()
waitErr := cmd.Wait()
ctxErr := ctx.Err()
cancel()
em.remove(entry)
code := 0
if waitErr != nil {
if ctxErr != nil {
code = -1 // cancelled or timed out
} else {
var exitErr *exec.ExitError
if errors.As(waitErr, &exitErr) {
code = exitErr.ExitCode()
} else {
code = -2 // other error
}
}
}
if onExit != nil {
p.mu.Lock()
_ = p.L.CallByParam(lua.P{
Fn: onExit,
NRet: 0,
Protect: true,
}, lua.LNumber(code))
p.mu.Unlock()
}
close(entry.done)
}()
// Build a Lua handle with :cancel() and :alive().
handle := L.NewTable()
L.SetField(handle, "cancel", L.NewFunction(func(L *lua.LState) int {
entry.cancel()
return 0
}))
L.SetField(handle, "alive", L.NewFunction(func(L *lua.LState) int {
select {
case <-entry.done:
L.Push(lua.LFalse)
default:
L.Push(lua.LTrue)
}
return 1
}))
L.SetField(handle, "id", lua.LNumber(id))
L.Push(handle)
return 1
}))
L.SetField(cliamp, "exec", tbl)
}
// homeEnv returns the user's home directory for subprocess HOME, preferring
// $HOME, then os.UserHomeDir(), falling back to os.TempDir() when unset.
// yt-dlp and ffmpeg both read HOME (~/.cache, ~/.config).
func homeEnv() string {
if home, ok := os.LookupEnv("HOME"); ok && home != "" {
return home
}
if h, err := os.UserHomeDir(); err == nil {
return h
}
return os.TempDir()
}