Skip to content

Add encryptcookie middleware migration#212

Merged
ReneWerner87 merged 1 commit into
masterfrom
add-migration-for-encryptcookie-middleware
Oct 8, 2025
Merged

Add encryptcookie middleware migration#212
ReneWerner87 merged 1 commit into
masterfrom
add-migration-for-encryptcookie-middleware

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented Oct 7, 2025

Summary

  • add a v3 migration that injects the new cookie name argument into encryptcookie Encryptor/Decryptor hooks
  • register the migration with the existing v3 migration list and cover inline, multiline, and idempotent cases with tests

Summary by CodeRabbit

  • New Features
    • Added an automated migration that updates encryptcookie middleware configurations to the new parameter format during upgrades.
    • Ensures safe, idempotent changes with clear migration logging.
    • Updated migration sequence to include this step for versions >=2.x to <4.0.
  • Tests
    • Added comprehensive tests covering single-line, multi-line, and idempotent migration scenarios to verify correctness and stability.

@welcome
Copy link
Copy Markdown

welcome Bot commented Oct 7, 2025

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@github-actions github-actions Bot added the ✏️ Feature New feature or request label Oct 7, 2025
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Oct 7, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Adds a new v3 migration that rewrites encryptcookie.Config blocks to include updated Encryptor/Decryptor parameters, registers it in the v3 migrations list, and provides tests covering single-line, multi-line, and idempotent cases.

Changes

Cohort / File(s) Summary
Migration registry update
cmd/internal/migrations/lists.go
Registers v3migrations.MigrateEncryptcookieConfig in the v3 migration sequence (>=2.0.0-0 to <4.0.0-0).
New migration implementation
cmd/internal/migrations/v3/encryptcookie_config.go
Adds MigrateEncryptcookieConfig to scan Go files for encryptcookie.Config{...} and update Encryptor/Decryptor function fields to include an extra first parameter via regex-based rewrite.
Tests for migration
cmd/internal/migrations/v3/encryptcookie_config_test.go
Adds tests for standard, multi-line config formats, and idempotency; asserts content changes and migration log output.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as Developer (CLI)
  participant CMD as cobra.Command
  participant Mig as MigrateEncryptcookieConfig
  participant FS as File System
  participant RW as internal.ChangeFileContent

  Dev->>CMD: run migration (v3)
  CMD->>Mig: invoke with cwd and versions
  Mig->>FS: read .go files in cwd
  Mig->>Mig: find encryptcookie.Config{...} via regex
  alt Matches found
    Mig->>Mig: add Encryptor/Decryptor extra param
    Mig->>RW: write updated content
    RW-->>Mig: success/error
    alt Success
      Mig-->>CMD: print "Migrating encryptcookie middleware configs"
    else Error
      Mig-->>CMD: return wrapped error
    end
  else No matches
    Mig-->>CMD: no-op
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

A rabbit taps keys with a migrator’s grin,
Tweaking cookies so bytes fit right in.
Encryptor, Decryptor—now greeting a name,
Hop, hop through files in a parameter game.
Tests nibble green leaves—idempotent delight,
Carrots compiled, the code hops tight. 🥕

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The pull request description provides a brief summary of changes but does not adhere to the repository’s template because it omits the required ## Commit formatting section and does not explicitly use the prescribed emoji guidelines or fully explain the rationale for the migration in the defined structure. Please update the description to follow the repository template by adding a detailed explanation of why the migration is needed and including the ## Commit formatting section with the emoji guidelines.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly and accurately summarizes the core change by indicating that an encryptcookie middleware migration is being added, directly reflecting the main purpose of the pull request without extraneous details or ambiguity.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-migration-for-encryptcookie-middleware

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8827133 and d334547.

📒 Files selected for processing (3)
  • cmd/internal/migrations/lists.go (1 hunks)
  • cmd/internal/migrations/v3/encryptcookie_config.go (1 hunks)
  • cmd/internal/migrations/v3/encryptcookie_config_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:51-54
Timestamp: 2024-07-01T03:33:22.283Z
Learning: Unit tests for key length enforcement in `DecryptCookie` have been added to ensure consistency and security in the encryption processes.
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:51-54
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests for key length enforcement in `DecryptCookie` have been added to ensure consistency and security in the encryption processes.
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:20-23
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests for key length enforcement in both `EncryptCookie` and `DecryptCookie` functions have been added to ensure robust validation and prevent potential runtime errors.
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:20-23
Timestamp: 2024-07-01T03:44:03.672Z
Learning: Unit tests for key length enforcement in both `EncryptCookie` and `DecryptCookie` functions have been added to ensure robust validation and prevent potential runtime errors.
📚 Learning: 2024-07-02T13:29:56.992Z
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:22-25
Timestamp: 2024-07-02T13:29:56.992Z
Learning: The `encryptcookie_test.go` file contains unit tests that validate key lengths for both `EncryptCookie` and `DecryptCookie` functions, ensuring that invalid key lengths raise appropriate errors.

