a3s 0.10.5

a3s — A3S coding agent CLI; `a3s code` launches the interactive TUI
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
use std::fmt;
use std::fs::File;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::Mutex;
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};

use a3s_boot::ilink::SecretValue;

const CREDENTIAL_SCHEMA_VERSION: u32 = 1;
const CREDENTIAL_FILE_NAME: &str = "credentials.json";
const MAX_CREDENTIAL_FILE_BYTES: u64 = 256 * 1024;
const MAX_BASE_URL_BYTES: usize = 2048;

#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Zeroize, ZeroizeOnDrop)]
pub(super) struct WeixinCredentials {
    pub(super) bot_token: SecretValue,
    pub(super) bot_id: SecretValue,
    pub(super) owner_id: SecretValue,
    pub(super) base_url: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(super) context_token: Option<SecretValue>,
}

impl WeixinCredentials {
    pub(super) fn new(
        bot_token: SecretValue,
        bot_id: SecretValue,
        owner_id: SecretValue,
        base_url: impl Into<String>,
        context_token: Option<SecretValue>,
    ) -> Result<Self, CredentialStoreError> {
        let credentials = Self {
            bot_token,
            bot_id,
            owner_id,
            base_url: base_url.into(),
            context_token,
        };
        credentials.validate()?;
        Ok(credentials)
    }

    fn validate(&self) -> Result<(), CredentialStoreError> {
        if self.base_url.is_empty()
            || self.base_url.len() > MAX_BASE_URL_BYTES
            || self.base_url.contains('\0')
        {
            return Err(CredentialStoreError::InvalidCredential);
        }
        Ok(())
    }
}

impl fmt::Debug for WeixinCredentials {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("WeixinCredentials")
            .field("bot_token", &"[REDACTED]")
            .field("bot_id", &"[REDACTED]")
            .field("owner_id", &"[REDACTED]")
            .field("base_url", &"[REDACTED]")
            .field("has_context_token", &self.context_token.is_some())
            .finish()
    }
}

#[async_trait]
pub(super) trait WeixinCredentialStore: Send + Sync {
    async fn load(&self) -> Result<Option<WeixinCredentials>, CredentialStoreError>;

    async fn save(&self, credentials: &WeixinCredentials) -> Result<(), CredentialStoreError>;

    async fn delete(&self) -> Result<(), CredentialStoreError>;
}

pub(super) struct PrivateFileCredentialStore {
    directory: PathBuf,
    operation_lock: Mutex<()>,
}

impl PrivateFileCredentialStore {
    pub(super) fn new(directory: impl Into<PathBuf>) -> Self {
        Self {
            directory: directory.into(),
            operation_lock: Mutex::new(()),
        }
    }

    fn credential_path(&self) -> PathBuf {
        self.directory.join(CREDENTIAL_FILE_NAME)
    }

    #[cfg(test)]
    pub(super) fn path_for_test(&self) -> PathBuf {
        self.credential_path()
    }
}

#[async_trait]
impl WeixinCredentialStore for PrivateFileCredentialStore {
    async fn load(&self) -> Result<Option<WeixinCredentials>, CredentialStoreError> {
        let _guard = self.operation_lock.lock().await;
        let path = self.credential_path();
        tokio::task::spawn_blocking(move || load_credentials(&path))
            .await
            .map_err(|_| CredentialStoreError::TaskJoin)?
    }

    async fn save(&self, credentials: &WeixinCredentials) -> Result<(), CredentialStoreError> {
        credentials.validate()?;
        let _guard = self.operation_lock.lock().await;
        let path = self.credential_path();
        let credentials = credentials.clone();
        tokio::task::spawn_blocking(move || save_credentials(&path, &credentials))
            .await
            .map_err(|_| CredentialStoreError::TaskJoin)?
    }

    async fn delete(&self) -> Result<(), CredentialStoreError> {
        let _guard = self.operation_lock.lock().await;
        let path = self.credential_path();
        tokio::task::spawn_blocking(move || delete_credentials(&path))
            .await
            .map_err(|_| CredentialStoreError::TaskJoin)?
    }
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct CredentialDocument {
    schema_version: u32,
    credentials: WeixinCredentials,
}

fn load_credentials(path: &Path) -> Result<Option<WeixinCredentials>, CredentialStoreError> {
    let Some(parent) = path.parent() else {
        return Err(CredentialStoreError::UnsafePath);
    };
    if !path_exists(parent)? {
        return Ok(None);
    }
    verify_private_directory(parent)?;
    let metadata = match std::fs::symlink_metadata(path) {
        Ok(metadata) => metadata,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => return Err(CredentialStoreError::Io(error)),
    };
    verify_private_file_metadata(&metadata)?;
    if metadata.len() == 0 || metadata.len() > MAX_CREDENTIAL_FILE_BYTES {
        return Err(CredentialStoreError::InvalidSize);
    }

    let mut options = std::fs::OpenOptions::new();
    options.read(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt;
        options.custom_flags(libc::O_NOFOLLOW);
    }
    let mut file = options.open(path).map_err(CredentialStoreError::Io)?;
    let opened_metadata = file.metadata().map_err(CredentialStoreError::Io)?;
    verify_private_file_metadata(&opened_metadata)?;
    if opened_metadata.len() == 0 || opened_metadata.len() > MAX_CREDENTIAL_FILE_BYTES {
        return Err(CredentialStoreError::InvalidSize);
    }
    let mut bytes = Zeroizing::new(Vec::with_capacity(opened_metadata.len() as usize));
    Read::by_ref(&mut file)
        .take(MAX_CREDENTIAL_FILE_BYTES + 1)
        .read_to_end(&mut bytes)
        .map_err(CredentialStoreError::Io)?;
    if bytes.len() as u64 > MAX_CREDENTIAL_FILE_BYTES {
        return Err(CredentialStoreError::InvalidSize);
    }
    let document: CredentialDocument =
        serde_json::from_slice(&bytes).map_err(CredentialStoreError::Serialization)?;
    if document.schema_version != CREDENTIAL_SCHEMA_VERSION {
        return Err(CredentialStoreError::UnsupportedSchema);
    }
    document.credentials.validate()?;
    Ok(Some(document.credentials))
}

fn save_credentials(
    path: &Path,
    credentials: &WeixinCredentials,
) -> Result<(), CredentialStoreError> {
    let parent = path.parent().ok_or(CredentialStoreError::UnsafePath)?;
    ensure_private_directory(parent)?;
    match std::fs::symlink_metadata(path) {
        Ok(metadata) => verify_private_file_metadata(&metadata)?,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
        Err(error) => return Err(CredentialStoreError::Io(error)),
    }
    let document = CredentialDocument {
        schema_version: CREDENTIAL_SCHEMA_VERSION,
        credentials: credentials.clone(),
    };
    let mut bytes =
        Zeroizing::new(serde_json::to_vec(&document).map_err(CredentialStoreError::Serialization)?);
    bytes.push(b'\n');
    if bytes.len() as u64 > MAX_CREDENTIAL_FILE_BYTES {
        return Err(CredentialStoreError::InvalidSize);
    }

    let mut temporary =
        tempfile::NamedTempFile::new_in(parent).map_err(CredentialStoreError::Io)?;
    set_private_file(temporary.as_file())?;
    temporary
        .write_all(&bytes)
        .map_err(CredentialStoreError::Io)?;
    temporary
        .as_file()
        .sync_all()
        .map_err(CredentialStoreError::Io)?;
    temporary
        .persist(path)
        .map_err(|error| CredentialStoreError::Io(error.error))?;
    let metadata = std::fs::symlink_metadata(path).map_err(CredentialStoreError::Io)?;
    verify_private_file_metadata(&metadata)?;
    sync_directory(parent)
}

fn delete_credentials(path: &Path) -> Result<(), CredentialStoreError> {
    let Some(parent) = path.parent() else {
        return Err(CredentialStoreError::UnsafePath);
    };
    if !path_exists(parent)? {
        return Ok(());
    }
    verify_private_directory(parent)?;
    match std::fs::symlink_metadata(path) {
        Ok(metadata) => verify_private_file_metadata(&metadata)?,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(CredentialStoreError::Io(error)),
    }
    std::fs::remove_file(path).map_err(CredentialStoreError::Io)?;
    sync_directory(parent)
}

fn path_exists(path: &Path) -> Result<bool, CredentialStoreError> {
    match std::fs::symlink_metadata(path) {
        Ok(_) => Ok(true),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(CredentialStoreError::Io(error)),
    }
}

#[cfg(unix)]
pub(super) fn ensure_private_directory(path: &Path) -> Result<(), CredentialStoreError> {
    use std::os::unix::fs::{DirBuilderExt, PermissionsExt};

    verify_ancestor_chain(path, true)?;
    match std::fs::symlink_metadata(path) {
        Ok(_) => verify_private_directory(path),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
            let mut builder = std::fs::DirBuilder::new();
            builder.recursive(true).mode(0o700);
            builder.create(path).map_err(CredentialStoreError::Io)?;
            std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))
                .map_err(CredentialStoreError::Io)?;
            verify_ancestor_chain(path, false)?;
            verify_private_directory(path)
        }
        Err(error) => Err(CredentialStoreError::Io(error)),
    }
}

#[cfg(not(unix))]
pub(super) fn ensure_private_directory(_path: &Path) -> Result<(), CredentialStoreError> {
    Err(CredentialStoreError::UnsupportedPlatform)
}

#[cfg(unix)]
pub(super) fn verify_private_directory(path: &Path) -> Result<(), CredentialStoreError> {
    use std::os::unix::fs::{MetadataExt, PermissionsExt};

    verify_ancestor_chain(path, false)?;
    let metadata = std::fs::symlink_metadata(path).map_err(CredentialStoreError::Io)?;
    if metadata.file_type().is_symlink() || !metadata.is_dir() {
        return Err(CredentialStoreError::UnsafePath);
    }
    if metadata.uid() != effective_uid() {
        return Err(CredentialStoreError::UnsafeOwner);
    }
    if metadata.permissions().mode() & 0o077 != 0 {
        return Err(CredentialStoreError::UnsafePermissions);
    }
    Ok(())
}

