a3s-use-browser 0.1.0

Typed browser rendering and session contracts for A3S Use
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Shared managed-runtime installation primitives.

use std::fs::OpenOptions;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

use fs2::FileExt;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tracing::warn;
use url::Url;

use a3s_use_core::UseResult;

use crate::pool::browser_error;

pub(crate) const RECEIPT_FILE: &str = ".a3s-install.json";
const INSTALL_LOCK_FILE: &str = ".install.lock";
const STAGE_PREFIX: &str = ".a3s-stage-";
const BACKUP_PREFIX: &str = ".a3s-backup-";

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ManagedInstallReceipt {
    pub schema_version: u32,
    pub provider: String,
    pub version: String,
    pub source_url: String,
    pub artifact_sha256: String,
    pub artifact_bytes: u64,
    pub executable_sha256: String,
    pub integrity_policy: String,
}

pub(crate) struct DownloadedArtifact {
    pub sha256: String,
    pub bytes: u64,
}

/// Process-wide lock guard. The operating system releases the lock on drop.
pub(crate) struct InstallLock {
    _file: std::fs::File,
}

pub(crate) async fn acquire_install_lock(root: &Path) -> UseResult<InstallLock> {
    tokio::fs::create_dir_all(root).await.map_err(|error| {
        browser_error(format!(
            "Failed to create managed Browser directory '{}': {error}",
            root.display()
        ))
    })?;
    let lock_path = root.join(INSTALL_LOCK_FILE);
    tokio::task::spawn_blocking(move || {
        let file = OpenOptions::new()
            .create(true)
            .read(true)
            .write(true)
            .truncate(false)
            .open(&lock_path)
            .map_err(|error| {
                browser_error(format!(
                    "Failed to open Browser install lock '{}': {error}",
                    lock_path.display()
                ))
            })?;
        file.lock_exclusive().map_err(|error| {
            browser_error(format!(
                "Failed to acquire Browser install lock '{}': {error}",
                lock_path.display()
            ))
        })?;
        Ok(InstallLock { _file: file })
    })
    .await
    .map_err(|error| browser_error(format!("Browser install lock task failed: {error}")))?
}

pub(crate) fn trusted_https_url(value: &str, allowed_hosts: &[&str]) -> UseResult<Url> {
    let url = Url::parse(value)
        .map_err(|error| browser_error(format!("Invalid Browser download URL: {error}")))?;
    let host = url
        .host_str()
        .ok_or_else(|| browser_error("Browser download URL has no host."))?;
    if url.scheme() != "https" || !allowed_hosts.contains(&host) {
        return Err(browser_error(format!(
            "Browser download source '{}' is not an approved HTTPS host.",
            url
        )));
    }
    Ok(url)
}

pub(crate) fn approved_redirect_policy(allowed_hosts: &[&str]) -> reqwest::redirect::Policy {
    let allowed_hosts: Vec<String> = allowed_hosts
        .iter()
        .map(|host| (*host).to_string())
        .collect();
    reqwest::redirect::Policy::custom(move |attempt| {
        if attempt.previous().len() >= 10 {
            return attempt.error("too many Browser download redirects");
        }
        let approved = attempt.url().scheme() == "https"
            && attempt
                .url()
                .host_str()
                .is_some_and(|host| allowed_hosts.iter().any(|allowed| allowed == host));
        if approved {
            attempt.follow()
        } else {
            let url = attempt.url().to_string();
            attempt.error(format!(
                "Browser download redirected to unapproved URL '{url}'"
            ))
        }
    })
}

pub(crate) fn validate_version_segment(value: &str) -> UseResult<&str> {
    let valid = !value.is_empty()
        && value != "."
        && value != ".."
        && value
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b'_'));
    if !valid {
        return Err(browser_error(format!(
            "Browser provider returned an invalid version identifier '{value}'."
        )));
    }
    Ok(value)
}

#[cfg(any(feature = "lightpanda", test))]
pub(crate) fn parse_published_sha256(value: &str) -> UseResult<String> {
    let digest = value.strip_prefix("sha256:").ok_or_else(|| {
        browser_error("Browser publisher digest does not use the sha256 algorithm.")
    })?;
    if digest.len() != 64 || !digest.bytes().all(|byte| byte.is_ascii_hexdigit()) {
        return Err(browser_error(
            "Browser publisher returned an invalid SHA-256 digest.",
        ));
    }
    Ok(digest.to_ascii_lowercase())
}

