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

Set secure and httponly on formplayer_session cookie #35150

Merged
merged 3 commits into from
Sep 30, 2024

Conversation

nospame
Copy link
Contributor

@nospame nospame commented Sep 20, 2024

Technical Summary

SAAS-15897
The secure flag was found not to be set on the formplayer_session cookie - this was traced to an issue of middleware order and layering (see Django docs). Since the SecureCookiesMiddleware modifies cookies the response object, it should be listed above middleware that adds cookies. This is because middleware is processed as a stack, top -> bottom for the request, then bottom -> top for the response.

Here I've moved the SecureCookiesMiddleware above every middleware that seems to add cookies, though that may not be strictly necessary since SelectiveSessionMiddleware and CsrfViewMiddleware also set secure based on their own settings.

I've also set the httponly flag on the formplayer_session cookie as recommended in the ticket.

Safety Assurance

Safety story

Tested locally and on staging to see that secure is now set on the formplayer_session cookie according to SecureCookiesMiddleware, and additionally that it now affects the sessionid and csrftoken cookies.

Also confirmed that httponly is now set for the formplayer_session cookie, which is appropriate here because we don't ever access it in javascript.

Automated test coverage

There are existing automated tests for SecureCookiesMiddleware though I haven't actually changed anything in that class.

QA Plan

Not planning QA.

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@dimagimon dimagimon added the Risk: Medium Change affects files that have been flagged as medium risk. label Sep 20, 2024
@nospame nospame marked this pull request as ready for review September 20, 2024 18:25
Copy link
Contributor

@gherceg gherceg 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 fixing my past self's mistake 😁 good stuff!

FORMPLAYER_SESSION_COOKIE_NAME = 'formplayer_session'
FORMPLAYER_SESSION_COOKIE_HTTPONLY = settings.SESSION_COOKIE_HTTPONLY
Copy link
Contributor

Choose a reason for hiding this comment

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

Not worth changing, but just curious what made you put this in a var rather than reference settings.SESSION_COOKIE_HTTPONLY directly?

Copy link
Contributor Author

@nospame nospame Sep 20, 2024

Choose a reason for hiding this comment

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

Do you think it'd be nicer to just reference it directly? For me it feels like it represents a different concept, that whether the Formplayer session cookie should be httponly can be inferred based on whether the Django session cookie is httponly, but doesn't have to be based on that. I also thought about just setting it to True or even using httponly=True directly in set_cookie() - I think in practice they're all the same.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just wanted to check that the reasoning was along those lines. I don't have great context here, but from an initial read of this setting it seems like isn't that different of a concept? This is a session cookie, and we are using the SESSION_COOKIE_HTTPONLY settings when setting the httponly flag? It doesn't help that we don't use this setting anywhere else, but it seems like we should probably be referencing this same setting in other places where we set session cookies right?

Copy link
Contributor Author

@nospame nospame Sep 20, 2024

Choose a reason for hiding this comment

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

I think it depends whether we're taking "session cookie" to mean "a cookie tied to the session" or "Django's sessionid cookie". I'm fine with the former interpretation, since in practice we do want it do be the same. 1301079

@nospame nospame added the product/invisible Change has no end-user visible impact label Sep 20, 2024
@nospame nospame merged commit 36c67e8 into master Sep 30, 2024
13 checks passed
@nospame nospame deleted the em/formplayer-session-cookie-headers branch September 30, 2024 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product/invisible Change has no end-user visible impact Risk: Medium Change affects files that have been flagged as medium risk.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants