Skip to content

Conversation

@morettimaxi
Copy link

@morettimaxi morettimaxi commented May 16, 2025

This change is Reviewable

🔒 Docker Hardening & Trivy Security Scan Integration
This PR introduces 3 key security-focused changes to improve the overall posture of our Docker-based FastAPI application:

🐳 1. Dockerfile Permissions Fix
We modified file permissions during image creation to avoid unsafe defaults like 775 that could allow any user to execute or modify files inside the container.

Before: Files were being copied with default permissions, sometimes resulting in 775, which is dangerous in multi-user environments.

Now: Explicit chmod 644 ensures:

Owner can read/write

Group and others can only read

No executable flag unless required

This prevents unauthorized execution or tampering from other users.

✅ Best practices applied: Least privilege, immutable file system.

🧪 2. Trivy Vulnerability Scanning
We integrated Aqua Security's Trivy as part of the CI/CD pipeline.

Docker image is scanned on every push, pull_request, or workflow_dispatch.

Results are uploaded as an artifact and printed in the GitHub Actions logs.

Current report includes Debian 12.10 base image and Python packages.

📄 Output format: clean table, stored in trivy-report.txt.

🚀 3. ENTRYPOINT Adjustment
We added a safe and minimal ENTRYPOINT to prevent arbitrary execution or misuse.

It avoids default shell context (no leftover commands)

Ensures predictable startup

Compatible with production container behavior

Summary by CodeRabbit

  • Chores
    • Introduced an automated Docker security scan for new code changes using GitHub Actions.
    • Improved Dockerfile formatting and build process for better maintainability.
    • Enhanced entrypoint script to ensure correct permissions on the database directory at startup.

@coderabbitai
Copy link

coderabbitai bot commented May 16, 2025

Walkthrough

A new GitHub Actions workflow for Docker image security scanning was added. The Dockerfile was reformatted and restructured, updating build steps, user creation, and permission handling. The entrypoint script was modified to fix directory permissions at startup. No changes were made to application code or exported entities.

Changes

File(s) Change Summary
.github/workflows/docker-security.yml Added a workflow to build the Docker image and run an Anchore security scan on pushes and PRs to the master branch.
Dockerfile Reformatted and restructured build steps, updated user creation, permission handling, and simplified comments.
scripts/entrypoint.sh Prepends a step to fix ownership of the /sqlite3-db directory before other logic.

Sequence Diagram(s)

sequenceDiagram
    participant GitHub Actions
    participant Docker Buildx
    participant Anchore Scan

    GitHub Actions->>Docker Buildx: Build Docker image from repository
    Docker Buildx-->>GitHub Actions: Docker image built
    GitHub Actions->>Anchore Scan: Run security scan on built image
    Anchore Scan-->>GitHub Actions: Report scan results (fail on issues)
Loading

Possibly related PRs

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@sonarqubecloud
Copy link

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
Dockerfile (1)

19-20: Avoid caching pip during upgrade and consider pinning pip version
Recommend adding --no-cache-dir to pip install --upgrade pip to prevent unnecessary cache usage:

-RUN pip install --upgrade pip && \
+RUN pip install --upgrade pip --no-cache-dir && \

Optionally, pin pip to a specific version for reproducible builds.

🧰 Tools
🪛 GitHub Check: Codacy Static Code Analysis

[warning] 19-19: Dockerfile#L19
Pin versions in pip. Instead of pip install <package> use pip install <package>==<version> or pip install --requirement <requirements file>

🪛 GitHub Check: CodeFactor

[warning] 19-19: Dockerfile#L19
Avoid use of cache directory with pip. Use pip install --no-cache-dir <package>. (DL3042)

.github/workflows/docker-security.yml (1)

24-29: Anchore security scanning configuration
Using anchore/scan-action@v2 with fail-build: true will block unsafe images. Consider adding an explicit severity threshold or Slack/PR comments on scan failures for quicker feedback loops.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 486a8af and 2b0c35a.

📒 Files selected for processing (3)
  • .github/workflows/docker-security.yml (1 hunks)
  • Dockerfile (1 hunks)
  • scripts/entrypoint.sh (1 hunks)
🧰 Additional context used
🪛 GitHub Check: Codacy Static Code Analysis
Dockerfile

[warning] 19-19: Dockerfile#L19
Pin versions in pip. Instead of pip install <package> use pip install <package>==<version> or pip install --requirement <requirements file>

🪛 GitHub Check: CodeFactor
Dockerfile

[warning] 19-19: Dockerfile#L19
Avoid use of cache directory with pip. Use pip install --no-cache-dir <package>. (DL3042)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Codacy Static Code Analysis
🔇 Additional comments (8)
Dockerfile (5)

8-15: Cache cleanup refinement in builder stage
The apt-get clean command effectively clears the package cache, and removing /var/lib/apt/lists/* is sufficient to slim the image. This approach is correct and follows best practices.


52-52: Ensure entrypoint script is executable
The chmod +x ./entrypoint.sh correctly makes the startup script runnable.


53-54: Clarify the database seed target directory
Copying sqlite3-db into ./docker-compose may be confusing (as that path typically holds Compose files). Please verify that ./docker-compose is the intended destination for the seed DB and consider renaming it (e.g., ./db-seeds) or documenting this choice.


56-60: Switch to a dedicated non-root user for runtime
Creating a system group and user (fastapi) and chown’ing /app and /sqlite3-db ensures the container process runs with least privilege. This aligns with best security practices.


65-69: Verify ENTRYPOINT privileges
Since USER fastapi is declared after the ENTRYPOINT, please confirm that Docker will execute the entrypoint script as root (so that chown in the script succeeds) and then drop to fastapi for the main process. If this isn’t guaranteed, consider moving USER fastapi below the build-time chown or performing privilege dropping inside the entrypoint.

scripts/entrypoint.sh (1)

3-4: Fixing permissions on the SQLite volume
Logging the permission fix and running chown -R fastapi:fastapi /sqlite3-db at startup ensures the mounted volume is writable by the app user. This change reliably addresses permission issues with the DB volume.

.github/workflows/docker-security.yml (2)

3-8: Validate workflow trigger branches
The workflow only runs on pushes and PRs to master. Verify that your default branch is indeed named master; otherwise, the scan won’t trigger.


21-23: Docker image build for scanning
Building the image via docker build -t python-samples-fastapi-restful . prepares it for Anchore analysis. You may later extend this step to tag images with a version or SHA for traceability.

@nanotaboada
Copy link
Owner

Thanks for the contribution @morettimaxi!

I'm closing this PR for now because several checks are failing. However, I will keep the branch and work on the following improvements:

  • Squash all commits into a single, clean commit
  • Follow conventional commit message guidelines
  • Test changes incrementally instead of bundling everything at once

I will likely create a GitHub issue to track these tasks and ensure consistency.
Once these updates are done, I'll reopen or create a new PR for review.

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.

2 participants