Why API security is non-negotiable in Rest API - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
When we build APIs, security checks happen every time a request comes in.
We want to understand how the time these checks take grows as more requests or data come in.
Analyze the time complexity of the following API security check snippet.
// Pseudocode for API security check
function checkApiRequest(request) {
if (!validateToken(request.token)) {
return "Unauthorized";
}
for (let permission of request.user.permissions) {
if (!hasAccess(permission, request.resource)) {
return "Forbidden";
}
}
return "Access Granted";
}
This code checks if the request token is valid, then loops through user permissions to verify access.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through user permissions to check access.
- How many times: Once per permission in the user's list.
As the number of permissions grows, the time to check them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 permissions | Up to 10 checks |
| 100 permissions | Up to 100 checks |
| 1000 permissions | Up to 1000 checks |
Pattern observation: The time grows directly with the number of permissions.
Time Complexity: O(n)
This means the time to check security grows in a straight line with the number of permissions.
[X] Wrong: "Security checks always take the same time no matter how many permissions there are."
[OK] Correct: Each permission must be checked, so more permissions mean more time spent.
Understanding how security checks scale helps you design APIs that stay fast and safe as they grow.
"What if we cached permission checks? How would that change the time complexity?"
Practice
Solution
Step 1: Understand the purpose of API security
API security is designed to protect sensitive data and control who can access the API.Step 2: Analyze the options
Only It protects sensitive data and prevents unauthorized access. mentions protection and preventing unauthorized access, which is the main goal of API security.Final Answer:
It protects sensitive data and prevents unauthorized access. -> Option CQuick Check:
API security = protect data and access [OK]
- Thinking security speeds up API
- Confusing security with data size
- Assuming open access is secure
Solution
Step 1: Identify secure practices for API endpoints
Using API keys or tokens is a standard way to control access to APIs.Step 2: Evaluate each option
Require an API key or token for access. requires keys or tokens, which is correct. Options A, C, and D are insecure practices.Final Answer:
Require an API key or token for access. -> Option AQuick Check:
API security = keys or tokens [OK]
- Using HTTP instead of HTTPS
- Allowing unrestricted IP access
- Exposing sensitive data in URLs
fetch('https://api.example.com/data', {
headers: { 'Authorization': 'Bearer abc123' }
})
.then(response => response.json())
.then(data => console.log(data));
What is the main purpose of the 'Authorization' header here?Solution
Step 1: Understand the 'Authorization' header role
The 'Authorization' header carries credentials like tokens to prove who is calling the API.Step 2: Match the header purpose with options
To provide a token proving the caller's identity. correctly states it provides a token for identity verification. Other options describe unrelated functions.Final Answer:
To provide a token proving the caller's identity. -> Option AQuick Check:
Authorization header = token for identity [OK]
- Confusing authorization with data format
- Thinking it sets timeout
- Assuming it encrypts data
app.get('/user', (req, res) => {
if (!req.headers['api_key']) {
res.status(401).send('Unauthorized');
return;
}
res.send('User data');
});
What is the main problem with this code?Solution
Step 1: Analyze the API key check
The code only checks if the 'api_key' header exists but does not verify if it is correct or valid.Step 2: Understand the security implication
Without validating the key, anyone sending any 'api_key' header can access the data, which is insecure.Final Answer:
It does not check if the API key is valid. -> Option DQuick Check:
API key must be validated, not just present [OK]
- Assuming presence means valid
- Confusing HTTP method with security
- Ignoring error handling importance
Solution
Step 1: Identify secure transport and authentication
HTTPS encrypts data in transit, API tokens verify caller identity, and permission checks protect privacy.Step 2: Compare options for best security practice
Use HTTPS, require API tokens, and validate user permissions before sending data. combines encryption, authentication, and authorization, which is the best approach. Others are insecure or incomplete.Final Answer:
Use HTTPS, require API tokens, and validate user permissions before sending data. -> Option BQuick Check:
HTTPS + tokens + permissions = secure API [OK]
- Ignoring encryption with HTTP
- Skipping authentication
- Not checking user permissions
