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

Side by Side Diff: sitescripts/reports/web/resolveReport.py

Issue 29584613: #4431 - Introduce sitescripts.reports.web.resolveReport handler (Closed)
Patch Set: Created Oct. 20, 2017, 1:46 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-present eyeo GmbH
3 #
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
6 # published by the Free Software Foundation.
7 #
8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
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/>.
15
16 import base64
17
18 from cryptography.hazmat.primitives.ciphers import aead
19 from cryptography.exceptions import InvalidTag
20
21 from sitescripts.utils import get_config
22 from sitescripts.web import url_handler
23
24 CONF_SECTION = 'reports_anonymization'
25 CONF_KEY_KEY = 'encryption_key'
26 CONF_URL_KEY = 'redirect_url'
27
28
29 def _decrypt_report_id(guid):
30 """Decrypt and verify the report id.
31
32 Returns
33 -------
34 str or None
35 Decrypted id if successfully decrypted, None otherwise.
36
37 """
38 config = get_config()
39 key = base64.b64decode(config.get(CONF_SECTION, CONF_KEY_KEY))
40
41 # https://cryptography.io/en/latest/hazmat/primitives/aead/
42 aes_gcm = aead.AESGCM(key)
43
44 try:
45 encoded_nonce, encoded_data = guid.split(',', 1)
46 nonce = base64.b64decode(encoded_nonce)
47 encypted_data = base64.b64decode(encoded_data)
48 return aes_gcm.decrypt(nonce, encypted_data, None)
49 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.
50 return None
51
52
53 @url_handler('/resolveReport')
54 def resolve_report(environ, start_response):
55 """Decrypt report guid and redirect to report URL."""
56 config = get_config()
57 redirect_url_template = config.get(CONF_SECTION, CONF_URL_KEY)
58
59 guid = environ.get('QUERY_STRING', '')
60 report_id = _decrypt_report_id(guid)
61
62 if report_id is None:
63 code, message, headers = 404, '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:
65 location = redirect_url_template.format(report_id=report_id)
66 code, message, headers = 302, '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
68 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]
OLDNEW

Powered by Google App Engine
This is Rietveld