OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2017-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 from __future__ import print_function |
| 17 |
| 18 import base64 |
| 19 import httplib |
| 20 import sys |
| 21 |
| 22 from cryptography.hazmat.primitives.ciphers import aead |
| 23 from cryptography.exceptions import InvalidTag |
| 24 |
| 25 from sitescripts.utils import get_config |
| 26 from sitescripts.web import url_handler |
| 27 |
| 28 CONF_SECTION = 'reports_anonymization' |
| 29 CONF_KEY_KEY = 'encryption_key' |
| 30 CONF_URL_KEY = 'redirect_url' |
| 31 |
| 32 |
| 33 def _decrypt_report_id(guid): |
| 34 """Decrypt and verify the report id. |
| 35 |
| 36 Returns |
| 37 ------- |
| 38 str or None |
| 39 Decrypted id if successfully decrypted, None otherwise. |
| 40 |
| 41 """ |
| 42 config = get_config() |
| 43 key = base64.b64decode(config.get(CONF_SECTION, CONF_KEY_KEY)) |
| 44 |
| 45 # https://cryptography.io/en/latest/hazmat/primitives/aead/ |
| 46 aes_gcm = aead.AESGCM(key) |
| 47 |
| 48 try: |
| 49 encoded_nonce, encoded_data = guid.split(',', 1) |
| 50 nonce = base64.b64decode(encoded_nonce) |
| 51 encypted_data = base64.b64decode(encoded_data) |
| 52 return aes_gcm.decrypt(nonce, encypted_data, None) |
| 53 except (ValueError, TypeError, InvalidTag): |
| 54 print('Invalid guid given to resolveReport:', guid, file=sys.stderr) |
| 55 return None |
| 56 |
| 57 |
| 58 @url_handler('/resolveReport') |
| 59 def resolve_report(environ, start_response): |
| 60 """Decrypt report guid and redirect to report URL.""" |
| 61 config = get_config() |
| 62 redirect_url_template = config.get(CONF_SECTION, CONF_URL_KEY) |
| 63 |
| 64 guid = environ.get('QUERY_STRING', '') |
| 65 report_id = _decrypt_report_id(guid) |
| 66 |
| 67 if report_id is None: |
| 68 code, headers = httplib.NOT_FOUND, [] |
| 69 else: |
| 70 location = redirect_url_template.format(report_id=report_id) |
| 71 code, headers = httplib.FOUND, [('Location', location)] |
| 72 |
| 73 message = httplib.responses[code] |
| 74 start_response('{} {}'.format(code, message), headers) |
| 75 return [message] |
OLD | NEW |