Repository navigation
fix: keep .env files out of the image build - #367
Conversation
.dockerignore re-includes pro_tes/ as a whole, so a pro_tes/.env or key file would land in the layer of the app's COPY. Exclude .env* (except .env.example), *.pem and *.key files everywhere, and test the patterns with Docker's last-match-wins semantics. Closes #361 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Match .pem and .key case-insensitively, add PKCS#12 files (.p12, .pfx), and normalize patterns in the test as Docker does (review). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideThis PR hardens the Docker build context by excluding environment files and key/certificate artifacts—even inside directories re-included by File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #367 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 23 23
Lines 2234 2234
=========================================
Hits 2234 2234 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/test_build.py" line_range="147" />
<code_context>
+ ".env",
+ "pro_tes/.env",
+ "pro_tes/.env.local",
+ "pro_tes/config/.env.production",
+ "pro_tes/tls.key",
+ "pro_tes/TLS.KEY",
</code_context>
<issue_to_address>
**Uppercase dotenv secrets enter images**
When a developer stores credentials in a file such as `pro_tes/.ENV.production`, the case-sensitive `**/.env*` rule does not match uppercase names, so `COPY pro_tes` includes the credentials in an image layer; the later `rm -rf /tmp/src` cannot remove that layer, and anyone with the image can recover them.
Exclude dotenv names case-insensitively and add uppercase variants to the build-context test.
</issue_to_address>
### Comment 2
<location path="tests/test_build.py" line_range="158" />
<code_context>
+ for path in (
+ "pro_tes/app.py",
+ "pro_tes/config.yaml",
+ "pro_tes/.env.example",
+ "pyproject.toml",
+ "scripts/image_requirements.py",
</code_context>
<issue_to_address>
**Example directories expose secrets**
When the build context contains a directory named `pro_tes/.env.example` with secret files beneath it, the `!**/.env.example` exception also matches a directory and its descendants, so the later exception overrides the `.env*` exclusion for files beneath it; secrets in a `pro_tes/.env.example/` directory are sent to the build and copied into the image.
Add a later exclusion for descendants of `.env.example` directories and test that their contents remain ignored.
</issue_to_address>Linked issues
#361 fix: keep .env files out of the image build · 2 of 2 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Exclude .env* files except .env.example, and *.pem and *.key files anywhere in the build context. | ✅ Met | The final .dockerignore patterns exclude these files after the pro_tes re-include; the test checks representative paths under pro_tes and confirms .env.example remains included there. |
| 2 | Add a test that fails if the patterns are removed or a later re-include overrides them. | ✅ Met | test_dockerignore_secrets evaluates the ordered patterns and asserts secret paths are ignored, so removing the exclusions or adding an overriding re-include causes an assertion to fail. |
Sourcery assessment
Approval pending. 2 findings to address first.
Blocking findings: tests/test_build.py:147, tests/test_build.py:158
Match .env* case-insensitively and keep files below directories named .env.example excluded (Sourcery review). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Summary
.dockerignoreexcludes everything and then re-includespro_tes/as a whole, so a localpro_tes/.envor key file would be sent to the build and land in the layer ofCOPY pro_tes(the laterrm -rfdoesn't remove it from that layer). This PR excludes, everywhere in the build context and after the re-includes:.env*in any case, except.env.example(and files below directories named.env.example);*.pemand*.key(any case), and PKCS#12 files (*.p12,*.pfx).tests/test_build.py::test_dockerignore_secretschecks the patterns with Docker's matching rules (last match wins, parent directories match, leading/dropped), for secret files at the root and underpro_tes/, and that the files the build needs are still sent.No docs change: the image and its build interface are unchanged (internal hardening).
Closes #361
Verification
pytest tests/test_build.py: fails before the.dockerignorechange (pro_tes/.env is sent to the build), passes after.coverage run -m pytest→ 1092 passed, 175 skipped (Helm/MongoDB/Docker tests need tools not available locally),coverage report100%.pre-commit run --files .dockerignore tests/test_build.py(ruff, ruff format, semgrep, gitleaks, ast-grep, mypy): passed.CI passed, image build, smoke test, scan, tests on mongomock and MongoDB 7.0, chart, docs, CodeQL, Codecov)..env, directories named.env.example), fixed and resolved.Declined review findings
examples/plugin-image/Dockerfile.dockerignorere-includeswheels/without the secret patterns: the wheels are only bind-mounted (RUN --mount=type=bind), so nothing ofwheels/lands in a layer; the file is copied by operators into a directory that holds onlyplugins.txtandwheels/.!**/.env.examplealso re-includes.env.examplefiles in directories excluded by*(e.g.,docs/): harmless, the Dockerfile never copies them.🤖 Generated with Claude Code