Skip to content

🐛 bug: clear plaintext cookie when encryption fails#4303

Merged
ReneWerner87 merged 2 commits into
mainfrom
fix-encryptcookie-error-handling
May 21, 2026
Merged

🐛 bug: clear plaintext cookie when encryption fails#4303
ReneWerner87 merged 2 commits into
mainfrom
fix-encryptcookie-error-handling

Conversation

@gaby

@gaby gaby commented May 21, 2026

Copy link
Copy Markdown
Member

Motivation

  • Prevent plaintext Set-Cookie headers from being emitted when encryptcookie fails to encrypt a response cookie after the handler returns, which could leak cookie values to the client.

Description

  • Remove the affected response cookie from c.Response().Header when cfg.Encryptor returns an error before returning the encryption error, and add a regression assertion ensuring no Set-Cookie headers are returned on encryption failure in Test_Middleware_EncryptionErrorPropagates.

Copilot AI review requested due to automatic review settings May 21, 2026 02:11
@gaby gaby requested a review from a team as a code owner May 21, 2026 02:11
@gaby gaby requested a review from sixcolors May 21, 2026 02:11
@gaby gaby requested review from ReneWerner87 and efectn May 21, 2026 02:11
@coderabbitai

coderabbitai Bot commented May 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6e800b34-ba11-43b8-a9a7-5ea31c483ad7

📥 Commits

Reviewing files that changed from the base of the PR and between 9c65be0 and 63fecdb.

📒 Files selected for processing (2)
  • middleware/encryptcookie/encryptcookie.go
  • middleware/encryptcookie/encryptcookie_test.go

Walkthrough

Middleware snapshots response cookie keys, encrypts them by key, and if any encryption fails removes non-except cookies from the response before returning the joined error. A test now asserts no Set-Cookie headers are present when encryption fails.

Changes

Cookie Encryption Error Handling

Layer / File(s) Summary
Snapshot keys & encryption loop
middleware/encryptcookie/encryptcookie.go
Collects response cookie keys into a slice before encrypting. On an encrypt failure, deletes non-except cookies from the response header and returns the joined error; on success writes encrypted cookie values back.
Test: no Set-Cookie on error
middleware/encryptcookie/encryptcookie_test.go
Adds a second cookie in the test handler and asserts that when encryption fails the response returns the error and contains no Set-Cookie header values.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • gofiber/fiber#3842: Earlier PR that modified the encryptcookie middleware's response-cookie encryption failure path and related tests.

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

🐰 I hop through headers, keys in a line,

Encrypt each cookie, one at a time.
If one goes wrong, I sweep them away,
No Set-Cookie for errors today.
A tiny clean test, and then I dine.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% 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
Title check ✅ Passed The title clearly and concisely summarizes the main change: fixing a bug where plaintext cookies must be cleared when encryption fails.
Description check ✅ Passed The PR description adequately covers the motivation and changes, but lacks completion of recommended sections from the repository's template (changelog, migration guide, benchmarks, etc.).
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-encryptcookie-error-handling

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


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.

@ReneWerner87 ReneWerner87 added this to v3 May 21, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 21, 2026
@codecov

codecov Bot commented May 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.28%. Comparing base (e7d4940) to head (63fecdb).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4303      +/-   ##
==========================================
- Coverage   91.32%   91.28%   -0.05%     
==========================================
  Files         132      132              
  Lines       12933    12939       +6     
==========================================
  Hits        11811    11811              
- Misses        708      713       +5     
- Partials      414      415       +1     
Flag Coverage Δ
unittests 91.28% <100.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request modifies the encryptcookie middleware to delete a cookie from the response header if encryption fails and adds a regression test assertion. A review comment identifies a security concern where other cookies intended for encryption might remain in plaintext because the loop terminates immediately upon error. The reviewer suggests iterating through all response cookies to remove any that are not excluded to prevent sensitive information exposure.

Comment thread middleware/encryptcookie/encryptcookie.go Outdated
@gaby gaby removed the aardvark label May 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 hardens the encryptcookie middleware’s failure behavior to avoid leaking plaintext response cookies when cookie encryption fails after the downstream handler has already set cookies.

Changes:

  • Delete the response cookie from the outgoing response headers when cfg.Encryptor returns an error, before propagating the error.
  • Add a regression assertion to ensure no Set-Cookie headers are returned when encryption fails in Test_Middleware_EncryptionErrorPropagates.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
middleware/encryptcookie/encryptcookie.go Deletes the affected response cookie from the response header before returning an encryption error.
middleware/encryptcookie/encryptcookie_test.go Adds an assertion that Set-Cookie is empty when encryption fails.

Comment thread middleware/encryptcookie/encryptcookie.go Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

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

@ReneWerner87 ReneWerner87 merged commit 2301dc4 into main May 21, 2026
24 of 25 checks passed
@github-project-automation github-project-automation Bot moved this to Done in v3 May 21, 2026
@ReneWerner87 ReneWerner87 deleted the fix-encryptcookie-error-handling branch May 21, 2026 11:10
@ReneWerner87 ReneWerner87 modified the milestones: v3, v3.3.0 May 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants