Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

Revert attribution deep-link switch and carrier removal (#7793, #7794) (#7804)

Louis Beaumontd121e440fec3
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/acquisition_handoff.rs b/apps/screenpipe-app-tauri/src-tauri/src/acquisition_handoff.rs
index 1df7f6925..31cfd59c8 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/acquisition_handoff.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/acquisition_handoff.rs
@@ -1,9 +1,8 @@
// screenpipe — AI that knows everything you've seen, said, or heard
// https://screenpipe.com
-//! Install-page deep link -> saved grant -> authenticated account. Best effort,
-//! off the recording/startup path. The server owns authorization and
-//! attribution rules; the installer itself carries nothing.
+//! Download possession -> authenticated account. Best effort, off the
+//! recording/startup path. The server owns authorization and attribution rules.
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicBool, Ordering};
@@ -13,8 +12,6 @@ use tauri::{AppHandle, Listener};
static WAKE: tokio::sync::Notify = tokio::sync::Notify::const_new();
static RETIREMENT: Retirement = Retirement::new();
static STORAGE: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
-static WORKER_ACTIVE: AtomicBool = AtomicBool::new(false);
-static DEEP_LINK_ACCEPTED: AtomicBool = AtomicBool::new(false);
const SERVICE: &str = if cfg!(feature = "enterprise-build") {
"com.screenpipe.enterprise.acquisition.v1"
@@ -65,6 +62,21 @@ impl Pending {
}
}
+#[cfg(target_os = "macos")]
+fn carrier() -> Option<String> {
+ use std::{ffi::CString, os::unix::ffi::OsStrExt};
+ let executable = std::env::current_exe().ok()?;
+ let bundle = executable.ancestors().find(|p| p.extension().is_some_and(|e| e == "app"))?;
+ let path = CString::new(bundle.as_os_str().as_bytes()).ok()?;
+ let name = CString::new("com.apple.application-instance").ok()?;
+ let mut bytes = [0u8; 36];
+ // Exact-size read; absent, truncated or oversized attributes are ignored.
+ let size = unsafe { libc::getxattr(path.as_ptr(), name.as_ptr(), bytes.as_mut_ptr().cast(), bytes.len(), 0, 0) };
+ if size != 36 { return None; }
+ let grant = String::from_utf8(bytes.to_vec()).ok()?.to_ascii_lowercase();
+ uuid4(&grant).then_some(grant)
+}
+
fn storage_account(profile: &std::path::Path, origin: &str) -> String {
use sha2::{Digest, Sha256};
format!("profile-{:x}", Sha256::digest(format!("{}\n{}", profile.display(), origin).as_bytes()))
@@ -139,47 +151,37 @@ async fn persist(pending: &Pending) -> Result<(), ()> {
save(pending).await
}
-/// `screenpipe://attribution?grant=<uuid4>`: the install page hands an opaque
-/// grant to the already-installed app. No installer bytes, filenames or manifests.
-pub(crate) fn grant_from_deep_link(url: &str) -> Option<String> {
- if !crate::deep_link::is_for_current_build(url) { return None; }
- let parsed = url.parse::<tauri::Url>().ok()?;
- let route = parsed.host_str().filter(|h| !h.is_empty())
- .or_else(|| parsed.path_segments().and_then(|mut s| s.next()))?;
- if route != "attribution" { return None; }
- let mut grants = parsed.query_pairs().filter(|(k, _)| k == "grant").map(|(_, v)| v.into_owned());
- let grant = grants.next()?.to_ascii_lowercase();
- // Exactly one grant; a second value is an ambiguous handoff, not a choice.
- if grants.next().is_some() { return None; }
- uuid4(&grant).then_some(grant)
+/// Download filenames carry only a random capability, never a user/source ID.
+fn grant_from_filename(name: &str, extension: &str) -> Option<String> {
+ let grant = name.strip_prefix("screenpipe-")?.strip_suffix(extension)?;
+ uuid4(grant).then(|| grant.to_owned())
}
-/// First grant wins. Saved proof is never replaced: the API already pinned a
-/// request/verifier to it, and a retired profile accepts nothing further.
-fn adopts_deep_link(saved: Option<&Pending>, retired: bool) -> bool {
- !retired && saved.is_none()
+#[cfg(target_os = "windows")]
+fn carrier() -> Option<String> {
+ carrier_from(std::env::current_exe().ok()?.parent()?)
}
-async fn accept(grant: String) -> Result<bool, ()> {
- let _guard = STORAGE.lock().await;
- let saved = load_saved().await?;
- if !adopts_deep_link(saved.as_ref(), RETIREMENT.disabled(&tombstone())) { return Ok(false); }
- save(&Pending::new(grant)).await?;
- Ok(true)
+#[cfg(target_os = "windows")]
+fn carrier_from(directory: &std::path::Path) -> Option<String> {
+ // The installer writes this once. Keep the read bounded so an untrusted or
+ // corrupt install directory cannot turn startup into an unbounded read.
+ let path = directory.join("acquisition-grant");
+ let metadata = std::fs::metadata(&path).ok()?;
+ if metadata.len() != 36 { return None; }
+ use std::io::Read;
+ let mut bytes = Vec::with_capacity(37);
+ std::fs::File::open(path).ok()?.take(37).read_to_end(&mut bytes).ok()?;
+ if bytes.len() != 36 { return None; }
+ let grant = String::from_utf8(bytes).ok()?;
+ uuid4(&grant).then_some(grant)
}
-/// Deep links arrive from the OS on every platform; the worker then claims the
-/// grant and binds it to the verified account.
-pub fn accept_deep_link(app: &AppHandle, url: &str) {
- let Some(grant) = grant_from_deep_link(url) else { return; };
- let app = app.clone();
- tauri::async_runtime::spawn(async move {
- match accept(grant).await {
- Ok(true) => { DEEP_LINK_ACCEPTED.store(true, Ordering::SeqCst); spawn_worker(app); }
- Ok(false) => {}
- Err(()) => tracing::debug!("acquisition deep link not stored; storage unavailable"),
- }
- });
+#[cfg(target_os = "linux")]
+fn carrier() -> Option<String> {
+ // AppImages execute from a temporary mount; APPIMAGE is the original file.
+ let path = std::path::PathBuf::from(std::env::var_os("APPIMAGE")?);
+ grant_from_filename(path.file_name()?.to_str()?, ".AppImage")
}
// Retirement never waits for the optional credential store. A late credential
@@ -250,33 +252,24 @@ fn watch_settings<R: tauri::Runtime>(
pub fn spawn(app: AppHandle) {
watch_settings(&app, crate::config::app_data_dir().join("store.bin"), retire);
- spawn_worker(app);
-}
-
-fn spawn_worker(app: AppHandle) {
- // One worker per process. A deep link accepted while it runs only wakes it;
- // one accepted as it exits restarts it below.
- if WORKER_ACTIVE.swap(true, Ordering::SeqCst) { wake(); return; }
- DEEP_LINK_ACCEPTED.store(false, Ordering::SeqCst);
tauri::async_runtime::spawn(async move {
// Keychain/settings can be temporarily unavailable during startup.
for _ in 0..240 {
- if run(app.clone()).await.is_ok() { break; }
+ if run(app.clone()).await.is_ok() { return; }
tokio::select! {
_ = tokio::time::sleep(Duration::from_secs(30)) => {},
_ = WAKE.notified() => {},
}
}
- WORKER_ACTIVE.store(false, Ordering::SeqCst);
- if DEEP_LINK_ACCEPTED.swap(false, Ordering::SeqCst) { spawn_worker(app); }
});
}
async fn run(app: AppHandle) -> Result<(), ()> {
if RETIREMENT.disabled(&tombstone()) { return Ok(()); }
- // Grants arrive only through the `screenpipe://attribution` deep link
- // and are persisted by `accept` before this worker runs.
- let Some(mut p) = load_saved().await? else { return Ok(()); };
+ let mut p = match load_saved().await? {
+ Some(p) => p,
+ None => match carrier() { Some(grant) => Pending::new(grant), None => return Ok(()) },
+ };
if p.retired { return Ok(()); }
// Persist proof BEFORE first claim, including when its HTTP response is lost.
persist(&p).await?;
@@ -364,6 +357,38 @@ async fn send(client: &reqwest::Client, origin: &reqwest::Url, action: &str, p:
#[cfg(test)]
mod tests {
use super::*;
+ #[test]
+ fn portable_filenames_reject_placeholders_and_ambiguous_names() {
+ let grant = "12345678-1234-4234-8234-123456789012";
+ for extension in [".exe", ".AppImage"] {
+ assert_eq!(grant_from_filename(&format!("screenpipe-{grant}{extension}"), extension), Some(grant.into()));
+ for name in [format!("screenpipe-{PLACEHOLDER}{extension}"), format!("screenpipe-{grant}.txt{extension}"), format!("../screenpipe-{grant}{extension}"), format!("screenpipe-{grant}{extension}.exe")] {
+ assert!(grant_from_filename(&name, extension).is_none());
+ }
+ }
+ }
+
+ #[cfg(target_os = "windows")]
+ #[test]
+ fn installed_windows_carrier_is_exact_and_bounded() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("acquisition-grant");
+ let grant = "12345678-1234-4234-8234-123456789012";
+ std::fs::write(&path, grant).unwrap();
+ assert_eq!(carrier_from(dir.path()), Some(grant.into()));
+ for invalid in [
+ PLACEHOLDER,
+ "12345678-1234-4234-7234-123456789012",
+ "12345678-1234-4234-C234-123456789012",
+ "12345678-1234-4234-8234-12345678901X",
+ ] {
+ std::fs::write(&path, invalid).unwrap();
+ assert!(carrier_from(dir.path()).is_none());
+ }
+ std::fs::write(&path, format!("{grant}\n")).unwrap();
+ assert!(carrier_from(dir.path()).is_none());
+ }
+
#[tokio::test]
async fn credential_store_preserves_proof_and_retirement_across_restart() {
let dir = tempfile::tempdir().unwrap();
@@ -383,36 +408,6 @@ mod tests {
assert!(decode_pending(&store.get(&account).await.unwrap().unwrap()).unwrap().retired);
}
- #[test]
- fn deep_link_carries_exactly_one_valid_grant_for_this_build() {
- let grant = "12345678-1234-4234-8234-123456789012";
- let s = crate::deep_link::scheme();
- let other = if s == "screenpipe" { "screenpipe-enterprise" } else { "screenpipe" };
- assert_eq!(grant_from_deep_link(&format!("{s}://attribution?grant={grant}")), Some(grant.into()));
- assert_eq!(grant_from_deep_link(&format!("{s}://attribution/?v=1&grant={}", grant.to_ascii_uppercase())), Some(grant.into()));
- for url in [
- format!("{s}://attribution?grant={PLACEHOLDER}"),
- format!("{s}://attribution?grant={grant}&grant={grant}"),
- format!("{s}://auth?grant={grant}"),
- format!("{s}://home?grant={grant}"),
- format!("https://screenpipe.com/attribution?grant={grant}"),
- format!("{other}://attribution?grant={grant}"),
- format!("{s}://attribution?grant=not-a-grant"),
- format!("{s}://attribution"),
- "not a url".to_string(),
- ] {
- assert!(grant_from_deep_link(&url).is_none(), "{url}");
- }
- }
-
- #[test]
- fn deep_link_never_replaces_saved_proof_or_revives_a_retired_profile() {
- let saved = Pending::new(uuid::Uuid::new_v4().to_string());
- assert!(adopts_deep_link(None, false));
- assert!(!adopts_deep_link(Some(&saved), false));
- assert!(!adopts_deep_link(None, true));
- }
-
#[test]
fn dev_e2e_and_preview_profiles_cannot_share_production_proof() {
let prod = storage_account(std::path::Path::new("/profile/prod"), "https://screenpipe.com");
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
index 325a16b83..af0ee5bbc 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -1241,25 +1241,6 @@ async fn main() {
// reason parameter) is still the current Apple event.
let from_autostart = launched_from_autostart();
- // Website-to-app attribution arrives as `screenpipe://attribution`.
- // The plugin delivers macOS open-URL events and single-instance
- // forwards; launch arguments are read once below. Accepting the
- // same URL twice is harmless: the first saved grant wins.
- {
- use tauri_plugin_deep_link::DeepLinkExt;
- let handle = app.handle().clone();
- app.deep_link().on_open_url(move |event| {
- for url in event.urls() {
- acquisition_handoff::accept_deep_link(&handle, url.as_str());
- }
- });
- if let Ok(Some(urls)) = app.deep_link().get_current() {
- for url in urls {
- acquisition_handoff::accept_deep_link(app.handle(), url.as_str());
- }
- }
- }
-
#[cfg(windows)]
{
use tauri_plugin_deep_link::DeepLinkExt;
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/server.rs b/apps/screenpipe-app-tauri/src-tauri/src/server.rs
index 7cbe719ba..f850f5deb 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/server.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/server.rs
@@ -199,7 +199,6 @@ async fn handle_focus(
}
if let Some(url) = payload.deep_link_url {
- crate::acquisition_handoff::accept_deep_link(&state.app_handle, &url);
let _ = state.app_handle.emit("deep-link-received", url);
}
diff --git a/apps/screenpipe-app-tauri/src-tauri/windows/hooks.nsh b/apps/screenpipe-app-tauri/src-tauri/windows/hooks.nsh
index 3ab61d8ee..7b8cec871 100644
--- a/apps/screenpipe-app-tauri/src-tauri/windows/hooks.nsh
+++ b/apps/screenpipe-app-tauri/src-tauri/windows/hooks.nsh
@@ -9,6 +9,88 @@
!include "x64.nsh"
!include "LogicLib.nsh"
+; Save a filename-carried acquisition grant once. This is deliberately a pure
+; NSIS parser: attribution is optional and never launches a shell or blocks an
+; install. Expected filename: screenpipe-<lowercase canonical UUID v4>.exe.
+!macro _SP_SaveAcquisitionGrant
+ Push $0
+ Push $1
+ Push $2
+ Push $3
+ ${IfNot} ${FileExists} "$INSTDIR\acquisition-grant"
+ StrLen $0 $EXEFILE
+ ${If} $0 == 51
+ StrCpy $1 $EXEFILE 11 0
+ StrCpy $2 $EXEFILE 4 47
+ ${If} $1 == "screenpipe-"
+ ${AndIf} $2 == ".exe"
+ StrCpy $1 $EXEFILE 36 11
+ ${If} $1 != "00000000-0000-4000-8000-000000000000"
+ StrCpy $2 $1 1 14
+ ${If} $2 == "4"
+ StrCpy $2 $1 1 19
+ ${If} $2 == "8"
+ ${OrIf} $2 == "9"
+ ${OrIf} $2 == "a"
+ ${OrIf} $2 == "b"
+ StrCpy $0 0
+ StrCpy $3 1
+ ${Do}
+ StrCpy $2 $1 1 $0
+ ${If} $0 == 8
+ ${OrIf} $0 == 13
+ ${OrIf} $0 == 18
+ ${OrIf} $0 == 23
+ ${If} $2 != "-"
+ StrCpy $3 0
+ ${ExitDo}
+ ${EndIf}
+ ${Else}
+ ${If} $2 != "0"
+ ${AndIf} $2 != "1"
+ ${AndIf} $2 != "2"
+ ${AndIf} $2 != "3"
+ ${AndIf} $2 != "4"
+ ${AndIf} $2 != "5"
+ ${AndIf} $2 != "6"
+ ${AndIf} $2 != "7"
+ ${AndIf} $2 != "8"
+ ${AndIf} $2 != "9"
+ ${AndIf} $2 != "a"
+ ${AndIf} $2 != "b"
+ ${AndIf} $2 != "c"
+ ${AndIf} $2 != "d"
+ ${AndIf} $2 != "e"
+ ${AndIf} $2 != "f"
+ StrCpy $3 0
+ ${ExitDo}
+ ${EndIf}
+ ${EndIf}
+ IntOp $0 $0 + 1
+ ${If} $0 >= 36
+ ${ExitDo}
+ ${EndIf}
+ ${Loop}
+ ${If} $3 == 1
+ ClearErrors
+ FileOpen $0 "$INSTDIR\acquisition-grant" w
+ ${IfNot} ${Errors}
+ FileWrite $0 $1
+ FileClose $0
+ ${EndIf}
+ ${EndIf}
+ ${EndIf}
+ ${EndIf}
+ ${EndIf}
+ ${EndIf}
+ ${EndIf}
+ ${EndIf}
+ Pop $3
+ Pop $2
+ Pop $1
+ Pop $0
+!macroend
+
; Stop the service through SCM without executing an installed helper. This is
; required when an administrator removed a hostile ProgramData namespace: old
; helpers logged from prepare-upgrade and could recreate that namespace with
@@ -262,6 +344,8 @@
!macroend
!macro NSIS_HOOK_POSTINSTALL
+ !insertmacro _SP_SaveAcquisitionGrant
+
; Binaries moved aside above. The orphan holding them has usually exited by
; now; whatever is left gets swept by the app on its next boot.
Delete "$INSTDIR\*.sp-old*"
diff --git a/docs/macos-acquisition-handoff.md b/docs/macos-acquisition-handoff.md
index d70145142..d43ae7c57 100644
--- a/docs/macos-acquisition-handoff.md
+++ b/docs/macos-acquisition-handoff.md
@@ -2,36 +2,76 @@
<!-- doc-covers: none -->
-The website saves the original, consented browser source before download. The
-installer is the ordinary signed DMG, EXE or AppImage and carries nothing. After
-install, the website's install page opens `screenpipe://attribution?grant=<uuid4>`
-through the operating system. The app persists that grant with a verifier,
-claims it, and binds it using the verified Clerk session. This runs in a
-background task, independent of recording. All platforms share one protocol.
+The website companion saves the original, consented browser source and places
+an opaque download grant in a platform carrier. The app persists a verifier,
+claims the grant, and binds it using the verified Clerk session. This runs in a
+background task, independent of recording. All platforms use the same claim,
+retry, account-boundary, and retirement protocol.
```text
-consented website visit -> install page "Open screenpipe"
- -> screenpipe://attribution?grant=<uuid4> -> Keychain / credential-store proof
- -> claim -> sign in from the app, in any browser -> authenticated bind
+signed app + xattr -> ZIP -> website personalizes UUID and ZIP checksums
+ -> macOS extracts app -> Keychain proof -> claim -> authenticated bind
+
+signed screenpipe-<UUID>.exe -> NSIS validates filename -> installed token
+ -> Windows credential-store proof -> claim -> authenticated bind
+
+signed screenpipe-<UUID>.AppImage -> APPIMAGE filename -> credential-store proof
+ -> claim -> authenticated bind
+```
+
+Retries reuse the same request and verifier. Saved proof survives app updates
+that remove the xattr. Account switch/logout invalidates in-flight work and saves
+a retirement marker before changing accounts, without waiting for credential
+storage. The native settings-store listener also retires immediately on analytics
+opt-out, including rapid off/on toggles and requests already in flight. A late
+credential write cannot erase a persisted marker, so restart remains retired.
+If the filesystem rejects the marker write, retirement is guaranteed only for
+the current process. Same-account JWT refresh wakes the
+worker without retiring the acquisition. Missing account provisioning is retried.
+No token, source data or HTTP error body is logged.
+
+Prepare artifacts locally from an already signed/notarized app:
+
+```sh
+python3 scripts/package-attribution-carrier.py \
+ --app /path/to/screenpipe.app --platform macos-arm \
+ --release VERSION --output /path/to/new-output-directory
```
-The app accepts only its own build scheme, the `attribution` route and exactly
-one valid v4 grant. The first saved grant wins: proof is never replaced, a
-retired profile accepts nothing, and the same URL delivered twice (open-URL
-event, single-instance forward, launch arguments, `/focus` handoff) is harmless.
-
-Retries reuse the same request and verifier. Saved proof survives app updates.
-Account switch/logout invalidates in-flight work and saves a retirement marker
-before changing accounts, without waiting for credential storage. The native
-settings-store listener also retires immediately on analytics opt-out, including
-rapid off/on toggles and requests already in flight. A late credential write
-cannot erase a persisted marker, so restart remains retired. If the filesystem
-rejects the marker write, retirement is guaranteed only for the current process.
-Same-account JWT refresh wakes the worker without retiring the acquisition.
-Missing account provisioning is retried. No token, source data or HTTP error
-body is logged.
-
-Not covered: a visitor who closes the install page before clicking, or who
-installs from a shared link, mirror or package manager, has no grant and stays
-an unknown source. Nothing in the installer, packager, manifests or release
-workflow is involved in attribution.
+The script checks app version, architecture, signature and metadata round-trip,
+then writes `screenpipe-attribution.zip` and `screenpipe-attribution.json`.
+For Linux or Windows, pass the original versioned release artifact instead:
+
+```sh
+python3 scripts/package-attribution-carrier.py \
+ --artifact /path/to/versioned-installer \
+ --platform linux \
+ --release VERSION --output /path/to/new-output-directory
+```
+
+Linux writes `screenpipe-attribution.AppImage`; Windows and Windows ARM write
+`screenpipe-attribution.exe`. Each has a `screenpipe-attribution.json` manifest
+containing the original byte length and SHA-256. Windows preparation requires a
+Windows host and checks Authenticode, version metadata, and architecture evidence
+in the versioned release filename. The NSIS bootstrap PE machine is not treated
+as payload architecture. Filename personalization preserves the signed bytes.
+Use `--platform windows` for x86_64 and `--platform windows-arm` for ARM64.
+
+The script never uploads artifacts or changes publication pointers. Production
+activation and versioned-artifact publication have not been performed. Human
+publication remains required. An ordinary, unpersonalized download installs and
+runs normally without optional attribution. Renaming a personalized Windows or
+Linux download before its first install/launch can lose attribution.
+
+The signature-exempt xattr and ZIP extraction behavior are documented by
+[Apple Developer Technical Support](https://developer.apple.com/forums/thread/796303).
+There is no ZIP or user-supplied sidecar for Windows. NSIS saves the first valid
+grant beside the installed executable and updates never replace it. Linux reads
+only the original AppImage path supplied by `APPIMAGE`.
+
+Before enabling website carrier admission, verify actual production artifacts,
+app startup, restart, login/logout, trial/purchase, updater recovery, and provider
+readback. The macOS test uses an ad-hoc signed fixture; Windows NSIS tests exercise
+the real grant macro but mock installer signature/version metadata boundaries;
+Linux uses a synthetic AppImage header. These local tests do not prove the full
+production download or signing path.
diff --git a/scripts/package-attribution-carrier.py b/scripts/package-attribution-carrier.py
new file mode 100644
index 000000000..624c87b82
--- /dev/null
+++ b/scripts/package-attribution-carrier.py
@@ -0,0 +1,186 @@
+#!/usr/bin/env python3
+# screenpipe — AI that knows everything you've seen, said, or heard
+# https://screenpipe.com
+"""Prepare a versioned download carrier from a release artifact. Never uploads/publishes."""
+import argparse
+import hashlib
+import json
+from pathlib import Path
+import plistlib
+import os
+import re
+import shutil
+import struct
+import subprocess
+import sys
+import tempfile
+import zipfile
+
+PLACEHOLDER = '00000000-0000-4000-8000-000000000000'
+
+
+def package(app, output, platform, release):
+ app, output = Path(app).resolve(), Path(output).resolve()
+ if app.suffix != '.app' or not re.fullmatch(r'\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?', release):
+ raise ValueError('signed .app and valid release required')
+ info = plistlib.loads((app / 'Contents/Info.plist').read_bytes())
+ if info.get('CFBundleShortVersionString') != release:
+ raise ValueError('release must match the app version')
+ executable = app / 'Contents/MacOS' / info['CFBundleExecutable']
+ expected_arch = {'macos-arm': 'arm64', 'macos-intel': 'x86_64'}[platform]
+ architectures = subprocess.check_output(['lipo', '-archs', str(executable)]).decode().split()
+ if not any(arch == expected_arch or (expected_arch == 'arm64' and arch == 'arm64e') for arch in architectures):
+ raise ValueError('platform does not match the signed executable')
+ subprocess.run(['codesign', '--verify', '--deep', '--strict', str(app)], check=True)
+ output.mkdir(parents=True, exist_ok=True)
+ archive = output / 'screenpipe-attribution.zip'
+ manifest = output / 'screenpipe-attribution.json'
+ if archive.exists() or manifest.exists():
+ raise ValueError('refusing to overwrite prepared artifacts')
+ with tempfile.TemporaryDirectory(prefix='screenpipe-carrier-') as tmp:
+ tmp = Path(tmp)
+ container = tmp / 'root'
+ container.mkdir()
+ copied = container / app.name
+ subprocess.run(['ditto', str(app), str(copied)], check=True)
+ subprocess.run(['xattr', '-w', 'com.apple.application-instance', PLACEHOLDER, str(copied)], check=True)
+ subprocess.run(['codesign', '--verify', '--deep', '--strict', str(copied)], check=True)
+ original = tmp / 'original.zip'
+ subprocess.run(['ditto', '-c', '-k', '--sequesterRsrc', str(container), str(original)], check=True)
+ member_name = '__MACOSX/._' + app.name
+ prepared = tmp / 'prepared.zip'
+ with zipfile.ZipFile(original) as src, zipfile.ZipFile(prepared, 'w', allowZip64=False) as dst:
+ for entry in src.infolist():
+ if entry.filename == member_name:
+ payload = src.read(entry)
+ if payload.count(PLACEHOLDER.encode()) != 1:
+ raise ValueError('ambiguous or absent xattr placeholder')
+ entry.compress_type = zipfile.ZIP_STORED
+ entry.extra = b''
+ entry.comment = b''
+ dst.writestr(entry, payload)
+ else:
+ with src.open(entry) as source, dst.open(entry, 'w') as target:
+ shutil.copyfileobj(source, target, 1024 * 1024)
+ with zipfile.ZipFile(prepared) as z:
+ entry = z.getinfo(member_name)
+ payload = z.read(entry)
+ central = z.start_dir
+ with prepared.open('rb') as f:
+ while True:
+ f.seek(central)
+ header = f.read(46)
+ if header[:4] != b'PK\x01\x02':
+ raise ValueError('missing carrier central entry')
+ namesize, extrasize, commentsize = struct.unpack_from('<HHH', header, 28)
+ name = f.read(namesize).decode('utf-8')
+ if name == member_name:
+ break
+ central += 46 + namesize + extrasize + commentsize
+ size = prepared.stat().st_size
+ if size > 2147483647:
+ raise ValueError('carrier exceeds the website streaming limit')
+ digest = hashlib.sha256()
+ with prepared.open('rb') as f:
+ for block in iter(lambda: f.read(1024 * 1024), b''):
+ digest.update(block)
+ result = dict(schema=1, platform=platform, release=release, size=size,
+ sha256=digest.hexdigest(), placeholder=PLACEHOLDER,
+ member=dict(name=member_name, data_offset=entry.header_offset+30+len(member_name.encode()),
+ data_length=len(payload), grant_offset=payload.index(PLACEHOLDER.encode()),
+ crc32=entry.CRC, local_crc_offset=entry.header_offset+14,
+ central_crc_offset=central+16))
+ # Check the actual archive extraction, including metadata and signature.
+ extracted = tmp / 'extracted'
+ subprocess.run(['ditto', '-x', '-k', str(prepared), str(extracted)], check=True)
+ extracted_app = extracted / app.name
+ received = subprocess.check_output(['xattr', '-p', 'com.apple.application-instance', str(extracted_app)]).decode().strip()
+ if received != PLACEHOLDER:
+ raise ValueError('archive lost the application instance attribute')
+ subprocess.run(['codesign', '--verify', '--deep', '--strict', str(extracted_app)], check=True)
+ shutil.copyfile(prepared, archive)
+ manifest.write_text(json.dumps(result, indent=2)+'\n')
+ return archive, manifest
+
+
+def package_filename(artifact, output, platform, release):
+ """Copy an immutable executable; only its delivered filename is personalized."""
+ artifact, output = Path(artifact).resolve(), Path(output).resolve()
+ if not re.fullmatch(r'\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?', release):
+ raise ValueError('valid release required')
+ if not re.search(r'(?:^|[_-])' + re.escape(release) + r'(?:[_-]|\.)', artifact.name):
+ raise ValueError('release must match the versioned artifact filename')
+ if platform not in ('linux', 'windows', 'windows-arm'):
+ raise ValueError('unsupported filename platform')
+ if platform == 'linux':
+ with artifact.open('rb') as source:
+ header = source.read(20)
+ if (artifact.suffix != '.AppImage' or header[:6] != b'\x7fELF\x02\x01' or
+ header[8:11] != b'AI\x02' or len(header) != 20 or struct.unpack_from('<H', header, 18)[0] != 62):
+ raise ValueError('Linux x86_64 type-2 AppImage required')
+ else:
+ if artifact.suffix.lower() != '.exe':
+ raise ValueError('Windows installer .exe required')
+ arch = (r'(?:^|[_-])(?:x64|x86_64|amd64)(?:[_-]|\.)' if platform == 'windows'
+ else r'(?:^|[_-])(?:arm64|aarch64)(?:[_-]|\.)')
+ if not re.search(arch, artifact.name, re.IGNORECASE):
+ raise ValueError('versioned release filename lacks target architecture evidence')
+ metadata = windows_metadata(artifact)
+ if metadata.get('Status') != 'Valid':
+ raise ValueError('installer must have a valid Authenticode signature')
+ versions = {str(metadata.get(key, '')).strip() for key in ('ProductVersion', 'FileVersion')}
+ if release not in versions:
+ raise ValueError('release must match Windows version metadata')
+ size = artifact.stat().st_size
+ if not 100 <= size <= 2147483647:
+ raise ValueError('carrier size outside streaming limit')
+ output.mkdir(parents=True, exist_ok=True)
+ target = output / ('screenpipe-attribution.exe' if platform.startswith('windows')
+ else 'screenpipe-attribution.AppImage')
+ manifest = output / 'screenpipe-attribution.json'
+ if target.exists() or manifest.exists():
+ raise ValueError('refusing to overwrite prepared artifacts')
+ digest = hashlib.sha256()
+ # Hash the exact bytes being copied, with exclusive creation.
+ with artifact.open('rb') as source, target.open('xb') as destination:
+ for block in iter(lambda: source.read(1024 * 1024), b''):
+ digest.update(block)
+ destination.write(block)
+ if target.stat().st_size != size:
+ target.unlink()
+ raise ValueError('artifact changed during copy')
+ target.chmod(artifact.stat().st_mode & 0o777)
+ with manifest.open('x') as destination:
+ json.dump(dict(schema=1, carrier='filename', platform=platform, release=release,
+ size=size, sha256=digest.hexdigest()), destination, indent=2)
+ destination.write('\n')
+ return target, manifest
+
+
+def windows_metadata(artifact):
+ """Read signature and version using Windows' supported PowerShell APIs."""
+ if sys.platform != 'win32':
+ raise ValueError('Windows packaging requires a Windows host')
+ script = ("$ErrorActionPreference='Stop';$p=$env:SCREENPIPE_ATTRIBUTION_ARTIFACT;"
+ "$s=Get-AuthenticodeSignature -LiteralPath $p;"
+ "$v=[Diagnostics.FileVersionInfo]::GetVersionInfo($p);"
+ "@{Status=[string]$s.Status;ProductVersion=$v.ProductVersion;"
+ "FileVersion=$v.FileVersion}|ConvertTo-Json -Compress")
+ output = subprocess.check_output([
+ 'powershell.exe', '-NoProfile', '-NonInteractive', '-Command', script
+ ], text=True, timeout=30, env={**os.environ, 'SCREENPIPE_ATTRIBUTION_ARTIFACT': str(artifact)})
+ return json.loads(output)
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description=__doc__)
+ inputs = parser.add_mutually_exclusive_group(required=True)
+ inputs.add_argument('--app')
+ inputs.add_argument('--artifact')
+ parser.add_argument('--output', required=True)
+ parser.add_argument('--platform', choices=['macos-arm', 'macos-intel', 'linux', 'windows', 'windows-arm'], required=True)
+ parser.add_argument('--release', required=True)
+ args = parser.parse_args()
+ prepare = package if args.app else package_filename
+ for artifact in prepare(args.app or args.artifact, args.output, args.platform, args.release):
+ print(artifact)
diff --git a/scripts/test_attribution_carrier.py b/scripts/test_attribution_carrier.py
new file mode 100644
index 000000000..7f9035790
--- /dev/null
+++ b/scripts/test_attribution_carrier.py
@@ -0,0 +1,155 @@
+# screenpipe — AI that knows everything you've seen, said, or heard
+# https://screenpipe.com
+"""Local attribution-carrier packaging regressions; never publish artifacts."""
+import importlib.util
+import hashlib
+import json
+import sys
+from pathlib import Path
+import plistlib
+import shutil
+import subprocess
+import tempfile
+import unittest
+from unittest import mock
+
+spec = importlib.util.spec_from_file_location('carrier', Path(__file__).with_name('package-attribution-carrier.py'))
+carrier = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(carrier)
+
+
+class CarrierTest(unittest.TestCase):
+ @unittest.skipUnless(sys.platform == "darwin", "requires macOS signing tools")
+ def test_signed_archive_metadata_and_overwrite_boundaries(self):
+ with tempfile.TemporaryDirectory(prefix='screenpipe-carrier-test-') as tmp:
+ root = Path(tmp)
+ app = root / 'Fixture.app'
+ executable = app / 'Contents/MacOS/Fixture'
+ executable.parent.mkdir(parents=True)
+ shutil.copyfile('/bin/echo', executable)
+ executable.chmod(0o755)
+ (app / 'Contents/Info.plist').write_bytes(plistlib.dumps(dict(
+ CFBundleIdentifier='com.screenpipe.carrier-fixture', CFBundleExecutable='Fixture',
+ CFBundleName='Fixture', CFBundlePackageType='APPL', CFBundleShortVersionString='0.3.100')))
+ subprocess.run(['codesign', '--force', '--sign', '-', str(app)], check=True)
+ architectures = subprocess.check_output(['lipo', '-archs', str(executable)]).decode()
+ platform = 'macos-arm' if 'arm64' in architectures else 'macos-intel'
+ output = root / 'output'
+ archive, manifest = carrier.package(app, output, platform, '0.3.100')
+ self.assertTrue(archive.is_file())
+ self.assertTrue(manifest.is_file())
+ # Packaging already verifies extraction, xattr preservation and the
+ # actual extracted signature. Also prove it never mutates the input.
+ attrs = subprocess.check_output(['xattr', str(app)]).decode()
+ self.assertNotIn('com.apple.application-instance', attrs)
+ with self.assertRaisesRegex(ValueError, 'overwrite'):
+ carrier.package(app, output, platform, '0.3.100')
+ with self.assertRaisesRegex(ValueError, 'version'):
+ carrier.package(app, root / 'wrong-version', platform, '0.3.101')
+
+ def test_appimage_bytes_unchanged_and_metadata_bounded(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ artifact = root / 'screenpipe_1.2.3_amd64.AppImage'
+ content = bytearray(1024)
+ content[:6] = b'\x7fELF\x02\x01'
+ content[8:11] = b'AI\x02'
+ content[18] = 62
+ artifact.write_bytes(content)
+ artifact.chmod(0o755)
+ prepared, manifest = carrier.package_filename(artifact, root / 'out', 'linux', '1.2.3')
+ self.assertEqual(prepared.read_bytes(), content)
+ self.assertEqual(artifact.read_bytes(), content)
+ data = json.loads(manifest.read_text())
+ self.assertEqual(data['sha256'], hashlib.sha256(content).hexdigest())
+ self.assertEqual(data['carrier'], 'filename')
+ with self.assertRaisesRegex(ValueError, 'overwrite'):
+ carrier.package_filename(artifact, root / 'out', 'linux', '1.2.3')
+ with self.assertRaisesRegex(ValueError, 'version'):
+ carrier.package_filename(artifact, root / 'bad-version', 'linux', '1.2.4')
+ content[18] = 183
+ artifact.write_bytes(content)
+ with self.assertRaisesRegex(ValueError, 'x86_64'):
+ carrier.package_filename(artifact, root / 'bad-arch', 'linux', '1.2.3')
+
+ def test_windows_bytes_metadata_architecture_and_overwrite_boundaries(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ content = b'MZ' + bytes(1022)
+ for platform, arch in [('windows', 'x64'), ('windows-arm', 'arm64')]:
+ artifact = root / f'screenpipe_1.2.3_{arch}-setup.exe'
+ artifact.write_bytes(content)
+ output = root / platform
+ metadata = dict(Status='Valid', ProductVersion='1.2.3', FileVersion='1.2.3')
+ with mock.patch.object(carrier, 'windows_metadata', return_value=metadata):
+ prepared, manifest = carrier.package_filename(artifact, output, platform, '1.2.3')
+ self.assertEqual(prepared.name, 'screenpipe-attribution.exe')
+ self.assertEqual(prepared.read_bytes(), content)
+ data = json.loads(manifest.read_text())
+ self.assertEqual(data['sha256'], hashlib.sha256(content).hexdigest())
+ self.assertEqual(data['carrier'], 'filename')
+ with self.assertRaisesRegex(ValueError, 'overwrite'):
+ carrier.package_filename(artifact, output, platform, '1.2.3')
+ wrong_arch = root / f'screenpipe_1.2.3_{"arm64" if arch == "x64" else "x64"}-setup.exe'
+ wrong_arch.write_bytes(content)
+ with self.assertRaisesRegex(ValueError, 'architecture'):
+ carrier.package_filename(wrong_arch, root / f'bad-{platform}', platform, '1.2.3')
+ with mock.patch.object(carrier, 'windows_metadata', return_value={**metadata, 'Status': 'NotSigned'}):
+ with self.assertRaisesRegex(ValueError, 'Authenticode'):
+ carrier.package_filename(artifact, root / f'unsigned-{platform}', platform, '1.2.3')
+ with mock.patch.object(carrier, 'windows_metadata', return_value={**metadata, 'ProductVersion': '1.2.4', 'FileVersion': '1.2.4'}):
+ with self.assertRaisesRegex(ValueError, 'version metadata'):
+ carrier.package_filename(artifact, root / f'version-{platform}', platform, '1.2.3')
+
+ @unittest.skipUnless(sys.platform == 'win32', 'requires Windows NSIS')
+ def test_actual_nsis_filename_grant_fixture(self):
+ makensis = shutil.which('makensis') or shutil.which('makensis.exe')
+ self.assertIsNotNone(makensis, 'makensis is required for the Windows fixture')
+ hooks = (Path(__file__).parents[1] / 'apps/screenpipe-app-tauri/src-tauri/windows/hooks.nsh').resolve()
+ with tempfile.TemporaryDirectory(prefix='screenpipe-nsis-attribution-') as tmp:
+ root = Path(tmp)
+ fixture = root / 'fixture.nsi'
+ fixture.write_text(
+ '; screenpipe — AI that knows everything you\'ve seen, said, or heard\n'
+ '; https://screenpipe.com\n'
+ 'Unicode true\nSilentInstall silent\nRequestExecutionLevel user\n'
+ f'!include "{hooks}"\n'
+ 'Name "Screenpipe attribution fixture"\nOutFile "fixture.exe"\nInstallDir "$TEMP\\screenpipe-attribution-fixture"\n'
+ 'Section\nCreateDirectory "$INSTDIR"\nFileOpen $0 "$INSTDIR\\installed" w\nFileWrite $0 "ok"\nFileClose $0\n'
+ '!insertmacro _SP_SaveAcquisitionGrant\nSectionEnd\n',
+ encoding='utf-8')
+ subprocess.run([makensis, '/V2', str(fixture)], cwd=root, check=True)
+
+ # Exercise the real PowerShell adapter, including a path whose
+ # spaces and metacharacters must remain data, never command text.
+ metadata_fixture = root / 'fixture $ [attribution].exe'
+ shutil.copyfile(root / 'fixture.exe', metadata_fixture)
+ self.assertEqual(carrier.windows_metadata(metadata_fixture)['Status'], 'NotSigned')
+
+ first = '12345678-1234-4234-8234-123456789012'
+ second = 'abcdefab-cdef-4abc-9def-abcdefabcdef'
+
+ def install(filename, directory):
+ installer = root / filename
+ shutil.copyfile(root / 'fixture.exe', installer)
+ subprocess.run([str(installer), '/S', f'/D={directory}'], check=True)
+ self.assertEqual((directory / 'installed').read_text(), 'ok')
+
+ valid_dir = root / 'valid'
+ install(f'screenpipe-{first}.exe', valid_dir)
+ self.assertEqual((valid_dir / 'acquisition-grant').read_text(), first)
+ install(f'screenpipe-{second}.exe', valid_dir)
+ self.assertEqual((valid_dir / 'acquisition-grant').read_text(), first)
+ for index, name in enumerate([
+ f'screenpipe-{carrier.PLACEHOLDER}.exe',
+ 'screenpipe-12345678-1234-4234-7234-123456789012.exe',
+ 'screenpipe-12345678-1234-4234-C234-123456789012.exe',
+ 'screenpipe.exe',
+ ]):
+ directory = root / f'invalid-{index}'
+ install(name, directory)
+ self.assertFalse((directory / 'acquisition-grant').exists())
+
+
+if __name__ == '__main__':
+ unittest.main()