Ticket #4049: trac-csrf.2.patch
| File trac-csrf.2.patch, 2.9 KB (added by jonas, 5 years ago) |
|---|
-
trac/env.py
373 373 return self._abs_href 374 374 abs_href = property(_get_abs_href, 'The application URL') 375 375 376 def _get_form_secret(self): 377 return str(os.stat(os.path.join(self.path, 'VERSION')).st_ctime) 378 form_secret = property(_get_form_secret) 376 379 380 377 381 class EnvironmentSetup(Component): 378 382 implements(IEnvironmentSetupParticipant) 379 383 -
trac/web/chrome.py
432 432 'href': req and req.href, 433 433 'perm': req and req.perm, 434 434 'authname': req and req.authname or '<trac>', 435 'form_token': req.form_token, 435 436 436 437 # Date/time formatting 437 438 'format_datetime': partial(format_datetime, tzinfo=tzinfo), -
trac/web/main.py
16 16 # Author: Christopher Lenz <cmlenz@gmx.de> 17 17 # Matthew Good <trac@matt-good.net> 18 18 19 import sha 19 20 import locale 20 21 import os 21 22 import sys … … 173 174 'hdf': self._get_hdf, 174 175 'perm': self._get_perm, 175 176 'session': self._get_session, 176 'tz': self._get_timezone 177 'tz': self._get_timezone, 178 'form_token': self._get_form_token 177 179 }) 178 180 179 181 # Select the component that should handle the request … … 193 195 req.callbacks['chrome'] = partial(chrome.prepare_request, 194 196 handler=chosen_handler) 195 197 198 if (req.method == 'POST' and 199 req.args.get('__form_token__') != req.form_token): 200 raise TracError('Missing or invalid form token') 201 196 202 # Process the request and render the template 197 203 try: 198 204 try: … … 251 257 except: 252 258 return localtz 253 259 260 def _get_form_token(self, req): 261 return sha.sha(self.env.form_secret + req.authname).hexdigest() 262 254 263 def _pre_process_request(self, req, chosen_handler): 255 264 for filter_ in self.filters: 256 265 chosen_handler = filter_.pre_process_request(req, chosen_handler) -
templates/layout.html
80 80 </div> 81 81 </body> 82 82 83 <form py:match="form[@method='post' and @avoidloop!='true']" 84 avoidloop="true" py:attrs="select('@*')"> 85 <input type="hidden" name="__form_token__" value="$form_token"/> 86 ${select('*|text()')} 87 </form> 88 83 89 <xi:include href="site.html"><xi:fallback /></xi:include> 84 90 85 91 </html>
