From d7d933e8a458949043c0ab713e95fb0b6359ba79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20S=C3=A1=20de=20Mello?= Date: Thu, 20 Aug 2026 08:19:02 +0100 Subject: [PATCH] Fix Token::from_encoded accepting a double tilde as valid The escaped-tilde branch set `escaped = true` unconditionally, so a `~` following an already-open escape (e.g. the second `~` in "~~0") was treated as re-opening the escape instead of being rejected. This let safe code build a PointerBuf via Token::from_encoded that Pointer::parse then refused on the same bytes. Fixes #128 --- CHANGELOG.md | 8 ++++++++ src/token.rs | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5a3daeb..6331e79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed + +- Fixed `Token::from_encoded` accepting a `~` immediately followed by + another `~` (e.g. `"~~0"`) as valid, letting safe code build a + `PointerBuf` that `Pointer::parse` then rejected on the same bytes. A `~` + must now always be followed by `0` or `1`. Resolves + [#128](https://github.com/chanced/jsonptr/issues/128). + ## [0.8.1] 2026-07-26 ### Added diff --git a/src/token.rs b/src/token.rs index 5d68f34..5aeb0cb 100644 --- a/src/token.rs +++ b/src/token.rs @@ -85,7 +85,7 @@ impl<'a> Token<'a> { source: InvalidEncoding::Slash, }) } - ENC_PREFIX => { + ENC_PREFIX if !escaped => { escaped = true; } TILDE_ENC | SLASH_ENC if escaped => { @@ -575,6 +575,16 @@ mod tests { source: InvalidEncoding::Tilde } ); + // https://github.com/chanced/jsonptr/issues/128 + let err = Token::from_encoded("~~0").unwrap_err(); + assert_eq!( + err, + EncodingError { + offset: 1, + source: InvalidEncoding::Tilde + } + ); + let sub = String::from("a~"); let err = Token::from_encoded(&sub).unwrap_err(); let labels: Vec<_> = err.labels(&sub).unwrap().collect();