pub(crate) async fn create_stage(root: &Path, label: &str) -> UseResult<PathBuf> {
    static NEXT_STAGE_ID: AtomicU64 = AtomicU64::new(1);
    for _ in 0..32 {
        let id = NEXT_STAGE_ID.fetch_add(1, Ordering::Relaxed);
        let path = root.join(format!("{STAGE_PREFIX}{label}-{}-{id}", std::process::id()));
        match tokio::fs::create_dir(&path).await {
            Ok(()) => return Ok(path),
            Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
            Err(error) => {
                return Err(browser_error(format!(
                    "Failed to create Browser staging directory '{}': {error}",
                    path.display()
                )))
            }
        }
    }
    Err(browser_error(
        "Failed to allocate a unique Browser staging directory.",
    ))
}

pub(crate) async fn cleanup_stale_stages(root: &Path) -> UseResult<()> {
    let mut entries = tokio::fs::read_dir(root).await.map_err(|error| {
        browser_error(format!(
            "Failed to inspect Browser install directory '{}': {error}",
            root.display()
        ))
    })?;
    while let Some(entry) = entries.next_entry().await.map_err(|error| {
        browser_error(format!(
            "Failed to inspect Browser install entry in '{}': {error}",
            root.display()
        ))
    })? {
        let name = entry.file_name();
        let name = name.to_string_lossy();
        if name.starts_with(STAGE_PREFIX) {
            let path = entry.path();
            let file_type = entry.file_type().await.map_err(|error| {
                browser_error(format!(
                    "Failed to inspect stale Browser path '{}': {error}",
                    path.display()
                ))
            })?;
            let result = if file_type.is_dir() {
                tokio::fs::remove_dir_all(&path).await
            } else {
                tokio::fs::remove_file(&path).await
            };
            if let Err(error) = result {
                return Err(browser_error(format!(
                    "Failed to remove stale Browser staging path '{}': {error}",
                    path.display()
                )));
            }
        } else if name.starts_with(BACKUP_PREFIX) {
            let path = entry.path();
            let Some(receipt) = load_receipt(&path) else {
                return Err(browser_error(format!(
                    "Cannot safely recover Browser install backup '{}': its receipt is missing or invalid.",
                    path.display()
                )));
            };
            validate_version_segment(&receipt.version)?;
            let target = root.join(&receipt.version);
            if tokio::fs::try_exists(&target).await.map_err(|error| {
                browser_error(format!(
                    "Failed to inspect Browser recovery target '{}': {error}",
                    target.display()
                ))
            })? {
                tokio::fs::remove_dir_all(&path).await.map_err(|error| {
                    browser_error(format!(
                        "Failed to remove stale Browser backup '{}': {error}",
                        path.display()
                    ))
                })?;
            } else {
                tokio::fs::rename(&path, &target).await.map_err(|error| {
                    browser_error(format!(
                        "Failed to recover Browser backup '{}' to '{}': {error}",
                        path.display(),
                        target.display()
                    ))
                })?;
            }
        }
    }
    Ok(())
}

pub(crate) async fn download_to_file(
    client: &reqwest::Client,
    url: Url,
    destination: &Path,
    max_bytes: u64,
) -> UseResult<DownloadedArtifact> {
    let mut response = client
        .get(url.clone())
        .send()
        .await
        .map_err(|error| browser_error(format!("Failed to download Browser runtime: {error}")))?
        .error_for_status()
        .map_err(|error| browser_error(format!("Browser runtime download failed: {error}")))?;
    if response
        .content_length()
        .is_some_and(|length| length > max_bytes)
    {
        return Err(browser_error(format!(
            "Browser runtime download exceeds the {max_bytes}-byte limit."
        )));
    }

    let mut file = tokio::fs::OpenOptions::new()
        .create_new(true)
        .write(true)
        .open(destination)
        .await
        .map_err(|error| {
            browser_error(format!(
                "Failed to create Browser download '{}': {error}",
                destination.display()
            ))
        })?;
    let mut hasher = Sha256::new();
    let mut total = 0_u64;
    while let Some(chunk) = response.chunk().await.map_err(|error| {
        browser_error(format!("Failed to read Browser runtime download: {error}"))
    })? {
        total = total
            .checked_add(chunk.len() as u64)
            .ok_or_else(|| browser_error("Browser runtime download size overflowed."))?;
        if total > max_bytes {
            return Err(browser_error(format!(
                "Browser runtime download exceeds the {max_bytes}-byte limit."
            )));
        }
        hasher.update(&chunk);
        file.write_all(&chunk).await.map_err(|error| {
            browser_error(format!(
                "Failed to write Browser download '{}': {error}",
                destination.display()
            ))
        })?;
    }
    file.flush().await.map_err(|error| {
        browser_error(format!(
            "Failed to flush Browser download '{}': {error}",
            destination.display()
        ))
    })?;
    file.sync_all().await.map_err(|error| {
        browser_error(format!(
            "Failed to sync Browser download '{}': {error}",
            destination.display()
        ))
    })?;

    Ok(DownloadedArtifact {
        sha256: format!("{:x}", hasher.finalize()),
        bytes: total,
    })
}