Applied to files:

  • cmd/internal/migrations/v3/encryptcookie_config_test.go
📚 Learning: 2024-10-08T19:06:06.583Z
Learnt from: gaby
PR: gofiber/fiber#3056
File: middleware/encryptcookie/utils.go:20-23
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests for key length enforcement in both `EncryptCookie` and `DecryptCookie` functions have been added to ensure robust validation and prevent potential runtime errors.

Applied to files:

  • cmd/internal/migrations/v3/encryptcookie_config_test.go
🧬 Code graph analysis (3)
cmd/internal/migrations/lists.go (1)
cmd/internal/migrations/v3/encryptcookie_config.go (1)
  • MigrateEncryptcookieConfig (14-56)
cmd/internal/migrations/v3/encryptcookie_config_test.go (1)
cmd/internal/migrations/v3/encryptcookie_config.go (1)
  • MigrateEncryptcookieConfig (14-56)
cmd/internal/migrations/v3/encryptcookie_config.go (1)
cmd/internal/helpers.go (1)
  • ChangeFileContent (54-110)
🔇 Additional comments (6)
cmd/internal/migrations/lists.go (1)

65-65: LGTM!

The migration is correctly registered in the v3 migrations list. The placement between MigrateLimiterConfig and MigrateCacheConfig is appropriate.

cmd/internal/migrations/v3/encryptcookie_config_test.go (3)

14-38: LGTM!

The test comprehensively covers the basic single-line function signature migration scenario. It verifies:

  • Both Encryptor and Decryptor functions are updated
  • The new _ string parameter is correctly inserted as the first parameter
  • The migration message is printed to the output

40-65: LGTM!

This test is essential for validating that the migration correctly handles multiline function signatures while preserving indentation. The assertion on line 63 verifies that the parameter is inserted with proper formatting, which is critical for maintaining code readability.


67-89: LGTM!

Excellent idempotence test. This ensures that running the migration multiple times is safe and won't corrupt already-migrated code. This is critical for real-world usage where migrations might be run multiple times.

cmd/internal/migrations/v3/encryptcookie_config.go (2)

31-31: extractBlock helper exists
The extractBlock function is defined in cmd/internal/migrations/v3/common.go; no further changes needed.


72-72: splitArgs helper exists: the function splitArgs is implemented in cmd/internal/migrations/v3/common.go (lines 194–246).


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.

@gaby gaby requested a review from Copilot October 7, 2025 13:02
@gaby
Copy link
Copy Markdown
Member Author

gaby commented Oct 7, 2025

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

LOL 😂

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds migration functionality for the encryptcookie middleware to update function signatures by adding a new cookie name parameter to Encryptor and Decryptor hooks.

  • Adds MigrateEncryptcookieConfig function to inject a new string parameter into encryptcookie function signatures
  • Registers the migration with the existing v3 migration list
  • Provides comprehensive test coverage for inline, multiline, and idempotent scenarios

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cmd/internal/migrations/v3/encryptcookie_config.go Implements the core migration logic to add cookie name parameter to encryptcookie functions
cmd/internal/migrations/v3/encryptcookie_config_test.go Comprehensive test suite covering various function signature formats and migration scenarios
cmd/internal/migrations/lists.go Registers the new encryptcookie migration with the v3 migration list

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread cmd/internal/migrations/v3/encryptcookie_config.go
Comment thread cmd/internal/migrations/v3/encryptcookie_config.go
@gaby gaby marked this pull request as ready for review October 7, 2025 13:03
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting

Comment on lines +72 to +82
args := splitArgs(params)
var nonEmpty []string
for _, arg := range args {
if strings.TrimSpace(arg) != "" {
nonEmpty = append(nonEmpty, arg)
}
}

if len(nonEmpty) != 2 {
return match
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Skip inline parameters when comments present

The guard len(nonEmpty) != 2 relies on splitArgs returning exactly two entries, but splitArgs treats comment-only segments as arguments. If a user formats the Encryptor/Decryptor literal with inline comments (e.g., Encryptor: func( value string, // cookie value key string, )), splitArgs produces three entries (value string, the comment block, and key string) and the migration returns the original code without adding the new _ string parameter. Such signatures remain on the old two-argument form and will fail to compile after the middleware expects three parameters. Consider ignoring comment-only tokens (e.g., by stripping // and /* */ blocks) before the length check or by counting parameters syntactically rather than string splitting.

Useful? React with 👍 / 👎.

@ReneWerner87 ReneWerner87 merged commit 677633b into master Oct 8, 2025
13 checks passed
@welcome
Copy link
Copy Markdown

welcome Bot commented Oct 8, 2025

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex ✏️ Feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants