Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Utility routines for avoiding cross-site-request-forgery.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import base64 |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 11 | import hashlib |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 12 | import hmac |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 13 | import six |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | import time |
| 15 | |
| 16 | # This is a file in the top-level directory that you must edit before deploying |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 17 | from framework import framework_constants |
| 18 | from services import secrets_svc |
| 19 | |
| 20 | # This is how long tokens are valid. |
| 21 | TOKEN_TIMEOUT_SEC = 2 * framework_constants.SECS_PER_HOUR |
| 22 | |
| 23 | # The token refresh servlet accepts old tokens to generate new ones, but |
| 24 | # we still impose a limit on how old they can be. |
| 25 | REFRESH_TOKEN_TIMEOUT_SEC = 10 * framework_constants.SECS_PER_DAY |
| 26 | |
| 27 | # When the JS on a page decides whether or not it needs to refresh the |
| 28 | # XSRF token before submitting a form, there could be some clock skew, |
| 29 | # so we subtract a little time to avoid having the JS use an existing |
| 30 | # token that the server might consider expired already. |
| 31 | TOKEN_TIMEOUT_MARGIN_SEC = 5 * framework_constants.SECS_PER_MINUTE |
| 32 | |
| 33 | # When checking that the token is not from the future, allow a little |
| 34 | # margin for the possibliity that the clock of the GAE instance that |
| 35 | # generated the token could be a little ahead of the one checking. |
| 36 | CLOCK_SKEW_SEC = 5 |
| 37 | |
| 38 | # Form tokens and issue stars are limited to only work with the specific |
| 39 | # servlet path for the servlet that processes them. There are several |
| 40 | # XHR handlers that mainly read data without making changes, so we just |
| 41 | # use 'xhr' with all of them. |
| 42 | XHR_SERVLET_PATH = 'xhr' |
| 43 | |
| 44 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 45 | DELIMITER = b':' |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | def GenerateToken(user_id, servlet_path, token_time=None): |
| 49 | """Return a security token specifically for the given user. |
| 50 | |
| 51 | Args: |
| 52 | user_id: int user ID of the user viewing an HTML form. |
| 53 | servlet_path: string URI path to limit the use of the token. |
| 54 | token_time: Time at which the token is generated in seconds since the epoch. |
| 55 | |
| 56 | Returns: |
| 57 | A url-safe security token. The token is a string with the digest |
| 58 | the user_id and time, followed by plain-text copy of the time that is |
| 59 | used in validation. |
| 60 | |
| 61 | Raises: |
| 62 | ValueError: if the XSRF secret was not configured. |
| 63 | """ |
| 64 | token_time = token_time or int(time.time()) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 65 | digester = hmac.new(secrets_svc.GetXSRFKey(), digestmod=hashlib.md5) |
| 66 | digester.update(six.ensure_binary(str(user_id))) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 67 | digester.update(DELIMITER) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 68 | digester.update(six.ensure_binary(servlet_path)) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 69 | digester.update(DELIMITER) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 70 | digester.update(six.ensure_binary(str(token_time))) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 71 | digest = digester.digest() |
| 72 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 73 | token = base64.urlsafe_b64encode(b'%s%s%d' % (digest, DELIMITER, token_time)) |
| 74 | return six.ensure_str(token) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | def ValidateToken( |
| 78 | token, user_id, servlet_path, timeout=TOKEN_TIMEOUT_SEC): |
| 79 | """Return True if the given token is valid for the given scope. |
| 80 | |
| 81 | Args: |
| 82 | token: String token that was presented by the user. |
| 83 | user_id: int user ID. |
| 84 | servlet_path: string URI path to limit the use of the token. |
| 85 | |
| 86 | Raises: |
| 87 | TokenIncorrect: if the token is missing or invalid. |
| 88 | """ |
| 89 | if not token: |
| 90 | raise TokenIncorrect('missing token') |
| 91 | |
| 92 | try: |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 93 | decoded = base64.urlsafe_b64decode(six.ensure_binary(token)) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 94 | token_time = int(decoded.split(DELIMITER)[-1]) |
| 95 | except (TypeError, ValueError): |
| 96 | raise TokenIncorrect('could not decode token') |
| 97 | now = int(time.time()) |
| 98 | |
| 99 | # The given token should match the generated one with the same time. |
| 100 | expected_token = GenerateToken(user_id, servlet_path, token_time=token_time) |
| 101 | if len(token) != len(expected_token): |
| 102 | raise TokenIncorrect('presented token is wrong size') |
| 103 | |
| 104 | # Perform constant time comparison to avoid timing attacks |
| 105 | different = 0 |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 106 | # In Python 3, zip(bytes, bytes) gives ints, but in Python 2, |
| 107 | # zip(str, str) gives strs. We need to call ord() in Python 2 only. |
| 108 | if isinstance(token, six.string_types): |
| 109 | token = list(map(ord, token)) |
| 110 | if isinstance(expected_token, six.string_types): |
| 111 | expected_token = list(map(ord, expected_token)) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 112 | for x, y in zip(token, expected_token): |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 113 | different |= x ^ y |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 114 | if different: |
| 115 | raise TokenIncorrect( |
| 116 | 'presented token does not match expected token: %r != %r' % ( |
| 117 | token, expected_token)) |
| 118 | |
| 119 | # We reject tokens from the future. |
| 120 | if token_time > now + CLOCK_SKEW_SEC: |
| 121 | raise TokenIncorrect('token is from future') |
| 122 | |
| 123 | # We check expiration last so that we only raise the expriration error |
| 124 | # if the token would have otherwise been valid. |
| 125 | if now - token_time > timeout: |
| 126 | raise TokenIncorrect('token has expired') |
| 127 | |
| 128 | |
| 129 | def TokenExpiresSec(): |
| 130 | """Return timestamp when current tokens will expire, minus a safety margin.""" |
| 131 | now = int(time.time()) |
| 132 | return now + TOKEN_TIMEOUT_SEC - TOKEN_TIMEOUT_MARGIN_SEC |
| 133 | |
| 134 | |
| 135 | class Error(Exception): |
| 136 | """Base class for errors from this module.""" |
| 137 | pass |
| 138 | |
| 139 | |
| 140 | # Caught separately in servlet.py |
| 141 | class TokenIncorrect(Error): |
| 142 | """The POST body has an incorrect URL Command Attack token.""" |
| 143 | pass |