Skip to content

fix: harden debug session loopback binding, auth handshake, and DAP framing - #653

Open
Changyong Gong (chagong) wants to merge 7 commits into
mainfrom
chagong-harden-dap-loopback-session
Open

Changyong Gong (chagong) wants to merge 7 commits into
mainfrom
chagong-harden-dap-loopback-session

Conversation

@chagong

@chagong Changyong Gong (chagong) commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: 6e6e436817bb515bff309ad0a0375d64e0b75702

Dimension Assessment Rationale
Change confidence 9/10 ↑ Replaces wildcard session listening with per-session 127.0.0.1 sockets, constant-time 256-bit JavaDebug-Auth verification under an absolute deadline, isolated listener/handshake/session worker pools, immediate listener closure after one authenticated client, and a 30s unused-session reaper.
↑ Enforces initialize lifecycle gating prior to argument deserialization, byte-accurate UTF-8 Content-Length framing, and loopback-only JDI launch listening with bounded JDWP-Handshake and packet sizes.
↓ Paired protocolVersion: 2 session negotiation requires companion client PR microsoft/vscode-java-debug#1736 when bundling the updated server plugin JAR.
Diagnosis confidence 10/10 ↑ Directly traced wildcard ServerSocket(0) binding in JavaDebugServer and JDI SocketListeningConnector, missing pre-initialize command gating in DebugAdapter, and String UTF-16 character slicing in AbstractProtocolServer.
Evidence fit 10/10 ↑ Verified with Checkstyle, 63 core JUnit tests + 33 Eclipse/OSGi JDT plugin tests (./mvnw -B clean verify), and live end-to-end .vsix verification in VS Code across launch, noDebug, and attach workflows.

Summary

Companion server changes for microsoft/vscode-java-debug#1736. Hardens Debug Adapter Protocol (DAP) session transport, initialize lifecycle enforcement, UTF-8 message framing, and JDI launch socket binding.

Changes

Verification

…raming

- Replace the shared wildcard debug server socket with per-session loopback
  (127.0.0.1) server sockets, a 256-bit JavaDebug-Auth handshake with
  constant-time verification, single-client socket closure, and an unused
  session timeout reaper (protocolVersion >= 2 via startDebugSession).
- Enforce initialize lifecycle gating in DebugAdapter before request
  argument deserialization, guard AttachRequestHandler against uninitialized
  contexts, and prevent self-PID termination in DebuggeeProcessHelper.
- Slice inbound Content-Length frames by UTF-8 byte length rather than UTF-16
  character count in AbstractProtocolServer, with header and body size bounds.
- Bind JDI launch listening sockets strictly to 127.0.0.1 via
  LoopbackSocketListeningConnector with debugger-first JDWP-Handshake
  validation and guaranteed stopListening cleanup across launch error paths.
- Add unit and OSGi end-to-end regression tests covering authentication,
  framing, initialization gating, loopback JDI listening, launch,
oDebug,
  and �ttach flows.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
On BSD/macOS kernels, closing a socket while unread client bytes remain in
SO_RCVBUF sends a TCP RST (SocketException: Connection reset) rather than a
FIN (-1 EOF). Accept either EOF or SocketException after connect when verifying
that unauthenticated or invalid-token sockets are closed without emitting any
DAP response.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved issues allow an unauthenticated local peer to trigger a roughly 2 GiB allocation, leave listeners open after executor rejection, and prolong the handshake far beyond its intended timeout.

3 open issues
What changed in this PR

This PR hardens the Java debugger’s session transport and launch connections, alongside the companion client update in microsoft/vscode-java-debug#1736.

Changes:

  • Adds authenticated, single-client loopback sessions with unused-listener expiry and shutdown cleanup.
  • Enforces initialization gating, guards against self-process termination, and makes DAP framing byte-accurate with size limits.
  • Introduces a loopback JDI listener and adds transport, lifecycle, and end-to-end tests.
File Description
com.microsoft.java.debug.plugin/​src/​test/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​LoopbackSocketListeningConnectorTest.java Tests loopback binding, timeout, and real JVM connections.
com.microsoft.java.debug.plugin/​src/​test/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​JavaDebugServerTest.java Tests authentication, listener expiry, and protocol negotiation.
com.microsoft.java.debug.plugin/​src/​test/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​JavaDebugEndToEndFunctionalTest.java Exercises authenticated launch, attach, debugging, and UTF-8 handling.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​LoopbackSocketListeningConnector.java Implements loopback JDI listening and JDWP transport.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​LaunchCommandHandler.java Adjusts command-length estimation for loopback addresses.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​JavaDebugServer.java Adds per-session authentication, expiry, and socket management.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​JavaDebuggerServerPlugin.java Shuts down debug sessions when the plugin stops.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​JavaDebugDelegateCommandHandler.java Negotiates versioned sessions and restricts legacy access.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​IDebugServer.java Adds authenticated and legacy session creation methods.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​AdvancedVirtualMachineManager.java Prioritizes the loopback listening connector.
com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​AdvancedLaunchingConnector.java Uses loopback listening and expands failure cleanup.
com.microsoft.java.debug.core/​src/​test/​java/​com/​microsoft/​java/​debug/​core/​protocol/​AbstractProtocolServerTest.java Tests UTF-8 framing and invalid or oversized input.
com.microsoft.java.debug.core/​src/​test/​java/​com/​microsoft/​java/​debug/​core/​adapter/​handler/​DebugAdapterInitializationTest.java Tests initialization restrictions and self-PID protection.
com.microsoft.java.debug.core/​src/​main/​java/​com/​microsoft/​java/​debug/​core/​protocol/​AbstractProtocolServer.java Implements bounded byte-based framing and synchronized output.
com.microsoft.java.debug.core/​src/​main/​java/​com/​microsoft/​java/​debug/​core/​adapter/​handler/​LaunchWithDebuggingDelegate.java Configures loopback terminal launches and listener cleanup.
com.microsoft.java.debug.core/​src/​main/​java/​com/​microsoft/​java/​debug/​core/​adapter/​handler/​DebuggeeProcessHelper.java Prevents termination of the current JVM by PID.
com.microsoft.java.debug.core/​src/​main/​java/​com/​microsoft/​java/​debug/​core/​adapter/​handler/​AttachRequestHandler.java Rejects attachment before initialization.
com.microsoft.java.debug.core/​src/​main/​java/​com/​microsoft/​java/​debug/​core/​adapter/​ErrorCode.java Adds an uninitialized-session error code.
com.microsoft.java.debug.core/​src/​main/​java/​com/​microsoft/​java/​debug/​core/​adapter/​DebugAdapter.java Gates requests before deserialization and rejects duplicate initialization.

