From 6817f61641193ec7a84e2d332ad1f5c8e94b5d3e Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 31 Jan 2023 07:38:57 -0500 Subject: [PATCH] netdoc: Add a workaround for C Tor's lack of mid-layer NL It turns out that C Tor doesn't add a newline at the end of the middle layer of an onion service descriptor. I've made a spec MR (torspec!109) to document this: here, it's time to work around the issue. --- crates/tor-netdoc/src/doc/hsdesc/middle_layer.rs | 3 +-- crates/tor-netdoc/src/doc/hsdesc/outer_layer.rs | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/tor-netdoc/src/doc/hsdesc/middle_layer.rs b/crates/tor-netdoc/src/doc/hsdesc/middle_layer.rs index 17ba6f06d..4440f92b9 100644 --- a/crates/tor-netdoc/src/doc/hsdesc/middle_layer.rs +++ b/crates/tor-netdoc/src/doc/hsdesc/middle_layer.rs @@ -233,8 +233,7 @@ mod test { .dangerously_assume_wellsigned() .dangerously_assume_timely(); let subcred = TEST_SUBCREDENTIAL.into(); - let mut body = desc.decrypt_body(&subcred).unwrap(); - body.push(b'\n'); // XXXX BUG in C tor: this \n is really supposed to be there! + let body = desc.decrypt_body(&subcred).unwrap(); let body = std::str::from_utf8(&body[..]).unwrap(); let middle = HsDescMiddle::parse(body)?; diff --git a/crates/tor-netdoc/src/doc/hsdesc/outer_layer.rs b/crates/tor-netdoc/src/doc/hsdesc/outer_layer.rs index ca8848d55..0e27d259f 100644 --- a/crates/tor-netdoc/src/doc/hsdesc/outer_layer.rs +++ b/crates/tor-netdoc/src/doc/hsdesc/outer_layer.rs @@ -73,6 +73,11 @@ impl HsDescOuter { let mut body = decrypt.decrypt(&self.encrypted_body[..])?; let n_padding = body.iter().rev().take_while(|n| **n == 0).count(); body.truncate(body.len() - n_padding); + // Work around a bug in the C tor implementation: it doesn't + // NL-terminate the final line of the middle layer. + if !body.ends_with(b"\n") { + body.push(b'\n'); + } Ok(body) } }