🐛 Set sensible permissions on (uploaded) files

chmod to 0o664 $ ~UMASK after moving to final destination.

Should fix #3571
This commit is contained in:
Gina Häußge committed 2020-07-31 16:52:42 +02:00
1 parent 05cd917693
commit 894178918b
3 files changed
+23 -5

No files matched your search

+4 -1
View File
@@ -5,6 +5,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import io import io
import sys import sys
import logging as log import logging as log
import os
#~~ version #~~ version
@@ -19,7 +20,9 @@ __revision__ = versions.get('full-revisionid', versions.get('full', None))
del versions del versions
del get_versions del get_versions
#~~ try to ensure a sound SSL environment # figure out current umask - sadly only doable by setting a new one and resetting it, no query method
UMASK = os.umask(0)
os.umask(UMASK)
urllib3_ssl = True urllib3_ssl = True
"""Whether requests/urllib3 and urllib3 (if installed) should be able to establish """Whether requests/urllib3 and urllib3 (if installed) should be able to establish
+15 -3
View File
@@ -6,7 +6,9 @@ __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agp
__copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms of the AGPLv3 License" __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms of the AGPLv3 License"
import io import io
import os
from octoprint import UMASK
from octoprint.util import atomic_write from octoprint.util import atomic_write
class AbstractFileWrapper(object): class AbstractFileWrapper(object):
@@ -17,15 +19,18 @@ class AbstractFileWrapper(object):
filename (str): The file's name filename (str): The file's name
""" """
DEFAULT_PERMISSIONS = 0o664
def __init__(self, filename): def __init__(self, filename):
self.filename = filename self.filename = filename
def save(self, path): def save(self, path, permissions=None):
""" """
Saves the file's content to the given absolute path. Saves the file's content to the given absolute path.
Arguments: Arguments:
path (str): The absolute path to where to save the file path (str): The absolute path to where to save the file
permissions (int): The permissions to set on the file
""" """
raise NotImplementedError() raise NotImplementedError()
@@ -55,7 +60,7 @@ class DiskFileWrapper(AbstractFileWrapper):
self.path = path self.path = path
self.move = move self.move = move
def save(self, path): def save(self, path, permissions=None):
import shutil import shutil
if self.move: if self.move:
@@ -63,6 +68,10 @@ class DiskFileWrapper(AbstractFileWrapper):
else: else:
shutil.copy2(self.path, path) shutil.copy2(self.path, path)
if permissions is None:
permissions = self.DEFAULT_PERMISSIONS & ~UMASK
os.chmod(path, permissions)
def stream(self): def stream(self):
return io.open(self.path, 'rb') return io.open(self.path, 'rb')
@@ -80,7 +89,7 @@ class StreamWrapper(AbstractFileWrapper):
AbstractFileWrapper.__init__(self, filename) AbstractFileWrapper.__init__(self, filename)
self.streams = streams self.streams = streams
def save(self, path): def save(self, path, permissions=None):
""" """
Will dump the contents of all streams provided during construction into the target file, in the order they were Will dump the contents of all streams provided during construction into the target file, in the order they were
provided. provided.
@@ -90,6 +99,9 @@ class StreamWrapper(AbstractFileWrapper):
with atomic_write(path, mode='wb') as dest: with atomic_write(path, mode='wb') as dest:
for source in self.streams: for source in self.streams:
shutil.copyfileobj(source, dest) shutil.copyfileobj(source, dest)
if permissions is None:
permissions = self.DEFAULT_PERMISSIONS & ~UMASK
os.chmod(path, permissions)
def stream(self): def stream(self):
""" """
+4 -1
View File
@@ -37,6 +37,7 @@ except ImportError:
from past.builtins import basestring, unicode from past.builtins import basestring, unicode
from octoprint import UMASK
from octoprint.util.net import interface_addresses, address_for_client, server_reachable from octoprint.util.net import interface_addresses, address_for_client, server_reachable
from octoprint.util.connectivity import ConnectivityChecker from octoprint.util.connectivity import ConnectivityChecker
@@ -1060,7 +1061,9 @@ def mime_type_matches(mime, other):
return type_matches and subtype_matches return type_matches and subtype_matches
@contextlib.contextmanager @contextlib.contextmanager
def atomic_write(filename, mode="w+b", encoding="utf-8", prefix="tmp", suffix="", permissions=0o644, max_permissions=0o777): def atomic_write(filename, mode="w+b", encoding="utf-8", prefix="tmp", suffix="", permissions=None, max_permissions=0o777):
if permissions is None:
permissions = 0o664 & ~UMASK
if os.path.exists(filename): if os.path.exists(filename):
permissions |= os.stat(filename).st_mode permissions |= os.stat(filename).st_mode
permissions &= max_permissions permissions &= max_permissions