2.3 - Producing Robust Programs (Part 1)

Defensive Design Considerations

- Defensive design is a set of techniques that can be used to reduce the likelihood of a program failing in a way that is difficult to detect.
- It considers every possible input from a user and tries to find ways that the user could break the program.

Key Aspects of Defensive Design:

Benefits of Defensive Design:


Input Validation

- Input validation is essential for defensive design. It helps ensure that user input is correct and safe to process, preventing potential security vulnerabilities and crashes.
- It can prevent attacks such as SQL injection or cross-site scripting (XSS) attacks, where an attacker can inject malicious code into the input and potentially execute it on the server.
- It involves checking that the input received by a program is valid and within the expected range. You can use things such as if statements to check for banned words, the length of a string, the presence of certain characters, or the type of data being entered.

SQL Injection Prevention

- SQL injection is a type of attack where an attacker can inject malicious SQL code into a query, allowing them to manipulate or extract data from the database.
- To prevent SQL injection, you can check your input for statments such as 'DROP table', and if this is found, either return an error message or simply ensure that the input is not processed further.

Auth example

Auth example

Maintainability

- Maintainability is the ability of a program to be easily modified, extended, and maintained over time.
- It is important for defensive design because it helps prevent errors and crashes that can occur when making changes to the code.
- Some ways to make code more maintainable include using naming conventions, the use of sub programs like functions, using comments to explain the purpose and functionality of different parts of the code, and using intentation to make the code easier to read.

Example:

Before:
Not very maintainable code at all
After:
Better code

Note that indentation is required in Python, so the first example won't run, but in almost every other language it is optional.