Content Security Policy (CSP) in Cybersecurity - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
Analyzing how Content Security Policy (CSP) processes rules helps us understand how it affects website security checks.
We want to know how the time to check CSP rules grows as the number of rules or resources increases.
Analyze the time complexity of the following CSP rule checking process.
// Simplified CSP check for resource loading
function checkCSP(resource, cspRules) {
for (let rule of cspRules) {
if (resource.type === rule.type && resource.url.startsWith(rule.source)) {
return true; // Allowed by this rule
}
}
return false; // No matching rule found
}
This code checks if a resource is allowed by comparing it against each CSP rule until a match is found.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all CSP rules to find a match.
- How many times: Up to once per rule, until a match is found or all rules checked.
As the number of CSP rules grows, the time to check a resource grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 rule checks |
| 100 | Up to 100 rule checks |
| 1000 | Up to 1000 rule checks |
Pattern observation: Doubling the number of rules roughly doubles the checks needed.
Time Complexity: O(n)
This means the time to check a resource grows linearly with the number of CSP rules.
[X] Wrong: "Checking CSP rules happens instantly no matter how many rules there are."
[OK] Correct: Each rule must be checked one by one until a match is found, so more rules mean more checks and more time.
Understanding how CSP rule checks scale helps you explain security feature performance clearly and confidently.
"What if CSP rules were stored in a hash map by resource type? How would the time complexity change?"
Practice
Solution
Step 1: Understand CSP's role in security
CSP is designed to restrict what content (like scripts, images) a website can load to prevent harmful content.Step 2: Compare options with CSP purpose
Only controlling content loading matches CSP's main goal; speeding up or design changes are unrelated.Final Answer:
To control which content the website is allowed to load -> Option DQuick Check:
CSP controls content loading = A [OK]
- Confusing CSP with website speed optimization
- Thinking CSP manages website design
- Assuming CSP stores user data
Solution
Step 1: Identify CSP header name
The official header to set CSP rules is namedContent-Security-Policy.Step 2: Eliminate unrelated headers
X-Content-Type-Optionscontrols MIME sniffing,Strict-Transport-Securityenforces HTTPS, andCache-Controlmanages caching, none set CSP.Final Answer:
Content-Security-Policy -> Option BQuick Check:
CSP header = Content-Security-Policy [OK]
- Confusing CSP header with security headers like HSTS
- Using Cache-Control as CSP header
- Mixing up header names with similar security headers
Content-Security-Policy: script-src 'self' https://trusted.com;Which script source is allowed to run on the website?
Solution
Step 1: Understand the directive meaning
The directivescript-src 'self' https://trusted.com;means scripts can load from the same origin ('self') and from https://trusted.com.Step 2: Analyze options against directive
Only scripts from the website itself and https://trusted.com correctly states scripts allowed from the website itself and trusted.com; others are incorrect or too broad.Final Answer:
Only scripts from the website itself and https://trusted.com -> Option CQuick Check:
script-src 'self' + trusted.com = A [OK]
- Assuming all external scripts are allowed
- Ignoring the 'self' keyword meaning
- Thinking no scripts are allowed due to strictness
Content-Security-Policy: default-src 'none'; img-src https://images.com;Why might images from the website itself not load?
Solution
Step 1: Understand default-src 'none'
The directivedefault-src 'none'blocks all content sources unless specifically allowed.Step 2: Analyze img-src directive
Only images from https://images.com are allowed; images from the website itself are not allowed because 'self' is not included.Final Answer:
Because default-src 'none' blocks all sources except those explicitly allowed -> Option AQuick Check:
default-src 'none' blocks all except allowed = D [OK]
- Assuming img-src allows all images by default
- Thinking header syntax is wrong without checking directives
- Blaming browser settings instead of CSP rules
Solution
Step 1: Understand blocking inline scripts
To block inline scripts, do not include 'unsafe-inline' in thescript-srcdirective.Step 2: Analyze options for script sources
Content-Security-Policy: script-src 'self'; allows scripts only from the website itself ('self') and blocks inline scripts by omission of 'unsafe-inline'. Content-Security-Policy: script-src 'self' 'unsafe-inline'; allows inline scripts, C allows all scripts, and D allows inline scripts but blocks others.Final Answer:
Content-Security-Policy: script-src 'self'; -> Option AQuick Check:
Allow 'self' only, no 'unsafe-inline' = B [OK]
- Including 'unsafe-inline' which allows inline scripts
- Using wildcard * which allows all scripts
- Misunderstanding default-src vs script-src directives
