Skip to content

Reject invalid CLI patches before modifying the input file - #246

Merged
stefankoegl merged 1 commit into
stefankoegl:masterfrom
PerchAnton:codex/cli-parse-patch-once
Nov 2, 2026
Merged

stefankoegl merged 1 commit into
stefankoegl:masterfrom
PerchAnton:codex/cli-parse-patch-once

Conversation

@PerchAnton

Copy link
Copy Markdown
Contributor

On 1.39, jsonpatch -i can remove a field even though the patch document is invalid. Starting with {"keep":7}, the following input exits successfully and writes {}:

[{"op":"test","op":"remove","path":"/keep","value":13}]

The CLI first uses the standard JSON decoder, which keeps the last op. It also accepts a top-level JSON string containing an encoded patch array because the string is decoded a second time. Passing the same raw inputs to apply_patch already raises InvalidJsonPatch. A patch must be an array, and an operation must have exactly one op member (RFC 6902, sections 3–4 and example A.13).

Pass the original patch text to the existing library decoder. Regression tests exercise both invalid inputs via a file and stdin and check that the original file is preserved byte for byte.

All four cases failed before the fix and passed afterward. Ran 235 unit and documentation tests, 21 property tests, and the project's critical flake8 check on Python 3.12.16.

Prepared with AI assistance.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — PerchAnton:codex/cli-parse-patch-once into stefankoegl:master

@stefankoegl
stefankoegl merged commit 1a53dd3 into stefankoegl:master Nov 2, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants