-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_synapse_webhook.py
245 lines (184 loc) · 12.3 KB
/
test_synapse_webhook.py
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
import os
import json
import logging
import synapse.common as s_common
import synapse.cortex as s_cortex
import synapse.lib.httpapi as s_httpapi
import synapse.tests.utils as s_test
import synapse.tools.genpkg as s_genpkg
logger = logging.getLogger(__name__)
dirname = os.path.dirname(__file__)
pkgproto = s_common.genpath(dirname, "synapse-webhook.yaml")
def check_defang(mesg: str):
"""Raise an exception if a message that should be defanged isn't properly defanged."""
if "defang" in mesg:
if not (
mesg.count("hxxp") > 0 and
mesg.count("[.]") > 0 and
mesg.count("http") == 0
):
raise ValueError("not properly defanged")
class DiscordApiMock(s_httpapi.Handler):
"""Mock implementation of a Discord webhook server."""
counter = 0
async def head(self):
raise NotImplementedError
async def get(self):
raise NotImplementedError
async def post(self):
if self.request.body:
d = json.loads(self.request.body.decode())
m = d.get("content")
if m is not None:
check_defang(m)
DiscordApiMock.counter += 1
self.set_status(200)
return
raise ValueError("bad discord webhook")
class SlackApiMock(s_httpapi.Handler):
"""Mock implementation of a Slack webhook server."""
counter = 0
async def head(self):
raise NotImplementedError
async def get(self):
raise NotImplementedError
async def post(self):
if self.request.body:
d = json.loads(self.request.body.decode())
m = d.get("text")
if m is not None:
check_defang(m)
SlackApiMock.counter += 1
self.set_status(200)
return
raise ValueError("bad slack webhook")
class TeamsApiMock(s_httpapi.Handler):
"""Mock implementation of a Teams webhook server."""
counter = 0
async def head(self):
raise NotImplementedError
async def get(self):
raise NotImplementedError
async def post(self):
if self.request.body:
d = json.loads(self.request.body.decode())
m = d.get("text")
if m is not None:
check_defang(m)
TeamsApiMock.counter += 1
self.set_status(200)
return
raise ValueError("bad teams webhook")
class KeybaseApiMock(s_httpapi.Handler):
"""Mock implementation of a Keybase webhook server."""
counter = 0
async def head(self):
raise NotImplementedError
async def get(self):
raise NotImplementedError
async def post(self):
if self.request.body:
d = json.loads(self.request.body.decode())
m = d.get("msg")
if m is not None:
check_defang(m)
KeybaseApiMock.counter += 1
self.set_status(200)
return
raise ValueError("bad keybase webhook")
class SynapseWebhookTest(s_test.SynTest):
async def _t_install_pkg(self, core: s_cortex.Cortex, prox: s_cortex.CoreApi | None = None):
"""Install and configure the Storm package."""
await s_genpkg.main((pkgproto, "--push", f"cell://{core.dirn}"))
if prox is not None:
nopriv = await prox.addUser("nopriv")
self.assertIsNotNone(nopriv)
await prox.addUserRule(nopriv["iden"], (True, ("zw", "webhook", "user")))
hipriv = await prox.addUser("hipriv")
self.assertIsNotNone(hipriv)
await prox.addUserRule(hipriv["iden"], (True, ("zw", "webhook", "user")))
await prox.addUserRule(hipriv["iden"], (True, ("zw", "webhook", "admin")))
async def test_add(self):
async with self.getTestCore() as core:
await self._t_install_pkg(core)
self.stormIsInPrint("Webhook added for service \"discord\"", await core.stormlist("zw.webhook.add asdf1 https://discord.com/api/webhooks/1111223423432/asdflkjsadlfkjadslfjlkfdsa-lkjczlknvznnmvxzc-oireoiwqrwoiuqroiweiuqorq"))
self.stormIsInPrint("Webhook added for service \"slack\"", await core.stormlist("zw.webhook.add asdf2 https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"))
self.stormIsInPrint("Webhook added for service \"teams\"", await core.stormlist("zw.webhook.add asdf3 https://asdfsafda.webhook.office.com/asdfasdfsafdsa/zxcvxzvxzvcxv/gdgfdsgfdsgfdg"))
self.stormIsInPrint("Webhook added for service \"keybase\"", await core.stormlist("zw.webhook.add asdf4 https://bots.keybase.io/webhookbot/asdasdasdasda_asdasdasdasda"))
self.stormIsInErr("Failed to detect webhook service", await core.stormlist("zw.webhook.add asdf3 https://reddit.com"))
self.stormIsInErr("Invalid service specified: zzz", await core.stormlist("zw.webhook.add --service zzz asdf5 https://reddit.com"))
self.stormIsInPrint("Webhook added for service \"teams\"", await core.stormlist("zw.webhook.add --service teams asdf5 https://reddit.com"))
self.stormIsInErr("A webhook already exists", await core.stormlist("zw.webhook.add --service teams asdf5 https://reddit.com"))
async def test_call(self):
async with self.getTestCoreAndProxy() as (core, prox):
await self._t_install_pkg(core, prox)
_, port = await core.addHttpsPort(0)
root = await core.auth.getUserByName("root")
await root.setPasswd("root")
core.addHttpApi("/discord", DiscordApiMock, {"cell": core})
core.addHttpApi("/slack", SlackApiMock, {"cell": core})
core.addHttpApi("/teams", TeamsApiMock, {"cell": core})
core.addHttpApi("/keybase", KeybaseApiMock, {"cell": core})
await core.callStorm(f"zw.webhook.add --service discord d1 https://root:[email protected]:{port}/discord")
await core.callStorm(f"zw.webhook.add --service slack --global s1 https://root:[email protected]:{port}/slack")
await core.callStorm(f"zw.webhook.add --service teams t1 https://root:[email protected]:{port}/teams")
await core.callStorm(f"zw.webhook.add --service keybase k1 https://root:[email protected]:{port}/keybase")
self.stormHasNoWarnErr(await core.stormlist("zw.webhook.send --no-verify d1 hello"))
self.eq(DiscordApiMock.counter, 1)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify d1 `{$node.value()}`"))
self.eq(DiscordApiMock.counter, 3)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify --defang d1 `defang {$node.value()}`"))
self.eq(DiscordApiMock.counter, 5)
self.stormHasNoWarnErr(await core.stormlist("zw.webhook.send --no-verify s1 hello"))
self.eq(SlackApiMock.counter, 1)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify s1 `{$node.value()}`"))
self.eq(SlackApiMock.counter, 3)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify --defang s1 `defang {$node.value()}`"))
self.eq(SlackApiMock.counter, 5)
self.stormHasNoWarnErr(await core.stormlist("zw.webhook.send --no-verify t1 hello"))
self.eq(TeamsApiMock.counter, 1)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify t1 `{$node.value()}`"))
self.eq(TeamsApiMock.counter, 3)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify --defang t1 `defang {$node.value()}`"))
self.eq(TeamsApiMock.counter, 5)
self.stormHasNoWarnErr(await core.stormlist("zw.webhook.send --no-verify k1 hello"))
self.eq(KeybaseApiMock.counter, 1)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify k1 `{$node.value()}`"))
self.eq(KeybaseApiMock.counter, 3)
self.stormHasNoWarnErr(await core.stormlist("[inet:url=http://google.com inet:url=http://bing.com] | zw.webhook.send --no-verify --defang k1 `defang {$node.value()}`"))
self.eq(KeybaseApiMock.counter, 5)
async with core.getLocalProxy(user="nopriv") as noprivcore:
self.stormIsInErr("Not authorized to use", [m async for m in noprivcore.storm("zw.webhook.send --no-verify d1 yeet")])
self.stormHasNoWarnErr([m async for m in noprivcore.storm("zw.webhook.send --no-verify s1 yeet")])
async with core.getLocalProxy(user="hipriv") as hiprivcore:
self.stormIsInErr("Not authorized to use", [m async for m in hiprivcore.storm("zw.webhook.send --no-verify d1 yeet")])
self.stormHasNoWarnErr([m async for m in hiprivcore.storm("zw.webhook.send --no-verify s1 yeet")])
async def test_list(self):
async with self.getTestCoreAndProxy() as (core, prox):
await self._t_install_pkg(core, prox)
self.stormIsInPrint("Webhook added for service \"discord\"", await core.stormlist("zw.webhook.add asdf1 https://discord.com/api/webhooks/1111223423432/asdflkjsadlfkjadslfjlkfdsa-lkjczlknvznnmvxzc-oireoiwqrwoiuqroiweiuqorq"))
self.stormIsInPrint("Webhook added for service \"slack\"", await core.stormlist("zw.webhook.add asdf2 https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"))
self.stormIsInPrint("Webhook added for service \"teams\"", await core.stormlist("zw.webhook.add asdf3 https://asdfsafda.webhook.office.com/asdfasdfsafdsa/zxcvxzvxzvcxv/gdgfdsgfdsgfdg"))
self.stormIsInPrint("Webhook added for service \"keybase\"", await core.stormlist("zw.webhook.add asdf4 https://bots.keybase.io/webhookbot/asdasdasdasda_asdasdasdasda"))
mesgs = await core.stormlist("zw.webhook.list")
self.stormHasNoWarnErr(mesgs)
self.stormIsInPrint("Webhook: asdf1", mesgs)
self.stormIsInPrint("Webhook: asdf2", mesgs)
self.stormIsInPrint("Webhook: asdf3", mesgs)
self.stormIsInPrint("Webhook: asdf4", mesgs)
async with core.getLocalProxy(user="nopriv") as noprivcore:
self.stormIsInErr("Not allowed to list", [m async for m in noprivcore.storm("zw.webhook.list --all")])
async with core.getLocalProxy(user="hipriv") as hiprivcore:
self.stormHasNoWarnErr([m async for m in hiprivcore.storm("zw.webhook.list --all")])
async def test_delete(self):
async with self.getTestCoreAndProxy() as (core, prox):
await self._t_install_pkg(core, prox)
self.stormIsInPrint("Webhook added for service \"discord\"", await core.stormlist("zw.webhook.add asdf1 https://discord.com/api/webhooks/1111223423432/asdflkjsadlfkjadslfjlkfdsa-lkjczlknvznnmvxzc-oireoiwqrwoiuqroiweiuqorq"))
self.stormIsInPrint("Webhook added for service \"slack\"", await core.stormlist("zw.webhook.add asdf2 https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX"))
self.stormIsInPrint("Webhook added for service \"teams\"", await core.stormlist("zw.webhook.add asdf3 https://asdfsafda.webhook.office.com/asdfasdfsafdsa/zxcvxzvxzvcxv/gdgfdsgfdsgfdg"))
self.stormIsInPrint("Webhook added for service \"keybase\"", await core.stormlist("zw.webhook.add asdf4 https://bots.keybase.io/webhookbot/asdasdasdasda_asdasdasdasda"))
self.stormIsInPrint("Webhook deleted: asdf1", await core.stormlist("zw.webhook.delete asdf1"))
async with core.getLocalProxy(user="nopriv") as noprivcore:
self.stormIsInErr("Not allowed to delete", [m async for m in noprivcore.storm("zw.webhook.delete asdf2")])
async with core.getLocalProxy(user="hipriv") as hiprivcore:
self.stormIsInPrint("Webhook deleted", [m async for m in hiprivcore.storm("zw.webhook.delete asdf2")])