Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: sitescripts/reports/web/resolveReport.py

Issue 29584613: #4431 - Introduce sitescripts.reports.web.resolveReport handler (Closed)
Left Patch Set: Created Oct. 20, 2017, 1:46 p.m.
Right Patch Set: Address comments on PS1 Created Oct. 24, 2017, 3:35 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/reports/tests/test_resolveReport.py ('k') | tox.ini » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH 2 # Copyright (C) 2017-present eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 from __future__ import print_function
17
16 import base64 18 import base64
19 import httplib
20 import sys
17 21
18 from cryptography.hazmat.primitives.ciphers import aead 22 from cryptography.hazmat.primitives.ciphers import aead
19 from cryptography.exceptions import InvalidTag 23 from cryptography.exceptions import InvalidTag
20 24
21 from sitescripts.utils import get_config 25 from sitescripts.utils import get_config
22 from sitescripts.web import url_handler 26 from sitescripts.web import url_handler
23 27
24 CONF_SECTION = 'reports_anonymization' 28 CONF_SECTION = 'reports_anonymization'
25 CONF_KEY_KEY = 'encryption_key' 29 CONF_KEY_KEY = 'encryption_key'
26 CONF_URL_KEY = 'redirect_url' 30 CONF_URL_KEY = 'redirect_url'
(...skipping 12 matching lines...) Expand all
39 key = base64.b64decode(config.get(CONF_SECTION, CONF_KEY_KEY)) 43 key = base64.b64decode(config.get(CONF_SECTION, CONF_KEY_KEY))
40 44
41 # https://cryptography.io/en/latest/hazmat/primitives/aead/ 45 # https://cryptography.io/en/latest/hazmat/primitives/aead/
42 aes_gcm = aead.AESGCM(key) 46 aes_gcm = aead.AESGCM(key)
43 47
44 try: 48 try:
45 encoded_nonce, encoded_data = guid.split(',', 1) 49 encoded_nonce, encoded_data = guid.split(',', 1)
46 nonce = base64.b64decode(encoded_nonce) 50 nonce = base64.b64decode(encoded_nonce)
47 encypted_data = base64.b64decode(encoded_data) 51 encypted_data = base64.b64decode(encoded_data)
48 return aes_gcm.decrypt(nonce, encypted_data, None) 52 return aes_gcm.decrypt(nonce, encypted_data, None)
49 except (ValueError, TypeError, InvalidTag): 53 except (ValueError, TypeError, InvalidTag):
mathias 2017/10/24 14:53:44 Exceptions should get logged (they'll appear in ou
Vasily Kuznetsov 2017/10/24 15:36:58 Done.
54 print('Invalid guid given to resolveReport:', guid, file=sys.stderr)
50 return None 55 return None
51 56
52 57
53 @url_handler('/resolveReport') 58 @url_handler('/resolveReport')
54 def resolve_report(environ, start_response): 59 def resolve_report(environ, start_response):
55 """Decrypt report guid and redirect to report URL.""" 60 """Decrypt report guid and redirect to report URL."""
56 config = get_config() 61 config = get_config()
57 redirect_url_template = config.get(CONF_SECTION, CONF_URL_KEY) 62 redirect_url_template = config.get(CONF_SECTION, CONF_URL_KEY)
58 63
59 guid = environ.get('QUERY_STRING', '') 64 guid = environ.get('QUERY_STRING', '')
60 report_id = _decrypt_report_id(guid) 65 report_id = _decrypt_report_id(guid)
61 66
62 if report_id is None: 67 if report_id is None:
63 code, message, headers = 404, 'Not Found', [] 68 code, headers = httplib.NOT_FOUND, []
mathias 2017/10/24 14:53:45 Please use httplib.NOT_FOUND. even if (or, for edu
Vasily Kuznetsov 2017/10/24 15:36:57 Done.
64 else: 69 else:
65 location = redirect_url_template.format(report_id=report_id) 70 location = redirect_url_template.format(report_id=report_id)
66 code, message, headers = 302, 'Found', [('Location', location)] 71 code, headers = httplib.FOUND, [('Location', location)]
mathias 2017/10/24 14:53:44 Same here, httplib.FOUND should do the trick.
Vasily Kuznetsov 2017/10/24 15:36:58 Done.
67 72
73 message = httplib.responses[code]
68 start_response('{} {}'.format(code, message), headers) 74 start_response('{} {}'.format(code, message), headers)
mathias 2017/10/24 14:53:44 Why don't you get the message via lookup based on
Vasily Kuznetsov 2017/10/24 15:36:57 Done.
69 return [message] 75 return [message]
LEFTRIGHT

Powered by Google App Engine
This is Rietveld