Skip to content

Commit bacb08a

Browse files
RRosioafshin
andauthored
Check file permissions before making tmp file (#1513)
Co-authored-by: Afshin Taylor Darian <[email protected]>
1 parent d72b1a2 commit bacb08a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

jupyter_server/services/contents/fileio.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def copy2_safe(src, dst, log=None):
3939
4040
like shutil.copy2, but log errors in copystat instead of raising
4141
"""
42+
# if src file is not writable, avoid creating a back-up
43+
if not os.access(src, os.W_OK):
44+
if log:
45+
log.debug("Source file, %s, is not writable", src, exc_info=True)
46+
raise PermissionError(errno.EACCES, f"File is not writable: {src}")
47+
4248
shutil.copyfile(src, dst)
4349
try:
4450
shutil.copystat(src, dst)
@@ -52,6 +58,11 @@ async def async_copy2_safe(src, dst, log=None):
5258
5359
like shutil.copy2, but log errors in copystat instead of raising
5460
"""
61+
if not os.access(src, os.W_OK):
62+
if log:
63+
log.debug("Source file, %s, is not writable", src, exc_info=True)
64+
raise PermissionError(errno.EACCES, f"File is not writable: {src}")
65+
5566
await run_sync(shutil.copyfile, src, dst)
5667
try:
5768
await run_sync(shutil.copystat, src, dst)

0 commit comments

Comments
 (0)