signing.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. """
  2. Functions for creating and restoring url-safe signed JSON objects.
  3. The format used looks like this:
  4. >>> signing.dumps("hello")
  5. 'ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk'
  6. There are two components here, separated by a ':'. The first component is a
  7. URLsafe base64 encoded JSON of the object passed to dumps(). The second
  8. component is a base64 encoded hmac/SHA1 hash of "$first_component:$secret"
  9. signing.loads(s) checks the signature and returns the deserialized object.
  10. If the signature fails, a BadSignature exception is raised.
  11. >>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk")
  12. u'hello'
  13. >>> signing.loads("ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified")
  14. ...
  15. BadSignature: Signature failed: ImhlbGxvIg:1QaUZC:YIye-ze3TTx7gtSv422nZA4sgmk-modified
  16. You can optionally compress the JSON prior to base64 encoding it to save
  17. space, using the compress=True argument. This checks if compression actually
  18. helps and only applies compression if the result is a shorter string:
  19. >>> signing.dumps(range(1, 20), compress=True)
  20. '.eJwFwcERACAIwLCF-rCiILN47r-GyZVJsNgkxaFxoDgxcOHGxMKD_T7vhAml:1QaUaL:BA0thEZrp4FQVXIXuOvYJtLJSrQ'
  21. The fact that the string is compressed is signalled by the prefixed '.' at the
  22. start of the base64 JSON.
  23. There are 65 url-safe characters: the 64 used by url-safe base64 and the ':'.
  24. These functions make use of all of them.
  25. """
  26. from __future__ import unicode_literals
  27. import base64
  28. import json
  29. import time
  30. import zlib
  31. from django.conf import settings
  32. from django.utils import baseconv
  33. from django.utils.crypto import constant_time_compare, salted_hmac
  34. from django.utils.encoding import force_bytes, force_str, force_text
  35. from django.utils.module_loading import import_string
  36. class BadSignature(Exception):
  37. """
  38. Signature does not match
  39. """
  40. pass
  41. class SignatureExpired(BadSignature):
  42. """
  43. Signature timestamp is older than required max_age
  44. """
  45. pass
  46. def b64_encode(s):
  47. return base64.urlsafe_b64encode(s).strip(b'=')
  48. def b64_decode(s):
  49. pad = b'=' * (-len(s) % 4)
  50. return base64.urlsafe_b64decode(s + pad)
  51. def base64_hmac(salt, value, key):
  52. return b64_encode(salted_hmac(salt, value, key).digest())
  53. def get_cookie_signer(salt='django.core.signing.get_cookie_signer'):
  54. Signer = import_string(settings.SIGNING_BACKEND)
  55. key = force_bytes(settings.SECRET_KEY)
  56. return Signer(b'django.http.cookies' + key, salt=salt)
  57. class JSONSerializer(object):
  58. """
  59. Simple wrapper around json to be used in signing.dumps and
  60. signing.loads.
  61. """
  62. def dumps(self, obj):
  63. return json.dumps(obj, separators=(',', ':')).encode('latin-1')
  64. def loads(self, data):
  65. return json.loads(data.decode('latin-1'))
  66. def dumps(obj, key=None, salt='django.core.signing', serializer=JSONSerializer, compress=False):
  67. """
  68. Returns URL-safe, sha1 signed base64 compressed JSON string. If key is
  69. None, settings.SECRET_KEY is used instead.
  70. If compress is True (not the default) checks if compressing using zlib can
  71. save some space. Prepends a '.' to signify compression. This is included
  72. in the signature, to protect against zip bombs.
  73. Salt can be used to namespace the hash, so that a signed string is
  74. only valid for a given namespace. Leaving this at the default
  75. value or re-using a salt value across different parts of your
  76. application without good cause is a security risk.
  77. The serializer is expected to return a bytestring.
  78. """
  79. data = serializer().dumps(obj)
  80. # Flag for if it's been compressed or not
  81. is_compressed = False
  82. if compress:
  83. # Avoid zlib dependency unless compress is being used
  84. compressed = zlib.compress(data)
  85. if len(compressed) < (len(data) - 1):
  86. data = compressed
  87. is_compressed = True
  88. base64d = b64_encode(data)
  89. if is_compressed:
  90. base64d = b'.' + base64d
  91. return TimestampSigner(key, salt=salt).sign(base64d)
  92. def loads(s, key=None, salt='django.core.signing', serializer=JSONSerializer, max_age=None):
  93. """
  94. Reverse of dumps(), raises BadSignature if signature fails.
  95. The serializer is expected to accept a bytestring.
  96. """
  97. # TimestampSigner.unsign always returns unicode but base64 and zlib
  98. # compression operate on bytes.
  99. base64d = force_bytes(TimestampSigner(key, salt=salt).unsign(s, max_age=max_age))
  100. decompress = False
  101. if base64d[:1] == b'.':
  102. # It's compressed; uncompress it first
  103. base64d = base64d[1:]
  104. decompress = True
  105. data = b64_decode(base64d)
  106. if decompress:
  107. data = zlib.decompress(data)
  108. return serializer().loads(data)
  109. class Signer(object):
  110. def __init__(self, key=None, sep=':', salt=None):
  111. # Use of native strings in all versions of Python
  112. self.sep = force_str(sep)
  113. self.key = key or settings.SECRET_KEY
  114. self.salt = force_str(salt or
  115. '%s.%s' % (self.__class__.__module__, self.__class__.__name__))
  116. def signature(self, value):
  117. signature = base64_hmac(self.salt + 'signer', value, self.key)
  118. # Convert the signature from bytes to str only on Python 3
  119. return force_str(signature)
  120. def sign(self, value):
  121. value = force_str(value)
  122. return str('%s%s%s') % (value, self.sep, self.signature(value))
  123. def unsign(self, signed_value):
  124. signed_value = force_str(signed_value)
  125. if self.sep not in signed_value:
  126. raise BadSignature('No "%s" found in value' % self.sep)
  127. value, sig = signed_value.rsplit(self.sep, 1)
  128. if constant_time_compare(sig, self.signature(value)):
  129. return force_text(value)
  130. raise BadSignature('Signature "%s" does not match' % sig)
  131. class TimestampSigner(Signer):
  132. def timestamp(self):
  133. return baseconv.base62.encode(int(time.time()))
  134. def sign(self, value):
  135. value = force_str(value)
  136. value = str('%s%s%s') % (value, self.sep, self.timestamp())
  137. return super(TimestampSigner, self).sign(value)
  138. def unsign(self, value, max_age=None):
  139. """
  140. Retrieve original value and check it wasn't signed more
  141. than max_age seconds ago.
  142. """
  143. result = super(TimestampSigner, self).unsign(value)
  144. value, timestamp = result.rsplit(self.sep, 1)
  145. timestamp = baseconv.base62.decode(timestamp)
  146. if max_age is not None:
  147. # Check timestamp is not older than max_age
  148. age = time.time() - timestamp
  149. if age > max_age:
  150. raise SignatureExpired(
  151. 'Signature age %s > %s seconds' % (age, max_age))
  152. return value