-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
216 lines (157 loc) · 6.64 KB
/
tests.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
# API Request Security PoC
# Michal Walkowski - https://mwalkowski.github.io/
# https://github.com/mwalkowski
#
import base64
import datetime
import json
import uuid
import pytest
from Crypto.Hash import SHA256
from Crypto.Signature import pkcs1_15 as pkcs_signature
from Crypto.Cipher import PKCS1_v1_5 as pkcs_cipher
from main import app as test_app, received_nonces, PRIVATE_KEY, PUBLIC_KEY
from main import NONCE_HEADER, NONCE_CREATED_AT_HEADER, SIGNATURE_HEADER, TIME_DIFF_TOLERANCE_IN_SECONDS
@pytest.fixture()
def app():
test_app.config.update({
"TESTING": True,
})
yield test_app
@pytest.fixture()
def client(app):
return app.test_client()
@pytest.fixture()
def runner(app):
return app.test_cli_runner()
def test_NONCE_HEADER_not_exists(client):
response = client.post("/signed-body", json={})
assert response.status_code == 403
assert response.json == {'error': F'Missing header: {NONCE_HEADER}'}
def test_NONCE_CREATED_AT_HEADER_not_exists(client):
response = client.post("/signed-body", headers={NONCE_HEADER: "a"}, json={})
assert response.status_code == 403
assert response.json == {'error': F'Missing header: {NONCE_CREATED_AT_HEADER}'}
def test_SIGNATURE_HEADER_not_exists(client):
headers = {
NONCE_HEADER: "a",
NONCE_CREATED_AT_HEADER: "b",
}
response = client.post("/signed-body", headers=headers, json={})
assert response.status_code == 403
assert response.json == {'error': F'Missing header: {SIGNATURE_HEADER}'}
def test_NONCE_HEADER_is_incorrect(client):
headers = {
NONCE_HEADER: "a",
NONCE_CREATED_AT_HEADER: "b",
SIGNATURE_HEADER: "aaaa"
}
response = client.post("/signed-body", headers=headers, json={})
assert response.status_code == 403
assert response.json == {'error': "Nonce is already used or incorrect"}
def test_NONCE_HEADER_pool(client):
for _ in range(30):
headers = {
NONCE_HEADER: uuid.uuid4(),
NONCE_CREATED_AT_HEADER: "b",
SIGNATURE_HEADER: "aaaa"
}
response = client.post("/signed-body", headers=headers, json={})
assert response.status_code == 403
assert received_nonces.qsize() == 10
def test_NONCE_HEADER_is_duplicated(client):
headers = {
NONCE_HEADER: uuid.uuid4(),
NONCE_CREATED_AT_HEADER: "b",
SIGNATURE_HEADER: "aaaa"
}
response = client.post("/signed-body", headers=headers, json={})
assert response.status_code == 403
assert response.json == {'error': "The request is time-barred"}
response = client.post("/signed-body", headers=headers, json={})
assert response.status_code == 403
assert response.json == {'error': "Nonce is already used or incorrect"}
def test_NONCE_CREATED_AT_HEADER_invalid(client):
headers = {
NONCE_HEADER: uuid.uuid4(),
NONCE_CREATED_AT_HEADER: "b",
SIGNATURE_HEADER: "aaaa"
}
response = client.post("/signed-body", headers=headers, json={})
assert response.status_code == 403
assert response.json == {'error': "The request is time-barred"}
def test_NONCE_CREATED_AT_HEADER_time_barred(client):
nonce_created_at = datetime.datetime.now(datetime.timezone.utc)
nonce_created_at -= datetime.timedelta(days=0, seconds=TIME_DIFF_TOLERANCE_IN_SECONDS + 1)
headers = {
NONCE_HEADER: uuid.uuid4(),
NONCE_CREATED_AT_HEADER: nonce_created_at.isoformat(),
SIGNATURE_HEADER: "aaaa"
}
response = client.post("/signed-body", headers=headers, json={})
assert response.status_code == 403
assert response.json == {'error': "The request is time-barred"}
def test_SIGNATURE_invalid(client):
headers = {
NONCE_HEADER: uuid.uuid4(),
NONCE_CREATED_AT_HEADER: datetime.datetime.now(datetime.timezone.utc).isoformat(),
SIGNATURE_HEADER: "aaaa"
}
response = client.post("/signed-body", headers=headers, json={'msg': 'msg'})
assert response.status_code == 403
assert response.json == {'error': "Invalid Signature"}
def test_SIGNATURE_ok(client):
nonce = uuid.uuid4()
nonce_created_at = datetime.datetime.now(datetime.timezone.utc).isoformat()
request_data = json.dumps({'msg': 'msg'})
signature_input = "{}{}{}{}{}".format('POST', '/signed-body', nonce, nonce_created_at, request_data)
signature_input_b64 = base64.standard_b64encode(signature_input.encode())
h = SHA256.new(signature_input_b64)
signature = base64.standard_b64encode(pkcs_signature.new(PRIVATE_KEY).sign(h)).decode('utf-8')
headers = {
NONCE_HEADER: nonce,
NONCE_CREATED_AT_HEADER: nonce_created_at,
SIGNATURE_HEADER: signature
}
response = client.post("/signed-body", headers=headers, json=json.loads(request_data))
assert response.status_code == 200
assert response.json == {"msg": "Ok!"}
def test_encrypted_body_ok(client):
msg = {'name': 'Michal'}
msg_b64 = base64.standard_b64encode(json.dumps(msg).encode())
encrypted_msg = {
'encryptedPayload': base64.standard_b64encode(pkcs_cipher.new(PUBLIC_KEY).encrypt(msg_b64)).decode()
}
response = client.post("/encrypted-body", json=encrypted_msg)
assert response.status_code == 200
assert response.json == {"msg": "Hello Michal"}
def test_encrypted_body_nok(client):
msg = {'name': 'Michal'}
msg_b64 = base64.standard_b64encode(json.dumps(msg).encode())
encrypted_msg = {
'encryptedPayload': msg_b64.decode()
}
response = client.post("/encrypted-body", json=encrypted_msg)
assert response.status_code == 403
assert response.json == {"error": "Decryption problem"}
def test_signed_encrypted_body_ok(client):
msg = {'name': 'Michal'}
msg_b64 = base64.standard_b64encode(json.dumps(msg).encode())
encrypted_msg = {
'encryptedPayload': base64.standard_b64encode(pkcs_cipher.new(PUBLIC_KEY).encrypt(msg_b64)).decode()
}
nonce = uuid.uuid4()
nonce_created_at = datetime.datetime.now(datetime.timezone.utc).isoformat()
request_data = json.dumps(encrypted_msg)
signature_input = "{}{}{}{}{}".format('POST', '/signed-body', nonce, nonce_created_at, request_data)
signature_input_b64 = base64.standard_b64encode(signature_input.encode())
h = SHA256.new(signature_input_b64)
signature = base64.standard_b64encode(pkcs_signature.new(PRIVATE_KEY).sign(h)).decode('utf-8')
headers = {
NONCE_HEADER: nonce,
NONCE_CREATED_AT_HEADER: nonce_created_at,
SIGNATURE_HEADER: signature
}
response = client.post("/encrypted-body", headers=headers, json=encrypted_msg)
assert response.status_code == 200
assert response.json == {"msg": "Hello Michal"}