Skip to content

fix: keep .env files out of the image build - #367

Merged
uniqueg merged 3 commits into
devfrom
fix/dockerignore-secrets
Oct 31, 2026
Merged

uniqueg merged 3 commits into
devfrom
fix/dockerignore-secrets

Conversation

@uniqueg

@uniqueg uniqueg commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Summary

.dockerignore excludes everything and then re-includes pro_tes/ as a whole, so a local pro_tes/.env or key file would be sent to the build and land in the layer of COPY pro_tes (the later rm -rf doesn'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);
  • *.pem and *.key (any case), and PKCS#12 files (*.p12, *.pfx).

tests/test_build.py::test_dockerignore_secrets checks the patterns with Docker's matching rules (last match wins, parent directories match, leading / dropped), for secret files at the root and under pro_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 .dockerignore change (pro_tes/.env is sent to the build), passes after.
  • Full suite with coverage: coverage run -m pytest → 1092 passed, 175 skipped (Helm/MongoDB/Docker tests need tools not available locally), coverage report 100%.
  • pre-commit run --files .dockerignore tests/test_build.py (ruff, ruff format, semgrep, gitleaks, ast-grep, mypy): passed.
  • Not run locally: a real Docker build (no Docker in the sandbox). CI: all checks passed (CI passed, image build, smoke test, scan, tests on mongomock and MongoDB 7.0, chart, docs, CodeQL, Codecov).
  • Review: adversarial and quality reviewer subagents, no high or medium findings; lows fixed (case-insensitive key patterns, PKCS#12, pattern normalization and comment detection in the test, docstring on the approximation). Sourcery: two threads (case of .env, directories named .env.example), fixed and resolved.

Declined review findings

  • examples/plugin-image/Dockerfile.dockerignore re-includes wheels/ without the secret patterns: the wheels are only bind-mounted (RUN --mount=type=bind), so nothing of wheels/ lands in a layer; the file is copied by operators into a directory that holds only plugins.txt and wheels/.
  • !**/.env.example also re-includes .env.example files in directories excluded by * (e.g., docs/): harmless, the Dockerfile never copies them.

🤖 Generated with Claude Code

uniqueg and others added 2 commits October 31, 2026 23:02
.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]>
@sourcery-ai

sourcery-ai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR hardens the Docker build context by excluding environment files and key/certificate artifacts—even inside directories re-included by .dockerignore—while preserving required build inputs. A focused test suite approximates Docker’s matching rules and validates secret omission alongside required-file inclusion.

File-Level Changes

Change Details Files
Harden the Docker build context against accidental inclusion of credentials and certificate material, including files re-included by broad directory rules.
  • Add .dockerignore exclusions for .env* while preserving .env.example, private-key extensions case-insensitively, and PKCS#12 files.
  • Apply the exclusions after directory re-inclusion patterns so secrets under pro_tes/ remain omitted before COPY.
  • Retain required application, configuration, and build-support files in the context.
.dockerignore
Add focused tests that model the relevant Docker ignore semantics and verify both secret exclusion and required-file inclusion.
  • Implement a helper for last-match-wins matching, parent-directory matching, pattern normalization, negation, and supported glob syntax.
  • Check root and nested .env, key, PEM, and PKCS#12 paths, including uppercase extensions.
  • Assert representative application files and .env.example remain available to the build.
tests/test_build.py

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov

codecov Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5d04da6) to head (f93b164).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@uniqueg
uniqueg marked this pull request as ready for review October 31, 2026 22:15

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread tests/test_build.py Resolved
Comment thread tests/test_build.py Resolved
Match .env* case-insensitively and keep files below directories named
.env.example excluded (Sourcery review).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@uniqueg
uniqueg merged commit be3e6bb into dev Oct 31, 2026
19 checks passed
@uniqueg
uniqueg deleted the fix/dockerignore-secrets branch October 31, 2026 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: keep .env files out of the image build

1 participant