Skip to content

Allow editing .env files as text and add update validation tests - #20

Merged
cyberinferno merged 2 commits into
masterfrom
feat/dot-env
Jun 23, 2026
Merged

Allow editing .env files as text and add update validation tests#20
cyberinferno merged 2 commits into
masterfrom
feat/dot-env

Conversation

@cyberinferno

@cyberinferno cyberinferno commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Treat .env and .env.* files as editable text files in the file editor service
  • Allow empty content updates for text files while still rejecting missing content payloads
  • Add service and handler tests covering .env detection and text file update behavior

Testing

  • Added unit tests for .env file type detection and matching rules
  • Added handler tests for empty-content updates and missing-content validation
  • Not run (not requested)

Summary by CodeRabbit

Release Notes

  • New Features

    • File updates now include automatic revision tracking with rollback capabilities on failure.
    • Environment files (.env, .env.local, etc.) are now supported as editable text files.
  • Improvements

    • File updates now support empty content.
  • Tests

    • Added test coverage for environment file handling and update validation.

@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@cyberinferno, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 42 minutes and 25 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan refill rate.

For paid Pro and Pro+ PR reviews, CodeRabbit uses rolling per-developer review limits. Reviews become available again as older review attempts age out of the rolling limit window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 52341393-04fa-40b3-9450-ec33aefb94f5

📥 Commits

Reviewing files that changed from the base of the PR and between 2c074c4 and 90574ce.

📒 Files selected for processing (5)
  • internal/server/file_system_routes.go
  • internal/server/file_system_text_test.go
  • internal/services/file_editor_service.go
  • internal/services/file_editor_service_item_test.go
  • internal/services/mock_FileEditorService.go
📝 Walkthrough

Walkthrough

Adds .env and .env.* filename detection to GetFileType so those files are classified as text. Introduces updateFileWithRevision, a locked, revisioned file-update helper with DB transaction and rollback/restore on failure. Changes TextFileUpdateAPIData to use *string for Content, and adds tests for empty-content acceptance and missing-field rejection.

Changes

Text file editing enhancements

Layer / File(s) Summary
.env file type detection
internal/services/file_editor_service.go, internal/services/file_editor_service_item_test.go
Adds EnvFileName and EnvFilePrefix constants, an isEnvFileName helper, and updates GetFileType to return FileTypeText for matching basenames before falling through to extension-based detection. Tests assert correct classification and isEnvFileName matching/rejection across several filename variants.
TextFileUpdateAPIData pointer content field
internal/server/file_system_routes.go
Introduces the exported TextFileUpdateAPIData struct with Content *string and updates the text-file update handler to decode into that type and dereference the pointer when hashing and writing.
updateFileWithRevision helper and text-file update tests
internal/server/file_system_routes.go, internal/server/file_system_text_test.go
Adds updateFileWithRevision (per-file OS lock, content-hash no-op guard, DB revision record creation, previous-content snapshot, write callback execution, commit, and rollback+restore on failure) and restoreFileAfterFailedWrite. Tests cover empty-content acceptance (HTTP 200, file emptied) and missing content field rejection (HTTP 400, file unchanged).

Sequence Diagram(s)

