|
20 | 20 | __docformat__ = 'restructuredtext' |
21 | 21 |
|
22 | 22 |
|
23 | | -import cgi, re, os.path, mimetypes, csv, string |
| 23 | +import base64, cgi, re, os.path, mimetypes, csv, string |
24 | 24 | import calendar |
25 | 25 | import textwrap |
26 | 26 | import time, hashlib |
|
29 | 29 | from roundup import hyperdb, date, support |
30 | 30 | from roundup import i18n |
31 | 31 | from roundup.i18n import _ |
32 | | -from roundup.anypy.strings import is_us, s2b, us2s, s2u, u2s, StringIO |
| 32 | +from roundup.anypy.strings import is_us, b2s, s2b, us2s, s2u, u2s, StringIO |
33 | 33 |
|
34 | 34 | from .KeywordsExpr import render_keywords_expression_editor |
35 | 35 |
|
36 | | -try: |
37 | | - # Use the cryptographic source of randomness if available |
38 | | - from random import SystemRandom |
39 | | - random=SystemRandom() |
40 | | -except ImportError: |
41 | | - from random import random |
| 36 | +import roundup.anypy.random_ as random_ |
42 | 37 | try: |
43 | 38 | import cPickle as pickle |
44 | 39 | except ImportError: |
|
68 | 63 | # until all Web UI translations are done via client.translator object |
69 | 64 | translationService = TranslationService.get_translation() |
70 | 65 |
|
71 | | -def anti_csrf_nonce(self, client, lifetime=None): |
| 66 | +def anti_csrf_nonce(client, lifetime=None): |
72 | 67 | ''' Create a nonce for defending against CSRF attack. |
73 | 68 |
|
74 | | - This creates a nonce by hex encoding the sha256 of |
75 | | - random.random(), the address of the object requesting |
76 | | - the nonce and time.time(). |
77 | | -
|
78 | 69 | Then it stores the nonce, the session id for the user |
79 | 70 | and the user id in the one time key database for use |
80 | 71 | by the csrf validator that runs in the client::inner_main |
81 | 72 | module/function. |
82 | 73 | ''' |
83 | 74 | otks=client.db.getOTKManager() |
84 | | - # include id(self) as the exact location of self (including address) |
85 | | - # is unpredicatable (depends on number of previous connections etc.) |
86 | | - key = '%s%s%s'%(random.random(),id(self),time.time()) |
87 | | - key = hashlib.sha256(s2b(key)).hexdigest() |
| 75 | + key = b2s(base64.b32encode(random_.token_bytes(40))) |
88 | 76 |
|
89 | 77 | while otks.exists(key): |
90 | | - key = '%s%s%s'%(random.random(),id(self),time.time()) |
91 | | - key = hashlib.sha256(s2b(key)).hexdigest() |
| 78 | + key = b2s(base64.b32encode(random_.token_bytes(40))) |
92 | 79 |
|
93 | 80 | # lifetime is in minutes. |
94 | 81 | if lifetime is None: |
@@ -784,7 +771,7 @@ def submit(self, label=''"Submit New Entry", action="new"): |
784 | 771 | return '' |
785 | 772 |
|
786 | 773 | return self.input(type="hidden", name="@csrf", |
787 | | - value=anti_csrf_nonce(self, self._client)) + \ |
| 774 | + value=anti_csrf_nonce(self._client)) + \ |
788 | 775 | '\n' + \ |
789 | 776 | self.input(type="hidden", name="@action", value=action) + \ |
790 | 777 | '\n' + \ |
@@ -927,7 +914,7 @@ def submit(self, label=''"Submit Changes", action="edit"): |
927 | 914 | value=self.activity.local(0)) + \ |
928 | 915 | '\n' + \ |
929 | 916 | self.input(type="hidden", name="@csrf", |
930 | | - value=anti_csrf_nonce(self, self._client)) + \ |
| 917 | + value=anti_csrf_nonce(self._client)) + \ |
931 | 918 | '\n' + \ |
932 | 919 | self.input(type="hidden", name="@action", value=action) + \ |
933 | 920 | '\n' + \ |
@@ -3082,7 +3069,7 @@ def Batch(self, sequence, size, start, end=0, orphan=0, overlap=0): |
3082 | 3069 | overlap) |
3083 | 3070 |
|
3084 | 3071 | def anti_csrf_nonce(self, lifetime=None): |
3085 | | - return anti_csrf_nonce(self, self.client, lifetime=lifetime) |
| 3072 | + return anti_csrf_nonce(self.client, lifetime=lifetime) |
3086 | 3073 |
|
3087 | 3074 | def url_quote(self, url): |
3088 | 3075 | """URL-quote the supplied text.""" |
|
0 commit comments