-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathpassword_remover_test.py
350 lines (321 loc) · 13.7 KB
/
password_remover_test.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
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
# Copyright 2023-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included
# in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
# in that file, in accordance with the Business Source License, use of this
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.
import json
import unittest
import password_remover
import pytest
class TestStripPasswordsFromUrl(unittest.TestCase):
def test_basic(self):
url_with_password = "http://bucket-1:foobar@localhost:8091"
url_no_password = password_remover.strip_password_from_url(url_with_password)
assert "foobar" not in url_no_password
assert "bucket-1" in url_no_password
class TestRemovePasswords(unittest.TestCase):
def test_basic(self):
json_with_passwords = r"""
{
"log": ["*"],
"databases": {
"db2": {
"server": "http://bucket-1:foobar@localhost:8091"
},
"db": {
"server": "http://bucket4:foobar@localhost:8091",
"bucket":"bucket-1",
"username":"bucket-1",
"password":"foobar",
"users": { "Foo": { "password": "foobar", "disabled": false, "admin_channels": ["*"] } },
"sync":
`
function(doc, oldDoc) {
if (doc.type == "reject_me") {
throw({forbidden : "Rejected document"})
} else if (doc.type == "bar") {
// add "bar" docs to the "important" channel
channel("important");
} else if (doc.type == "secret") {
if (!doc.owner) {
throw({forbidden : "Secret documents \ must have an owner field"})
}
} else {
// all other documents just go into all channels listed in the doc["channels"] field
channel(doc.channels)
}
}
`
}
}
}
"""
with_passwords_removed = password_remover.remove_passwords(json_with_passwords)
assert "foobar" not in with_passwords_removed
def test_alternative_config(self):
sg_config = '{"Interface":":4984","AdminInterface":":4985","Facebook":{"Register":true},"Log":["*"],"Databases":{"todolite":{"server":"http://localhost:8091","pool":"default","bucket":"default","password":"foobar","name":"todolite","sync":"\\nfunction(doc, oldDoc) {\\n // NOTE this function is the same across the iOS, Android, and PhoneGap versions.\\n if (doc.type == \\"task\\") {\\n if (!doc.list_id) {\\n throw({forbidden : \\"Items must have a list_id.\\"});\\n }\\n channel(\\"list-\\"+doc.list_id);\\n } else if (doc.type == \\"list\\" || (doc._deleted \\u0026\\u0026 oldDoc \\u0026\\u0026 oldDoc.type == \\"list\\")) {\\n // Make sure that the owner propery exists:\\n var owner = oldDoc ? oldDoc.owner : doc.owner;\\n if (!owner) {\\n throw({forbidden : \\"List must have an owner.\\"});\\n }\\n\\n // Make sure that only the owner of the list can update the list:\\n if (doc.owner \\u0026\\u0026 owner != doc.owner) {\\n throw({forbidden : \\"Cannot change owner for lists.\\"});\\n }\\n\\n var ownerName = owner.substring(owner.indexOf(\\":\\")+1);\\n requireUser(ownerName);\\n\\n var ch = \\"list-\\"+doc._id;\\n if (!doc._deleted) {\\n channel(ch);\\n }\\n\\n // Grant owner access to the channel:\\n access(ownerName, ch);\\n\\n // Grant shared members access to the channel:\\n var members = !doc._deleted ? doc.members : oldDoc.members;\\n if (Array.isArray(members)) {\\n var memberNames = [];\\n for (var i = members.length - 1; i \\u003e= 0; i--) {\\n memberNames.push(members[i].substring(members[i].indexOf(\\":\\")+1))\\n };\\n access(memberNames, ch);\\n }\\n } else if (doc.type == \\"profile\\") {\\n channel(\\"profiles\\");\\n var user = doc._id.substring(doc._id.indexOf(\\":\\")+1);\\n if (user !== doc.user_id) {\\n throw({forbidden : \\"Profile user_id must match docid.\\"});\\n }\\n requireUser(user);\\n access(user, \\"profiles\\");\\n }\\n}\\n","users":{"GUEST":{"name":"","admin_channels":["*"],"all_channels":null,"disabled":true}}}}}'
with_passwords_removed = password_remover.remove_passwords(sg_config)
assert "foobar" not in with_passwords_removed
def test_config_fragment(self):
db_config = """
{
"username": "bucket1",
"name": "db",
"bucket": "bucket1",
"server": "http://localhost:8091",
"password": "foobar",
"pool": "default"
}
"""
with_passwords_removed = password_remover.remove_passwords(db_config)
assert "foobar" not in with_passwords_removed
pass
def test_non_parseable_config(self):
"""
If a config is not JSON parseable, make sure passwords are not stored in result
"""
unparseable_json_with_passwords = r"""
{
"log": ["*"],
"databases": {
"db2": {
"server": "http://bucket-1:foobar@localhost:8091"
},
"db": {
"server": "http://localhost:8091",
"bucket":"bucket-1",
"username":"bucket-1",
"password":"foobar",
"users": { "GUEST": { "disabled": false, "admin_channels": ["*"] } },
"sync":
function(doc, oldDoc) {
if (doc.type == "reject_me") {
throw({forbidden : "Rejected document"})
} else if (doc.type == "bar") {
// add "bar" docs to the "important" channel
channel("important");
} else if (doc.type == "secret") {
if (!doc.owner) {
throw({forbidden : "Secret documents \ must have an owner field"})
}
} else {
// all other documents just go into all channels listed in the doc["channels"] field
channel(doc.channels)
}
}
`
}
}
}
"""
with_passwords_removed = password_remover.remove_passwords(
unparseable_json_with_passwords
)
assert "foobar" not in with_passwords_removed
class TestTagUserData(unittest.TestCase):
def test_basic(self):
json_with_userdata = """
{
"databases": {
"db": {
"server": "http://bucket4:foobar@localhost:8091",
"bucket":"bucket-1",
"username":"bucket-user",
"password":"foobar",
"users": {
"FOO": {
"password": "foobar",
"disabled": false,
"admin_channels": ["uber_secret_channel"]
},
"bar": { "password": "baz" }
}
},
"db2": { "server": "http://bucket-1:foobar@localhost:8091" }
}
}
"""
tagged = password_remover.tag_userdata_in_server_config(json_with_userdata)
assert "<ud>uber_secret_channel</ud>" in tagged
assert "<ud>foo</ud>" in tagged # everything is lower cased
assert "<ud>bucket-user</ud>" in tagged
assert (
"<ud>baz</ud>" not in tagged
) # passwords shouldn't be tagged, they get removed
assert "<ud>bucket-1</ud>" not in tagged # bucket name is actually metadata
class TestConvertToValidJSON(unittest.TestCase):
def test_basic(self):
invalid_json = r"""
{
"log": ["*"],
"databases": {
"db": {
"server": "walrus:",
"users": { "GUEST": { "disabled": false, "admin_channels": ["*"] } },
"sync":
`
function(doc, oldDoc) {
if (doc.type == "reject_me") {
throw({forbidden : "Rejected document"})
} else if (doc.type == "bar") {
// add "bar" docs to the "important" channel
channel("important");
} else if (doc.type == "secret") {
if (!doc.owner) {
throw({forbidden : "Secret documents \ must have an owner field"})
}
} else {
// all other documents just go into all channels listed in the doc["channels"] field
channel(doc.channels)
}
}
`
}
}
}
"""
valid_json = password_remover.convert_to_valid_json(invalid_json)
got_exception = True
try:
parsed_json = json.loads(valid_json)
json.dumps(parsed_json, indent=4)
got_exception = False
except Exception as e:
print("Exception: {0}".format(e))
assert got_exception is False, "Failed to convert to valid JSON"
def test_basic_two_sync_functions(self):
invalid_json = r"""
{
"log": ["*"],
"databases": {
"db": {
"server": "walrus:",
"users": { "GUEST": { "disabled": false, "admin_channels": ["*"] } },
"sync":
`
function(doc, oldDoc) {
if (doc.type == "reject_me") {
throw({forbidden : "Rejected document"})
} else if (doc.type == "bar") {
// add "bar" docs to the "important" channel
channel("important");
} else if (doc.type == "secret") {
if (!doc.owner) {
throw({forbidden : "Secret documents \ must have an owner field"})
}
} else {
// all other documents just go into all channels listed in the doc["channels"] field
channel(doc.channels)
}
}
`
},
"db2": {
"server": "walrus:",
"users": { "GUEST": { "disabled": false, "admin_channels": ["*"] } },
"sync":
`
function(doc, oldDoc) {
if (doc.type == "reject_me") {
throw({forbidden : "Rejected document"})
} else if (doc.type == "bar") {
// add "bar" docs to the "important" channel
channel("important");
} else if (doc.type == "secret") {
if (!doc.owner) {
throw({forbidden : "Secret documents \ must have an owner field"})
}
} else {
// all other documents just go into all channels listed in the doc["channels"] field
channel(doc.channels)
}
}
`
}
}
}
"""
valid_json = password_remover.convert_to_valid_json(invalid_json)
got_exception = True
try:
parsed_json = json.loads(valid_json)
json.dumps(parsed_json, indent=4)
got_exception = False
except Exception as e:
print("Exception: {0}".format(e))
assert got_exception is False, "Failed to convert to valid JSON"
class TestLowerKeys(unittest.TestCase):
def test_basic(self):
json_text_input = """{
"Name": "Couchbase, Inc.",
"Address": {
"Street": "3250 Olcott St",
"City": "Santa Clara",
"State": "CA",
"Zip_Code": 95054
},
"Products": [
"Couchbase Server",
"Sync Gateway",
"Couchbase Lite"
]
}"""
json_dict_actual = password_remover.lower_keys_dict(json_text_input)
json_dict_expected = json.loads(
"""{
"name": "Couchbase, Inc.",
"address": {
"street": "3250 Olcott St",
"city": "Santa Clara",
"state": "CA",
"zip_code": 95054
},
"products": [
"Couchbase Server",
"Sync Gateway",
"Couchbase Lite"
]
}"""
)
# Sort the lists(if any) in both dictionaries before dumping to a string.
json_text_actual = json.dumps(json_dict_actual, sort_keys=True)
json_text_expected = json.dumps(json_dict_expected, sort_keys=True)
assert json_text_expected == json_text_actual
@pytest.mark.parametrize(
"input_str, expected",
[
(
b'{"foo": "bar"}',
'{"foo": "bar"}',
),
(
b'{"foo": `bar`}',
'{"foo": "bar"}',
),
(
b'{"foo": `bar\nbaz\nboo`}',
r'{"foo": "bar\nbaz\nboo"}',
),
(
b'{"foo": `bar\n"baz\n\tboo`}',
r'{"foo": "bar\n\"baz\n\tboo"}',
),
(
b'{"foo": `bar\n`, "baz": `howdy`}',
r'{"foo": "bar\n", "baz": "howdy"}',
),
(
b'{"foo": `bar\r\n`, "baz": `\r\nhowdy`}',
r'{"foo": "bar\n", "baz": "\nhowdy"}',
),
(
b'{"foo": `bar\\baz`, "something": `else\\is\\here`}',
r'{"foo": "bar\\baz", "something": "else\\is\\here"}',
),
],
)
def test_convert_to_valid_json(input_str, expected):
assert password_remover.convert_to_valid_json(input_str) == expected
password_remover.get_parsed_json(input_str)