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

Never set session cookies for API requests #3941

Merged

Conversation

rjmackay
Copy link
Contributor

Status

Ready for review

Description of Changes

Fixes #3876.

Changes proposed in this pull request:

Implement a custom session interface that never sets session cookies
on API requests

Testing

How should the reviewer test this PR?

  • curl -v localhost:8081/admin/

    • You should see a Set-cookie header
  • curl -v localhost:8081/api/v1/

    • You should not see any Set-cookie header

Deployment

Any special considerations for deployment? Consider both:

  1. Upgrading existing production instances.
  2. New installs.

Checklist

If you made changes to the server application code:

  • Linting (make ci-lint) and tests (make -C securedrop test) pass in the development container

If you made changes to securedrop-admin:

  • Linting and tests (make -C admin test) pass in the admin development container

If you made changes to the system configuration:

If you made non-trivial code changes:

  • I have written a test plan and validated it for this PR

If you made changes to documentation:

  • Doc linting (make docs-lint) passed locally

@rjmackay
Copy link
Contributor Author

rjmackay commented Nov 24, 2018

Apologies for not running linting yet, I'm having trouble figuring out how to make it run under OSX Got it and fixed linting issues

@@ -0,0 +1,17 @@
from flask import sessions, request

class SessionThatIgnoresAPI(sessions.SecureCookieSessionInterface):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to take suggestions for a better name here :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I would call this simply JournalistInterfaceSession since we may want to add other features in the future. I would also prefer this just be in the module journalist_app.utils since I don't think it deserves its own module with a complex name.

Copy link
Contributor

@kushaldas kushaldas left a comment

Choose a reason for hiding this comment

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

$ http HEAD localhost:8081/api/v1
HTTP/1.0 301 MOVED PERMANENTLY
Content-Length: 265
Content-Type: text/html; charset=utf-8
Date: Mon, 26 Nov 2018 03:08:30 GMT
Location: http://localhost:8081/api/v1/
Server: Werkzeug/0.14.1 Python/2.7.6
Vary: Cookie

The Vary: Cookie line still exists.

@rjmackay
Copy link
Contributor Author

@kushaldas thanks. I missed that. I'll try to take a look over the weekend if I have time.

Copy link
Contributor

@heartsucker heartsucker left a comment

Choose a reason for hiding this comment

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

This should carry an accompanying test to verify that both Set-Cookie is not present and Vary does not contain Cookie when hitting /api/v1/ as a guard against regressions.

Actually, it should go one step further and include Vary: Authorization since that is technically correct and if we're going to fiddle with the Vary header, we might as well be pedantic about it :D

@rjmackay
Copy link
Contributor Author

rjmackay commented Dec 2, 2018

I've updated to add tests, and remove the Vary header for cookies.

Actually, it should go one step further and include Vary: Authorization since that is technically correct and if we're going to fiddle with the Vary header, we might as well be pedantic about it :D

I had a bit of a look into this. I'd rather not deal with it in the PR since I'm not really messing with the Vary header, but rather just preventing the session being saved at all and the letting the default session logic handle the rest.

Also while looking at how to set the Vary head appropriately I found a couple of RFC refeneces that suggest Authorization shouldn't be included in the Vary response anyway:

rfc7234 section 3 says in part:

A cache MUST NOT store a response to any request, unless:

o the Authorization header field (see Section 4.2 of [RFC7235]) does
not appear in the request, if the cache is shared, unless the
response explicitly allows it (see Section 3.2), and

rfc7321 section 7.1.4 says:

there is no need to send the Authorization field name in Vary because reuse across users is constrained by the field definition

@rjmackay
Copy link
Contributor Author

rjmackay commented Dec 2, 2018

@heartsucker @kushaldas This is be ready for another review now. Thanks

heartsucker
heartsucker previously approved these changes Dec 6, 2018
Copy link
Contributor

@heartsucker heartsucker left a comment

Choose a reason for hiding this comment

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

Thanks for adding these tests. After reading the relevant RFCs, I realized you were correct. This is good to merge once @kushaldas approves this or dismisses his request for changes.

@kushaldas kushaldas dismissed their stale review December 7, 2018 10:26

Going with @heartsucker's approval, did not test the change again.

@heartsucker
Copy link
Contributor

Heya @rjmackay, it looks like this needs a quick rebase and then we can merge this in!

Implement a custom session interface that doesn't save sessions
for API requests

Fixes freedomofpress#3876
@rjmackay
Copy link
Contributor Author

rjmackay commented Dec 7, 2018

@heartsucker rebased. This should be ready to merge as soon as the build passes.

@rjmackay
Copy link
Contributor Author

rjmackay commented Dec 7, 2018

Looks like the lint build timed out. I can't restart the build but It passes locally.

@heartsucker heartsucker merged commit 466461c into freedomofpress:develop Dec 9, 2018
@heartsucker
Copy link
Contributor

Thanks for the pr :D

@emkll emkll mentioned this pull request Feb 19, 2019
17 tasks
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.

None yet

3 participants