CSRF protection in Jenkins - Time & Space Complexity
We want to understand how the time needed to check CSRF tokens grows as the number of requests increases in Jenkins pipelines.
How does the system handle more requests without slowing down too much?
Analyze the time complexity of the following Jenkins pipeline snippet that checks CSRF tokens.
pipeline {
agent any
stages {
stage('Check CSRF') {
steps {
script {
if (!currentBuild.rawBuild.getCause(hudson.security.csrf.CrumbIssuer.class)) {
error('CSRF token missing or invalid')
}
}
}
}
}
}
This code checks if the CSRF token is present and valid for each build request.
Look for repeated checks or loops in the code.
- Primary operation: Checking the CSRF token for each build request.
- How many times: Once per build request, no loops inside the snippet.
The time to check CSRF tokens grows linearly with the number of build requests.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 token checks |
| 100 | 100 token checks |
| 1000 | 1000 token checks |
Pattern observation: Each new request adds one token check, so the work grows steadily with requests.
Time Complexity: O(n)
This means the time to check CSRF tokens increases directly with the number of requests.
[X] Wrong: "CSRF token checks happen multiple times per request, so time grows faster than requests."
[OK] Correct: The check runs once per request, so time grows only as requests increase, not faster.
Understanding how security checks like CSRF token validation scale helps you design pipelines that stay fast and safe as usage grows.
"What if the CSRF check included scanning a list of allowed tokens? How would the time complexity change?"