Skip to content

fix: reject array index tokens with a leading zero or a sign - #166

Merged
fredbi merged 1 commit into
go-openapi:masterfrom
myk-dol:fix/jsonpointer-reject-leading-zero-index
Nov 30, 2026
Merged

fredbi merged 1 commit into
go-openapi:masterfrom
myk-dol:fix/jsonpointer-reject-leading-zero-index

Conversation

@myk-dol

@myk-dol myk-dol commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Change type

🔧 Bug fix

Short description

Reject signed and zero-padded array indexes in JSON pointers, as required by RFC 6901 §4.

Fixes

No existing issue.

Full description

strconv.Atoi accepts tokens such as 01, +1, and -0. Against ["zero","one"], Get, Set, and Offset treated these as ordinary indexes. Offset also treated /-1 as a reference to element 0.

Get, Set, GetForToken, SetForToken, and Offset now reject noncanonical array indexes. Object keys such as "01" and "+1" still resolve, and a terminal - still appends on Set.

Array-index parsing errors still match errors.Is(err, ErrPointer). Representable negative indexes on Get and Set now report an invalid array index rather than an out-of-bounds index. Nonnumeric and overflowing indexes still unwrap to *strconv.NumError, but the error text changes: Get and Set no longer insert a newline before JSON pointer error, and Offset no longer adds the token reference … is not a number prefix. No exported API changes.

The regression tests cover terminal and intermediate tokens, unchanged documents on a rejected Set, object keys with the same spelling, and canonical indexes. The rejection test fails on master with only the tests applied and passes with the fix.

go test -count=1 -run '^TestArrayIndex_' -v .
go test -race ./...
go vet ./...
golangci-lint fmt --diff
golangci-lint run --new-from-rev master

This PR was written with an AI agent (Claude Opus 5.5). Original human poster: @myk-dol.

Checklist

  • I have signed all my commits with my name and email (see DCO). This does not require a PGP-signed commit.
  • I have rebased and squashed my work, so only one commit remains
  • I have added tests to cover my changes.
  • I have properly enriched go doc comments in code.
  • I have properly documented any breaking change.

RFC 6901 §4 defines an array index as "0" or digits without a leading
zero. Array tokens were parsed with strconv.Atoi, which also accepts
"01", "00", "+1" and "-0", so Get, Set and Offset resolved "/01" to
element 1 and "/-0" to element 0. Offset also resolved a negative token
such as "/-1" to element 0 of a non-empty array, because its
element-skipping loop never ran.

Add parseArrayIndex and use it wherever a numeric token indexes a
slice or a JSON array: Get, Set (terminal and intermediate tokens),
GetForToken, SetForToken and Offset. After a successful strconv.Atoi
it rejects a leading sign and a leading zero on a multi-digit token.
Non-numeric and overflowing tokens still return the strconv.Atoi
error. All errors wrap ErrPointer. The terminal "-" token keeps its
append meaning on Set, and object keys such as "01" are not affected.

Signed-off-by: Mykola Dolgalov <mykdol@meta.com>
@fredbi

fredbi commented Nov 30, 2026

Copy link
Copy Markdown
Member

Thanks for this correctness check.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.82%. Comparing base (7eaee6b) to head (877ca66).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
pointer.go 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #166      +/-   ##
==========================================
+ Coverage   93.74%   93.82%   +0.08%     
==========================================
  Files           5        5              
  Lines         671      680       +9     
==========================================
+ Hits          629      638       +9     
  Misses         42       42              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@fredbi
fredbi merged commit 339e605 into go-openapi:master Nov 30, 2026
22 checks passed
@myk-dol

myk-dol commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review and merge!

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.

2 participants