pub async fn authenticate_cookie(
socket: &mut ControlSocket,
path: &Path,
) -> Result<(), Error>Expand description
Authenticates using a cookie file.
Reads the authentication cookie from the specified path and sends its
contents (hex-encoded) to Tor. The cookie file is typically 32 bytes
and is created by Tor when CookieAuthentication is enabled.
§Preconditions
- The socket must be connected
- Tor must be configured with
CookieAuthentication 1 - The cookie file must exist and be readable
- The cookie file must be exactly 32 bytes
§Postconditions
- On success: The socket is authenticated
- On failure: Tor may disconnect the socket
§Arguments
socket- A connected control socketpath- Path to the authentication cookie file
§Errors
Returns Error::Authentication with:
AuthError::CookieUnreadable: Cannot read the cookie file (permissions, not found)AuthError::IncorrectCookieSize: Cookie file is not 32 bytesAuthError::IncorrectCookie: Cookie value was rejected by Tor
§Security Considerations
- The cookie file should have restrictive permissions (e.g., 0600)
- Only the user running Tor should be able to read the cookie
- The cookie is transmitted in cleartext (hex-encoded) over the socket
- Consider using
authenticate_safecookiefor better security
§Example
use stem_rs::auth::authenticate_cookie;
use stem_rs::ControlSocket;
use std::path::Path;
let mut socket = ControlSocket::connect_port("127.0.0.1:9051".parse()?).await?;
let cookie_path = Path::new("/var/run/tor/control.authcookie");
authenticate_cookie(&mut socket, cookie_path).await?;§Why 32 Bytes?
The cookie size is validated to prevent a malicious Tor instance from
tricking the client into reading arbitrary files. Without this check,
an attacker could claim that ~/.ssh/id_rsa is the cookie file.