Repository navigation
Expand file tree
/
Copy pathexample01.py
More file actions
58 lines (47 loc) · 1.96 KB
/
Copy pathexample01.py
File metadata and controls
58 lines (47 loc) · 1.96 KB
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
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
""" Example Code Example """
from typing import Dict, List
class WebserverConnection:
"""_Simulates grossly simplified webserver_"""
users = ["admin", "Alice", "Bob"]
roles: Dict[str, List[str]] = {
"read": ["admin", "Alice", "Bob"],
"write": ["admin"],
}
def __init__(self, username: str):
# TODO: implement authentication logic
pass
def post(
self, form_data: Dict[str, str] = {"session_user": "", "action": ""}
) -> tuple[Dict[str, str], str]:
"""
form_data: Represents request data send in by the client
"""
action = form_data.get("action", "")
session_user = form_data.get("session_user", "")
if session_user not in self.users:
return form_data, "401: Please login"
action_data = self._submit(session_user, action)
return form_data, action_data
def _submit(self, username: str, action: str) -> str:
if action in self.roles and username in self.roles[action]:
return f"200: user '{username}' has '{action}' access"
return f"401: user '{username}' does not have '{action}' access"
#####################
# Attempting to exploit above code example
#####################
# Alice is logged in server-side (session_user is 'Alice')
# User should be authenticated server-side via a session token, cookie or JWT.
USER = "Alice"
web = WebserverConnection(USER)
# expected 'normal' operation with no data:
html_form, body = web.post()
print(f"html_form={html_form}, body={body}")
html_form = {"session_user": USER, "action": "write"}
html_form, body = web.post(html_form)
print(f"html_form={html_form}, body={body}")
# Attacker tries to tamper by adding 'session_user': 'admin' to the html_form
html_form = {"action": "write", "session_user": "admin"}
html_form, body = web.post(html_form)
print(f"html_form={html_form}, body={body}")