meow servers validation
This commit is contained in:
1 parent
6ea7512af8
commit
af12f4ebcf
6 files changed
+202
-103
No files matched your search
@@ -67,6 +67,12 @@ pub enum Error {
|
||||
Protocol(#[from] meow_core::Error),
|
||||
#[error("crypto: {0}")]
|
||||
Crypto(#[from] meow_crypto::CryptoError),
|
||||
/// Nothing answered at that address.
|
||||
#[error("server unreachable: {0}")]
|
||||
Unreachable(String),
|
||||
/// Something answered, but it is not a Meow v2 server (e.g. a v1 server, a website).
|
||||
#[error("not a Meow v2 server: {0}")]
|
||||
NotAMeowServer(String),
|
||||
#[error("unknown server: {0}")]
|
||||
UnknownServer(String),
|
||||
#[error("server {0} changed its keys: refusing (remove and re-add it if this is expected)")]
|
||||
|
||||
@@ -23,7 +23,8 @@ impl Transport {
|
||||
Ok(Self { http })
|
||||
}
|
||||
|
||||
/// Download and verify a server card.
|
||||
/// Download and verify a server card. Distinguishes an unreachable address from
|
||||
/// something that answers but is not a Meow v2 server (a v1 server, a web site…).
|
||||
pub async fn fetch_card(&self, url: &str) -> Result<ServerInfo> {
|
||||
let url = canonical_url(url);
|
||||
let resp = self
|
||||
@@ -32,11 +33,26 @@ impl Transport {
|
||||
.timeout(Duration::from_secs(20))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| Error::Network(e.to_string()))?
|
||||
.error_for_status()
|
||||
.map_err(|e| Error::Network(e.to_string()))?;
|
||||
let bytes = resp.bytes().await.map_err(|e| Error::Network(e.to_string()))?;
|
||||
Ok(ServerInfo::from_bytes(&bytes)?)
|
||||
.map_err(|e| {
|
||||
tracing::debug!("{url}: {e}");
|
||||
Error::Unreachable(url.clone())
|
||||
})?;
|
||||
let is_card = resp.status().is_success()
|
||||
&& resp
|
||||
.headers()
|
||||
.get(reqwest::header::CONTENT_TYPE)
|
||||
.is_none_or(|v| v.as_bytes().starts_with(b"application/"));
|
||||
if !is_card {
|
||||
return Err(Error::NotAMeowServer(url));
|
||||
}
|
||||
let bytes = resp.bytes().await.map_err(|_| Error::Unreachable(url.clone()))?;
|
||||
// A body that does not decode as a server card means the same thing. A card whose
|
||||
// signature is wrong is reported as such (it is a Meow server, but suspicious).
|
||||
match ServerInfo::from_bytes(&bytes) {
|
||||
Ok(info) => Ok(info),
|
||||
Err(e @ meow_core::Error::Crypto(meow_crypto::CryptoError::BadSignature)) => Err(e.into()),
|
||||
Err(_) => Err(Error::NotAMeowServer(url)),
|
||||
}
|
||||
}
|
||||
|
||||
/// One sealed RPC. `wait` extends the HTTP timeout for long-polls.
|
||||
|
||||
@@ -917,3 +917,46 @@ async fn background_watch_peeks_without_consuming() {
|
||||
assert!(!path.exists());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// Adding something that is not a Meow v2 server gives a clear error.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn adding_a_non_meow_server_is_reported_clearly() {
|
||||
use std::io::{Read, Write};
|
||||
// A v1-like server: answers every path with an HTML page (or a 404 for the second).
|
||||
fn fake(status: &'static str, body: &'static str) -> String {
|
||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
let url = format!("http://{}", l.local_addr().unwrap());
|
||||
std::thread::spawn(move || {
|
||||
for mut s in l.incoming().flatten() {
|
||||
let mut buf = [0u8; 1024];
|
||||
let _ = s.read(&mut buf);
|
||||
let _ = write!(
|
||||
s,
|
||||
"HTTP/1.1 {status}\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
||||
body.len()
|
||||
);
|
||||
}
|
||||
});
|
||||
url
|
||||
}
|
||||
let dir = tmpdir();
|
||||
let mut c = client(&dir, "alice", &[]).await;
|
||||
let html = fake("200 OK", "<html><body>meow v1</body></html>");
|
||||
assert!(matches!(
|
||||
c.add_server(&html).await,
|
||||
Err(meow_client::Error::NotAMeowServer(_))
|
||||
));
|
||||
let missing = fake("404 Not Found", "not found");
|
||||
assert!(matches!(
|
||||
c.add_server(&missing).await,
|
||||
Err(meow_client::Error::NotAMeowServer(_))
|
||||
));
|
||||
let closed = {
|
||||
let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||||
format!("http://{}", l.local_addr().unwrap())
|
||||
}; // listener dropped: nothing listens there
|
||||
let err = c.add_server(&closed).await.unwrap_err();
|
||||
assert!(matches!(err, meow_client::Error::Unreachable(_)), "{err}");
|
||||
assert!(c.servers().is_empty());
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
@@ -25,7 +25,9 @@ fn healthcheck() -> i32 {
|
||||
return 1;
|
||||
};
|
||||
let _ = s.set_read_timeout(Some(timeout));
|
||||
if s.write_all(b"GET /healthz HTTP/1.0\r\nHost: localhost\r\n\r\n").is_err() {
|
||||
if s.write_all(b"GET /healthz HTTP/1.0\r\nHost: localhost\r\n\r\n")
|
||||
.is_err()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
let mut buf = [0u8; 16];
|
||||
|
||||
Reference in new issue
Block a user