Serverless security considerations in Cybersecurity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we look at serverless security, we want to understand how the time to check or enforce security changes as the system grows.
We ask: How does the effort to keep serverless functions safe grow when there are more functions or more events?
Analyze the time complexity of the following security check process for serverless functions.
for function in serverless_functions:
for event in function.events:
if not validate_event(event):
alert_security_team(function, event)
This code checks each event of every serverless function to ensure it meets security rules, alerting if something is wrong.
Look at what repeats in the code.
- Primary operation: Checking each event with
validate_event. - How many times: For every function, it checks all its events.
The time to finish grows as the number of functions and their events grow.
| Input Size (n functions, m events each) | Approx. Operations |
|---|---|
| 10 functions, 5 events | 50 checks |
| 100 functions, 5 events | 500 checks |
| 1000 functions, 5 events | 5000 checks |
Pattern observation: The total checks grow by multiplying the number of functions by the number of events per function.
Time Complexity: O(n * m)
This means the time to check security grows proportionally with both the number of functions and the number of events each has.
[X] Wrong: "Checking security for one function means the time stays the same no matter how many functions exist."
[OK] Correct: Each function adds more events to check, so total time grows with more functions, not stays fixed.
Understanding how security checks scale helps you design safer serverless systems and shows you can think about real-world growth challenges.
"What if we only checked a fixed number of events per function regardless of total events? How would the time complexity change?"
Practice
Solution
Step 1: Understand the principle of least privilege
Least privilege means giving only the permissions necessary for a task, nothing extra.Step 2: Apply least privilege to serverless functions
Serverless functions should have minimal permissions to reduce risk if compromised.Final Answer:
Grant only the minimum permissions needed for the function to work -> Option DQuick Check:
Least privilege = Grant minimum permissions [OK]
- Giving too many permissions increases risk
- Using default permissions without review
- Assuming permissions can be broad safely
Solution
Step 1: Recognize the importance of input validation
Validating inputs prevents malicious or malformed data from causing harm.Step 2: Apply validation before processing inputs
Rejecting invalid data early protects the function and backend systems.Final Answer:
Validate inputs against expected formats and reject invalid data -> Option BQuick Check:
Input validation = Check and reject bad data [OK]
- Trusting inputs from authenticated users blindly
- Skipping validation to speed up development
- Validating inputs only after processing
def handler(event):
user_input = event.get('input')
if not user_input:
return 'No input'
return f'Processed: {user_input}'What is a potential security risk in this code?
Solution
Step 1: Analyze input handling in the function
The function accepts user input but does not check if it is safe or clean.Step 2: Identify missing input validation or sanitization
Without validation, malicious input could cause injection or other attacks.Final Answer:
It does not validate or sanitize the user input -> Option AQuick Check:
Missing input validation = Security risk [OK]
- Thinking encryption is needed for all inputs
- Confusing return usage with security
- Ignoring input validation importance
import cryptography
def encrypt(data):
return cryptography.encrypt(data)What is the main error?
Solution
Step 1: Check the cryptography module usage
The cryptography library requires specific classes and methods for encryption, not a direct encrypt function.Step 2: Identify the incorrect function call
Calling cryptography.encrypt(data) will cause an error because no such function exists directly.Final Answer:
The cryptography module does not have a direct encrypt function -> Option AQuick Check:
cryptography.encrypt() does not exist [OK]
- Assuming all modules have simple encrypt() functions
- Ignoring import errors
- Confusing encryption with data formatting
Solution
Step 1: Identify best security practices for serverless apps
Key practices include least privilege, input validation, encryption, and monitoring.Step 2: Evaluate each option against these practices
Use least privilege permissions, validate inputs, encrypt data at rest and in transit, and monitor logs for suspicious activity includes all important steps; others skip critical protections.Final Answer:
Use least privilege permissions, validate inputs, encrypt data at rest and in transit, and monitor logs for suspicious activity -> Option CQuick Check:
Combine key security steps = Strong protection [OK]
- Skipping input validation or monitoring
- Granting excessive permissions
- Relying only on cloud defaults
