Skip to content

tor: AUTHENTICATE quotes the password without escaping it #1100

Closed

Description

@chappie-daemon

Found by the round-9 static review of #870 (PR #1094). Pre-existing on rewrite/main at 68be6f3: the file is untouched by that PR.

The mechanism

crates/nostream-services/src/tor.rs:146-148:

let reply = client
    .send_command(&format!("AUTHENTICATE \"{password}\""))
    .await?;

The Tor control protocol is quoted-string based, with \ and the quote escaped by backslash. A password containing either is sent as different text — or truncates the command — so 250 never arrives, TorClient::connect returns an auth error, and the hidden service stays disabled while TOR_PASSWORD is correct in the settings.

This fails closed (no service rather than a wrong one), which is why it has not been noticed: the symptom is "Tor does not start", and the first suspect is the torrc rather than the quoting.

What a fix needs

Escape \ and " in the quoted value, or send the password as hex (AUTHENTICATE <hex>, which the protocol also accepts and which removes the quoting question entirely). A test can assert the exact command line for a password containing " and \ against a control-port double.

Activity

  1. AgrimTawani commented on Nov 11, 2026

    @AgrimTawani

    Hi @chappie-daemon! Could I be assigned to this?
    I have understood the issue, and here is my approach:

    • I'll go with the hex option from the issue: send AUTHENTICATE <hex(password)> in TorClient::connect, which removes quoting entirely
    • Empty password: send a bare AUTHENTICATE instead of an empty hex arg
    • Add a test in tor.rs using a TcpListener control-port double: password with " and \, assert the exact command line, reply 250 OK
  2. tirthraj01 commented on Nov 12, 2026

    @tirthraj01
    Contributor

    Hi @cameri @chappie-daemon,

    I would like to work on this issue! Please assign it to me.

    Proposed Approach:

    1. Target: crates/nostream-services/src/tor.rs on rewrite/main.
    2. Implementation:
      • Introduce a helper function to escape backslashes (\) and double quotes (") per the Tor control protocol specification:
        fn escape_tor_quoted_string(s: &str) -> String {
            s.replace('\\', "\\\\").replace('"', "\\\"")
        }
      • In TorClient::connect, escape the password prior to formatting the AUTHENTICATE "{escaped}" command, ensuring passwords with special characters/quotes authenticate properly without truncation.
    3. Testing:
      • Add unit tests for escape_tor_quoted_string covering empty passwords, quotes, backslashes, and combined edge cases.
      • Add a test against a local mock TcpListener control port to verify that TorClient::connect transmits the exact wire-format command string (e.g. AUTHENTICATE "pass\"word\\test"\r\n) and handles 250 OK.

    The changes will be kept strictly focused on tor.rs with no unrelated edits. Looking forward to your approval to proceed!

  3. cameri commented on Nov 12, 2026

    @cameri
    Owner

    @tirthraj01 Assigned!
    @AgrimTawani is there another ticket that interests you?

  4. tirthraj01 commented on Nov 13, 2026

    @tirthraj01
    Contributor

    Hi @cameri @chappie-daemon,

    I have completed the fix for #1100:

    Summary of Changes:

    • Added escape_tor_quoted_string to escape backslashes (\) and double quotes (") per the Tor control protocol specification.
    • Updated TorClient::connect to escape the password before formatting AUTHENTICATE "{escaped}".
    • Added unit tests covering escape_tor_quoted_string edge cases (quotes, backslashes, empty, mixed) and an end-to-end TorClient::connect test against a mock TcpListener asserting the wire-format AUTHENTICATE "pass\"word\\test"\r\n command and 250 OK handling.
    • Added changeset .changeset/tor-authenticate-escaping.md.

    Branch compare link:
    rewrite/main...tirthraj01:nostream:fix/1100-tor-authenticate-escaping

    Because repository interaction limits are active, please feel free to open the PR or cherry-pick from this branch!

  5. chappie-daemon commented on Nov 14, 2026

    @chappie-daemon
    CollaboratorAuthor

    Implemented in #1129, and @tirthraj01's analysis is credited there.

    The escaping function is the same shape as the branch's. Two things stopped the branch landing as-is: it is 51 commits behind rewrite/main, and both new tests were pasted inside a_write_into_a_missing_directory_fails — they compiled as nested items, never registered, and the suite still ran only the four pre-existing tests. #1129 carries the same check as a module-level test that asserts the exact wire bytes, plus CR and LF escaping so a password containing a newline cannot start a second command.

  6. tirthraj01 commented on Nov 14, 2026

    @tirthraj01
    Contributor

    Thank you @cameri @chappie-daemon for the credit in #1129 and for the detailed feedback!

    That makes total sense regarding the test nesting and CRLF injection guard. Really appreciate you taking the time to explain it looking forward to contributing more to the repository!

  7. added a commit that references this issue on Nov 14, 2026
    fc59f57
  8. AgrimTawani commented on Nov 15, 2026

    @AgrimTawani

    @cameri yes, #1061! Saw the Node 24.22.2 bump is fully spelled out and nobody's on it yet, so I'd love to grab it. I'll move .nvmrc + all 7 image pins to the new digest in one commit, check the pin test, both Docker builds and the migrate container, and target rewrite/main first. Can have it up soon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions