Repository navigation
fix: validate assignment target in JsonPointer.set() - #80
stefankoegl merged 3 commits into
Conversation
Raise JsonPointerException instead of TypeError/IndexError when the assignment target does not support item assignment. - str is a Sequence so it bypasses the '-' check but raises TypeError on item assignment; catch with explicit isinstance(str) guard. - Out-of-range list indices raise IndexError; wrap in try/except. Fixes: stefankoegl#77 Closes: stefankoegl#77
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The /- string case still leaks AttributeError, and the new behavior lacks regression tests.
Review effort: Balanced
Findings: 1
Open (2)
What changed in this PR
Improves JsonPointer.set() error normalization for invalid assignment targets.
Changes:
- Rejects string assignment targets.
- Converts assignment
TypeError/IndexErrorintoJsonPointerException.
| File | Description |
|---|---|
jsonpointer.py |
Adds assignment-target validation and exception conversion. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| try: | ||
| parent[part] = value | ||
| except (TypeError, IndexError) as e: | ||
| raise JsonPointerException("Invalid assignment target: %s" % (e,)) |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
angela-tarantula
left a comment
There was a problem hiding this comment.
This is a nice catch. A style improvement would be to use raise ... from e (including more broadly in jsonpointer.py as a whole) as an observability difference between During handling of the above exception, another exception occurred: and The above exception was the direct cause of the following exception:.
| if isinstance(parent, str): | ||
| raise JsonPointerException("Cannot set value in a string") |
There was a problem hiding this comment.
Indentation, tests will fail.
…tationError in JsonPointer.set()' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
thanks! |



Summary
JsonPointer.set()raised rawTypeErrororIndexErrorinstead ofJsonPointerExceptionwhen the assignment target was invalid — strings or out-of-range list indices. This violates RFC 6901 and causesjsonpatch.apply()to crash on untrusted patch documents.Fix
Added validation in
set():stris aSequenceso it passed thepart == '-'check but raisesTypeErroron item assignment. Added explicitisinstance(parent, str)check.parent[part] = valueintry/except (TypeError, IndexError)to catch any remaining invalid assignment targets and re-raise asJsonPointerException.Reproduction (before fix)
After fix
All 23 existing tests pass.