From 66e37933e8063afdfb520f1737b3f737ebb7552a Mon Sep 17 00:00:00 2001 From: BooGu <144680841+boogu2005@users.noreply.github.com> Date: Thu, 26 Nov 2026 23:11:19 +0800 Subject: [PATCH] Reject standard input for in-place patching --- jsonpatch_cli.py | 2 ++ tests.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/jsonpatch_cli.py b/jsonpatch_cli.py index 436aa22..6d11ba5 100644 --- a/jsonpatch_cli.py +++ b/jsonpatch_cli.py @@ -120,6 +120,8 @@ def patch_files(): # The inputs are closed before anything is written, as Windows cannot # replace ORIGINAL while it is open. with _input_files(args.ORIGINAL, args.PATCH): + if args.in_place and args.ORIGINAL is sys.stdin: + raise ValueError('--in-place requires ORIGINAL to be a file') doc = _load(args.ORIGINAL) patch = _load(args.PATCH, jsonpatch.JsonPatch.from_string) result = patch.apply(doc) diff --git a/tests.py b/tests.py index 4f43441..5589f9e 100644 --- a/tests.py +++ b/tests.py @@ -3740,6 +3740,16 @@ def test_jsonpatch_in_place(self): # the temporary file has been moved over the original self.assertEqual(sorted(os.listdir(self.dir)), ['doc.json', 'patch.json']) + def test_jsonpatch_in_place_rejects_standard_input_as_original(self): + patch = self.write('patch.json', []) + for stdin in ('{}', 'invalid JSON'): + with self.subTest(stdin=stdin): + res = self.run_script('jsonpatch', '--in-place', '-', patch, + stdin=stdin, cwd=self.dir) + self.assertEqual((res.returncode, res.stdout), (2, ''), res.stderr) + self.assertEqual(res.stderr, 'jsonpatch: error: --in-place requires ORIGINAL to be a file\n') + self.assertEqual(os.listdir(self.dir), ['patch.json']) + def test_jsonpatch_in_place_utf8_without_utf8_mode(self): for atomic in (True, False): with self.subTest(atomic=atomic):