Skip to content
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

Support for the Partitioned cookie attribute #2131

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ def set_cookie_header(key, value)
else
raise ArgumentError, "Invalid :same_site value: #{value[:same_site].inspect}"
end
partitioned =
if value[:partitioned]
raise ArgumentError, 'Partitioned cookies must be set with `secure`' unless value[:secure]
flavio-b marked this conversation as resolved.
Show resolved Hide resolved
"; partitioned"
end
value = value[:value]
else
key = escape(key)
Expand All @@ -297,7 +302,7 @@ def set_cookie_header(key, value)
value = [value] unless Array === value

return "#{key}=#{value.map { |v| escape v }.join('&')}#{domain}" \
"#{path}#{max_age}#{expires}#{secure}#{httponly}#{same_site}"
"#{path}#{max_age}#{expires}#{secure}#{httponly}#{same_site}#{partitioned}"
end

# :call-seq:
Expand Down
9 changes: 9 additions & 0 deletions test/spec_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,15 @@ def initialize(*)
Rack::Utils.set_cookie_header('na e', value: 'value', escape_key: false).must_equal 'na e=value'
end

it "sets partitioned cookie attribute" do
assert_raises(ArgumentError, 'Partitioned cookies must be set with `secure`') do
Rack::Utils.set_cookie_header('name', {value: 'value', partitioned: true})
end

Rack::Utils.set_cookie_header('name', {value: 'value', secure: true, partitioned: true})
.must_equal 'name=value; secure; partitioned'
end

it "deletes cookies in header field" do
header = []

Expand Down