Skip to content

[SECURITY] Command Injection in HandleViewLogsGET via container name#114

Draft
arumes31 wants to merge 2 commits into
mainfrom
fix/security-command-injection-view-logs-4572037813102938627
Draft

[SECURITY] Command Injection in HandleViewLogsGET via container name#114
arumes31 wants to merge 2 commits into
mainfrom
fix/security-command-injection-view-logs-4572037813102938627

Conversation

@arumes31
Copy link
Copy Markdown
Owner

@arumes31 arumes31 commented Jun 4, 2026

This PR addresses a security vulnerability (command injection risk) in the HandleViewLogsGET handler.

Changes:

  1. Hardened HandleViewLogsGET: Switched from exec.Command to exec.CommandContext using the request's context. This ensures that the docker logs command is terminated if the HTTP request is cancelled or times out.
  2. Hardened HandleForceRestartPOST: Switched to exec.CommandContext with a dedicated 30-second timeout context for background container restarts. This prevents orphaned or hung processes.
  3. Security Regression Test: Added TestHandleViewLogsGET_Security to internal/handlers/security_test.go. This test specifically injects malicious container names and verifies that the config.IsValidContainerName validation (which was already present but now reinforced with better execution patterns) correctly blocks and logs the attempt.
  4. Code Quality: Added the missing context import to internal/handlers/web_handlers.go and ran gofmt.

These changes align the web handlers with the security patterns already established in the API handlers.


PR created automatically by Jules for task 4572037813102938627 started by @arumes31

- Hardened HandleViewLogsGET by switching to exec.CommandContext with request context.
- Hardened HandleForceRestartPOST by switching to exec.CommandContext with a 30s timeout context.
- Added TestHandleViewLogsGET_Security to internal/handlers/security_test.go to verify container name validation.
- Ensured all handlers are using context-aware command execution for better lifecycle management and security.

Co-authored-by: arumes31 <114224498+arumes31@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

πŸ‘‹ Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a πŸ‘€ emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jun 4, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

βš™οΈ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: eeab013a-70c0-41fd-8948-2f59334dbcb2

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • πŸ” Trigger review
✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/security-command-injection-view-logs-4572037813102938627

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Add the "--" end-of-options separator to the `docker logs` invocation so a
validated container name can never be interpreted as a docker flag (e.g.
-v, --since), and annotate the call with `#nosec G204 G702` plus a written
justification. Also surface the previously-discarded CombinedOutput error
to address the G104 unhandled-error smell.

Clears the gosec `test` gate for this PR (gosec G204/G702: 0 issues);
build, vet, and the security tests pass locally.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@arumes31
Copy link
Copy Markdown
Owner Author

arumes31 commented Jun 4, 2026

@

Maintainer review β€” verdict: REFACTOR (committed)

Thanks for this fix β€” the config.IsValidContainerName filtering and the dedicated TestHandleViewLogsGET_Security are exactly the right instinct, and the test correctly asserts the malicious entry is blocked and logged as invalid.

I took this to REFACTOR and pushed one commit (74ef17b) for a single reason: gosecs taint analyzer still flagged G702 at the exec.CommandContext call, since input validation alone is not provable to the analyzer and the value was still passed where Docker could interpret it as a flag.

Changes in 74ef17b:

  1. Added the -- end-of-options separator to docker logs ... -- <name> β€” defense-in-depth that defeats flag-style injection (-v, --since=...) independent of the regex. This matches the approach already used in [SECURITY] Command Injection in HandleAPILogsStreamGET via container nameΒ #113.
  2. Added #nosec G204 G702 with a written justification on the exact flagged line, clearing the test gate.
  3. Surfaced the previously-discarded CombinedOutput error (out, _ := β†’ out, err :=) β€” better UX on a failed docker logs and removes a G104 unhandled-error smell.

Local verification: go build ./... clean Β· go vet clean Β· TestHandleViewLogsGET_Security + TestHandleAPILogsStreamGET_Security PASS Β· gosec -include=G204,G702 ./internal/handlers/... β†’ 0 issues.

Note: This clears this PRs own gosec finding. The repo-wide test/build gates may stay red from pre-existing base-branch issues (gosec G104 elsewhere + a go mod download failure in the Docker build) β€” those are tracked separately and are not introduced by this PR.
@

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.

1 participant