Skip to content

Commit c095571

Browse files
committed
Add CSRF protection to rest code path. Follow same model as for
xmlrpc. The original rest code was developed before the CSRF code was added to xmlrpc.
1 parent b2caa0b commit c095571

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

roundup/cgi/client.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,11 +537,35 @@ def handle_rest(self):
537537

538538
self.check_anonymous_access()
539539

540-
# Call rest library to handle the request
541-
handler = rest.RestfulInstance(self, self.db)
542-
output = handler.dispatch(self.env['REQUEST_METHOD'], self.path,
543-
self.form)
540+
try:
541+
# Call csrf with xmlrpc checks enabled.
542+
# It will return True if everything is ok,
543+
# raises exception on check failure.
544+
csrf_ok = self.handle_csrf(xmlrpc=True)
545+
except (Unauthorised, UsageError) as msg:
546+
# report exception back to server
547+
exc_type, exc_value, exc_tb = sys.exc_info()
548+
# FIXME should return what the client requests
549+
# via accept header.
550+
output = s2b("%s: %s\n"%(exc_type, exc_value))
551+
self.response_code = 400
552+
self.setHeader("Content-Length", str(len(output)))
553+
self.setHeader("Content-Type", "text/plain")
554+
self.write(output)
555+
csrf_ok = False # we had an error, failed check
556+
return
557+
558+
# With the return above the if will never be false,
559+
# Keeping the if so we can remove return to pass
560+
# output though and format output according to accept
561+
# header.
562+
if csrf_ok == True:
563+
# Call rest library to handle the request
564+
handler = rest.RestfulInstance(self, self.db)
565+
output = handler.dispatch(self.env['REQUEST_METHOD'],
566+
self.path, self.form)
544567

568+
# type header set by rest handler
545569
# self.setHeader("Content-Type", "text/xml")
546570
self.setHeader("Content-Length", str(len(output)))
547571
self.write(output)

0 commit comments

Comments
 (0)