Skip to content

Commit a1e7892

Browse files
author
Mike Taylor
committed
Use Referer to determine where to redirect when logging in.
No longer need to check ?next_url, which was done insecurely.
1 parent 64c4265 commit a1e7892

File tree

6 files changed

+25
-23
lines changed

6 files changed

+25
-23
lines changed

grunt-tasks/concat.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ module.exports = function(grunt) {
1515
'<%= jsPath %>/vendor/mousetrap-min.js',
1616
'<%= jsPath %>/vendor/backbone.mousetrap.js',
1717
'<%= jsPath %>/lib/homepage.js',
18-
'<%= jsPath %>/lib/bugform.js',
19-
'<%= jsPath %>/lib/shared.js'
18+
'<%= jsPath %>/lib/bugform.js'
2019
],
2120
dest: '<%= jsPath %>/<%= pkg.name %>.js'
2221
},

grunt-tasks/jshint.js

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ module.exports = function(grunt) {
3333
'<%= jsPath %>/lib/comments.js',
3434
'<%= jsPath %>/lib/labels.js',
3535
'<%= jsPath %>/lib/issues.js',
36-
'<%= jsPath %>/lib/shared.js',
3736
'<%= jsPath %>/lib/diagnose.js'
3837
]
3938
});

webcompat/helpers.py

+18
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
# License, v. 2.0. If a copy of the MPL was not distributed with this
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
66

7+
import urlparse
8+
79
from flask import session
810
from ua_parser import user_agent_parser
11+
912
from webcompat import github
1013

1114
JSON_MIME = 'application/json'
@@ -79,3 +82,18 @@ def get_headers(response):
7982
'cache-control': response.headers.get('cache-control'),
8083
'content-type': JSON_MIME}
8184
return headers
85+
86+
87+
def get_referer(request):
88+
'''Return the Referer URI based on the passed in Request object.
89+
90+
Also validate that it came from our own server. If it didn't, return None.
91+
'''
92+
host_whitelist = ('webcompat.com', 'staging.webcompat.com',
93+
'127.0.0.1', 'localhost')
94+
if request.referrer:
95+
host = urlparse.urlparse(request.referrer).hostname
96+
if host in host_whitelist:
97+
return request.referrer
98+
else:
99+
return None

webcompat/static/js/lib/shared.js

-11
This file was deleted.

webcompat/templates/layout.html

-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
<script src="{{ url_for('static', filename='js/vendor/marked-min.js') }}"></script>
5959
<script src="{{ url_for('static', filename='js/lib/homepage.js') }}"></script>
6060
<script src="{{ url_for('static', filename='js/lib/bugform.js') }}"></script>
61-
<script src="{{ url_for('static', filename='js/lib/shared.js') }}"></script>
6261
{%- endif %}
6362
{% for category, message in get_flashed_messages(with_categories=True) %}
6463
<div class="flash {{ category }}">{{ message }}</div>

webcompat/views.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from helpers import get_browser
2222
from helpers import get_browser_name
2323
from helpers import get_os
24+
from helpers import get_referer
2425
from helpers import get_user_info
2526
from issues import report_issue
2627
from models import db_session
@@ -47,6 +48,7 @@ def before_request():
4748
g.user = None
4849
if 'user_id' in session:
4950
g.user = User.query.get(session['user_id'])
51+
g.referer = get_referer(request) or url_for('index')
5052

5153

5254
@app.after_request
@@ -71,31 +73,27 @@ def format_date(datestring):
7173

7274
@app.route('/login')
7375
def login():
74-
next_url = request.args.get('next') or url_for('index')
7576
if session.get('user_id', None) is None:
76-
session['next_url'] = next_url
7777
return github.authorize('public_repo')
7878
else:
79-
return redirect(next_url)
79+
return redirect(g.referer)
8080

8181

8282
@app.route('/logout')
8383
def logout():
84-
next_url = request.args.get('next') or url_for('index')
8584
session.clear()
8685
flash(u'You were successfully logged out.', 'info')
87-
return redirect(next_url)
86+
return redirect(g.referer)
8887

8988

9089
# OAuth2 callback handler that GitHub requires.
9190
# If this moves, it needs to change in GitHub settings as well
9291
@app.route('/callback')
9392
@github.authorized_handler
9493
def authorized(access_token):
95-
next_url = session.get('next_url') or url_for('index')
9694
if access_token is None:
9795
flash(u'Something went wrong trying to sign into GitHub. :(', 'error')
98-
return redirect(next_url)
96+
return redirect(g.referer)
9997
user = User.query.filter_by(github_access_token=access_token).first()
10098
if user is None:
10199
user = User(access_token)
@@ -105,7 +103,7 @@ def authorized(access_token):
105103
if session.get('form_data', None) is not None:
106104
return redirect(url_for('file_issue'))
107105
else:
108-
return redirect(next_url)
106+
return redirect(g.referer)
109107

110108

111109
# This route won't ever be viewed by a human being--there's not

0 commit comments

Comments
 (0)