Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Add same_site attribute and tests #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/cookiejar/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Cookie
# indicates specific ports on the HTTP server which should receive this
# cookie if contacted.
attr_reader :ports
# [String] SameSite cookie attribute value - see
# https://tools.ietf.org/html/draft-west-first-party-cookies-07
attr_reader :same_site
# [Time] Time when this cookie was first evaluated and created.
attr_reader :created_at

Expand Down Expand Up @@ -240,10 +243,12 @@ def self.compute_search_domains(request_uri)
# Call {from_set_cookie} to create a new Cookie instance
def initialize(args)
@created_at, @name, @value, @domain, @path, @secure,
@http_only, @version, @comment, @comment_url, @discard, @ports \
@http_only, @version, @comment, @comment_url, @discard, @ports, \
@same_site \
= args.values_at \
:created_at, :name, :value, :domain, :path, :secure,
:http_only, :version, :comment, :comment_url, :discard, :ports
:http_only, :version, :comment, :comment_url, :discard, :ports,
:same_site

@created_at ||= Time.now
@expiry = args[:max_age] || args[:expires_at]
Expand Down
2 changes: 1 addition & 1 deletion lib/cookiejar/cookie_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def self.parse_set_cookie(set_cookie_value)
when :httponly
args[:http_only] = true
when :samesite
args[:samesite] = keyvalue.downcase
args[:same_site] = keyvalue.downcase
else
fail InvalidCookieError, "Unknown cookie parameter '#{key}'"
end
Expand Down
4 changes: 4 additions & 0 deletions spec/cookie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
expect(cookie.name).to eq 'GALX'
expect(cookie.secure).to be_truthy
end
it 'should accept SameSite attribute' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

cookie = Cookie.from_set_cookie 'https://www.google.com/a/blah', 'GALX=RgmSftjnbPM;samesite=strict'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Metrics/LineLength: Line is too long. [105/80]

expect(cookie.same_site).to eq 'strict'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end
describe '#from_set_cookie2' do
it 'should give back the input names and values' do
Expand Down
4 changes: 4 additions & 0 deletions spec/cookie_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
higher = Cookie.from_set_cookie 'http://foo.com/bar/baz/', 'foo=bar;path=/bar/'
CookieValidation.validate_cookie('http://foo.com/bar/baz/', higher)
end
it 'should accept SameSite attribute' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

cookie = Cookie.from_set_cookie 'http://127.0.0.1/', 'foo=bar;samesite=strict'
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Metrics/LineLength: Line is too long. [84/80]

expect(CookieValidation.validate_cookie('http://127.0.0.1/', cookie)).to be_truthy
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
Metrics/LineLength: Line is too long. [88/80]

end
end
describe '#cookie_base_path' do
it "should leave '/' alone" do
Expand Down