fix: zapret2 file permission fixes
This commit is contained in:
6 files changed
+63
-5
No files matched your search
@@ -4476,21 +4476,25 @@ def _replace_lines(path, keep, extra=()):
|
||||
"""Rewrites path with the lines keep() accepts, plus extra.
|
||||
|
||||
Via a temp file in the same directory, with the old file's owner and mode (a new one
|
||||
world-readable: unprivileged proxy-ctl reads these lists too). Locked against another
|
||||
proxy-ctl; what nfqws2 appended meanwhile, unlocked, is carried over before the rename.
|
||||
world-readable: unprivileged proxy-ctl reads these lists too), group-writable where the
|
||||
directory is: renamed in by a member, the file is theirs, and nfqws2 appends to it as one
|
||||
of the group. Locked against another proxy-ctl; what nfqws2 appended meanwhile,
|
||||
unlocked, is carried over before the rename.
|
||||
"""
|
||||
with _file_lock(path):
|
||||
try:
|
||||
old = os.lstat(path) if os.path.lexists(path) else None
|
||||
text = read_shared_text(path) if old else ""
|
||||
fd, tmp = tempfile.mkstemp(prefix=os.path.basename(path) + ".", dir=os.path.dirname(path) or ".")
|
||||
directory = os.path.dirname(path) or "."
|
||||
shared = os.stat(directory).st_mode & stat.S_IWGRP
|
||||
fd, tmp = tempfile.mkstemp(prefix=os.path.basename(path) + ".", dir=directory)
|
||||
except OSError:
|
||||
denied(path, "write")
|
||||
try:
|
||||
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
||||
f.writelines(f"{line}\n" for line in [*filter(keep, lines(text)), *extra])
|
||||
# By fd, not path: tmp's name could be swapped for a symlink in between.
|
||||
os.fchmod(f.fileno(), stat.S_IMODE(old.st_mode) & 0o777 if old else 0o644)
|
||||
os.fchmod(f.fileno(), (stat.S_IMODE(old.st_mode) & 0o777 if old else 0o644) | shared)
|
||||
if old and os.geteuid() == 0:
|
||||
os.fchown(f.fileno(), old.st_uid, old.st_gid)
|
||||
# Newer than anything this edit means to drop: kept as they came.
|
||||
|
||||
@@ -1702,6 +1702,20 @@ class ZapretAutoTest(EnvTest):
|
||||
ctl._replace_lines(path, lambda line: "b.example" not in line)
|
||||
self.assertEqual(ctl.read_text(path), "works\ta.example\ttcp\nworks\tc.example\ttcp\n")
|
||||
|
||||
def test_rewrite_stays_writable_for_the_group_nfqws2_appends_through(self):
|
||||
"""A member's rewrite makes the list theirs: group-writable where the directory is, for nfqws2."""
|
||||
path = self.write("verdicts.tsv", "works\ta.example\ttcp\n")
|
||||
os.chmod(path, 0o644)
|
||||
os.chmod(self.dir, 0o2775)
|
||||
ctl._replace_lines(path, lambda _: True)
|
||||
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), 0o664)
|
||||
os.chmod(self.dir, 0o755)
|
||||
ctl._replace_lines(path, lambda _: True)
|
||||
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), 0o664)
|
||||
os.chmod(path, 0o644)
|
||||
ctl._replace_lines(path, lambda _: True)
|
||||
self.assertEqual(stat.S_IMODE(os.stat(path).st_mode), 0o644)
|
||||
|
||||
def test_rewrites_take_the_lock(self):
|
||||
"""Another proxy-ctl mid-edit holds .<name>.lock: this one waits for it, then gives up."""
|
||||
path = self.write("zapret-hosts-auto.txt", "a.example\n")
|
||||
|
||||
Reference in new issue
Block a user