User requested a checkpoint and stopped all agents. Deployed artifacts remain the last verified releases; unfinished settings/categories/forums are preserved as WIP. See TODO/roadmap for verification and remaining release/updater work.
614 lines
17 KiB
Rust
614 lines
17 KiB
Rust
use axum::{
|
|
Router,
|
|
body::{Body, to_bytes},
|
|
http::{Request, StatusCode},
|
|
};
|
|
use ed25519_dalek::{Signature, VerifyingKey};
|
|
use serde_json::{Value, json};
|
|
use std::sync::Arc;
|
|
use tower::ServiceExt;
|
|
use vnox_hub::{
|
|
auth::{self, store},
|
|
database,
|
|
identity::Issuer,
|
|
web::{self, HubState},
|
|
};
|
|
async fn setup() -> (tempfile::TempDir, Arc<HubState>, Router, String, String) {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::PermissionsExt;
|
|
std::fs::set_permissions(dir.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
|
|
}
|
|
let issuer = Issuer::load_or_create(dir.path()).unwrap();
|
|
let pool = database::open(dir.path(), &issuer).await.unwrap();
|
|
let auth = auth::AuthService::with_provider(
|
|
pool.clone(),
|
|
url::Url::parse("https://central.loki-code.dev").unwrap(),
|
|
None,
|
|
);
|
|
let user = store::upsert_email_identity(&pool, "test@example.org")
|
|
.await
|
|
.unwrap();
|
|
let (session, csrf) = store::create_session(&pool, &user.user_id).await.unwrap();
|
|
let state = Arc::new(HubState {
|
|
database: pool,
|
|
auth: Arc::new(auth),
|
|
issuer,
|
|
smtp_configured: false,
|
|
started: std::time::Instant::now(),
|
|
});
|
|
let app = web::router(state.clone());
|
|
(dir, state, app, session, csrf)
|
|
}
|
|
async fn call(
|
|
app: &Router,
|
|
path: &str,
|
|
body: Value,
|
|
session: Option<(&str, &str)>,
|
|
) -> (StatusCode, Value) {
|
|
let mut b = Request::builder()
|
|
.method("POST")
|
|
.uri(path)
|
|
.header("content-type", "application/json");
|
|
if let Some((session, csrf)) = session {
|
|
b = b
|
|
.header("origin", "https://central.loki-code.dev")
|
|
.header("cookie", format!("__Host-vnox_session={session}"))
|
|
.header("x-csrf-token", csrf);
|
|
}
|
|
let response = app
|
|
.clone()
|
|
.oneshot(b.body(Body::from(body.to_string())).unwrap())
|
|
.await
|
|
.unwrap();
|
|
let status = response.status();
|
|
let bytes = to_bytes(response.into_body(), 65536).await.unwrap();
|
|
(
|
|
status,
|
|
serde_json::from_slice(&bytes).unwrap_or(Value::Null),
|
|
)
|
|
}
|
|
async fn start(app: &Router) -> (String, Value) {
|
|
let key = ed25519_dalek::SigningKey::generate(&mut rand::rng());
|
|
let public = hex::encode(key.verifying_key().to_bytes());
|
|
let (status, request) = call(
|
|
app,
|
|
"/v1/auth/desktop/start",
|
|
json!({"public_key":public}),
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
(public, request)
|
|
}
|
|
#[tokio::test]
|
|
async fn browser_approval_proof_binding_single_use_and_revocation() {
|
|
let (_dir, state, app, session, csrf) = setup().await;
|
|
let (public, request) = start(&app).await;
|
|
let code = request["user_code"].as_str().unwrap();
|
|
let secret = request["device_secret"].as_str().unwrap();
|
|
assert!(
|
|
!request["verification_uri"]
|
|
.as_str()
|
|
.unwrap()
|
|
.contains(secret)
|
|
);
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/poll",
|
|
json!({"device_secret":secret}),
|
|
None
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::ACCEPTED
|
|
);
|
|
assert_eq!(
|
|
call(&app, "/v1/auth/desktop/approve", json!({"code":code}), None)
|
|
.await
|
|
.0,
|
|
StatusCode::FORBIDDEN
|
|
);
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/approve",
|
|
json!({"code":code}),
|
|
Some((&session, "wrong"))
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::FORBIDDEN
|
|
);
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/approve",
|
|
json!({"code":code}),
|
|
Some((&session, &csrf))
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::OK
|
|
);
|
|
let (status, approved) = call(
|
|
&app,
|
|
"/v1/auth/desktop/poll",
|
|
json!({"device_secret":secret}),
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
let claims = hex::decode(approved["account_proof"]["claims"].as_str().unwrap()).unwrap();
|
|
let claim: Value = serde_json::from_slice(&claims).unwrap();
|
|
assert_eq!(claim["public_key"], public);
|
|
assert_eq!(claim["subject"], approved["user"]["user_id"]);
|
|
assert_eq!(
|
|
claim["expires_at"].as_i64().unwrap() - claim["issued_at"].as_i64().unwrap(),
|
|
600
|
|
);
|
|
let key = VerifyingKey::from_bytes(
|
|
&hex::decode(&state.issuer.public_key)
|
|
.unwrap()
|
|
.try_into()
|
|
.unwrap(),
|
|
)
|
|
.unwrap();
|
|
let sig = Signature::from_slice(
|
|
&hex::decode(approved["account_proof"]["signature"].as_str().unwrap()).unwrap(),
|
|
)
|
|
.unwrap();
|
|
let mut transcript = b"VNOX-CENTRAL-ACCOUNT-v1\0".to_vec();
|
|
transcript.extend_from_slice(&claims);
|
|
key.verify_strict(&transcript, &sig).unwrap();
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/poll",
|
|
json!({"device_secret":secret}),
|
|
None
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::GONE
|
|
);
|
|
let refresh = approved["refresh_secret"].as_str().unwrap();
|
|
let body = json!({"refresh_secret":refresh});
|
|
assert_eq!(
|
|
call(&app, "/v1/auth/desktop/refresh", body.clone(), None)
|
|
.await
|
|
.0,
|
|
StatusCode::OK
|
|
);
|
|
let stored: String = sqlx::query_scalar("SELECT refresh_hash FROM desktop_devices")
|
|
.fetch_one(&state.database)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(stored, store::hash(refresh));
|
|
assert_ne!(stored, refresh);
|
|
assert_eq!(
|
|
call(&app, "/v1/auth/desktop/revoke", body.clone(), None)
|
|
.await
|
|
.0,
|
|
StatusCode::NO_CONTENT
|
|
);
|
|
assert_eq!(
|
|
call(&app, "/v1/auth/desktop/refresh", body, None).await.0,
|
|
StatusCode::UNAUTHORIZED
|
|
);
|
|
}
|
|
#[tokio::test]
|
|
async fn canceled_expired_and_invalid_requests_fail_closed() {
|
|
let (_dir, state, app, session, csrf) = setup().await;
|
|
for expire in [false, true] {
|
|
let (_, request) = start(&app).await;
|
|
let secret = request["device_secret"].as_str().unwrap();
|
|
let code = request["user_code"].as_str().unwrap();
|
|
if expire {
|
|
sqlx::query("UPDATE desktop_authorizations SET expires_at=0")
|
|
.execute(&state.database)
|
|
.await
|
|
.unwrap();
|
|
} else {
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/cancel",
|
|
json!({"device_secret":secret}),
|
|
None
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::NO_CONTENT
|
|
);
|
|
}
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/approve",
|
|
json!({"code":code}),
|
|
Some((&session, &csrf))
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::GONE
|
|
);
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/poll",
|
|
json!({"device_secret":secret}),
|
|
None
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::GONE
|
|
);
|
|
}
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/start",
|
|
json!({"public_key":"bad"}),
|
|
None
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::BAD_REQUEST
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn cancel_also_revokes_an_enrollment_whose_poll_response_was_lost() {
|
|
let (_dir, _state, app, session, csrf) = setup().await;
|
|
let (_, request) = start(&app).await;
|
|
let code = request["user_code"].as_str().unwrap();
|
|
let device = json!({"device_secret":request["device_secret"]});
|
|
assert_eq!(
|
|
call(
|
|
&app,
|
|
"/v1/auth/desktop/approve",
|
|
json!({"code":code}),
|
|
Some((&session, &csrf))
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::OK
|
|
);
|
|
let (status, approved) = call(&app, "/v1/auth/desktop/poll", device.clone(), None).await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
assert_eq!(
|
|
call(&app, "/v1/auth/desktop/cancel", device, None).await.0,
|
|
StatusCode::NO_CONTENT
|
|
);
|
|
let refresh = json!({"refresh_secret":approved["refresh_secret"]});
|
|
assert_eq!(
|
|
call(&app, "/v1/auth/desktop/refresh", refresh, None)
|
|
.await
|
|
.0,
|
|
StatusCode::UNAUTHORIZED
|
|
);
|
|
}
|
|
|
|
async fn enroll(
|
|
app: &Router,
|
|
session: &str,
|
|
csrf: &str,
|
|
) -> (ed25519_dalek::SigningKey, String, Value) {
|
|
let key = ed25519_dalek::SigningKey::generate(&mut rand::rng());
|
|
let (_, request) = call(
|
|
app,
|
|
"/v1/auth/desktop/start",
|
|
json!({"public_key":hex::encode(key.verifying_key().to_bytes())}),
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(
|
|
call(
|
|
app,
|
|
"/v1/auth/desktop/approve",
|
|
json!({"code":request["user_code"]}),
|
|
Some((session, csrf))
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::OK
|
|
);
|
|
let (status, approved) = call(
|
|
app,
|
|
"/v1/auth/desktop/poll",
|
|
json!({"device_secret":request["device_secret"]}),
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
(
|
|
key,
|
|
approved["refresh_secret"].as_str().unwrap().to_owned(),
|
|
approved["user"].clone(),
|
|
)
|
|
}
|
|
fn test_png() -> Vec<u8> {
|
|
let picture = image::DynamicImage::ImageRgba8(image::RgbaImage::from_pixel(
|
|
64,
|
|
64,
|
|
image::Rgba([20, 100, 220, 255]),
|
|
));
|
|
let mut bytes = std::io::Cursor::new(Vec::new());
|
|
picture
|
|
.write_to(&mut bytes, image::ImageFormat::Png)
|
|
.unwrap();
|
|
bytes.into_inner()
|
|
}
|
|
async fn mutate(
|
|
app: &Router,
|
|
method: &str,
|
|
path: &str,
|
|
key: &ed25519_dalek::SigningKey,
|
|
secret: &str,
|
|
bytes: Vec<u8>,
|
|
timestamp: i64,
|
|
) -> (StatusCode, Value) {
|
|
use ed25519_dalek::Signer;
|
|
use sha2::{Digest, Sha256};
|
|
let timestamp = timestamp.to_string();
|
|
let mut signed = b"VNOX-CENTRAL-PROFILE-v1\0".to_vec();
|
|
signed.extend_from_slice(method.as_bytes());
|
|
signed.push(0);
|
|
signed.extend_from_slice(path.as_bytes());
|
|
signed.push(0);
|
|
signed.extend_from_slice(timestamp.as_bytes());
|
|
signed.push(0);
|
|
signed.extend_from_slice(&Sha256::digest(&bytes));
|
|
let request = Request::builder()
|
|
.method(method)
|
|
.uri(path)
|
|
.header("authorization", format!("Bearer {secret}"))
|
|
.header(
|
|
"x-vnox-public-key",
|
|
hex::encode(key.verifying_key().to_bytes()),
|
|
)
|
|
.header("x-vnox-timestamp", timestamp)
|
|
.header(
|
|
"x-vnox-signature",
|
|
hex::encode(key.sign(&signed).to_bytes()),
|
|
)
|
|
.header(
|
|
"content-type",
|
|
if path.ends_with("profile") {
|
|
"application/json"
|
|
} else {
|
|
"image/png"
|
|
},
|
|
)
|
|
.body(Body::from(bytes))
|
|
.unwrap();
|
|
let response = app.clone().oneshot(request).await.unwrap();
|
|
let status = response.status();
|
|
let bytes = to_bytes(response.into_body(), 65536).await.unwrap();
|
|
(
|
|
status,
|
|
serde_json::from_slice(&bytes).unwrap_or(Value::Null),
|
|
)
|
|
}
|
|
#[tokio::test]
|
|
async fn desktop_avatar_upload_delete_and_website_refresh_share_one_account() {
|
|
let (_dir, state, app, session, csrf) = setup().await;
|
|
let (key, secret, user) = enroll(&app, &session, &csrf).await;
|
|
let user_id = user["user_id"].as_str().unwrap();
|
|
let website_url = vnox_hub::avatar::replace(&state.database, user_id, &test_png())
|
|
.await
|
|
.unwrap();
|
|
let (status, fresh) = call(
|
|
&app,
|
|
"/v1/auth/desktop/refresh",
|
|
json!({"refresh_secret":secret}),
|
|
None,
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
assert_eq!(fresh["user"]["avatar_url"], website_url);
|
|
let claims: Value = serde_json::from_slice(
|
|
&hex::decode(fresh["account_proof"]["claims"].as_str().unwrap()).unwrap(),
|
|
)
|
|
.unwrap();
|
|
assert_eq!(claims["avatar_url"], website_url);
|
|
let (status, uploaded) = mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/avatar",
|
|
&key,
|
|
&secret,
|
|
test_png(),
|
|
store::now(),
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
assert_eq!(uploaded["user"]["user_id"], user_id);
|
|
let url = uploaded["user"]["avatar_url"].as_str().unwrap();
|
|
assert_ne!(
|
|
url, website_url,
|
|
"desktop normalizes source to a fresh stored 256px PNG"
|
|
);
|
|
let response = app
|
|
.clone()
|
|
.oneshot(Request::builder().uri(url).body(Body::empty()).unwrap())
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(response.status(), StatusCode::OK);
|
|
let bytes = to_bytes(response.into_body(), 1024 * 1024).await.unwrap();
|
|
let image = image::load_from_memory(&bytes).unwrap();
|
|
assert_eq!((image.width(), image.height()), (256, 256));
|
|
let (status, deleted) = mutate(
|
|
&app,
|
|
"DELETE",
|
|
"/v1/auth/desktop/avatar",
|
|
&key,
|
|
&secret,
|
|
Vec::new(),
|
|
store::now(),
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
assert_eq!(
|
|
deleted["user"]["avatar_url"],
|
|
format!("/v1/avatars/default-v2/{user_id}")
|
|
);
|
|
}
|
|
#[tokio::test]
|
|
async fn desktop_profile_name_changes_without_handle_or_identity_reassignment() {
|
|
let (_dir, _state, app, session, csrf) = setup().await;
|
|
let (key, secret, user) = enroll(&app, &session, &csrf).await;
|
|
let (status, updated) = mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/profile",
|
|
&key,
|
|
&secret,
|
|
br#"{"display_name":" New Name "}"#.to_vec(),
|
|
store::now(),
|
|
)
|
|
.await;
|
|
assert_eq!(status, StatusCode::OK);
|
|
assert_eq!(updated["user"]["display_name"], "New Name");
|
|
assert_eq!(updated["user"]["handle"], user["handle"]);
|
|
assert_eq!(updated["user"]["user_id"], user["user_id"]);
|
|
let (_, fresh) = call(
|
|
&app,
|
|
"/v1/auth/desktop/refresh",
|
|
json!({"refresh_secret":secret}),
|
|
None,
|
|
)
|
|
.await;
|
|
let claim: Value = serde_json::from_slice(
|
|
&hex::decode(fresh["account_proof"]["claims"].as_str().unwrap()).unwrap(),
|
|
)
|
|
.unwrap();
|
|
assert_eq!(claim["display_name"], "New Name");
|
|
for name in ["", "\nBad", "Bad\u{202e}Name"] {
|
|
assert_eq!(
|
|
mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/profile",
|
|
&key,
|
|
&secret,
|
|
serde_json::to_vec(&json!({"display_name":name})).unwrap(),
|
|
store::now()
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::UNPROCESSABLE_ENTITY
|
|
);
|
|
}
|
|
assert_eq!(
|
|
mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/profile",
|
|
&key,
|
|
&secret,
|
|
serde_json::to_vec(&json!({"display_name":"x".repeat(65)})).unwrap(),
|
|
store::now()
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::UNPROCESSABLE_ENTITY
|
|
);
|
|
}
|
|
#[tokio::test]
|
|
async fn desktop_avatar_requires_live_secret_device_signature_and_bounded_valid_png() {
|
|
let (_dir, state, app, session, csrf) = setup().await;
|
|
let (key, secret, _) = enroll(&app, &session, &csrf).await;
|
|
let other = ed25519_dalek::SigningKey::generate(&mut rand::rng());
|
|
for (signer, secret, time) in [
|
|
(&other, secret.as_str(), store::now()),
|
|
(
|
|
&key,
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
store::now(),
|
|
),
|
|
(&key, secret.as_str(), store::now() - 61),
|
|
] {
|
|
assert_eq!(
|
|
mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/avatar",
|
|
signer,
|
|
secret,
|
|
test_png(),
|
|
time
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::UNAUTHORIZED
|
|
);
|
|
}
|
|
assert_eq!(
|
|
mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/avatar",
|
|
&key,
|
|
&secret,
|
|
b"not png".to_vec(),
|
|
store::now()
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::UNSUPPORTED_MEDIA_TYPE
|
|
);
|
|
assert_eq!(
|
|
mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/avatar",
|
|
&key,
|
|
&secret,
|
|
vec![0; 2 * 1024 * 1024 + 1],
|
|
store::now()
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::PAYLOAD_TOO_LARGE
|
|
);
|
|
sqlx::query("UPDATE desktop_devices SET expires_at=0")
|
|
.execute(&state.database)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
mutate(
|
|
&app,
|
|
"DELETE",
|
|
"/v1/auth/desktop/avatar",
|
|
&key,
|
|
&secret,
|
|
Vec::new(),
|
|
store::now()
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::UNAUTHORIZED
|
|
);
|
|
sqlx::query("DELETE FROM desktop_devices")
|
|
.execute(&state.database)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
mutate(
|
|
&app,
|
|
"POST",
|
|
"/v1/auth/desktop/profile",
|
|
&key,
|
|
&secret,
|
|
br#"{"display_name":"Changed"}"#.to_vec(),
|
|
store::now()
|
|
)
|
|
.await
|
|
.0,
|
|
StatusCode::UNAUTHORIZED
|
|
);
|
|
}
|