Skip to content
10 changes: 10 additions & 0 deletions desktop/ox-host-web/src/push_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ impl Default for PushService {
}
}

impl PushService {
/// Whether this host's relay can deliver the fixed approval-action
/// category. Only the built-in service is verified: a custom origin may
/// ignore the category, and a relay with a host credential uses another
/// alert path. A disabled service delivers nothing.
pub fn supports_approval_actions(&self) -> bool {
self.enabled && !self.credential && self.origin == DEFAULT_ORIGIN
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Edit {
Expand Down
27 changes: 27 additions & 0 deletions desktop/ox-host-web/src/push_service_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ fn a_host_without_a_file_uses_the_built_in_service() {
assert!(PushService::default().enabled);
}

#[test]
fn only_the_enabled_built_in_service_supports_approval_actions() {
assert!(PushService::default().supports_approval_actions());
assert!(
!PushService {
enabled: false,
..PushService::default()
}
.supports_approval_actions()
);
assert!(
!PushService {
origin: "https://push.example".into(),
..PushService::default()
}
.supports_approval_actions()
);
// A host credential belongs to a relay that uses its own alert path.
assert!(
!PushService {
credential: true,
..PushService::default()
}
.supports_approval_actions()
);
}

#[test]
fn turning_push_off_and_on_keeps_a_chosen_origin() {
let dir = dir();
Expand Down
145 changes: 143 additions & 2 deletions desktop/ox-host-web/src/remote_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ enum DeviceAction {
/// Read or change the authenticated phone's notification-action opt-in.
NativePushApprovalActions {
enabled: Option<bool>,
/// Ask the reply to state whether the configured relay can deliver
/// the fixed approval-action category. Absent keeps the reply's
/// original `{"enabled": bool}` shape for older callers.
include_support: Option<bool>,
},
}

Expand Down Expand Up @@ -251,12 +255,36 @@ pub(super) async fn settings(
.push_preferences(&id, *change)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
)),
DeviceAction::NativePushApprovalActions { enabled } => {
DeviceAction::NativePushApprovalActions {
enabled,
include_support,
} => {
let support = if include_support.unwrap_or(false) {
Some(
state
.push_service(None)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.supports_approval_actions(),
)
} else {
None
};
// A phone that asked for the capability may not persist
// an enable the configured relay cannot honor. Disable
// stays available, and a legacy caller keeps its old
// behavior.
if support == Some(false) && *enabled == Some(true) {
return Err(StatusCode::BAD_REQUEST);
}
let enabled = state
.approval_actions(&id, *enabled)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(DENY)?;
Some(json!({ "enabled": enabled }))
let mut reply = json!({ "enabled": enabled });
if let Some(support) = support {
reply["support"] = support.into();
}
Some(reply)
}
DeviceAction::HostModePolicy { set, before } => {
let policy = match (set, before) {
Expand Down Expand Up @@ -1056,6 +1084,119 @@ mod lookup_tests {
}
}

#[tokio::test]
async fn approval_action_support_follows_the_configured_relay() {
use crate::admin::DeviceClass;
use crate::push_service::{DEFAULT_ORIGIN, Edit, PushService};

let dir = tempfile::tempdir().unwrap();
let app = app(dir.path(), dir.path().join("missing.sock"));
let key = SigningKey::random(&mut OsRng);
let (phone, cookie) = pair(
&mut app.state.lock().unwrap(),
&key,
"Phone",
DeviceClass::Phone,
);
let ask = |request: &str| signed(&app, &phone, &key, request);
let read = r#"{"action":"native_push_approval_actions","include_support":true}"#;
let enable =
r#"{"action":"native_push_approval_actions","enabled":true,"include_support":true}"#;
let disable =
r#"{"action":"native_push_approval_actions","enabled":false,"include_support":true}"#;
let set_push = |change: &dyn Fn(&mut PushService)| {
let state = app.state.lock().unwrap();
let before = state.push_service(None).unwrap();
let mut after = before.clone();
change(&mut after);
state.push_service(Some(Edit { before, after })).unwrap();
};

// The built-in service is the verified path: it advertises support and
// stores an enable.
assert_eq!(
call(app.clone(), &cookie, ask(read)).await.unwrap().0,
json!({"enabled": false, "support": true})
);
assert_eq!(
call(app.clone(), &cookie, ask(enable)).await.unwrap().0,
json!({"enabled": true, "support": true})
);

// A disabled service advertises nothing, but stays changeable.
set_push(&|service| service.enabled = false);
assert_eq!(
call(app.clone(), &cookie, ask(read)).await.unwrap().0,
json!({"enabled": true, "support": false})
);
assert_eq!(
call(app.clone(), &cookie, ask(disable)).await.unwrap().0,
json!({"enabled": false, "support": false})
);
assert_eq!(
call(app.clone(), &cookie, ask(enable)).await.unwrap_err(),
StatusCode::BAD_REQUEST
);

// A custom stateless relay is not verified; an enable is refused
// before it reaches the device record.
set_push(&|service| {
service.enabled = true;
service.origin = "https://push.example".into();
});
assert_eq!(
call(app.clone(), &cookie, ask(enable)).await.unwrap_err(),
StatusCode::BAD_REQUEST
);
assert_eq!(
call(app.clone(), &cookie, ask(read)).await.unwrap().0,
json!({"enabled": false, "support": false})
);

// A relay with a host credential is another alert path.
let path = dir.path().join(crate::push_relay::host_config::FILE_NAME);
std::fs::write(
&path,
format!(
r#"{{"origin":"https://relay.example","credential":"{}"}}"#,
"A".repeat(43)
),
)
.unwrap();
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600)).unwrap();
assert_eq!(
call(app.clone(), &cookie, ask(read)).await.unwrap().0,
json!({"enabled": false, "support": false})
);
assert_eq!(
call(app.clone(), &cookie, ask(enable)).await.unwrap_err(),
StatusCode::BAD_REQUEST
);

// Back on the built-in service, support and the opt-in return.
set_push(&|service| service.origin = DEFAULT_ORIGIN.into());
assert_eq!(
call(app.clone(), &cookie, ask(read)).await.unwrap().0,
json!({"enabled": false, "support": true})
);
assert_eq!(
call(app.clone(), &cookie, ask(enable)).await.unwrap().0,
json!({"enabled": true, "support": true})
);

// A malformed capability field is refused by the request shape.
assert_eq!(
call(
app.clone(),
&cookie,
ask(r#"{"action":"native_push_approval_actions","include_support":"yes"}"#)
)
.await
.unwrap_err(),
StatusCode::BAD_REQUEST
);
}

#[tokio::test]
async fn project_memory_edit_requires_the_signature_for_the_exact_request() {
let dir = tempfile::tempdir().unwrap();
Expand Down
7 changes: 6 additions & 1 deletion desktop/ox-mobile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ goal reached, failed), and can turn the host's push service on when it is off.
The setting shows **On** after the host confirms enrollment. If permission is
off, **Open iOS Settings** takes you to the app's system settings. **Turn off**
saves your choice on the phone and updates the current host. Use **Check
again** after reconnecting if an update could not reach the host.
again** after reconnecting if an update could not reach the host. Once alerts
are on, **Approval actions** lets this phone show **Allow once** and **Deny**
on eligible approval alerts. It is off by default. The switch appears when the
paired host supports it and sends through the built-in OX relay. This first
release does not verify approval-action delivery for a self-operated relay or
a relay with a host credential, so the switch stays hidden there.

## Home and projects

Expand Down
Loading
Loading