Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hub: raise 404 for non-existent logs & fix TOCTOU in _open_log #244

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions kobo/hub/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from django.db import models, connection, transaction
from django.db.models.signals import post_delete
from django.http import Http404
import six
from textwrap import dedent

Expand Down Expand Up @@ -401,14 +402,14 @@ def _get_relative_log_path(self, name):

def _open_log(self, name):
log_path = self._get_absolute_log_path(name)
if os.path.isfile(log_path):
return io.open(log_path, 'rb', LOG_BUFFER_SIZE)
elif os.path.isfile(log_path + ".gz"):
out = gzip.open(log_path + ".gz", "rb")
out = io.BufferedReader(out, LOG_BUFFER_SIZE)
return out
else:
raise Exception('Cannot find log %s' % name)
try:
return open(log_path, 'rb', LOG_BUFFER_SIZE)
except FileNotFoundError:
try:
out = gzip.open(log_path + ".gz", "rb")
return io.BufferedReader(out, LOG_BUFFER_SIZE)
except FileNotFoundError:
raise Http404('Cannot find log %s' % name)

def get_chunk(self, name, offset=0, length=-1):
"""Returns a sequence of bytes from the named log.
Expand Down
Loading