Cyber security · GCSE · OCR J277 1.4.2, AQA 8525 3.6.3, Edexcel 1CP2 5.3.2 · about 15 min
Validating input, access levels in code, and testing for security.
[1 mark]How does a program reduce the risk of SQL injection?
[1 mark]What does 'fail safely' mean?
[1 mark]What is the principle of least access?
[1 mark]Why should you test with invalid and boundary input?
[1 mark]What does this program print?
def ok(t):
return t.isdigit() and 0 <= int(t) <= 100
print(ok('50'), ok('999'), ok('go'))True False False
50 is valid; 999 is out of range; 'go' is not a number.
Write allowed(role, action) using the can_do table, returning True or False. For each (user, role, action) in requests, print <user> (<role>) <action>: allowed or : denied. Validate first: if the role is not in can_do, print <user> (<role>) <action>: unknown role instead. At the end print denied or blocked: <n>.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
can_do = {"student": ["view"], "teacher": ["view", "edit"], "admin": ["view", "edit", "delete"]}
# user, role, action
requests = [
("Sam", "student", "view"),
("Sam", "student", "edit"),
("Mr Lee", "teacher", "edit"),
("root", "admin", "delete"),
("ghost", "hacker", "delete"),
]The hint students can ask for: Check the role exists before anything else, and say so if it does not. Otherwise look the role up and see whether the action is in the list of things it may do. Count everything that was not allowed.
from bugbot import *
connect()
can_do = {"student": ["view"], "teacher": ["view", "edit"], "admin": ["view", "edit", "delete"]}
requests = [
("Sam", "student", "view"),
("Sam", "student", "edit"),
("Mr Lee", "teacher", "edit"),
("root", "admin", "delete"),
("ghost", "hacker", "delete"),
]
def allowed(role, action):
return action in can_do.get(role, [])
blocked = 0
for user, role, action in requests:
if role not in can_do:
print(f"{user} ({role}) {action}: unknown role")
blocked = blocked + 1
elif allowed(role, action):
print(f"{user} ({role}) {action}: allowed")
else:
print(f"{user} ({role}) {action}: denied")
blocked = blocked + 1
print("denied or blocked:", blocked)
Any program that meets the task's checks is marked correct in the simulator; this is one way, not the only way.