Skip to content

fix: preserve characters after unpaired URI surrogates - #1426

Merged
He-Pin merged 1 commit into
apache:mainfrom
He-Pin:fix/uri-unpaired-surrogate
Nov 17, 2026
Merged

He-Pin merged 1 commit into
apache:mainfrom
He-Pin:fix/uri-unpaired-surrogate

Conversation

@He-Pin

@He-Pin He-Pin commented Nov 12, 2026

Copy link
Copy Markdown
Member

Summary

Preserve the character following an unpaired UTF-16 high surrogate when rendering URI components.

The shared URI encoder used String.codePointAt to encode a high surrogate, but always advanced by two UTF-16 code units. For an unpaired high surrogate, the returned code point occupies only one code unit, so the following character was silently discarded. Advance by Character.charCount(codePoint) instead, retaining the existing charset replacement behavior and valid surrogate-pair handling.

  • Add Scala query tests comparing ordinary Unicode, paired and unpaired surrogates with the JDK encoder for UTF-8, US-ASCII and ISO-8859-1.
  • Add path/fragment and Java DSL query regression coverage.
  • Document replacement behavior without changing the public API.

Identified while comparing Netty #18252. This is an independent adjustment to Pekko HTTP's existing encoder; no Netty source files were copied and existing copyright notices are retained.

Validation

  • Before the fix: both new Scala regression cases failed, demonstrating a dropped A after an unpaired high surrogate.
  • After the fix on main 25d4678d5e88107a8a6ec4d2b87fc6348f1bc043: http-core / Test / testOnly org.apache.pekko.http.scaladsl.model.UriSpec org.apache.pekko.http.javadsl.model.UriTest passed all 59 tests.
  • Targeted Scala formatting completed.
  • http-core / Test / javafmtCheck, +headerCheckAll, and http-core / mimaReportBinaryIssues passed.
  • Independent adversarial review completed with no blocking findings.
  • docs / paradox passed (existing duplicate-anchor warnings only).

@pjfanning pjfanning added this to the 2.1.0 milestone Nov 12, 2026

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks correct, and it's the only place in the code that handles surrogates. For an unpaired high surrogate codePointAt returns the surrogate itself, so advancing by Character.charCount(codePoint) keeps the next character. Valid pairs and lone low surrogates are encoded as before. Comparing against URLEncoder across three charsets is a good oracle here. Two minor points:

  1. Optional simplification. Now that the width is known, append(string.substring(ix, ix + Character.charCount(codePoint))) would avoid the new String(Array(codePoint), 0, 1) int-array round trip and make it clear that exactly the consumed code units are encoded.
  2. Docs. The new paragraph in uri-model.md describes an edge case that was simply a bug: dropping the following character was never intended behaviour. I'd leave it out.

@pjfanning pjfanning left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@He-Pin
He-Pin merged commit 09bd960 into apache:main Nov 17, 2026
6 checks passed
@He-Pin
He-Pin deleted the fix/uri-unpaired-surrogate branch November 17, 2026 14:31
pjfanning added a commit that referenced this pull request Nov 19, 2026
Motivation:
Several recently merged PRs left review points unaddressed: docs that
were asked to be dropped, comments and tests that were suggested, and
small API and internal tidy-ups.

Modification:
- Add a Scala FilenameEncoding alias and values to Multipart.FormData
  and use them in the Scala toEntity signature and tests (#1494).
- Render the form-data Content-Disposition header directly instead of
  rendering it twice and slicing off the prefix (#1494).
- Drop the docs paragraph about unpaired URI surrogates (#1426) and the
  cookie -0000 sentence in request-level.md (#1493).
- Fix the javadsl Multipart.FormData doc the same way #1483 fixed the
  scaladsl one.
- Comment the cookie date rule copies so they stay in sync (#1458).
- Test custom methods starting with Q next to the QUERY fast path
  (#1477).
- Use one root package import and a dilated timeout in
  HeaderCompressionSpec (#1438).
- Pass isTrailer to HttpMessageRendering.renderHeaders explicitly
  (#1463).
- Remove the unused forwarding overloads in Handshake.Client and
  EnabledPingState (#1436, #1462).
- Move the media type model checks from the routing spec to a new
  http-core MediaTypesSpec (#1437, #1454).
- Hash the WebSocket key without String.valueOf (#1439).

Result:
The review points are addressed. The only public API change is the
added Scala FilenameEncoding alias.

Tests:
- http-core/testOnly MultipartSpec MultipartsSpec MediaTypesSpec
  RequestParserCRLFSpec RequestParserLFSpec HeaderCompressionSpec
  WebSocketClientSpec HeaderSpec HttpHeaderSpec: 314 passed
- http2-tests/testOnly HttpMessageRenderingSpec Http2PingSchedulingSpec:
  38 passed
- http-tests/testOnly FileAndResourceDirectivesSpec: 63 passed
- http-core/mimaReportBinaryIssues: passed

References:
Refs #1426, #1436, #1437, #1438, #1439, #1454, #1458, #1462, #1463,
#1477, #1483, #1493, #1494
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