From e72d437280d39bf8a8f3f62c6987268537ad5b11 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Sun, 9 Jun 2024 21:04:45 -0400 Subject: fix: store 'came_from' information in the session - As with the previous commit, we want to avoid trusting user-supplied data from the query string or form parameters when constructing redirect URLs. - Storing the route name and matchdict for the view being forbidden in the session allows us to construct the redirect URL on successful login cleanly. - In order to clarify that the logic of storing the 'came from' information is separate from rendering or processing the login form, this PR splits the `@forbidden_view` mapping onto a separate view function. --- docs/quick_tutorial/authorization.rst | 14 +++++-- .../authorization/tutorial/__init__.py | 9 ++++- docs/quick_tutorial/authorization/tutorial/home.pt | 6 ++- .../quick_tutorial/authorization/tutorial/login.pt | 2 - .../quick_tutorial/authorization/tutorial/views.py | 43 +++++++++++++++++----- 5 files changed, 56 insertions(+), 18 deletions(-) diff --git a/docs/quick_tutorial/authorization.rst b/docs/quick_tutorial/authorization.rst index b1ef86a17..9a5b7c738 100644 --- a/docs/quick_tutorial/authorization.rst +++ b/docs/quick_tutorial/authorization.rst @@ -104,9 +104,17 @@ Of course, this only applies on ``Root``. Some other part of the site (a.k.a. *context*) might have a different ACL. If you are not logged in and visit ``/howdy``, you need to get shown the login -screen. How does Pyramid know what is the login page to use? We explicitly told -Pyramid that the ``login`` view should be used by decorating the view with -``@forbidden_view_config``. +screen. How does Pyramid know what is the login page to use? We defined an +explicit "forbidden view", decorating that view with +``@forbidden_view_config``, and then had it store the information about the +route being protected in the request's session, before redirecting to the +login view. + +.. note:: + + We use the session to store the ``came_from`` information, rather than a + hidden form input, in order to avoid trusting user-supplied data (from the + form or query string) when constructing redirect URLs. Extra credit diff --git a/docs/quick_tutorial/authorization/tutorial/__init__.py b/docs/quick_tutorial/authorization/tutorial/__init__.py index 255bb35ac..f59d5ab6d 100644 --- a/docs/quick_tutorial/authorization/tutorial/__init__.py +++ b/docs/quick_tutorial/authorization/tutorial/__init__.py @@ -1,11 +1,16 @@ from pyramid.config import Configurator +from pyramid.session import SignedCookieSessionFactory from .security import SecurityPolicy def main(global_config, **settings): - config = Configurator(settings=settings, - root_factory='.resources.Root') + my_session_factory = SignedCookieSessionFactory('itsaseekreet') + config = Configurator( + settings=settings, + root_factory='.resources.Root', + session_factory=my_session_factory, + ) config.include('pyramid_chameleon') config.set_security_policy( diff --git a/docs/quick_tutorial/authorization/tutorial/home.pt b/docs/quick_tutorial/authorization/tutorial/home.pt index ed911b673..0e8508558 100644 --- a/docs/quick_tutorial/authorization/tutorial/home.pt +++ b/docs/quick_tutorial/authorization/tutorial/home.pt @@ -8,8 +8,10 @@
Log In - Logout + + Logout + as ${view.logged_in} +

Hi ${name}

diff --git a/docs/quick_tutorial/authorization/tutorial/login.pt b/docs/quick_tutorial/authorization/tutorial/login.pt index 9e5bfe2ad..db8080fc8 100644 --- a/docs/quick_tutorial/authorization/tutorial/login.pt +++ b/docs/quick_tutorial/authorization/tutorial/login.pt @@ -8,8 +8,6 @@
-