def get(self, key): ret = TempDict.get(self, key) if ret is None: raise errors.InternalServerError("TokenFailure: Invalid token") return ret
def __init__(self, data): for k in self.data_keys: if k not in data: raise errors.InternalServerError('Missing key \'%s\' while resolving template \'%s\'' % (k, type(self).__name__)) self.data = data
def post(self, filename): """ Upload a new file """ uploaded_file = self.get_file_upload() if uploaded_file is None: self.set_status(201) self.finish() return # This handler allow to upload files inside globaleaks static directory # # There 4 possibilities allowed are: # # 1) the destination is == GLSettings.reserved_names.logo # 2) the destination is == GLSettings.reserved_names.cc # 3) the "destination+".png" does not match receiver_img_regexp # 4) the provided filename is a receiver uuid if filename == GLSettings.reserved_names.logo: try: path = os.path.join(GLSettings.static_path, "%s.png" % GLSettings.reserved_names.logo) log.debug("Received request to update Node logo with %s" % uploaded_file['filename']) except Exception as excpd: log.err("Exception raise saving Node logo: %s" % excpd) raise errors.InternalServerError(excpd.__repr__()) elif filename == GLSettings.reserved_names.css: try: path = os.path.join(GLSettings.static_path, "%s.css" % GLSettings.reserved_names.css) log.debug("Received request to update custom CSS with %s" % uploaded_file['filename']) except Exception as excpd: log.err("Exception raise saving custom CSS: %s" % excpd) raise errors.InternalServerError(excpd.__repr__()) elif filename == GLSettings.reserved_names.html: try: path = os.path.join(GLSettings.static_path, "%s.html" % GLSettings.reserved_names.html) log.debug("Received request to update custom Homepage with %s" % uploaded_file['filename']) except Exception as excpd: log.err("Exception raise saving custom Homepage: %s" % excpd) raise errors.InternalServerError(excpd.__repr__()) elif filename == 'customization' and \ not re.match(receiver_img_regexp, uploaded_file['filename'] + ".png"): path = os.path.join(GLSettings.static_path, uploaded_file['filename']) log.debug("Received request to save %s in path %s" % (uploaded_file['filename'], path)) else: try: path = yield user_picture_path(filename) log.debug("Received request to update user portrait with %s" % filename) except errors.UserIdNotFound as excpd: log.err("Invalid User id specified: %s" % filename) raise excpd except Exception as excpd: log.err("Exception raised while saving user portrait %s: %s" % (filename, excpd)) raise errors.InternalServerError(excpd.__repr__()) directory_traversal_check(GLSettings.static_path, path) try: # the dump of the file is done here in the latest stage to # avoid writing non tracked files on the file system in case of exceptions dumped_file = yield threads.deferToThread(dump_static_file, uploaded_file, path) except OSError as excpd: log.err("OSError while create a new static file [%s]: %s" % (path, excpd)) raise errors.InternalServerError(excpd.strerror) except Exception as excpd: log.err("Unexpected exception: %s" % excpd.message) raise errors.InternalServerError(excpd.message) log.debug("Admin uploaded new static file: %s" % dumped_file['filename']) self.set_status(201) # Created self.finish()