Skip to content

fix(514): fix compatibility with rack 3 #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

deril
Copy link

@deril deril commented May 20, 2025

All PRs:

  • Has tests
  • Documentation updated

fixes #514 by adding compatibility with Rack 3 which doesn't support muplitple headers joined with \n

deril added 2 commits May 20, 2025 12:37
Do not join cookies with new like if they weren't before
Add `Rack::Headers` wrapping to middleware to
prevent header manipulation issues. Added a test
to verify cookies remain as an array when flagged
if already in array format.
@Copilot Copilot AI review requested due to automatic review settings May 20, 2025 11:24
Copy link

@Copilot 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 fixes compatibility with Rack 3 by updating the handling of cookie headers, ensuring that cookies passed as either an array or a newline-delimited string are correctly processed.

  • Update test expectations to use newline-delimited strings for cookie headers
  • Modify middleware to handle cookies based on their original type (array vs. string)
  • Add a new test to verify that an array remains an array after processing

Reviewed Changes

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

File Description
spec/lib/secure_headers/middleware_spec.rb Updates test cases for handling cookie header formats
lib/secure_headers/middleware.rb Adjusts cookie processing logic for Rack 3 compatibility
Comments suppressed due to low confidence (1)

spec/lib/secure_headers/middleware_spec.rb:86

  • [nitpick] Ensure that this test also covers possible edge cases, such as improper formatting or unexpected delimiters in the cookie header, to provide comprehensive validation of the splitting logic.
cookie_middleware = Middleware.new(lambda { |env| [200, env.merge("Set-Cookie" => "_session=foobar\n_guest=true"), "app"] })

# Support Rails 2.3 / Rack 1.1 arrays as headers
cookies = cookies.split("\n") unless cookies.is_a?(Array)
cookies = headers["Set-Cookie"]
return unless cookies

Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding an inline comment that explains the rationale for checking the cookie type (array vs. string) based on Rack 3 behavior. This can help maintain clarity for future developers.

Suggested change
# Rack 3 may return the "Set-Cookie" header as either an array or a string.
# This check ensures compatibility with both formats.

Copilot uses AI. Check for mistakes.

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.

SecureHeaders middleware erases all cookies in Rack 3 due to \n joining
1 participant