Add CKV_DOCKER_1005: Detect secrets and private keys in ENV/ARG Docker instructions #7342
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
🧾 Pull Request
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
🧠 Description
This pull request introduces a new Dockerfile security check
CKV_DOCKER_1005,titled “Ensure no secrets or private keys are stored in ENV or ARG instructions.”
This enhancement improves Checkov’s Dockerfile scanning capabilities by identifying hardcoded secrets and credentials that appear within
ENVorARGinstructions. Such secrets can be unintentionally exposed through:docker history)By flagging these vulnerabilities early, this check helps teams strengthen their DevSecOps posture and adhere to secure build practices.
🪲 Fixes
Fixes: N/A (new feature addition — introduces a new policy check)
🔒 New/Edited Policies
Policy Overview
CKV_DOCKER_1005ENVandARGinstructionsThis check parses each Dockerfile instruction and inspects its value for known secret patterns, API tokens, or private key headers.
It ensures that sensitive credentials are not embedded in Docker images, where they can be leaked during build or distribution.
Patterns Detected
key=,token=,secret=,password=,pwd=,auth=,credential=AKIA...andASIA...ghp_...eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...xoxb-,xoxa-,xoxp------BEGIN PRIVATE KEY-----API_KEY,ACCESS_TOKEN,CLIENT_SECRETEach match is reported with full metadata including instruction type (
ENV/ARG), file path, and line numbers.🛠 Remediation
1️⃣ Docker BuildKit Secrets
Leverage Docker’s built-in secret mounting to securely inject secrets at build time.
dockerfile
❌ Insecure
ENV API_KEY=abcd1234
✅ Secure
RUN --mount=type=secret,id=my_api_key
export API_KEY=$(cat /run/secrets/my_api_key)
📘 Reference: Docker BuildKit Secrets Documentation```
##2️⃣ Use External Secret Managers
This ensures secrets are stored and retrieved securely without ever embedding them into Docker images.
##3️⃣ Inject Secrets at Runtime
These methods prevent secrets from being cached or versioned in Docker layers or repositories.
⚙️ Technical Details of Implementation
File:
checkov/dockerfile/checks/SecretsInEnvArg.pyBaseDockerfileCheck["ENV", "ARG"]scan_resource_conf()to parse Dockerfile instructionsCheckResult.FAILED→ when secret-like patterns are detectedCheckResult.PASSED→ when all values are safeFile:
checkov/dockerfile/checks/SecretsInEnvArg.yamlid,name,category,severity, andremediationTests:
tests/dockerfile/checks/graph_checks/SecretsInEnvArg/Dockerfile.pass→ safe, clean DockerfileDockerfile.fail→ includes API keys and tokensDockerfile.privatekey.fail→ includes a private key stringtest_SecretsInEnvArg.py→ validates all cases and verifies fail/pass logic🧪 Test Results
CLI Verification
bash
checkov --framework dockerfile --check CKV_DOCKER_1005 -d tests/dockerfile/checks/graph_checks/SecretsInEnvArg --compact
Output:
dockerfile scan results:
Passed checks: 1, Failed checks: 2, Skipped checks: 0
Check: CKV_DOCKER_1005: "Ensure no secrets or private keys are stored in ENV or ARG instructions"
PASSED for resource: /Dockerfile.pass.
FAILED for resource: /Dockerfile.privatekey.fail.ENV
FAILED for resource: /Dockerfile.fail.ARG
2️⃣ Pytest Validation
pytest tests/dockerfile/checks/graph_checks/SecretsInEnvArg/test_SecretsInEnvArg.py -v
##✅ Results:
Both secret detection and private key exposure tests pass successfully.
✅ Checklist
##🧩 Impact
This new policy significantly strengthens Dockerfile scanning by detecting both generic credentials and private keys, bridging a key security gap in image build pipelines. It empowers developers to identify misconfigurations early and adopt secure build practices in alignment with modern DevSecOps standards.