a3s-use-core 0.2.10

Shared typed contracts for A3S Use domains
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
use serde::{Deserialize, Serialize};

use crate::UseResult;

use super::validation::{
    strictly_sorted_unique, valid_dns_name, valid_http_path, valid_permission_name,
    valid_portable_scope_path, valid_segment,
};
use super::{
    canonical_digest, canonical_json, contract_error, parse_contract, PluginSurfaceKind,
    PluginSurfaceRef, PLUGIN_PERMISSION_SCHEMA,
};

const PERMISSION_ERROR: &str = "use.plugin.permission_invalid";
const MAX_SURFACE_PERMISSIONS: usize = 256;
const MAX_ITEMS_PER_PERMISSION: usize = 256;
const MAX_RESOURCE_BYTES: u64 = 16 * 1024 * 1024 * 1024 * 1024;
const MAX_TASK_TIMEOUT_MS: u64 = 24 * 60 * 60 * 1000;
const MAX_CAPTURE_BYTES: u64 = 16 * 1024 * 1024 * 1024;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct PluginPermissionCeiling {
    pub schema: String,
    pub surfaces: Vec<SurfacePermissionCeiling>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SurfacePermissionCeiling {
    pub surface: PluginSurfaceRef,
    pub native_execution: bool,
    pub child_process: bool,
    pub filesystem: Vec<FilesystemPermission>,
    pub network_egress: Vec<NetworkEgressPermission>,
    pub private_service: bool,
    pub secrets: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resources: Option<ResourcePermissionCeiling>,
    pub ui_http: Vec<UiHttpPermission>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FilesystemScope {
    PluginData,
    Temporary,
    Workspace,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum FilesystemAccess {
    Read,
    ReadWrite,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct FilesystemPermission {
    pub scope: FilesystemScope,
    pub path: String,
    pub access: FilesystemAccess,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct NetworkEgressPermission {
    pub host: String,
    pub ports: Vec<u16>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ResourcePermissionCeiling {
    pub cpu_millis: u64,
    pub memory_bytes: u64,
    pub pids: u32,
    pub ephemeral_storage_bytes: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task_timeout_ms: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_stdout_bytes: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_stderr_bytes: Option<u64>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HttpMethod {
    Delete,
    Get,
    Patch,
    Post,
    Put,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UiHttpPermission {
    pub tool_id: String,
    pub methods: Vec<HttpMethod>,
    pub path_prefixes: Vec<String>,
}

impl PluginPermissionCeiling {
    pub fn from_json(input: &[u8]) -> UseResult<Self> {
        parse_contract(
            input,
            "plugin permission ceiling",
            PERMISSION_ERROR,
            Self::validate,
        )
    }

    pub fn validate(&self) -> UseResult<()> {
        if self.schema != PLUGIN_PERMISSION_SCHEMA
            || self.surfaces.len() > MAX_SURFACE_PERMISSIONS
            || self
                .surfaces
                .windows(2)
                .any(|pair| pair[0].surface >= pair[1].surface)
        {
            return Err(permission_error(
                "Plugin permission surfaces must use the supported schema and be sorted uniquely.",
            ));
        }
        for surface in &self.surfaces {
            surface.validate()?;
        }
        Ok(())
    }

    pub fn canonical_bytes(&self) -> UseResult<Vec<u8>> {
        self.validate()?;
        canonical_json(self, "plugin permission ceiling", PERMISSION_ERROR)
    }

    pub fn descriptor_digest(&self) -> UseResult<String> {
        Ok(canonical_digest(&self.canonical_bytes()?))
    }

    pub fn is_within(&self, ceiling: &Self) -> UseResult<bool> {
        self.validate()?;
        ceiling.validate()?;
        Ok(self.surfaces.iter().all(|granted| {
            ceiling
                .surfaces
                .iter()
                .find(|allowed| allowed.surface == granted.surface)
                .is_some_and(|allowed| surface_is_within(granted, allowed))
        }))
    }
}

impl SurfacePermissionCeiling {
    fn validate(&self) -> UseResult<()> {
        if !valid_segment(&self.surface.id)
            || matches!(
                self.surface.kind,
                PluginSurfaceKind::Flow | PluginSurfaceKind::Okf | PluginSurfaceKind::Skill
            )
            || self.filesystem.len() > MAX_ITEMS_PER_PERMISSION
            || self.network_egress.len() > MAX_ITEMS_PER_PERMISSION
            || self.secrets.len() > MAX_ITEMS_PER_PERMISSION
            || self.ui_http.len() > MAX_ITEMS_PER_PERMISSION
            || !strictly_sorted_unique(&self.filesystem)
            || !strictly_sorted_unique(&self.network_egress)
            || !strictly_sorted_unique(&self.secrets)
            || self
                .ui_http
                .windows(2)
                .any(|pair| pair[0].tool_id >= pair[1].tool_id)
        {
            return Err(permission_error(
                "A plugin surface permission ceiling is invalid or noncanonical.",
            ));
        }

        for filesystem in &self.filesystem {
            if !valid_portable_scope_path(&filesystem.path) {
                return Err(permission_error(
                    "Filesystem permissions require portable scope-relative paths.",
                ));
            }
        }
        for egress in &self.network_egress {
            if !valid_dns_name(&egress.host)
                || egress.ports.is_empty()
                || egress.ports.len() > 16
                || egress.ports.contains(&0)
                || !strictly_sorted_unique(&egress.ports)
            {
                return Err(permission_error(
                    "Network egress permissions require exact hosts and sorted nonzero ports.",
                ));
            }
        }
        if self
            .secrets
            .iter()
            .any(|secret| !valid_permission_name(secret))
        {
            return Err(permission_error(
                "Secret permissions may contain only stable secret names.",
            ));
        }
        if let Some(resources) = &self.resources {
            resources.validate()?;
        }
        for binding in &self.ui_http {
            binding.validate()?;
        }

        match self.surface.kind {
            PluginSurfaceKind::Ui => {
                if self.native_execution
                    || self.child_process
                    || !self.filesystem.is_empty()
                    || !self.network_egress.is_empty()
                    || self.private_service
                    || !self.secrets.is_empty()
                    || self.resources.is_some()
                {
                    return Err(permission_error(
                        "UI surfaces cannot request ambient execution, filesystem, network, secret, or resource authority.",
                    ));
                }
            }
            PluginSurfaceKind::Tool | PluginSurfaceKind::Mcp => {
                if !self.ui_http.is_empty() || self.resources.is_none() {
                    return Err(permission_error(
                        "Executable Tool and MCP surfaces require resource ceilings and cannot declare UI HTTP bindings.",
                    ));
                }
            }
            PluginSurfaceKind::Flow | PluginSurfaceKind::Okf | PluginSurfaceKind::Skill => {
                return Err(permission_error(
                    "Flow, OKF, and Skill surfaces cannot carry runtime permission ceilings.",
                ));
            }
        }

        if !self.native_execution
            && !self.child_process
            && self.filesystem.is_empty()
            && self.network_egress.is_empty()
            && !self.private_service
            && self.secrets.is_empty()
            && self.resources.is_none()
            && self.ui_http.is_empty()
        {
            return Err(permission_error(
                "Empty surface permission ceilings must be omitted.",
            ));
        }
        Ok(())
    }
}

impl ResourcePermissionCeiling {
    fn validate(&self) -> UseResult<()> {
        if self.cpu_millis == 0
            || self.cpu_millis > 1_000_000
            || self.memory_bytes == 0
            || self.memory_bytes > MAX_RESOURCE_BYTES
            || self.pids == 0
            || self.pids > 1_000_000
            || self.ephemeral_storage_bytes == 0
            || self.ephemeral_storage_bytes > MAX_RESOURCE_BYTES
            || self
                .task_timeout_ms
                .is_some_and(|value| value == 0 || value > MAX_TASK_TIMEOUT_MS)
            || self
                .max_stdout_bytes
                .is_some_and(|value| value == 0 || value > MAX_CAPTURE_BYTES)
            || self
                .max_stderr_bytes
                .is_some_and(|value| value == 0 || value > MAX_CAPTURE_BYTES)
        {
            return Err(permission_error(
                "Plugin resource permission ceilings are outside supported bounds.",
            ));
        }
        Ok(())
    }
}

impl UiHttpPermission {
    fn validate(&self) -> UseResult<()> {
        if !valid_segment(&self.tool_id)
            || self.methods.is_empty()
            || self.methods.len() > 16
            || !strictly_sorted_unique(&self.methods)
            || self.path_prefixes.is_empty()
            || self.path_prefixes.len() > 64
            || !strictly_sorted_unique(&self.path_prefixes)
            || self.path_prefixes.iter().any(|path| !valid_http_path(path))
        {
            return Err(permission_error(
                "A UI HTTP binding permission is invalid or noncanonical.",
            ));
        }
        Ok(())
    }
}

fn permission_error(message: impl Into<String>) -> crate::UseError {
    contract_error(PERMISSION_ERROR, message)
}

fn surface_is_within(
    granted: &SurfacePermissionCeiling,
    ceiling: &SurfacePermissionCeiling,
) -> bool {
    (!granted.native_execution || ceiling.native_execution)
        && (!granted.child_process || ceiling.child_process)
        && (!granted.private_service || ceiling.private_service)
        && granted
            .filesystem
            .iter()
            .all(|permission| filesystem_is_within(permission, &ceiling.filesystem))
        && granted
            .network_egress
            .iter()
            .all(|permission| network_is_within(permission, &ceiling.network_egress))
        && granted
            .secrets
            .iter()
            .all(|secret| ceiling.secrets.binary_search(secret).is_ok())
        && resources_are_within(granted.resources.as_ref(), ceiling.resources.as_ref())
        && granted
            .ui_http
            .iter()
            .all(|permission| ui_http_is_within(permission, &ceiling.ui_http))
}

fn filesystem_is_within(granted: &FilesystemPermission, ceiling: &[FilesystemPermission]) -> bool {
    ceiling.iter().any(|allowed| {
        granted.scope == allowed.scope
            && path_is_within(&granted.path, &allowed.path)
            && matches!(
                (granted.access, allowed.access),
                (FilesystemAccess::Read, _)
                    | (FilesystemAccess::ReadWrite, FilesystemAccess::ReadWrite)
            )
    })
}

fn network_is_within(
    granted: &NetworkEgressPermission,
    ceiling: &[NetworkEgressPermission],
) -> bool {
    granted.ports.iter().all(|port| {
        ceiling.iter().any(|allowed| {
            allowed.host == granted.host && allowed.ports.binary_search(port).is_ok()
        })
    })
}

fn resources_are_within(
    granted: Option<&ResourcePermissionCeiling>,
    ceiling: Option<&ResourcePermissionCeiling>,
) -> bool {
    match (granted, ceiling) {
        (None, _) => true,
        (Some(_), None) => false,
        (Some(granted), Some(ceiling)) => {
            granted.cpu_millis <= ceiling.cpu_millis
                && granted.memory_bytes <= ceiling.memory_bytes
                && granted.pids <= ceiling.pids
                && granted.ephemeral_storage_bytes <= ceiling.ephemeral_storage_bytes
                && optional_limit_is_within(granted.task_timeout_ms, ceiling.task_timeout_ms)
                && optional_limit_is_within(granted.max_stdout_bytes, ceiling.max_stdout_bytes)
                && optional_limit_is_within(granted.max_stderr_bytes, ceiling.max_stderr_bytes)
        }
    }
}

fn ui_http_is_within(granted: &UiHttpPermission, ceiling: &[UiHttpPermission]) -> bool {
    ceiling
        .iter()
        .find(|allowed| allowed.tool_id == granted.tool_id)
        .is_some_and(|allowed| {
            granted
                .methods
                .iter()
                .all(|method| allowed.methods.binary_search(method).is_ok())
                && granted.path_prefixes.iter().all(|path| {
                    allowed
                        .path_prefixes
                        .iter()
                        .any(|prefix| http_path_is_within(path, prefix))
                })
        })
}

fn optional_limit_is_within(granted: Option<u64>, ceiling: Option<u64>) -> bool {
    granted.is_none_or(|granted| ceiling.is_some_and(|ceiling| granted <= ceiling))
}

fn path_is_within(path: &str, parent: &str) -> bool {
    parent == "."
        || path == parent
        || path
            .strip_prefix(parent)
            .is_some_and(|suffix| suffix.starts_with('/'))
}

fn http_path_is_within(path: &str, prefix: &str) -> bool {
    prefix == "/"
        || path == prefix
        || path
            .strip_prefix(prefix)
            .is_some_and(|suffix| suffix.starts_with('/'))
}