2.3.1 Defensive design · Paper 2
Defensive design: programs that are hard to break
Each section has pictures and things to try, then its key points and key words. Exam practice is at the end of the page.
You need to know
- Anticipating misuse, and planning for every likely input
- Authentication to confirm who a user is
- Input validation and how to deal with invalid data
- Maintainability: sub programs, naming conventions, indentation and comments
- Writing validation and simple username and password checks yourself
You don't need to know
- OCR doesn't rule anything out here.
Defensive design
Programs should be designed to cope with users who make mistakes or try to break them.
Try it
Try to log in. Then let a bot guess the password, with the lock on and then off.
Key points
Anticipating misuse
- Defensive design means planning for things going wrong before they happen.
- Users make mistakes: typing letters into a number box, leaving boxes empty, or typing far too much.
- Some users try to break or get into a program on purpose, e.g. guessing passwords.
- Programmers plan for this with validation, authentication and clear instructions, so the program doesn't crash.
Authentication
Before this: Input validation
- Authentication means checking a user is who they say they are.
- The most common way is a username and password, checked against stored details.
- Limiting the number of attempts stops people guessing forever.
- You need to be able to write a simple username and password check.
Key words
Say what each one means, then tap to check.
Input validation
Validation checks that input is sensible before it is used. It can't check the input is correct.
| Check | Example |
|---|---|
| Range | Age is between 11 and 18 |
| Type | Age is a whole number |
| Length | Password is at least 8 characters |
| Presence | Name box isn't empty |
| Format | Postcode looks like a postcode |
age = int(input("Age: ")) while age < 11 or age > 18: age = int(input("Must be 11 to 18. Age: "))
Try it
Try to sneak bad data past the form. Then turn on one check at a time.
Try it
Which validation check is it?
- The number of tickets can't be left blank
- A test score must be from 0 to 100
- A password must have at least 8 characters
- A date must be written like 05/11/2026
- A quantity must be a whole number, not text
- A username can be no longer than 15 characters
Key points
Input validation
Before this: Anticipating misuse2.2.1Condition-controlled loops (while)
- Validation checks input is sensible before the program uses it.
- Range check: a number is between limits, e.g. age 11 to 18.
- Type check: the data is the right type, e.g. a whole number.
- Length check: e.g. a password has at least 8 characters.
- Presence check: something has been entered.
- Format check: data matches a pattern, e.g. a postcode.
- Validation can't check data is correct. A valid age could still be the wrong age.
Key words
Say what each one means, then tap to check.
Maintainable code
Code that is easy for others (and you, later) to understand and fix:
Try it
Tick each improvement and watch the same program get easier to read.
Key points
Writing code others can read
Before this: 2.2.3Procedures
- Maintainable code is easy for someone else (or you, later) to read, fix and update.
- Sub programs split code into clear, reusable parts.
- Naming conventions: names that say what they store, e.g. totalScore not x.
- Indentation shows which code is inside loops and ifs.
- Comments explain what parts of the code do.
Key words
Say what each one means, then tap to check.
Exam practice
12 questions in the style of the OCR exam, 34 marks in total. Try each one, then check the mark scheme.
Defensive design
Hint
Think about the boxes on a login screen. What could someone leave empty, type too much into, or keep trying?
Give means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- Leave the username / password blank.
- Type something far too long.
- Keep guessing passwords (brute force).
- Enter code / symbols to try to break it.
1 mark for each, up to 2.
Hint
What happens to a program if users type something it doesn't expect? What might people do on purpose?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- Users will enter unexpected data / try to break the program…
- …so planning for it stops the program crashing / being hacked.
1 mark for each point.
Hint
You need a count of attempts, a loop that stops after 3 tries or a correct password, and an if after the loop to pick the message.
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
5 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Count attempts, starting at 0.
- Loop until correct or 3 attempts used.
- Input the password inside the loop and add 1 to attempts.
- Output Access granted if correct.
- Output Locked out after 3 wrong attempts.
Also accept: A for loop that runs 3 times and stops early when the password is right.
Don't accept: Outputting Locked out after every wrong guess. It should only appear after the third wrong attempt.
Example answer
attempts = 0 correct = false while attempts < 3 AND correct == false pw = input("Password") attempts = attempts + 1 if pw == "Pa55word" then correct = true endif endwhile if correct then print("Access granted") else print("Locked out") endif
1 mark for each point. Python is fine too.
Hint
It is about who the user is, not about the data they type.
State means give a short, clear answer. You don't need to explain it.
1 mark: one answer is enough.
Show mark scheme
- Checking / confirming the identity of a user.
Don't accept: Checking the data entered is sensible. That is validation.
Input validation
- An age that must be 11 to 18
- A username box that must not be empty
- A password that must be at least 8 characters
Hint
Is each one about a number between limits, something being there at all, or how many characters there are?
Suggest means use what you know to give a sensible idea for this situation.
3 marks: check every part before you finish.
Show mark scheme
- a) Range check
- b) Presence check
- c) Length check
Don't accept: a) "Length check". An age needs a range check.
1 mark for each.
Hint
Get the number first. Then use a while loop that keeps asking while the number is less than 1 OR more than 10.
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
4 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Input a number (as an integer).
- Loop while the number is less than 1 or more than 10.
- Ask again inside the loop with a message.
- Loop ends when valid.
Don't accept: while num < 1 AND num > 10. No number can be both, so the loop never runs.
Example answer
num = int(input("Enter 1 to 10")) while num < 1 OR num > 10 num = int(input("Invalid. Enter 1 to 10")) endwhile
1 mark for each point. Python is fine too.
Maintainable code
Hint
Pick two of: comments, sensible names, indentation, sub programs. For each one, say how it helps someone reading the code.
Give means give a short, clear answer. You don't need to explain it.
4 marks: make 4 separate points.
Show mark scheme
- Comments… explain what the code does.
- Meaningful names… show what each variable stores.
- Indentation… shows the structure / what is inside loops and ifs.
- Sub programs… split code into parts that can be read / tested on their own.
Also accept: Constants for values that don't change, so they only need updating in one place.
Don't accept: Naming a way with no explanation.
Example answer
Add comments that explain what each part of the code does, so another programmer can quickly understand it. Use meaningful variable names, such as totalScore instead of x, so it is clear what each variable stores.
For each way: 1 mark for the way, 1 mark for how it helps.
a = int(input()) b = int(input()) if a > b then print(a) else print(b) endif
Hint
Would you know what a and b store? Can you see which lines are inside the if?
Give means give a short, clear answer. You don't need to explain it.
2 marks: make 2 separate points.
Show mark scheme
- Use meaningful variable names, e.g. first / second.
- Indent the code inside the if.
- Add comments.
- Add prompts to the inputs.
1 mark for each, up to 2.
Mixed questions on the whole page
Hint
For each check, name it and then say what it checks about the age.
Describe means say what it is like or what happens. Make each point a separate fact.
4 marks: make 4 separate points.
Show mark scheme
- Range check…
- …the age must be between 11 and 18.
- Type check…
- …the age must be a whole number.
- Presence check…
- …the age box must not be left empty.
Also accept: Length check, e.g. the age must be 1 or 2 digits long.
Don't accept: Verification / double entry. That checks the data was typed correctly, not that it is sensible.
Don't accept: Naming a check with no description.
Example answer
A range check makes sure the age entered is between 11 and 18, so an age such as 40 is rejected. A type check makes sure the age is a whole number, so text such as "twelve" is rejected.
For each check: 1 mark for naming it, 1 mark for describing it. Up to 4.
Hint
Is 14/03/2010 a sensible date of birth for a student? What does validation actually check?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- The date is in the right format and range, so it passes the validation checks.
- Validation only checks that data is sensible / allowed, not that it is correct.
1 mark for each point.
Hint
If there is no limit, what could an attacker do? How could the program stop that?
Explain means say what happens and why. Words like “because” and “so” help.
2 marks: aim for 2 things said, such as a point and then the reason for it.
Show mark scheme
- Someone could keep guessing (a brute force attack) until they get the password right.
- Limit the number of attempts, e.g. lock the account after 3 wrong passwords.
Also accept: Add a wait after each wrong attempt, so guessing takes much longer.
1 mark for the problem, 1 mark for the fix.
Hint
This is a length check. Use .length (or len() in Python) in a while loop condition.
Write means write the code or algorithm. OCR Exam Reference Language or Python both get marks.
3 marks: each part that works earns marks, even if the whole thing isn't perfect.
Show mark scheme
- Inputs the password.
- A loop that repeats while the length is less than 8, asking again inside the loop.
- Outputs Password saved after the loop.
Also accept: len(password) < 8 (Python).
Don't accept: if password.length < 8 on its own. It only asks again once, not until it is long enough.
Example answer
password = input("Create a password") while password.length < 8 password = input("Too short. Use at least 8 characters") endwhile print("Password saved")
1 mark for each point. Python is fine too.