sequenceDiagram
  rect rgba(100, 149, 237, 0.5)
    Note over Client,DB: Text file update with revisioning
  end
  participant Client
  participant Handler as text-file update handler
  participant updateFileWithRevision
  participant DB as DB transaction
  participant Disk as filesystem

  Client->>Handler: PUT /api/file-tree/text-file {content: "..."}
  Handler->>Handler: decode into TextFileUpdateAPIData (*string)
  Handler->>updateFileWithRevision: path, buildContent(*req.Content), writeCallback
  updateFileWithRevision->>Disk: acquire OS lock, read existing bytes
  updateFileWithRevision->>updateFileWithRevision: hash old vs new bytes
  alt content unchanged
    updateFileWithRevision-->>Handler: no-op, return early
  end
  updateFileWithRevision->>DB: begin tx, insert revision record
  updateFileWithRevision->>Disk: write previous content to revision dir
  updateFileWithRevision->>DB: update revision with snapshot path
  updateFileWithRevision->>Disk: execute write callback (new content)
  alt write succeeds
    updateFileWithRevision->>DB: update revision status = completed, commit
    updateFileWithRevision-->>Handler: revision_id
    Handler-->>Client: HTTP 200
  else write fails
    updateFileWithRevision->>DB: rollback
    updateFileWithRevision->>Disk: restoreFileAfterFailedWrite (best-effort)
    Handler-->>Client: HTTP 500
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • omnihance/omnihance-a3-agent#10: Adds a drop-file GET/PUT endpoint that uses the same revisioned/locked write workflow introduced by updateFileWithRevision in file_system_routes.go.
  • omnihance/omnihance-a3-agent#13: Directly mirrors the updateFileWithRevision flow and revision restore/failure handling changes in internal/server/file_system_routes.go.

Poem

🐇 Hopping through the .env fields,
Where text files now reveal their yields,
A revision locked with careful care,
Old bytes restored beyond compare,
Empty content? Still allowed here!
The rabbit stamps each changeset clear. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately captures the main changes: enabling .env file editing as text and adding validation tests for content handling.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/dot-env

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.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/server/file_system_routes.go (1)

779-792: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Revision snapshots are stored with overly permissive filesystem modes.

Line 779 creates revision directories as 0755, and Line 792 writes prior-content snapshots as 0644. With .env support, revision copies can contain secrets and become readable by other local users.

🔧 Proposed fix
- if err = s.fileEditor.MkdirAll(revisionDir, 0755); err != nil {
+ if err = s.fileEditor.MkdirAll(revisionDir, 0700); err != nil {
    ...
 }

- if err = s.fileEditor.WriteFile(revisionPath, previousData, 0644); err != nil {
+ if err = s.fileEditor.WriteFile(revisionPath, previousData, ctx.info.Mode().Perm()); err != nil {
    ...
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/server/file_system_routes.go` around lines 779 - 792, The revision
directory and file permissions are overly permissive, allowing other local users
to read sensitive data. Change the permissions in the s.fileEditor.MkdirAll call
from 0755 to 0700 to restrict directory access to the owner only, and change the
permissions in the s.fileEditor.WriteFile call from 0644 to 0600 to restrict
file access to the owner only. This ensures revision snapshots containing
secrets are not readable by other local users.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/server/file_system_routes.go`:
- Around line 1010-1012: The WriteTextFileData method call within the
updateFileWithRevision closure on line 1012 writes files with fixed 0644
permissions, which unintentionally relaxes permissions on previously restricted
files like .env files with 0600 permissions. To fix this, before calling
WriteTextFileData, retrieve the current file's permission metadata from the
existing file at ctx.cleanPath, and then pass those original permissions to
WriteTextFileData (or a similar method that accepts permission parameters) to
ensure the updated file retains its original permission settings.

---

Outside diff comments:
In `@internal/server/file_system_routes.go`:
- Around line 779-792: The revision directory and file permissions are overly
permissive, allowing other local users to read sensitive data. Change the
permissions in the s.fileEditor.MkdirAll call from 0755 to 0700 to restrict
directory access to the owner only, and change the permissions in the
s.fileEditor.WriteFile call from 0644 to 0600 to restrict file access to the
owner only. This ensures revision snapshots containing secrets are not readable
by other local users.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2aebe3dc-8fba-464c-9d25-2e7780bcaca9

📥 Commits

Reviewing files that changed from the base of the PR and between 286017f and 2c074c4.

📒 Files selected for processing (4)
  • internal/server/file_system_routes.go
  • internal/server/file_system_text_test.go
  • internal/services/file_editor_service.go
  • internal/services/file_editor_service_item_test.go

Comment thread internal/server/file_system_routes.go Outdated
@cyberinferno
cyberinferno merged commit 7c86794 into master Jun 23, 2026
2 checks passed
@cyberinferno
cyberinferno deleted the feat/dot-env branch June 23, 2026 06:40
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