#[cfg(not(unix))]
pub(super) fn verify_private_directory(_path: &Path) -> Result<(), CredentialStoreError> {
    Err(CredentialStoreError::UnsupportedPlatform)
}

#[cfg(unix)]
pub(super) fn verify_private_file_metadata(
    metadata: &std::fs::Metadata,
) -> Result<(), CredentialStoreError> {
    use std::os::unix::fs::{MetadataExt, PermissionsExt};

    if metadata.file_type().is_symlink() || !metadata.is_file() {
        return Err(CredentialStoreError::UnsafePath);
    }
    if metadata.uid() != effective_uid() {
        return Err(CredentialStoreError::UnsafeOwner);
    }
    if metadata.permissions().mode() & 0o077 != 0 {
        return Err(CredentialStoreError::UnsafePermissions);
    }
    Ok(())
}

#[cfg(not(unix))]
pub(super) fn verify_private_file_metadata(
    _metadata: &std::fs::Metadata,
) -> Result<(), CredentialStoreError> {
    Err(CredentialStoreError::UnsupportedPlatform)
}

#[cfg(unix)]
pub(super) fn set_private_file(file: &File) -> Result<(), CredentialStoreError> {
    use std::os::unix::fs::PermissionsExt;

    file.set_permissions(std::fs::Permissions::from_mode(0o600))
        .map_err(CredentialStoreError::Io)
}

#[cfg(not(unix))]
pub(super) fn set_private_file(_file: &File) -> Result<(), CredentialStoreError> {
    Err(CredentialStoreError::UnsupportedPlatform)
}

#[cfg(unix)]
pub(super) fn sync_directory(path: &Path) -> Result<(), CredentialStoreError> {
    use std::os::unix::fs::OpenOptionsExt;

    let directory = std::fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_DIRECTORY | libc::O_NOFOLLOW)
        .open(path)
        .map_err(CredentialStoreError::Io)?;
    directory.sync_all().map_err(CredentialStoreError::Io)
}

#[cfg(unix)]
fn verify_ancestor_chain(
    path: &Path,
    allow_missing_suffix: bool,
) -> Result<(), CredentialStoreError> {
    use std::os::unix::fs::{MetadataExt, PermissionsExt};
    use std::path::Component;

    if !path.is_absolute()
        || path
            .components()
            .any(|component| matches!(component, Component::CurDir | Component::ParentDir))
    {
        return Err(CredentialStoreError::UnsafePath);
    }

    let effective_uid = effective_uid();
    let mut missing = false;
    let mut current = PathBuf::new();
    for component in path.components() {
        current.push(component.as_os_str());
        if missing {
            continue;
        }
        let metadata = match std::fs::symlink_metadata(&current) {
            Ok(metadata) => metadata,
            Err(error) if allow_missing_suffix && error.kind() == std::io::ErrorKind::NotFound => {
                missing = true;
                continue;
            }
            Err(error) => return Err(CredentialStoreError::Io(error)),
        };
        if metadata.file_type().is_symlink() || !metadata.is_dir() {
            return Err(CredentialStoreError::UnsafePath);
        }
        let owner = metadata.uid();
        if owner != 0 && owner != effective_uid {
            return Err(CredentialStoreError::UnsafeOwner);
        }
        let mode = metadata.permissions().mode();
        let writable_by_others = mode & 0o022 != 0;
        let sticky = mode & 0o1000 != 0;
        if writable_by_others && !sticky {
            return Err(CredentialStoreError::UnsafePermissions);
        }
    }
    Ok(())
}

#[cfg(unix)]
fn effective_uid() -> u32 {
    // SAFETY: `geteuid` has no preconditions and does not dereference memory.
    unsafe { libc::geteuid() }
}

#[cfg(not(unix))]
pub(super) fn sync_directory(_path: &Path) -> Result<(), CredentialStoreError> {
    Err(CredentialStoreError::UnsupportedPlatform)
}

#[derive(Debug, Error)]
pub(super) enum CredentialStoreError {
    #[cfg(not(unix))]
    #[error("Weixin credential storage is not supported on this platform")]
    UnsupportedPlatform,
    #[error("Weixin credential path is unsafe")]
    UnsafePath,
    #[error("Weixin credential path ownership is unsafe")]
    UnsafeOwner,
    #[error("Weixin credential permissions are unsafe")]
    UnsafePermissions,
    #[error("Weixin credential file has an invalid size")]
    InvalidSize,
    #[error("Weixin credential schema is unsupported")]
    UnsupportedSchema,
    #[error("Weixin credential data is invalid")]
    InvalidCredential,
    #[error("Weixin credential storage I/O failed")]
    Io(#[source] std::io::Error),
    #[error("Weixin credential serialization failed")]
    Serialization(#[source] serde_json::Error),
    #[error("Weixin credential storage task failed")]
    TaskJoin,
}