Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion elastos/crates/elastos-server/src/api/gateway_home_runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
use super::*;

fn home_projection_token_error(err: anyhow::Error) -> (StatusCode, Json<serde_json::Value>) {
if err.to_string() == "home launch token auth session is not active" {
(
StatusCode::UNAUTHORIZED,
Json(serde_json::json!({ "error": "invalid or expired session" })),
)
} else {
gateway_internal_error(err)
}
}

pub(super) async fn home_launch(
State(state): State<GatewayState>,
headers: HeaderMap,
Expand Down Expand Up @@ -117,7 +128,7 @@ pub(super) async fn home_launch(
executable_actor,
&context,
)
.map_err(gateway_internal_error)?;
.map_err(home_projection_token_error)?;
let expires_at = gateway_home_token::require_carried_home_launch_token(
&state.data_dir,
&token,
Expand Down Expand Up @@ -1881,6 +1892,57 @@ async fn home_list_runtime_capsules(
mod tests {
use super::*;

#[test]
fn home_token_projection_lapsed_grant_returns_unauthorized() {
let data = tempfile::tempdir().unwrap();
let context = HomeLaunchTokenContext {
principal_id: "person:local:alice".into(),
session_id: "auth:alice".into(),
proof_binding_id: Some("proof:alice".into()),
grant_id: "grant:alice".into(),
};
let mut grant = elastos_runtime::auth::AuthSessionGrantV1 {
schema: elastos_runtime::auth::AuthSessionGrantV1::SCHEMA.into(),
principal_id: context.principal_id.clone(),
session_id: context.session_id.clone(),
proof_binding_id: context.proof_binding_id.clone().unwrap(),
grant_id: context.grant_id.clone(),
issued_at: now_ts(),
expires_at: now_ts() + 300,
apps: vec![HOME_CAPSULE_ID.into()],
};
crate::auth::store_session_grant(data.path(), grant.clone()).unwrap();
let token =
issue_home_launch_token_with_context(data.path(), HOME_CAPSULE_ID, &context).unwrap();
let mut headers = HeaderMap::new();
headers.insert("host", "localhost:61180".parse().unwrap());
headers.insert("origin", "http://localhost:61180".parse().unwrap());
headers.insert("x-elastos-home-token", token.parse().unwrap());
let validated =
require_home_launch_token_context(data.path(), &headers, HOME_CAPSULE_ID).unwrap();
// Expire the stored grant after frame validation, before projection minting.
grant.expires_at = now_ts();
crate::auth::store_session_grant(data.path(), grant).unwrap();
let (status, Json(body)) = issue_home_projection_launch_token_with_context(
data.path(),
CHAT_ROOM_CAPSULE_ID,
CHAT_ROOM_CAPSULE_ID,
&validated,
)
.map_err(home_projection_token_error)
.unwrap_err();
assert_eq!(status, StatusCode::UNAUTHORIZED);
assert_eq!(body["error"], "invalid or expired session");
}

#[test]
fn home_token_projection_internal_error_keeps_server_status() {
let (status, Json(body)) =
home_projection_token_error(anyhow::anyhow!("failed to load signing key"));
assert_eq!(status, StatusCode::INTERNAL_SERVER_ERROR);
assert_eq!(body["error"], "failed to load signing key");
}

#[tokio::test]
async fn native_shell_metadata_refuses_generic_home_launch_before_runtime_start() {
let directory = tempfile::tempdir().unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,15 @@ pub(super) async fn home_shared_content_check(
return reply("not_contact", extra);
};
let now = now_ts();
let expires_at = match gateway_home_token::grant_bound_token_expiry(
&state.data_dir,
&home,
now,
SHARED_CONTENT_CHECK_TTL_SECS,
) {
Ok(expires_at) => expires_at,
Err(err) => return (StatusCode::FORBIDDEN, err.to_string()).into_response(),
};
let online = match state.collaboration_presence_product_port.as_ref() {
Some(port) => port
.authorized_snapshot(now)
Expand All @@ -456,7 +465,7 @@ pub(super) async fn home_shared_content_check(
cid: cid.clone(),
peer_did,
message: source,
expires_at: now + SHARED_CONTENT_CHECK_TTL_SECS,
expires_at,
},
now,
));
Expand Down
153 changes: 123 additions & 30 deletions elastos/crates/elastos-server/src/api/gateway_home_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,18 @@ pub(crate) fn issue_home_launch_token_for_auth_grant(
if !grant.apps.iter().any(|allowed| allowed == app) {
anyhow::bail!("auth session grant is not authorized for app");
}
issue_home_launch_token_with_context(
// Recovery prepares tokens before it atomically commits the new grant.
issue_home_launch_token_at(
data_dir,
app,
&HomeLaunchContext::direct(app),
&HomeLaunchTokenContext {
principal_id: grant.principal_id.clone(),
session_id: grant.session_id.clone(),
proof_binding_id: Some(grant.proof_binding_id.clone()),
grant_id: grant.grant_id.clone(),
},
now_ts(),
Some(grant.expires_at),
)
}

Expand All @@ -262,7 +265,13 @@ pub(in crate::api) fn issue_home_launch_token_with_context(
app: &str,
context: &HomeLaunchTokenContext,
) -> anyhow::Result<String> {
issue_home_launch_token_at(data_dir, &HomeLaunchContext::direct(app), context, now_ts())
issue_home_launch_token_at(
data_dir,
&HomeLaunchContext::direct(app),
context,
now_ts(),
None,
)
}

pub(in crate::api) fn issue_home_projection_launch_token_with_context(
Expand All @@ -276,6 +285,7 @@ pub(in crate::api) fn issue_home_projection_launch_token_with_context(
&HomeLaunchContext::projection(selected_resource, executable_actor),
context,
now_ts(),
None,
)
}

Expand All @@ -284,6 +294,7 @@ fn issue_home_launch_token_at(
launch_context: &HomeLaunchContext,
context: &HomeLaunchTokenContext,
issued_at: u64,
grant_expires_at: Option<u64>,
) -> anyhow::Result<String> {
launch_context.validate()?;
if context.principal_id.trim().is_empty()
Expand All @@ -292,26 +303,12 @@ fn issue_home_launch_token_at(
{
anyhow::bail!("home launch token is missing authority context");
}
let mut expires_at = issued_at.saturating_add(HOME_LAUNCH_TOKEN_TTL_SECS);
// Home can replace a frame only under its current parent authority.
if launch_context.authority_actor == HOME_CAPSULE_ID
&& launch_context.selected_resource != HOME_CAPSULE_ID
{
if let Some(proof_binding_id) = context.proof_binding_id.as_deref() {
let grant = crate::auth::load_active_session_grant(
&home_launch_auth_data_dir(data_dir),
&context.session_id,
now_ts(),
)?;
if grant.principal_id != context.principal_id
|| grant.proof_binding_id != proof_binding_id
|| grant.grant_id != context.grant_id
{
anyhow::bail!("home launch token authority context mismatch");
}
expires_at = expires_at.min(grant.expires_at);
}
}
let expires_at = match grant_expires_at {
Some(expiry) => issued_at
.saturating_add(HOME_LAUNCH_TOKEN_TTL_SECS)
.min(expiry),
None => grant_bound_token_expiry(data_dir, context, issued_at, HOME_LAUNCH_TOKEN_TTL_SECS)?,
};
let (signing_key, _did) = elastos_identity::load_or_create_did(data_dir)?;
let envelope = HomeLaunchTokenEnvelope {
payload: HomeLaunchTokenPayload {
Expand Down Expand Up @@ -343,6 +340,31 @@ fn issue_home_launch_token_at(
Ok(base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(serde_json::to_vec(&envelope)?))
}

pub(super) fn grant_bound_token_expiry(
data_dir: &std::path::Path,
context: &HomeLaunchTokenContext,
issued_at: u64,
ttl_secs: u64,
) -> anyhow::Result<u64> {
let expires_at = issued_at.saturating_add(ttl_secs);
let Some(proof_binding_id) = context.proof_binding_id.as_deref() else {
return Ok(expires_at);
};
let grant = crate::auth::load_active_session_grant(
&home_launch_auth_data_dir(data_dir),
&context.session_id,
now_ts(),
)
.map_err(|_| anyhow::anyhow!("home launch token auth session is not active"))?;
if grant.principal_id != context.principal_id
|| grant.proof_binding_id != proof_binding_id
|| grant.grant_id != context.grant_id
{
anyhow::bail!("home launch token authority context mismatch");
}
Ok(expires_at.min(grant.expires_at))
}

#[cfg(test)]
pub(crate) fn issue_expired_home_launch_token_with_context(
data_dir: &std::path::Path,
Expand All @@ -354,6 +376,7 @@ pub(crate) fn issue_expired_home_launch_token_with_context(
&HomeLaunchContext::direct(app),
context,
now_ts().saturating_sub(HOME_LAUNCH_TOKEN_TTL_SECS + 1),
None,
)
}

Expand All @@ -370,14 +393,21 @@ pub(crate) fn require_home_launch_token_context(
headers: &HeaderMap,
expected_app: &str,
) -> anyhow::Result<HomeLaunchTokenContext> {
require_home_launch_grant(data_dir, headers, expected_app).map(|required| required.context)
}

pub(super) fn require_home_launch_grant(
data_dir: &std::path::Path,
headers: &HeaderMap,
expected_app: &str,
) -> anyhow::Result<RequiredHomeLaunchToken> {
require_home_launch_token_for_any_from_with_origin(
data_dir,
headers,
&[expected_app],
None,
HomeLaunchOriginPolicy::Browser,
)
.map(|required| required.context)
}

pub(crate) fn require_home_launch_token_for_any_context(
Expand Down Expand Up @@ -1040,6 +1070,21 @@ mod tests {
.encode(serde_json::to_vec(&envelope).unwrap())
}

fn sign_context_for_test(
data_dir: &std::path::Path,
launch_context: HomeLaunchContext,
context: &HomeLaunchTokenContext,
) -> String {
let token = issue_home_launch_token(data_dir, &launch_context.executable_actor).unwrap();
let mut envelope = decode_launch_token(&token);
envelope.payload.launch_context = launch_context;
envelope.payload.principal_id = context.principal_id.clone();
envelope.payload.session_id = context.session_id.clone();
envelope.payload.proof_binding_id = context.proof_binding_id.clone();
envelope.payload.grant_id = context.grant_id.clone();
sign_launch_token_for_test(data_dir, envelope, HOME_LAUNCH_TOKEN_DOMAIN)
}

#[test]
fn home_sessions_are_isolated_between_localhost_ports() {
let first = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -1381,6 +1426,41 @@ mod tests {
.contains("authority context mismatch"));
}

#[test]
fn proof_bound_tokens_end_with_the_grant() {
let data = tempfile::tempdir().unwrap();
let now = now_ts();
let grant = elastos_runtime::auth::AuthSessionGrantV1 {
schema: elastos_runtime::auth::AuthSessionGrantV1::SCHEMA.to_string(),
grant_id: "grant:expiry".into(),
session_id: "auth:expiry".into(),
principal_id: "person:local:expiry".into(),
proof_binding_id: "proof:expiry".into(),
issued_at: now,
expires_at: now + 300,
apps: vec![HOME_CAPSULE_ID.into()],
};
let context = HomeLaunchTokenContext {
principal_id: grant.principal_id.clone(),
session_id: grant.session_id.clone(),
proof_binding_id: Some(grant.proof_binding_id.clone()),
grant_id: grant.grant_id.clone(),
};
let direct =
issue_home_launch_token_for_auth_grant(data.path(), HOME_CAPSULE_ID, &grant).unwrap();
crate::auth::store_session_grant(data.path(), grant.clone()).unwrap();
let projection = issue_home_projection_launch_token_with_context(
data.path(),
"chat-room",
"chat-room",
&context,
)
.unwrap();
for token in [direct, projection] {
assert_eq!(decode_launch_token(&token).payload.exp, grant.expires_at);
}
}

#[test]
fn launch_token_rejects_expiry_stale_schema_and_mixed_shape() {
let data_dir = tempfile::tempdir().unwrap();
Expand Down Expand Up @@ -1532,9 +1612,17 @@ mod tests {
principal_id: "person:local:bob".to_string(),
..stored.clone()
};
let token =
issue_home_launch_token_with_context(data_dir.path(), SYSTEM_CAPSULE_ID, &substituted)
.unwrap();
assert!(issue_home_launch_token_with_context(
data_dir.path(),
SYSTEM_CAPSULE_ID,
&substituted
)
.is_err());
let token = sign_context_for_test(
data_dir.path(),
HomeLaunchContext::direct(SYSTEM_CAPSULE_ID),
&substituted,
);
assert!(require_home_launch_token_context(
data_dir.path(),
&capsule_headers(&token),
Expand All @@ -1558,12 +1646,17 @@ mod tests {
..stored.clone()
},
] {
let token = issue_home_launch_token_with_context(
assert!(issue_home_launch_token_with_context(
data_dir.path(),
SYSTEM_CAPSULE_ID,
&substituted,
&substituted
)
.unwrap();
.is_err());
let token = sign_context_for_test(
data_dir.path(),
HomeLaunchContext::direct(SYSTEM_CAPSULE_ID),
&substituted,
);
let error = require_home_launch_token_context(
data_dir.path(),
&capsule_headers(&token),
Expand Down
Loading
Loading