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

Cookie filter #799

Closed
wants to merge 28 commits into from
Closed

Cookie filter #799

wants to merge 28 commits into from

Conversation

panda73111
Copy link
Contributor

Relevant issue: #792
This branch will add cookie management adhering to RFC 6265.
Right now, it adds a simple filter for cookies sent, regarding their "Domain" attribute.

  • Cookies passed to ClientSession without a "Domain" attribute (as SimpleCookie() or dict/tuple) are still shared across all requests, to keep backwards compatibility.
  • Leading dots are ignored (Domain=.example.com becomes Domain=example.com)
  • Cookies with matching domain are sent (Domain=example.com is sent to sub.example.com, but not the other way around)
  • Only shared cookies are sent to addresses that are not a domain, like an IP address

More filters (for the remaining attributes and for storing incoming cookies) will be added as soon as I have the time, until eventually ClientSession behaves like stated in RFC 6265.
CORS is completely new to me and low priority until further research.
This branch can be merged into master at any time, I'll add pull requests as needed and all tests pass.

  • The Domain Attribute
  • The Secure Attribute
  • The Path Attribute
  • The Expires Attribute
  • The Max-Age Attribute

EDIT 23.02.2016:

  • The Domain attribute of received cookies is set to the response hostname if empty, their host-only-flag is set to True
  • Cookies from IP addresses are rejected

EDIT 29.02.2016:

  • Cookies with secure-flag set are only sent to secure hosts (https://)

EDIT 18.03.2016:

  • Cookies with matching path are sent
  • If received cookies do not have the path-attribute defined, it is set to the response path

EDIT 20.03.2016:

  • Stored cookies are deleted when the time defined in the max-age- or expires-attribute is reached. A timer is set via loop.call_later()/call_at() when a cookie is added, that removes the cookie from the jar.

"""Returns this session's cookies filtered by their attributes"""
# TODO: filter by 'expires', 'path', ...
netloc = urllib.parse.urlsplit(url).netloc
is_ip = IPV4_PATTERN.match(netloc) is not None
Copy link
Member

Choose a reason for hiding this comment

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

ipaddress.IPAddress(netloc) to support both IPv4 and IPv6 in the right way without regexps (which accepts invalid IPv4 addresses).

@asvetlov asvetlov added this to the 0.22 milestone Feb 24, 2016
@asvetlov
Copy link
Member

Looks interesting. I need reread RFC first for making careful review.
I hope to make it during weekend.

Anyway I have a strong feeling that we need to fix cookies problem in the next aiohttp release.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 98.24% when pulling 4959234 on panda73111:cookie_filter into 496779e on KeepSafe:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 98.119% when pulling 1cfb2c6 on panda73111:cookie_filter into 496779e on KeepSafe:master.

@panda73111
Copy link
Contributor Author

I added the Expires- and Max-Age-attributes, by scheduling deletion of the cookie via call_later()/call_at(). But this gets problematic as soon as the user changes these attributes, the cookie would still be removed at the original timestamp. How should this be handled? For "Expires", an absolute date, the cookie could just remain in the cookie jar, and just not be sent if the time of sending the request is after the date defined in the "Expires" attribute. But "Max-Age" only consists of a time span in seconds, of which it is not possible to know when it has been set by the user.

@panda73111
Copy link
Contributor Author

...Or maybe any other comments? This is basically done, as it is.

(invalid values, host only cookie, domain matching, path matching, date parsing)
@auvipy
Copy link

auvipy commented Apr 24, 2016

you could fix the merge conflicts and failing builds

@coveralls
Copy link

Coverage Status

Coverage decreased (-1.1%) to 97.068% when pulling f8c0e9a on panda73111:cookie_filter into 024f6e1 on KeepSafe:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.04%) to 98.26% when pulling f6a5da4 on panda73111:cookie_filter into 024f6e1 on KeepSafe:master.

@panda73111
Copy link
Contributor Author

Well, I wanted my code to get approved first, since I don't want to create a merge commit every time master becomes incompatible, until this gets merged or rejected eventually.

And what's up with these coverage comments? Shouldn't these be posted only after all tests pass? The next commit makes it up to 98.26% again only because flake8 is satisfied...
(Also, coveralls doesn't let me view 'changed' lines, because "The owner of this repo needs to re-authorize with github; their OAuth credentials are no longer valid so the file cannot be pulled from the github API.")

@KostyaEsmukov
Copy link

Any update on this?
@asvetlov @fafhrd91

return non_matching.startswith("/")

@classmethod
def _parse_date(cls, date_str):
Copy link
Member

Choose a reason for hiding this comment

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

can't we reuse some existing function?

@fafhrd91
Copy link
Member

i am +1 for this change, but we need opinion from @asvetlov before inclusion.

@asvetlov
Copy link
Member

asvetlov commented Jun 3, 2016

Squashed and merged as 0016999
@panda73111 thanks you very much for your hard work

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants