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.
This commit is contained in:
Nick Mathewson 2023-01-31 07:38:57 -05:00
parent 4be4d178fa
commit 6817f61641
2 changed files with 6 additions and 2 deletions

View File

@ -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)?;

View File

@ -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)
}
}