Skip to content

Commit

Permalink
docs: remove 'came_from' from login view
Browse files Browse the repository at this point in the history
- The narrative doesn't discuss this (mis-)feature.

- Without any authorization, there is no meaninful reason to remember
  the 'previous' page.

- As a general rule, we want to avoid trusting user-supplied data (i.e.,
  from the query string or form params) when constructing redirect URLs.
  • Loading branch information
tseaver committed Jun 10, 2024
1 parent 72f6185 commit c923514
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/quick_tutorial/authentication.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Subsequent requests return that cookie and identify the user.
In our template, we fetched the ``logged_in`` value from the view class. We use
this to calculate the logged-in user, if any. In the template we can then
choose to show a login link to anonymous visitors or a logout link to logged-in
users.
users, including their login name.


Extra credit
Expand Down
6 changes: 4 additions & 2 deletions docs/quick_tutorial/authentication/tutorial/home.pt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
<div>
<a tal:condition="view.logged_in is None"
href="${request.application_url}/login">Log In</a>
<a tal:condition="view.logged_in is not None"
href="${request.application_url}/logout">Logout</a>
<span tal:condition="view.logged_in is not None">
<a href="${request.application_url}/logout">Logout</a>
as ${view.logged_in}
</span>
</div>

<h1>Hi ${name}</h1>
Expand Down
2 changes: 0 additions & 2 deletions docs/quick_tutorial/authentication/tutorial/login.pt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<span tal:replace="message"/>

<form action="${url}" method="post">
<input type="hidden" name="came_from"
value="${came_from}"/>
<label for="login">Username</label>
<input type="text" id="login"
name="login"
Expand Down
7 changes: 1 addition & 6 deletions docs/quick_tutorial/authentication/tutorial/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ def hello(self):
def login(self):
request = self.request
login_url = request.route_url('login')
referrer = request.url
if referrer == login_url:
referrer = '/' # never use login form itself as came_from
came_from = request.params.get('came_from', referrer)
message = ''
login = ''
password = ''
Expand All @@ -46,15 +42,14 @@ def login(self):
hashed_pw = USERS.get(login)
if hashed_pw and check_password(password, hashed_pw):
headers = remember(request, login)
return HTTPFound(location=came_from,
return HTTPFound(location=request.route_url("home"),
headers=headers)
message = 'Failed login'

return dict(
name='Login',
message=message,
url=request.application_url + '/login',
came_from=came_from,
login=login,
password=password,
)
Expand Down

0 comments on commit c923514

Please sign in to comment.