pub(crate) async fn sha256_file(path: &Path) -> UseResult<String> {
    let mut file = tokio::fs::File::open(path).await.map_err(|error| {
        browser_error(format!(
            "Failed to open Browser executable '{}': {error}",
            path.display()
        ))
    })?;
    let mut hasher = Sha256::new();
    let mut buffer = vec![0_u8; 128 * 1024];
    loop {
        let read = file.read(&mut buffer).await.map_err(|error| {
            browser_error(format!(
                "Failed to hash Browser executable '{}': {error}",
                path.display()
            ))
        })?;
        if read == 0 {
            break;
        }
        hasher.update(&buffer[..read]);
    }
    Ok(format!("{:x}", hasher.finalize()))
}

pub(crate) async fn write_receipt(
    install_dir: &Path,
    receipt: &ManagedInstallReceipt,
) -> UseResult<()> {
    let bytes = serde_json::to_vec_pretty(receipt)
        .map_err(|error| browser_error(format!("Failed to encode install receipt: {error}")))?;
    let path = install_dir.join(RECEIPT_FILE);
    let mut file = tokio::fs::OpenOptions::new()
        .create_new(true)
        .write(true)
        .open(&path)
        .await
        .map_err(|error| {
            browser_error(format!(
                "Failed to create Browser install receipt '{}': {error}",
                path.display()
            ))
        })?;
    file.write_all(&bytes).await.map_err(|error| {
        browser_error(format!(
            "Failed to write Browser install receipt '{}': {error}",
            path.display()
        ))
    })?;
    file.sync_all().await.map_err(|error| {
        browser_error(format!(
            "Failed to sync Browser install receipt '{}': {error}",
            path.display()
        ))
    })
}

pub(crate) fn has_complete_receipt(install_dir: &Path, provider: &str, version: &str) -> bool {
    load_receipt(install_dir).is_some_and(|receipt| {
        receipt.schema_version == 1
            && receipt.provider == provider
            && receipt.version == version
            && receipt.artifact_sha256.len() == 64
            && receipt.executable_sha256.len() == 64
    })
}

fn load_receipt(install_dir: &Path) -> Option<ManagedInstallReceipt> {
    let bytes = std::fs::read(install_dir.join(RECEIPT_FILE)).ok()?;
    serde_json::from_slice(&bytes).ok()
}

pub(crate) async fn activate_directory(stage: &Path, target: &Path) -> UseResult<()> {
    let Some(parent) = target.parent() else {
        return Err(browser_error(
            "Browser install target has no parent directory.",
        ));
    };
    let backup = parent.join(format!(
        "{BACKUP_PREFIX}{}-{}-{}",
        target
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("runtime"),
        std::process::id(),
        NEXT_ACTIVATION_ID.fetch_add(1, Ordering::Relaxed)
    ));

    let had_target = tokio::fs::try_exists(target).await.map_err(|error| {
        browser_error(format!(
            "Failed to inspect Browser install target '{}': {error}",
            target.display()
        ))
    })?;
    if had_target {
        tokio::fs::rename(target, &backup).await.map_err(|error| {
            browser_error(format!(
                "Failed to stage existing Browser install '{}': {error}",
                target.display()
            ))
        })?;
    }

    if let Err(error) = tokio::fs::rename(stage, target).await {
        if had_target {
            let _ = tokio::fs::rename(&backup, target).await;
        }
        return Err(browser_error(format!(
            "Failed to activate Browser install '{}': {error}",
            target.display()
        )));
    }
    if had_target {
        if let Err(error) = tokio::fs::remove_dir_all(&backup).await {
            warn!(
                "Activated Browser install but failed to remove backup '{}': {error}",
                backup.display()
            );
        }
    }
    Ok(())
}

static NEXT_ACTIVATION_ID: AtomicU64 = AtomicU64::new(1);

#[cfg(test)]
#[path = "install_tests.rs"]
mod tests;