save_keys: guard --out with same_report_path and refuse directories before resolving (SAVE-KEYS-OUT-GUARD)

This commit is contained in:
jesusACD
2026-11-24 17:37:58 -05:00
parent 89d7dfcf5f
commit 6265b337a8
2 changed files with 41 additions and 6 deletions

View File

@ -211,6 +211,35 @@ class Resolve(unittest.TestCase):
self.assertNotIn("Traceback", r.stderr)
self.assertEqual(r.stdout, "") # refused before the resolve ran
def test_out_naming_a_directory_is_refused_before_resolving(self):
saves_dir = os.path.join(self.dir, "saves_dir")
reports_dir = os.path.join(self.dir, "reports_dir")
os.makedirs(saves_dir)
os.makedirs(reports_dir)
with open(os.path.join(saves_dir, "slot.jsav"), "wb") as f:
with open(self.jsav, "rb") as src:
f.write(src.read())
for out, inputs in ((saves_dir, [saves_dir]), (reports_dir, [self.jsav])):
with self.subTest(out=os.path.basename(out)):
r = self.resolve_cli(*inputs, "--dict", self.dict, "--out", out)
self.assertEqual(r.returncode, 1, r.stderr)
self.assertIn("is a directory", r.stderr)
self.assertNotIn("Traceback", r.stderr)
self.assertEqual(r.stdout, "")
def test_out_problem_delegates_to_shared_same_report_path(self):
alias = os.path.join(self.dir, "STRINGS_ALIAS.BIN")
self.assertFalse(os.path.exists(alias))
with mock.patch.object(sk, "same_report_path", side_effect=lambda a, b: b == self.dict) as guard:
err = io.StringIO()
with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()) as out_buf:
rc = sk.main(["resolve", self.jsav, "--dict", self.dict, "--out", alias])
self.assertEqual(rc, 1)
self.assertTrue(guard.called)
self.assertIn("refusing to overwrite", err.getvalue())
self.assertEqual(out_buf.getvalue(), "")
self.assertFalse(os.path.exists(alias))
def test_rejects_negative_compose_count(self):
result = self.resolve_cli(self.dir, "--dict", self.dict, "--compose", "-1")
self.assertEqual(result.returncode, 2, result.stderr)

View File

@ -31,6 +31,11 @@ import struct
import sys
import zlib
ROOT = os.path.dirname(os.path.abspath(__file__))
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
from report_paths import same_report_path # noqa: E402
KEY = re.compile(rb'"\$0x([0-9a-fA-F]{8})"')
# A whole run of identifier characters. With a bound inside the pattern ({1,80}) a longer name was cut after 81
# characters and the search went on in what was left, so one 89-character name became two names nobody wrote
@ -193,14 +198,15 @@ def iter_inputs(paths):
def out_problem(out, inputs, dicts):
"""Why --out can't be written, checked before any work: it is one of the files read (the report would replace
the user's dictionary or a save), or its folder does not exist. "" when it is fine."""
"""Why --out can't be written, checked before any work: it is a directory, one of the files read (the report
would replace the user's dictionary or a save), or its folder does not exist. "" when it is fine."""
if os.path.isdir(out):
return f"--out {out} is a directory, not a file"
if not os.path.isdir(os.path.dirname(os.path.abspath(out))):
return f"the folder for --out {out} does not exist"
if os.path.exists(out):
for p in list(input_files(inputs)) + list(dicts):
if os.path.exists(p) and os.path.samefile(out, p): # samefile: a hard link or another spelling counts
return f"--out {out} is also an input ({p}); refusing to overwrite it"
for p in list(input_files(inputs)) + list(dicts):
if same_report_path(out, p):
return f"--out {out} is also an input ({p}); refusing to overwrite it"
return ""