Robust and secure programming Flashcards

1
Q

Steps to install bandit and analyse a piece of code

A

pip install bandit and bandit -r path/to/your/code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Three examples of security flaws that bandit can find

A

Use of dangerous functions, weak cryptographic practices and insecure deserialization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Name THREE specific techniques that can be used to make programs more robust.

A

Input validation, error handling and exception management, and defensive programming.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Using a code fragment, explain how “Input validation” works

A

Input validation is the process of ensuring that user inputs meet certain criteria before proceeding with further processing. def get_positive_integer(): try: user_input = int(input(“Enter a positive integer: “)) if user_input <= 0: print(“Error: Please enter a positive integer.”) else: return user_input except ValueError: print(“Error: Input must be an integer.”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Using a code fragment, explain how “Defensive programming” works

A

Defensive programming is a coding style that anticipates potential errors and incorporates safeguards to prevent them.def calculate_average(numbers): if not isinstance(numbers, list): raise TypeError(“Input must be a list of numbers”) if len(numbers) == 0: return 0 total = 0 for num in numbers: if not isinstance(num, (int, float)): raise TypeError(“All elements in the list must be numbers”) total += num return total / len(numbers)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Using a code fragment, explain how “Error handling and exception management” works

A

Use try/except.def divide_numbers(a, b): try: result = a / b print(“Result of division:”, result) except ZeroDivisionError: print(“Error: Division by zero is not allowed.”) except TypeError: print(“Error: Please provide numeric values for division.”) except Exception as e: print(“An unexpected error occurred:”, e)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The following function, written in Python, takes a file name as input and then shows the contents of the file on the screen. Explain why it is not a robust program? Propose TWO different mechanism to make it robust.

A

It lacks error handling and resource management. Use try and except to catch any errors while opening and using the file. Use a with statement (context manager) to open the file. This ensures that the file handle is automatically closed when the block of code exits, even if an exception occurs. It helps prevent resource leaks and ensures proper cleanup.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

You have been asked to develop a strategy to evaluate the security of the codebase for the control panel software. Describe THREE actions you would take in carrying out this evaluation.

A

Code Review and Static Analysis
Penetration Testing and Vulnerability Assessment
Threat Modeling and Risk Analysis.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe TWO things you should do before including an open-source library in yoursoftware.

A

Review the License and Assess the Quality and Maintenance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe ONE positive and ONE negative of using open source code inyour project.

A

Positive: Faster Development
Negative: Dependency Risks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Robust Programming in Python

A

Python has built-in exception handling, optional type hints, and extensive testing ecosystem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Robust Programming in Javascript

A

Javascript with its evolving error handling capabilities, type safety improvements with TypeScript, and a good collection of testing frameworks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Assume there is a module named UploadFile(path) which takes a string as its parameter (it is the path of a file on your local computer) and then uploads the file to the server. This function has various outcomes depending on if the upload was successful. Write the code that you would use to run the synchronous UploadFile(path)in a way that doesn’t crash if the upload fails (for example if the file does not exist in the given path)

A

Wrap it in a try/except clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Assume that you are using this piece of code (UploadFile(path)) in a critical system. Thus, you have been asked to conduct a static security audit on the code. Give TWO examples and descriptions of the kind of problems you would expect a static security audit to find.

A

Input Validation Issues: One common problem that a static security audit may find is inadequate input validation. This includes failing to properly validate user input or external data before processing it. For example, if the UploadFile(path) function does not perform sufficient validation on the input file path, an attacker could potentially manipulate the path to access unauthorised files on the system or exploit path traversal vulnerabilities.

Insecure File Handling: Another issue that a static security audit may uncover is insecure file handling practices. This includes improper file permissions, insecure temporary file creation, or lack of proper file sanitization. For instance, if the UploadFile(path) function does not enforce proper file permissions or sanitization when handling uploaded files, it could allow an attacker to overwrite or execute arbitrary files on the system, leading to unauthorized access or code execution vulnerabilities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Static security audit

A

The assessment is performed without executing the application’s code. It involves analysing the application’s source code, configuration files, and documentation to identify potential security vulnerabilities, design flaws, or coding errors. Static analysis tools and manual code review techniques are commonly used as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Dynamic security audit

A

In a dynamic security audit, the assessment involves actively interacting with the running application to evaluate its security controls and behaviors in real-time. Dynamic analysis tools, penetration testing, and vulnerability scanning are used to simulate attacks, probe for weaknesses, and identify security vulnerabilities that may be exploitable during runtime.

17
Q

Give TWO examples and descriptions of the kind of problems you would expect a static security audit to find.

A

Injection vulnerabilities, such as SQL injection.

Sensitive information, such as passwords or API keys is mishandled or exposed in the application’s source code.

18
Q

Secure Programming with Python

A

Python’s built-in features and libraries for secure coding practices, such as input validation, secure random number generation, and cryptographic functions. Strong typing and dynamic runtime checks help prevent common security vulnerabilities, such as type confusion and buffer overflows.

19
Q

Secure Programming with Javascript

A

Best practices such as using strict mode, input validation, and sanitisation, help mitigate security risks.

20
Q

You have used the bandit code analyser to detect problems in Python code. Present a code fragment in python which illustrates a problem that bandit can detect.

A

def delete_file(filename): os.system(“rm “ + filename)

21
Q

You are writing code for a webserver in JavaScript that should keep running at all costs. There is a risky function that queries a database that is unreliable. Write the code you would use to run the synchronous ‘checkDatabase()’ function in a way that does not crash the program if it fails.

A

Wrap the function checkDatabase() in a try-catch block. try { checkDatabase(); } catch (error) { console.error(“Error checking database”);}