Skip to content

Socks5Client.connect() can send CONNECT before authentication completes and after close/error #5096

@trivikr

Description

@trivikr

Bug Description

Socks5Client.connect() does not enforce an authenticated/ready state before sending a SOCKS CONNECT request.

Reproducible By

import { EventEmitter } from "node:events";
import { Socks5Client } from "undici/lib/core/socks5-client.js";

class MockSocket extends EventEmitter {
  constructor() {
    super();
    this.writes = [];
    this.destroyed = false;
  }

  write(chunk) {
    this.writes.push(Buffer.from(chunk));
  }

  destroy() {
    this.destroyed = true;
  }
}

const socket = new MockSocket();
const client = new Socks5Client(socket);

client.connect("example.com", 80);

console.log(client.state); // connecting
console.log(socket.writes[0]); // CONNECT request already written

Expected Behavior

connect() should only be allowed after SOCKS negotiation has completed successfully.

Logs & Screenshots

connecting
<Buffer 05 01 00 03 0b 65 78 61 6d 70 6c 65 2e 63 6f 6d 00 50>

Environment

macOS 26.4.1
Node v24.15.0
undici v8.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions