1use semver::Version;
5#[cfg(target_os = "macos")]
6use subc_os::local_peer::CodeRequirementOutcome;
7use subc_os::local_peer::{CapturedPeer, ObservedPeer};
8use subc_protocol::peer::OperatorApp;
9
10pub const MACOS_OPERATOR_APP_ENABLED: bool = true;
11pub const WINDOWS_OPERATOR_APP_ENABLED: bool = false;
12pub const LINUX_OPERATOR_APP_ENABLED: bool = false;
13pub const PHONE_OPERATOR_ENABLED: bool = false;
14pub(crate) const EVIDENCE_BUDGET: std::time::Duration = std::time::Duration::from_millis(200);
15
16pub const RELEASE_FLOOR: Version = Version::new(u64::MAX, u64::MAX, u64::MAX);
18
19fn effective_release_floor() -> Version {
20 #[cfg(feature = "dev-desktop-approval")]
21 {
22 option_env!("SUBC_DEV_OPERATOR_APP_FLOOR")
23 .and_then(|floor| Version::parse(floor).ok())
24 .unwrap_or(RELEASE_FLOOR)
25 }
26 #[cfg(not(feature = "dev-desktop-approval"))]
27 {
28 RELEASE_FLOOR
29 }
30}
31
32#[cfg(feature = "dev-desktop-approval")]
33pub(crate) fn development_floor_diagnostic() -> Option<String> {
34 option_env!("SUBC_DEV_OPERATOR_APP_FLOOR")
35 .map(|_| format!("SUBC_DEV_OPERATOR_APP_FLOOR={}", effective_release_floor()))
36}
37
38pub const MACOS_REQUIREMENT: &str = concat!(
41 "anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] exists ",
42 "and certificate leaf[field.1.2.840.113635.100.6.1.13] exists ",
43 "and certificate leaf[subject.OU] = \"5R5846NBPW\" ",
44 "and identifier \"io.cortexkit.alfonso.desktop\" ",
45 "and entitlement[\"com.apple.security.get-task-allow\"] absent ",
46 "and entitlement[\"com.apple.security.cs.disable-library-validation\"] absent ",
47 "and entitlement[\"com.apple.security.cs.allow-dyld-environment-variables\"] absent"
48);
49
50#[derive(Clone, Copy, Debug)]
51#[non_exhaustive]
52pub struct CompiledApp {
53 pub id: Option<&'static str>,
54 pub macos_bundle_id: &'static str,
55 pub macos_executable: &'static str,
56 pub macos_bundle: &'static str,
57 pub windows_executable: &'static str,
58 pub linux_executable: &'static str,
59 pub windows_publisher: Option<&'static str>,
60 pub windows_path: Option<&'static str>,
61 pub linux_path: Option<&'static str>,
62 pub linux_release_index_identity: Option<&'static str>,
63}
64
65pub const COMPILED_APP: CompiledApp = CompiledApp {
66 id: Some("alfonso-desktop"),
67 macos_bundle_id: "io.cortexkit.alfonso.desktop",
68 macos_executable: "Alfonso",
69 macos_bundle: "Alfonso.app",
70 windows_executable: "Alfonso.exe",
71 linux_executable: "alfonso",
72 windows_publisher: None,
73 windows_path: None,
74 linux_path: None,
75 linux_release_index_identity: None,
76};
77
78pub fn meets_release_floor(version: Option<&str>, floor: &Version) -> bool {
79 *floor != RELEASE_FLOOR
81 && version
82 .and_then(|version| Version::parse(version).ok())
83 .is_some_and(|version| version.cmp_precedence(floor).is_ge())
84}
85
86#[allow(dead_code)]
93#[derive(Clone, Copy, Debug, PartialEq, Eq)]
94pub(crate) enum AppOpenPlatform {
95 MacOS,
96 Windows,
97 Linux,
98 Other,
99}
100
101impl AppOpenPlatform {
102 pub fn current() -> Self {
103 #[cfg(target_os = "macos")]
104 return Self::MacOS;
105 #[cfg(windows)]
106 return Self::Windows;
107 #[cfg(target_os = "linux")]
108 return Self::Linux;
109 #[cfg(not(any(target_os = "macos", windows, target_os = "linux")))]
110 return Self::Other;
111 }
112}
113
114#[derive(Clone, Debug)]
115pub(crate) struct OperatorAppStamp {
116 pub app: OperatorApp,
117 #[cfg(target_os = "macos")]
118 peer: CapturedPeer,
119 #[cfg(target_os = "macos")]
120 requirement: &'static str,
121 #[cfg(target_os = "macos")]
122 floor: Version,
123 generation: std::sync::Arc<()>,
124 pub(crate) launch_incarnation: Option<u64>,
128}
129
130impl OperatorAppStamp {
131 #[cfg(test)]
132 pub(crate) fn fixture(app: &str, pid: u32) -> Self {
133 Self {
134 app: OperatorApp::new(app, pid),
135 #[cfg(target_os = "macos")]
136 peer: ObservedPeer::Unobserved.into(),
137 #[cfg(target_os = "macos")]
138 requirement: MACOS_REQUIREMENT,
139 #[cfg(target_os = "macos")]
140 floor: RELEASE_FLOOR,
141 generation: Default::default(),
142 launch_incarnation: None,
143 }
144 }
145
146 #[cfg(windows)]
150 pub(crate) fn launch_stamp(app: OperatorApp, _peer: CapturedPeer, incarnation: u64) -> Self {
151 Self {
152 app,
153 generation: Default::default(),
154 launch_incarnation: Some(incarnation),
155 }
156 }
157
158 pub(crate) fn delivery_enabled(&self, policy: &OperatorAppPolicy) -> bool {
162 if self.launch_incarnation.is_some() {
163 policy.windows_enabled
164 } else {
165 policy.macos_enabled
166 }
167 }
168
169 pub(crate) async fn revalidate_with_launches(
172 &self,
173 launches: &crate::app_open::LaunchTable,
174 ) -> bool {
175 if let Some(incarnation) = self.launch_incarnation {
176 return launches.is_live_incarnation(&self.app.app, incarnation);
177 }
178 self.revalidate_native().await
179 }
180
181 #[cfg(all(test, target_os = "macos"))]
182 pub(crate) fn revalidate(&self) -> bool {
183 self.check_requirement()
184 }
185
186 pub(crate) fn same_generation(&self, other: &Self) -> bool {
187 std::sync::Arc::ptr_eq(&self.generation, &other.generation)
188 }
189
190 async fn revalidate_native(&self) -> bool {
191 #[cfg(target_os = "macos")]
192 if self.peer.has_audit_token() {
193 let stamp = self.clone();
194 return crate::peer_identity_lane::PeerIdentityLane::native()
195 .run(move || stamp.check_requirement())
196 .await
197 .unwrap_or(false);
198 }
199 false
200 }
201
202 #[cfg(target_os = "macos")]
203 fn check_requirement(&self) -> bool {
204 matches!(self.peer.check_code_requirement(self.requirement),
205 CodeRequirementOutcome::Satisfied(code)
206 if meets_release_floor(code.short_version.as_deref(), &self.floor))
207 }
208}
209
210#[derive(Clone, Debug)]
211pub(crate) struct OperatorAppPolicy {
212 pub macos_enabled: bool,
213 pub windows_enabled: bool,
214 pub linux_enabled: bool,
215 pub phone_enabled: bool,
216 app: CompiledApp,
217 #[cfg(target_os = "macos")]
218 requirement: &'static str,
219 floor: Version,
220 #[cfg(all(test, target_os = "macos"))]
221 requirement_checks: std::sync::Arc<std::sync::atomic::AtomicUsize>,
222 #[cfg(test)]
223 pub(crate) test_lane: Option<std::sync::Arc<crate::peer_identity_lane::PeerIdentityLane>>,
224 pub windows_path: Option<std::path::PathBuf>,
228 pub linux_path: Option<std::path::PathBuf>,
229 #[cfg_attr(not(windows), allow(dead_code))]
235 pub windows_breakaway: bool,
236 #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
241 pub linux_force_direct: bool,
242}
243
244impl Default for OperatorAppPolicy {
245 fn default() -> Self {
246 Self {
247 macos_enabled: MACOS_OPERATOR_APP_ENABLED,
248 windows_enabled: WINDOWS_OPERATOR_APP_ENABLED,
249 linux_enabled: LINUX_OPERATOR_APP_ENABLED,
250 phone_enabled: PHONE_OPERATOR_ENABLED,
251 app: COMPILED_APP,
252 #[cfg(target_os = "macos")]
253 requirement: MACOS_REQUIREMENT,
254 floor: effective_release_floor(),
255 #[cfg(all(test, target_os = "macos"))]
256 requirement_checks: Default::default(),
257 #[cfg(test)]
258 test_lane: None,
259 windows_path: COMPILED_APP.windows_path.map(std::path::PathBuf::from),
260 linux_path: COMPILED_APP.linux_path.map(std::path::PathBuf::from),
261 windows_breakaway: true,
262 linux_force_direct: false,
263 }
264 }
265}
266
267impl OperatorAppPolicy {
268 pub(crate) fn app_id(&self) -> Option<&'static str> {
274 self.app.id
275 }
276
277 #[cfg(windows)]
278 pub(crate) fn release_floor(&self) -> &Version {
279 &self.floor
280 }
281
282 pub(crate) fn installed_path(&self, platform: AppOpenPlatform) -> Option<std::path::PathBuf> {
283 match platform {
284 AppOpenPlatform::Windows => self.windows_path.clone(),
285 AppOpenPlatform::Linux => self.linux_path.clone(),
286 AppOpenPlatform::MacOS | AppOpenPlatform::Other => None,
287 }
288 }
289
290 pub(crate) fn switch_enabled(&self, platform: AppOpenPlatform) -> bool {
291 match platform {
292 AppOpenPlatform::MacOS => self.macos_enabled,
293 AppOpenPlatform::Windows => self.windows_enabled,
294 AppOpenPlatform::Linux => self.linux_enabled,
295 AppOpenPlatform::Other => false,
296 }
297 }
298
299 #[cfg(windows)]
304 pub(crate) fn windows_publisher(&self) -> Option<&'static str> {
305 self.app.windows_publisher
306 }
307
308 #[cfg(test)]
309 pub(crate) fn stamp(&self, peer: CapturedPeer) -> Option<OperatorAppStamp> {
310 #[cfg(target_os = "macos")]
311 if !self.macos_enabled {
312 return None;
313 }
314 #[cfg(target_os = "macos")]
315 return self.config_identity(peer);
316 #[cfg(not(target_os = "macos"))]
317 {
318 let _ = peer;
319 None
320 }
321 }
322
323 #[cfg(target_os = "macos")]
324 pub(crate) fn config_identity(&self, peer: CapturedPeer) -> Option<OperatorAppStamp> {
325 if self.floor == RELEASE_FLOOR {
326 return None;
327 }
328 let app = self.app.id?;
329 let ObservedPeer::Observed(observation) = peer.observation() else {
330 return None;
331 };
332 let pid = observation.pid;
333 #[cfg(test)]
334 self.requirement_checks
335 .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
336 let CodeRequirementOutcome::Satisfied(code) = peer.check_code_requirement(self.requirement)
337 else {
338 return None;
339 };
340 if !meets_release_floor(code.short_version.as_deref(), &self.floor) {
341 return None;
342 }
343 Some(OperatorAppStamp {
344 app: OperatorApp::new(app, pid),
345 peer,
346 requirement: self.requirement,
347 floor: self.floor.clone(),
348 generation: Default::default(),
349 launch_incarnation: None,
350 })
351 }
352
353 pub(crate) fn may_recognize(&self, peer: &CapturedPeer) -> bool {
354 #[cfg(test)]
355 if self.test_lane.is_some() {
356 return true;
357 }
358 self.floor != RELEASE_FLOOR && self.app.id.is_some() && peer.has_audit_token()
359 }
360
361 pub(crate) async fn recognize(
362 &self,
363 peer: CapturedPeer,
364 deadline: std::time::Instant,
365 ) -> Option<(OperatorAppStamp, ObservedPeer)> {
366 #[cfg(test)]
367 if let Some(lane) = &self.test_lane {
368 lane.run_until(deadline, || ()).await;
369 return None;
370 }
371 #[cfg(target_os = "macos")]
372 {
373 let policy = self.clone();
374 crate::peer_identity_lane::PeerIdentityLane::native()
375 .run_until(deadline, move || {
376 let stamp = policy.config_identity(peer.clone())?;
377 Some((stamp, peer.observe_code()))
378 })
379 .await?
380 }
381 #[cfg(not(target_os = "macos"))]
382 {
383 let _ = (peer, deadline);
384 None
385 }
386 }
387}
388
389#[cfg(any(test, feature = "test-support"))]
393#[derive(Clone, Debug)]
394pub struct TestOperatorAppPolicy {
395 pub macos_enabled: bool,
396 pub windows_enabled: bool,
397 pub linux_enabled: bool,
398 pub app: &'static str,
399 pub requirement: &'static str,
400 pub floor: Version,
401 pub windows_publisher: Option<&'static str>,
402 pub windows_path: Option<std::path::PathBuf>,
403 pub linux_path: Option<std::path::PathBuf>,
404 pub windows_breakaway: bool,
405 pub linux_force_direct: bool,
406}
407
408#[cfg(any(test, feature = "test-support"))]
409impl From<TestOperatorAppPolicy> for OperatorAppPolicy {
410 fn from(test: TestOperatorAppPolicy) -> Self {
411 Self {
412 macos_enabled: test.macos_enabled,
413 windows_enabled: test.windows_enabled,
414 linux_enabled: test.linux_enabled,
415 phone_enabled: PHONE_OPERATOR_ENABLED,
416 app: CompiledApp {
417 id: Some(test.app),
418 windows_publisher: test.windows_publisher,
419 ..COMPILED_APP
420 },
421 #[cfg(target_os = "macos")]
422 requirement: test.requirement,
423 floor: test.floor,
424 #[cfg(all(test, target_os = "macos"))]
425 requirement_checks: Default::default(),
426 #[cfg(test)]
427 test_lane: None,
428 windows_path: test.windows_path,
429 linux_path: test.linux_path,
430 windows_breakaway: test.windows_breakaway,
431 linux_force_direct: test.linux_force_direct,
432 }
433 }
434}
435
436#[cfg(test)]
437mod tests;