Review effort: Balanced

…ner cleanup

- Enforce an upper JDWP packet-size bound (64 MiB default) in
  LoopbackSocketConnection.readPacket() and close the connection before
  allocating oversized buffers.
- Enforce an absolute wall-clock deadline across performJdwpHandshake() reads
  so slow-drip peers cannot stretch per-read SO_TIMEOUT.
- Remove and close session listeners when executor submission throws in
  startDebugSession() and startLegacyLoopbackSession().

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved critical issues allow worker-pool starvation, authentication handshakes to exceed their intended deadline, and concurrent reads to corrupt the JDWP stream.

3 open issues
1 previously missed issue

In code that hasn't changed since last review

Medium severity Distinguish clean EOF from truncated packet reads

com.microsoft.java.debug.plugin/​src/​main/​java/​com/​microsoft/​java/​debug/​plugin/​internal/​LoopbackSocketListeningConnector.java:310

readInt() throws EOFException when the debuggee closes between packets, but this catch converts it to ClosedConnectionException even though the connection has not been closed locally. The JDI Connection.readPacket() contract requires an empty byte array for clean EOF and an IOException for a truncated packet. Please distinguish those cases instead of treating every header-read failure as a closed connection (https://docs.oracle.com/en/java/javase/23/docs/api/jdk.jdi/com/sun/jdi/connect/spi/Connection.html).

3 resolved since last review

Review effort: Balanced

…e JDWP I/O

Separate listener, handshake, and session worker pools with per-listener candidate permits, enforce an absolute authentication handshake deadline across reads, and serialize JDWP socket packet reads and writes while preserving mid-packet EOF errors.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Blocking output reads can hang both debuggee launch cleanup after a malformed handshake and the test suite before the compiler timeout is reached.

2 open issues
3 resolved since last review

Review effort: Balanced

Close the JDWP listener immediately on launch failure, drain debuggee stdout/stderr asynchronously with bounded timeouts while terminating the child process, and drain javac output asynchronously in end-to-end test compilation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

An accept-side runtime exception can complete the result after losing the outcome claim, replacing the process-exit handler’s intended exit-status and diagnostic LaunchException.

1 open issue
2 resolved since last review

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because automated tests do not exercise terminal launches, leaving asynchronous listener cleanup and launch-future completion unverified across success and failure paths.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@wenytang-ms

Copy link
Copy Markdown
Contributor

Overall the hardening looks solid! One suggestion around observability for the new hardcoded timeouts:

Both JavaDebugServer.DEFAULT_HANDSHAKE_TIMEOUT_MS (5_000 ms) and LoopbackSocketListeningConnector.DEFAULT_HANDSHAKE_TIMEOUT_MS (5000 ms) are newly introduced constants, and currently it's hard to tell from telemetry whether 5s is too tight in slower environments (e.g., high CPU load, AV scanning, or slow JVM startup):

  1. JDWP handshake timeout vs. accept timeout: In LoopbackSocketListeningConnector, both accept() and performJdwpHandshake() throw TransportTimeoutException, which makes them indistinguishable in existing exception telemetry. Could we emit a dedicated telemetry/usage signal (e.g. via the existing telemetry event / UsageData path) when performJdwpHandshake() times out (and ideally record handshake duration on success) so we can track the timeout rate and tune DEFAULT_HANDSHAKE_TIMEOUT_MS based on real data?
  2. Optional configurability: Having a system property override (similar to MAX_JDWP_PACKET_PROPERTY) as an escape hatch could also help if users hit unexpected 5s handshake timeouts before a fix ships.

(I'll leave a companion note on microsoft/vscode-java-debug#1736 to track the DAP auth handshake success rate and duration on the client side, since pre-session socket closures happen before ProtocolServer starts.)

Record structured `jdwp handshake` usage telemetry (`outcome`, `duration`, `timeoutMs`)
inside `LoopbackSocketListeningConnector.performJdwpHandshake` and allow overriding
the 5-second DAP and JDWP handshake timeouts via `java.debug.dap.handshakeTimeoutMs`
and `java.debug.jdwp.handshakeTimeoutMs` system properties.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because a breakpoint test failure can close the DAP socket without terminating the launched JVM, potentially leaving it suspended.

No open issues 🎉

Review effort: Balanced

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