9.2 - Defensive Design
The concept and purpose of defensive design
Defensive design involves creating programs that are robust and resistant to errors or misuse, helping to ensure they run smoothly without breaking. Even well-tested programs from major companies often need updates to fix issues, but defensive techniques aim to minimise problems from the start.
This approach focuses on building safeguards into the code. As a result, programs become more reliable, reducing the chance of crashes or unexpected behaviour when users interact with them in unintended ways.
Key goals of defensive design
- Anticipate misuse - Programmers think about how users might enter incorrect data or use features wrongly, then add protections to handle these situations.
- Maintain code quality - Well-organised and documented code makes it easier to update and fix, keeping the program stable over time.
- Reduce errors through testing - Thorough checks during development help identify and eliminate bugs before the program is released.
Misuse happens when users do something unexpected, like entering invalid information or trying to access restricted areas. Defensive design prevents these actions from causing the program to fail, ensuring it continues to function as intended.
Input validation and its types
Input validation is a key part of defensive design that checks whether data entered by a user meets specific rules before it is processed by the program. This helps prevent errors or security issues caused by invalid or harmful inputs, such as stopping a program from crashing due to unexpected data.
By validating inputs, programmers can ensure only acceptable data passes through, making the program more secure and reliable. For example, validation might confirm that an email address includes an '@' symbol and ends with a valid domain like '.com' or '.co.uk'.
Common types of input validation checks
Programs often combine several checks to thoroughly verify data.
Main types of input validation:
- Range check - Ensures the data falls within an allowed range.
- Presence check - Confirms that some data has been entered.
- Format check - Verifies the data follows the correct structure, like a date.
- Look-up table - Compares the data against a predefined list of acceptable values.
- Length check - Checks that the data is the correct length.
These checks work together to block invalid data early, reducing risks and improving the program's overall stability.
Example of applying input validation
Imagine a programmer is creating a system for user registrations on a website. They want usernames to be longer than 5 characters, shorter than 12 characters, and start with an uppercase letter. To achieve this, they would use:
- A length check to confirm the username is not longer or shorter than the lengths allowed.
- A format check to ensure it begins with an uppercase letter.
This combination prevents common errors and ensures only suitable usernames are accepted.
Authentication methods and security measures
Authentication is the process of verifying a user's identity before granting access to certain parts of a program or sensitive data. It acts as a barrier to unauthorised entry, much like a security guard checking identification.
A common method is using a username paired with a password. When a user tries to log in, the program prompts for these details and compares them against stored records to confirm the match.
Ways to enhance password-based authentication
To make authentication more secure without overly complicating the user experience, programmers can implement these strategies:
- Strong password requirements - Encourage or enforce strong passwords and require users to update them regularly.
- Limit failed attempts - After a set number of incorrect tries, lock the account temporarily to prevent brute-force attacks.
- Random character selection - On login, ask for specific characters from the password, adding an extra layer of verification.
These measures help protect against unauthorised access while keeping the system user-friendly.
Balancing security with program functionality
While strong defensive design, including validation and authentication, is essential for safety, it's important not to overdo it. Too much authentication can make a program frustrating to use, potentially driving users away.
For instance, excessive authentication steps might slow down legitimate users, affecting the program's ease of use. Programmers must find a balance: enough safeguards to prevent misuse and errors, but not so many that they hinder normal functionality. This ensures the program remains both secure and practical for everyday tasks.