Mitigation Strategy: Strict Jenkinsfile
Source Control
Description:
- Establish a Git Repository: The
Jenkinsfile
(which defines the Declarative Pipeline) must be stored in a Git repository. - Branching Model: Use a branching model (e.g., Gitflow) with feature branches and pull requests for merging to the
main
branch. - Branch Protection: Configure branch protection on
main
:- Require pull request reviews.
- Require status checks (builds, tests).
- Restrict direct pushes.
- Code Reviews: Mandatory code reviews for all
Jenkinsfile
changes, specifically checking for:- Code injection in Groovy.
- Secret exposure.
script
block misuse.- Parameter misuse.
when
condition logic.
- Commit Signing (Recommended): Developers sign commits (GPG/SSH).
- Regular Access Audits: Review repository access and permissions.
-
Threats Mitigated:
- Code Injection (Severity: Critical): Prevents direct injection of malicious Groovy code into the
Jenkinsfile
. - Unauthorized Changes (Severity: High): Ensures all
Jenkinsfile
changes are reviewed/approved. - Compromised Developer Accounts (Severity: High): Limits the impact of compromised accounts on the
Jenkinsfile
.
- Code Injection (Severity: Critical): Prevents direct injection of malicious Groovy code into the
-
Impact:
- Code Injection: Risk significantly reduced (Critical to Low/Medium).
- Unauthorized Changes: Risk significantly reduced (High to Low).
- Compromised Accounts: Impact mitigated.
-
Currently Implemented:
- Example: "Implemented for
infrastructure-pipelines
repo. Branch protection onmain
, one reviewer required, CI checks enforced. Code reviews mandatory."
- Example: "Implemented for
-
Missing Implementation:
- Example: "Commit signing not implemented. Access audits ad-hoc.
application-pipelines
repo lacks branch protection."
- Example: "Commit signing not implemented. Access audits ad-hoc.
Mitigation Strategy: Parameter Sanitization and Validation (within the Pipeline)
Description:
- Identify All Parameters: List all parameters the pipeline accepts.
- Define Expected Types: Determine data type and constraints for each parameter.
- Implement Validation (in Groovy):
- String: Use regular expressions (e.g.,
^[a-zA-Z0-9]+$
). - Integer: Parse as integer, use
try-catch
. - Boolean: Accept "true"/"false" (case-insensitive).
- Choice: Define allowed values.
- Avoid Direct Interpolation: Never directly embed parameters in shell/Groovy without escaping/sanitization.
- String: Use regular expressions (e.g.,
- Safe
params
Object Use: Access viaparams
, be aware of types, use type-safe methods. - Reject Invalid Input: Fail build or use a safe default.
- Input Sanitization (Groovy): If input is used as code (HTML, SQL), sanitize it (HTML escaping, URL encoding, etc., using Groovy libraries).
-
Threats Mitigated:
- Code Injection (Severity: Critical): Prevents injecting malicious code via pipeline parameters.
- Cross-Site Scripting (XSS) (Severity: High): Mitigates XSS if input is displayed (relevant for
input
step). - Unexpected Behavior (Severity: Medium): Prevents unexpected behavior from invalid input.
-
Impact:
- Code Injection: Risk significantly reduced (Critical to Low/Medium).
- XSS: Risk significantly reduced (High to Low).
- Unexpected Behavior: Risk reduced (Medium to Low).
-
Currently Implemented:
- Example: "Basic validation in
deploy-application
pipeline using regex, but not consistent."
- Example: "Basic validation in
-
Missing Implementation:
- Example: "Comprehensive validation missing in
build-image
pipeline. XSS sanitization missing forinput
steps."
- Example: "Comprehensive validation missing in
Mitigation Strategy: Limit the Use of script
Blocks
Description:
- Prefer Declarative Directives: Use built-in directives (
agent
,stages
,steps
,post
, etc.) overscript
blocks. - Minimize
script
Block Code: If unavoidable, keepscript
blocks short and simple. - Justify and Review: Every
script
block must be justified in a comment and thoroughly reviewed, assessing:- Could it be a Declarative directive?
- Security implications of the code.
- Code injection potential.
- Isolate
script
Blocks: If interacting with sensitive data, isolate in a separate stage, usewithCredentials
.
-
Threats Mitigated:
- Code Injection (Severity: Critical): Reduces the attack surface for Groovy code injection.
- Increased Complexity (Severity: Medium): Makes the pipeline easier to understand, reducing vulnerability likelihood.
-
Impact:
- Code Injection: Risk reduced (Critical to Medium/High).
- Increased Complexity: Complexity reduced.
-
Currently Implemented:
- Example: "Team encouraged to use Declarative directives, but no formal policy. Older pipelines use many
script
blocks."
- Example: "Team encouraged to use Declarative directives, but no formal policy. Older pipelines use many
-
Missing Implementation:
- Example: "Formal review process for
script
blocks missing. No automated check for excessive use."
- Example: "Formal review process for
Mitigation Strategy: Secure Secret Management (within the Pipeline)
Description:
- Jenkins Credentials Plugin: All secrets (passwords, API keys, etc.) must be stored using the Jenkins Credentials plugin. Never hardcode.
- Appropriate Credential Type: Use the correct type (Secret text, Username with password, etc.).
withCredentials
Binding: UsewithCredentials
to bind secrets to environment variables only within the stage they are needed.- Avoid Echoing: Do not print secret environment variables.
- Mask Passwords (Global Setting): Enable "Mask Passwords" in Jenkins global configuration.
- Regular Rotation: Rotate secrets and update Jenkins credentials.
- Least Privilege: Grant access to credentials only to the pipelines and users that require them.
-
Threats Mitigated:
- Secret Exposure (Severity: Critical): Prevents exposure in
Jenkinsfile
, logs, or environment. - Credential Theft (Severity: High): Reduces impact by limiting scope and rotating secrets.
- Secret Exposure (Severity: Critical): Prevents exposure in
-
Impact:
- Secret Exposure: Risk significantly reduced (Critical to Low).
- Credential Theft: Impact reduced.
-
Currently Implemented:
- Example: "Credentials plugin used.
withCredentials
mostly used, but inconsistencies exist."
- Example: "Credentials plugin used.
-
Missing Implementation:
- Example: "Secret rotation not automated. 'Mask Passwords' not enabled. Older pipelines may have hardcoded secrets."
Mitigation Strategy: Controlled Shared Library Usage (with Declarative)
Description:
- Secure Shared Library Repo: Separate, secure Git repo, same controls as
Jenkinsfile
repo. - Version Control: Semantic versioning. Pipelines specify the exact version (e.g.,
@Library('[email protected]') _
). - Code Reviews: Rigorous code reviews for shared libraries, focusing on security.
- Dependency Management: Scan for vulnerabilities in dependencies (OWASP Dependency-Check, Snyk).
- Restrict Access: Limit which pipelines can load specific libraries.
- Testing: Thorough unit/integration tests for shared libraries.
- Secure Loading: Ensure libraries are loaded from a trusted source and integrity is verified.
-
Threats Mitigated:
- Code Injection (Severity: Critical): Prevents malicious code via compromised shared libraries.
- Dependency Vulnerabilities (Severity: High): Reduces risk from vulnerable dependencies.
- Unauthorized Code Execution (Severity: High): Prevents unauthorized pipelines from using privileged libraries.
-
Impact:
- Code Injection: Risk significantly reduced (Critical to Low/Medium).
- Dependency Vulnerabilities: Risk reduced (High to Medium/Low).
- Unauthorized Code Execution: Risk reduced (High to Low).
-
Currently Implemented:
- Example: "Shared libraries in separate Git repo, code reviews required. Versioning used, but not always enforced."
-
Missing Implementation:
- Example: "Dependency scanning not automated. Access controls for libraries not configured. Limited testing."
Mitigation Strategy: Simple and Secure when
Conditions
Description:
- Prefer Built-in Conditions: Use built-in
when
conditions (branch
,environment
, etc.) over custom Groovy. - Keep it Simple: Avoid complex, nested
when
conditions. Refactor to shared library functions if needed (with security). - Avoid Untrusted Input: Do not use user input directly in
when
conditions, especiallyexpression
. Validate/sanitize first. - Thorough Testing: Test
when
conditions extensively with various inputs. - Code Review Focus: Reviews must examine
when
conditions for logic errors, bypasses, injection.
-
Threats Mitigated:
- Logic Errors/Bypasses (Severity: Medium/High): Prevents unintended stage execution/skipping.
- Code Injection (Severity: Critical): Reduces risk if input is used in
when
(especiallyexpression
).
-
Impact:
- Logic Errors/Bypasses: Risk reduced (Medium/High to Low).
- Code Injection: Risk reduced (Critical to Low/Medium).
-
Currently Implemented:
- Example: "No specific guidelines on
when
condition complexity."
- Example: "No specific guidelines on
-
Missing Implementation:
- Example: "Code reviews don't consistently focus on
when
security. No automated check for complex/vulnerable conditions."
- Example: "Code reviews don't consistently focus on
Mitigation Strategy: Secure input
Step Configuration (within the Pipeline)
Description:
- Sanitize Input (Groovy): Always sanitize user input from
input
before using it where it could be interpreted as code.- HTML Escaping: Use
StringEscapeUtils
or similar for web display. - URL Encoding: For URLs.
- Shell Escaping: For shell commands (avoid direct interpolation; use parameters).
- HTML Escaping: Use
- Restrict Input Types: Use specific types (
choice
,booleanParam
,password
) overstring
. - Validate Input (Groovy): Implement validation rules, even with sanitization.
- Limit Input Scope: Use
input
only in specific stages where necessary. - Avoid Sensitive Data: Do not use
input
for secrets. Use Credentials plugin. - Timeout: Set a reasonable timeout for the
input
step.
-
Threats Mitigated:
- Cross-Site Scripting (XSS) (Severity: High): Prevents injecting JavaScript via
input
. - Code Injection (Severity: Critical): Prevents injecting code (e.g., shell) via
input
. - Denial of Service (DoS) (Severity: Medium): Timeout prevents indefinite blocking.
- Cross-Site Scripting (XSS) (Severity: High): Prevents injecting JavaScript via
-
Impact:
- XSS: Risk significantly reduced (High to Low).
- Code Injection: Risk significantly reduced (Critical to Low/Medium).
- DoS: Risk reduced (Medium to Low).
-
Currently Implemented:
- Example: "
input
used in a few pipelines, but sanitization inconsistent."
- Example: "
-
Missing Implementation:
- Example: "No HTML escaping or sanitization for
input
data. Validation is basic."
- Example: "No HTML escaping or sanitization for