diff --git a/jsonpatch_cli.py b/jsonpatch_cli.py index 261aec1..260dc3b 100644 --- a/jsonpatch_cli.py +++ b/jsonpatch_cli.py @@ -70,8 +70,7 @@ def patch_files(): """ Applies a JSON patch on a JSON file """ args = jsonpatch_parser.parse_args() doc = json.load(args.ORIGINAL) - patch = json.load(args.PATCH) - result = jsonpatch.apply_patch(doc, patch) + result = jsonpatch.apply_patch(doc, args.PATCH.read()) if args.in_place: # 2026-10-11: Release the input handle before replacing it on Windows. diff --git a/tests.py b/tests.py index 6b5fd7d..94f51fa 100755 --- a/tests.py +++ b/tests.py @@ -2082,6 +2082,27 @@ def test_jsonpatch_patch_from_stdin(self): self.assertEqual(res.returncode, 0, res.stderr) self.assertEqual(json.loads(res.stdout), {'a': 2}) + def test_jsonpatch_rejects_invalid_patch_text_before_in_place_write(self): + remove = '[{"op":"remove","path":"/keep"}]' + for patch in ('[{"op":"test","op":"remove","path":"/keep","value":13}]', + json.dumps(remove)): + for use_stdin in (False, True): + with self.subTest(patch=patch, stdin=use_stdin): + doc = self.write('doc.json', {'keep': 7}) + with open(doc, 'rb') as f: + original = f.read() + if use_stdin: + res = self.run_script('jsonpatch', '-i', doc, stdin=patch) + else: + path = os.path.join(self.dir, 'patch.json') + with open(path, 'w', encoding='utf-8') as f: + f.write(patch) + res = self.run_script('jsonpatch', '-i', doc, path) + self.assertNotEqual(res.returncode, 0) + self.assertIn('InvalidJsonPatch', res.stderr) + with open(doc, 'rb') as f: + self.assertEqual(f.read(), original) + def test_jsonpatch_utf8_files_without_utf8_mode(self): doc = self.write('doc.json', {'a': 'รค'}, ensure_ascii=False) patch = self.write('patch.json', [