-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript_test.go
201 lines (162 loc) · 5.01 KB
/
script_test.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
194
195
196
197
198
199
200
201
package suexec
import (
. "github.com/r7kamura/gospel"
"io/ioutil"
"os"
"testing"
)
func TestSuexecScript(t *testing.T) {
Describe(t, "NewScript", func() {
It("should be success when a path exists", func() {
s, err := NewScript("index.pl", ".")
Expect(s).To(Exist)
Expect(err == nil).To(Exist)
})
It("should not be success when a script not exists", func() {
s, err := NewScript("script not found", ".")
Expect(s == nil).To(Exist)
Expect(os.IsExist(err)).To(Equal, false)
})
})
Describe(t, "IsSetuid", func() {
It("script is not setuid", func() {
s, _ := NewScript("index.pl", ".")
Expect(s.IsSetuid()).To(Equal, false)
Expect(s.IsSetgid()).To(Equal, false)
})
It("script is setuid", func() {
script, _ := ioutil.TempFile("", "")
script.Chmod(0700 | os.ModeSetuid)
defer os.Remove(script.Name())
s, _ := NewScript(script.Name(), ".")
Expect(s.IsSetuid()).To(Equal, true)
})
})
Describe(t, "IsSetgid", func() {
It("script is not setgid", func() {
script, _ := ioutil.TempFile("", "")
script.Chmod(700)
defer os.Remove(script.Name())
s, _ := NewScript(script.Name(), ".")
Expect(s.IsSetgid()).To(Equal, false)
})
It("script is setgid", func() {
script, _ := ioutil.TempFile("", "")
script.Chmod(700 | os.ModeSetgid)
defer os.Remove(script.Name())
s, _ := NewScript(script.Name(), ".")
Expect(s.IsSetgid()).To(Equal, true)
})
})
Describe(t, "IfOwnerMatch", func() {
It("owner match with specified uid, gid", func() {
s, _ := NewScript("index.pl", ".")
uid := os.Getuid()
gid := os.Getgid()
Expect(s.IfOwnerMatch(uid, gid)).To(Equal, true)
})
It("owner does not match with specified uid, gid", func() {
s, _ := NewScript("index.pl", ".")
uid := os.Getuid() + 1000
gid := os.Getgid() + 1000
Expect(s.IfOwnerMatch(uid, gid)).To(Equal, false)
})
})
Describe(t, "NewScript", func() {
It("index.pl is secure", func() {
s, err := NewScript("index.pl", ".")
Expect(s).To(NotEqual, nil)
Expect(err).To(Equal, nil)
Expect(s.path_info).To(NotEqual, nil)
Expect(s.cwd_info).To(NotEqual, nil)
})
It("index.pl is secure", func() {
s, err := NewScript("not exists", ".")
Expect(s).To(Exist)
Expect(err).To(Exist)
})
It("index.pl is secure", func() {
s, err := NewScript("index.pl", "./1234567890")
Expect(s).To(Exist)
Expect(err).To(Exist)
})
})
Describe(t, "HasSecurePath", func() {
It("index.pl is secure", func() {
s := Script{path: "index.pl", cwd: "."}
Expect(s.HasSecurePath()).To(Equal, true)
})
It("bin/index.pl is secure", func() {
s := Script{path: "bin/index.pl", cwd: "."}
Expect(s.HasSecurePath()).To(Equal, true)
})
It("../index.pl is insecure", func() {
s := Script{path: "..//index.pl", cwd: "."}
Expect(s.HasSecurePath()).To(Equal, false)
})
It("/index.pl is insecure path", func() {
s := Script{path: "/index.pl", cwd: "."}
Expect(s.HasSecurePath()).To(Equal, false)
})
It("bin/../index.pl is insecure path", func() {
s := Script{path: "bin/../index.pl", cwd: "."}
Expect(s.HasSecurePath()).To(Equal, false)
})
})
Describe(t, "IsWritableByOthers", func() {
It("script is not writable by others", func() {
script, _ := ioutil.TempFile("", "")
defer os.Remove(script.Name())
script.Chmod(0700)
s, _ := NewScript(script.Name(), ".")
Expect(s.IsWritableByOthers()).To(Equal, false)
})
It("script is writable by others", func() {
insecure_modes := []os.FileMode{0720, 0702, 0730, 0703, 0760, 0706, 0766, 0777}
for _, mode := range insecure_modes {
script, _ := ioutil.TempFile("", "")
defer os.Remove(script.Name())
script.Chmod(mode)
s, _ := NewScript(script.Name(), ".")
Expect(s.IsWritableByOthers()).To(Equal, true)
}
})
})
Describe(t, "IsExecutable", func() {
It("script is executable", func() {
script, _ := ioutil.TempFile("", "")
defer os.Remove(script.Name())
script.Chmod(0700)
s, _ := NewScript(script.Name(), ".")
Expect(s.IsExecutable()).To(Equal, true)
})
It("script is not executable", func() {
script, _ := ioutil.TempFile("", "")
defer os.Remove(script.Name())
script.Chmod(0600)
s, _ := NewScript(script.Name(), ".")
Expect(s.IsExecutable()).To(Equal, false)
})
})
Describe(t, "IsDirWritableByOthers", func() {
It("directory is not writable by others", func() {
tempdir, _ := ioutil.TempDir("", "")
defer os.RemoveAll(tempdir)
os.Chmod(tempdir, 0700)
script, _ := ioutil.TempFile(tempdir, "")
s, _ := NewScript(script.Name(), tempdir)
Expect(s.IsDirWritableByOthers()).To(Equal, false)
})
It("directory is writable by others", func() {
insecure_modes := []os.FileMode{0720, 0702, 0730, 0703, 0760, 0706, 0766, 0777}
for _, mode := range insecure_modes {
tempdir, _ := ioutil.TempDir("", "")
defer os.RemoveAll(tempdir)
os.Chmod(tempdir, mode)
script, _ := ioutil.TempFile(tempdir, "")
s, _ := NewScript(script.Name(), tempdir)
Expect(s.IsDirWritableByOthers()).To(Equal, true)
}
})
})
}