safe.go

 1package tools
 2
 3import (
 4	"runtime"
 5	"slices"
 6	"strings"
 7)
 8
 9var safeCommands = []string{
10	// Bash builtins and core utils
11	"cal",
12	"date",
13	"df",
14	"du",
15	"echo",
16	"env",
17	"free",
18	"groups",
19	"hostname",
20	"id",
21	"kill",
22	"killall",
23	"ls",
24	"nice",
25	"nohup",
26	"printenv",
27	"ps",
28	"pwd",
29	"set",
30	"time",
31	"timeout",
32	"top",
33	"type",
34	"uname",
35	"unset",
36	"uptime",
37	"whatis",
38	"whereis",
39	"which",
40	"whoami",
41
42	// Git
43	"git blame",
44	"git branch",
45	"git config --get",
46	"git config --list",
47	"git describe",
48	"git diff",
49	"git grep",
50	"git log",
51	"git ls-files",
52	"git ls-remote",
53	"git remote",
54	"git rev-parse",
55	"git shortlog",
56	"git show",
57	"git status",
58	"git tag",
59}
60
61var chainingMetacharacters = []string{
62	";",
63	"|",
64	"&&",
65	"$(",
66	"`",
67}
68
69// containsCommandChaining reports whether s contains shell metacharacters
70// that enable command chaining or substitution.
71func containsCommandChaining(s string) bool {
72	return slices.ContainsFunc(chainingMetacharacters, func(c string) bool {
73		return strings.Contains(s, c)
74	})
75}
76
77func init() {
78	if runtime.GOOS == "windows" {
79		safeCommands = append(
80			safeCommands,
81			// Windows-specific commands
82			"ipconfig",
83			"nslookup",
84			"ping",
85			"systeminfo",
86			"tasklist",
87			"where",
88		)
89	}
90}