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
58 changes: 58 additions & 0 deletions desktop/ox-host-web/src/remote_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ enum DeviceAction {
},
/// Disable this caller's own paired phone and revoke it at the relay.
NativePushDisable,
/// Read or change the authenticated phone's notification-action opt-in.
NativePushApprovalActions {
enabled: Option<bool>,
},
}

#[derive(Deserialize)]
Expand Down Expand Up @@ -247,6 +251,13 @@ pub(super) async fn settings(
.push_preferences(&id, *change)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
)),
DeviceAction::NativePushApprovalActions { enabled } => {
let enabled = state
.approval_actions(&id, *enabled)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
.ok_or(DENY)?;
Some(json!({ "enabled": enabled }))
}
DeviceAction::HostModePolicy { set, before } => {
let policy = match (set, before) {
(Some(set), Some(before)) => state.save_mode_policy(*set, *before),
Expand Down Expand Up @@ -998,6 +1009,53 @@ mod lookup_tests {
settings(Extract(app), headers(cookie), Ok(Json(envelope))).await
}

#[tokio::test]
async fn approval_action_opt_in_requires_the_phones_signed_request() {
use crate::admin::DeviceClass;

let dir = tempfile::tempdir().unwrap();
let app = app(dir.path(), dir.path().join("missing.sock"));
let phone_key = SigningKey::random(&mut OsRng);
let desktop_key = SigningKey::random(&mut OsRng);
let ((phone, phone_cookie), (desktop, desktop_cookie)) = {
let mut state = app.state.lock().unwrap();
(
pair(&mut state, &phone_key, "Phone", DeviceClass::Phone),
pair(&mut state, &desktop_key, "Desktop", DeviceClass::Desktop),
)
};
let read = r#"{"action":"native_push_approval_actions"}"#;
let enable = r#"{"action":"native_push_approval_actions","enabled":true}"#;
let mut tampered = signed(&app, &phone, &phone_key, read);
tampered.request = enable.to_owned().into();
assert_eq!(
call(app.clone(), &phone_cookie, tampered)
.await
.unwrap_err(),
DENY
);
assert_eq!(
call(
app.clone(),
&desktop_cookie,
signed(&app, &desktop, &desktop_key, enable)
)
.await
.unwrap_err(),
DENY
);
for (request, enabled) in [(read, false), (enable, true), (read, true)] {
let reply = call(
app.clone(),
&phone_cookie,
signed(&app, &phone, &phone_key, request),
)
.await
.unwrap();
assert_eq!(reply.0, json!({ "enabled": enabled }));
}
}

#[tokio::test]
async fn project_memory_edit_requires_the_signature_for_the_exact_request() {
let dir = tempfile::tempdir().unwrap();
Expand Down
83 changes: 83 additions & 0 deletions desktop/ox-host-web/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub(crate) struct Registered {
pub push: Option<Subscription>,
#[serde(default, skip_serializing_if = "Preferences::is_default")]
push_preferences: Preferences,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
approval_actions: bool,
}
impl Registered {
fn current(&self) -> bool {
Expand Down Expand Up @@ -838,6 +840,7 @@ impl State {
key: key.into(),
push: None,
push_preferences: Preferences::default(),
approval_actions: false,
};
if self
.disk
Expand Down Expand Up @@ -1105,6 +1108,26 @@ impl State {
subscribed,
})
}

/// Read or change a phone's explicit opt-in for notification decisions.
/// Enrollment and ordinary alert preferences are independent settings.
pub(crate) fn approval_actions(
&mut self,
id: &str,
enabled: Option<bool>,
) -> io::Result<Option<bool>> {
if !self.alive(id) || self.device_class(id) != Some(DeviceClass::Phone) {
return Ok(None);
}
let current = self.disk.devices[id].approval_actions;
if let Some(enabled) = enabled.filter(|enabled| *enabled != current) {
self.update(|disk| {
disk.devices.get_mut(id).unwrap().approval_actions = enabled;
})?;
}
Ok(Some(enabled.unwrap_or(current)))
}

pub(crate) fn allows_push(&self, id: &str, event: Event) -> bool {
self.disk
.devices
Expand Down Expand Up @@ -1224,6 +1247,66 @@ mod tests {
assert!(!s.prove(&id, Purpose::Connect, &next, &sign(&key, &next)));
assert!(s.identity("[email protected]", &cookie).is_none());
}
#[test]
fn notification_action_opt_in_belongs_to_one_live_phone_pairing() {
use std::os::unix::fs::PermissionsExt;

let dir = tempfile::tempdir().unwrap();
fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o700)).unwrap();
let mut state = State::load(dir.path()).unwrap();
state
.set_origin(Some("https://fixture.tail.ts.net".into()), 8443)
.unwrap();
let mut register = |label, class| {
state
.register(
"[email protected]",
"fixture-key",
label,
class,
false,
None,
)
.unwrap()
.1
};
let phone = register("Phone", DeviceClass::Phone);
let other = register("Second phone", DeviceClass::Phone);
let desktop = register("Desktop", DeviceClass::Desktop);
assert!(
serde_json::to_value(&state.disk.devices[&phone])
.unwrap()
.get("approval_actions")
.is_none()
);
assert_eq!(state.approval_actions(&phone, None).unwrap(), Some(false));
assert_eq!(state.approval_actions(&desktop, Some(true)).unwrap(), None);
assert_eq!(
state.approval_actions(&phone, Some(true)).unwrap(),
Some(true)
);
assert_eq!(state.approval_actions(&other, None).unwrap(), Some(false));

let mut restored = State::load(dir.path()).unwrap();
assert_eq!(restored.approval_actions(&phone, None).unwrap(), Some(true));
assert_eq!(
restored.approval_actions(&other, None).unwrap(),
Some(false)
);
assert_eq!(restored.approval_actions(&desktop, None).unwrap(), None);
assert_eq!(
restored.approval_actions(&phone, Some(false)).unwrap(),
Some(false)
);
let mut restored = State::load(dir.path()).unwrap();
assert_eq!(
restored.approval_actions(&phone, None).unwrap(),
Some(false)
);
restored.revoke(&phone).unwrap();
assert_eq!(restored.approval_actions(&phone, Some(true)).unwrap(), None);
}

#[test]
fn an_invitation_without_a_login_pairs_any_tailnet_user_under_that_login() {
let dir = tempfile::tempdir().unwrap();
Expand Down
Loading