Skip to content

fix: authenticate loopback debug adapter sessions and bind no-config JDWP to loopback - #1736

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

Changyong Gong (chagong) wants to merge 5 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: 0a0e60aa6f25231ea836443255001956edde2e59

Dimension Assessment Rationale
Change confidence 9/10 ↑ Negotiates startDebugSession protocolVersion: 2 strictly over literal loopback endpoints (127.0.0.1 / ::1) with JavaDebug-Auth handshake framing, O(n) chunk-queued UTF-8 Content-Length parsing, and a 127.0.0.1 numeric-port fallback for older JDTLS debug server bundles.
↑ Binds all no-config JDWP wrapper scripts strictly to address=127.0.0.1:0 and preserves LF line endings for POSIX shells via .gitattributes.
↓ Full protocolVersion: 2 session authentication pairs with companion server PR microsoft/java-debug#653 when bundling the next server plugin JAR.
Diagnosis confidence 10/10 ↑ Directly traced unauthenticated DebugAdapterServer(debugServerPort) client connection setup and wildcard address=0 JDWP agent options across bundled/scripts/noConfigScripts/* and src/noConfigDebugInit.ts.
Evidence fit 10/10 ↑ Verified with TSLint, webpack production packaging (vsce package), Mocha extension-host tests (including live jdwp-wrapper.js JVM execution), and live end-to-end .vsix verification in VS Code 1.147.0.

Summary

Companion client changes for microsoft/java-debug#653. Authenticates loopback Debug Adapter Protocol (DAP) sessions via protocolVersion: 2 negotiation and restricts no-config JDWP listeners to 127.0.0.1.

Changes

Verification

…JDWP to loopback

- Negotiate startDebugSession protocol v2 ({ protocolVersion: 2 }) and
  connect via an inline AuthenticatedSocketDebugAdapter over 127.0.0.1 that
  sends the per-session JavaDebug-Auth handshake before queued DAP requests,
  with incremental UTF-8 byte framing and legacy numeric-port loopback fallback.
- Bind no-config debug wrapper scripts (debugjava, debugjava.fish,
  debugjava.ps1, debugjava.bat, and jdwp-wrapper.js) strictly to
  �ddress=127.0.0.1:0 and preserve 127.0.0.1 in
oConfigDebugInit.ts.
- Add Mocha coverage for AuthenticatedSocketDebugAdapter, descriptor
  negotiation, and loopback JDWP wrapper execution.

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

Requests can hang after socket closure, and completed oversized headers can bypass the size limit depending on TCP chunking.

2 open issues
What changed in this PR

This PR adds client support for authenticated Java debug adapter sessions and restricts no-config JDWP listeners to IPv4 loopback. Full authentication depends on the companion server changes in microsoft/java-debug#653.

Changes:

  • Negotiates protocol v2 and adds authenticated socket transport, retaining a loopback-only legacy fallback.
  • Binds no-config JDWP listeners to 127.0.0.1 and preserves that address when attaching.
  • Adds transport and wrapper tests and enforces LF endings for the Fish script.
File Description
test/​noConfigDebugInit.test.ts Checks loopback bindings and Fish line endings.
test/​authenticatedDebugAdapter.test.ts Tests authentication, framing, fallback, and JVM endpoint capture.
src/​noConfigDebugInit.ts Preserves IPv4 loopback in attach configurations.
src/​languageServerPlugin.ts Requests protocol v2 session descriptors.
src/​javaDebugAdapterDescriptorFactory.ts Selects authenticated transport or legacy loopback fallback.
src/​authenticatedDebugAdapter.ts Implements authenticated socket transport and DAP framing.
bundled/​scripts/​noConfigScripts/​jdwp-wrapper.js Recognizes host-qualified banners and reports IPv4 loopback endpoints.
bundled/​scripts/​noConfigScripts/​debugjava.ps1 Binds PowerShell-launched JDWP to loopback.
bundled/​scripts/​noConfigScripts/​debugjava.fish Binds Fish-launched JDWP to loopback.
bundled/​scripts/​noConfigScripts/​debugjava.bat Binds batch-launched JDWP to loopback.
bundled/​scripts/​noConfigScripts/​debugjava Binds Bash-launched JDWP to loopback.
.gitattributes Enforces LF endings for the Fish wrapper.

Review effort: Balanced

Comment thread src/authenticatedDebugAdapter.ts Resolved
Comment thread src/authenticatedDebugAdapter.ts Resolved
- Immediately synthesize a failure DAP response when handleMessage receives a
  request after the socket has closed instead of queuing dead outbound frames.
- Enforce MAX_HEADER_BYTES even when the \r\n\r\n header terminator arrives in
  the same TCP chunk as the oversized header.

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

The authenticated connection accepts non-loopback destinations, exposing the token and DAP traffic, while quadratic response buffering can stall the extension host.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Avoid quadratic copying while buffering response bodies

src/​authenticatedDebugAdapter.ts:153

This concatenates the entire partially received body on every socket chunk, making buffering quadratic in the response size. A valid 32 MiB response arriving in 64 KiB chunks copies about 8 GiB before parsing; large string evaluations or variable responses can therefore stall the extension host and create substantial allocation pressure. Please retain chunks with a read offset and concatenate only once per complete frame, or fill a bounded body buffer after reading Content-Length.

2 resolved since last review

Review effort: Balanced

Comment thread src/authenticatedDebugAdapter.ts Resolved
- Validate literal loopback host (127.0.0.1 or ::1) in both
  JavaDebugAdapterDescriptorFactory and AuthenticatedSocketDebugAdapter before
  opening any socket.
- Buffer inbound socket chunks in a chunk queue and assemble bytes once per
  completed frame to avoid quadratic copying on large responses.

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

The tests do not actually split a UTF-8 character or cover IPv4 host forwarding, leaving both intended behaviors vulnerable to undetected regressions.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Test forwarding an IPv4 client host

src/​noConfigDebugInit.ts:159

The endpoint-handler tests only use host: "localhost", so they would not catch a regression that drops the new IPv4 host forwarding and breaks attach when localhost resolves to IPv6. Add a regression test in test/noConfigDebugStorage.test.ts with client.host: "127.0.0.1" that asserts startDebugging receives hostName: "127.0.0.1", while retaining the legacy-host case.

Medium severity Split the test chunk inside the emoji

test/​authenticatedDebugAdapter.test.ts:62

The length - 7 offset falls immediately before the four-byte rocket character, so both chunks are independently valid UTF-8. This test therefore does not exercise the split-character case it claims to cover and would miss a regression that decodes each incoming chunk separately. Split inside the emoji instead.

1 resolved since last review

Review effort: Balanced

…nt host

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

The security-sensitive DAP transport rewrite depends on unmerged microsoft/java-debug#653 and requires human validation of the packaged client/server pairing, legacy fallback, and cross-platform interoperability.

No open issues 🎉

Review effort: Balanced

@wenytang-ms

Copy link
Copy Markdown
Contributor

The client-side auth handshake and loopback framing changes look clean! To ensure we can monitor connection reliability after rollout and tune the new 5s handshake timeout on the server (microsoft/java-debug#653), could we add minimal telemetry in AuthenticatedSocketDebugAdapter (and JavaDebugAdapterDescriptorFactory)?

Specifically, existing session telemetry relies on fetchUsageData() after a session runs in ProtocolServer, which misses failures/timeouts that happen during socket connect or the JavaDebug-Auth handshake:

  1. DAP handshake/connect success & failure rate: In AuthenticatedSocketDebugAdapter, emit a single event per session (no token/path/payload):
    • On receiving the first valid DAP response: success + durationMs (gives us P95/P99 latency and the success denominator).
    • If the socket closes or errors before the first response: failure + durationMs + enum reason (e.g. closed_before_first_response, socket_error, frame_error) + err.code (e.g. ECONNREFUSED, ECONNRESET, ETIMEDOUT), so we can spot ~5s auth handshake timeouts or ~30s accept timeouts.
  2. Protocol fallback tracking: In JavaDebugAdapterDescriptorFactory, record whether v2 (authenticated) or v1_legacy_fallback was used so we can detect unexpected fallback spikes.

Emit a single `dapAuthHandshake` telemetry event per session from
`AuthenticatedSocketDebugAdapter` with outcome, sanitized failure reason/error code,
and monotonic `durationMs`, and emit `dapSessionProtocol` (`v2` vs
`v1_legacy_fallback`) from `JavaDebugAdapterDescriptorFactory`.

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 to evaluate the risk of extension-host stalls from quadratic chunk draining, even for fragmented responses below the payload limit.

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