clawdstrike 0.2.5

Security guards and policy engine for AI agent execution
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Sparse registry index client for fetching package metadata.
//!
//! Follows a pattern similar to `remote_extends.rs` for content-addressed HTTP
//! caching with ETag-based revalidation.

use std::collections::BTreeMap;
use std::fs;
use std::io::Read;
use std::path::PathBuf;
use std::time::Duration;

use serde::{Deserialize, Serialize};

use super::manifest::PkgType;
use super::{encode_url_path_segment, normalize_package_name};
use crate::error::{Error, Result};

/// Default registry base URL.
pub const DEFAULT_REGISTRY_URL: &str = "https://registry.clawdstrike.dev";
/// Maximum accepted sparse-index response body size (8 MiB).
const MAX_INDEX_RESPONSE_BYTES: u64 = 8 * 1024 * 1024;

/// A sparse-index client that fetches and caches package metadata from a remote
/// registry.
#[derive(Clone, Debug)]
pub struct RegistryIndex {
    /// Registry base URL (e.g. `https://registry.clawdstrike.dev`).
    base_url: String,
    /// Local cache directory for index entries.
    cache_dir: PathBuf,
    /// Shared blocking client for connection/TLS reuse across fetches.
    http_client: reqwest::blocking::Client,
}

/// All known versions for a single package.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PackageIndexEntry {
    /// Package name.
    pub name: String,
    /// Available versions in chronological order.
    pub versions: Vec<IndexVersion>,
}

/// A single version record within a package index entry.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct IndexVersion {
    /// Semver version string.
    pub version: String,
    /// Content-address checksum (`sha256:<hex>`).
    pub checksum: String,
    /// Dependencies: name -> version constraint string.
    #[serde(default)]
    pub dependencies: BTreeMap<String, String>,
    /// Whether this version has been yanked.
    #[serde(default)]
    pub yanked: bool,
    /// Package type.
    pub pkg_type: PkgType,
}

/// Cached ETag metadata for a given index entry.
#[derive(Clone, Debug, Serialize, Deserialize)]
struct CacheMetadata {
    etag: Option<String>,
}

impl RegistryIndex {
    /// Create a new index client with default cache directory
    /// (`~/.clawdstrike/registry-cache/index/`).
    pub fn new(base_url: &str) -> Result<Self> {
        let home = dirs::home_dir()
            .ok_or_else(|| Error::PkgError("cannot determine home directory".to_string()))?;
        let cache_dir = home
            .join(".clawdstrike")
            .join("registry-cache")
            .join("index");
        Self::with_cache_dir(base_url, cache_dir)
    }

    /// Create an index client with a custom cache directory.
    pub fn with_cache_dir(base_url: &str, cache_dir: PathBuf) -> Result<Self> {
        fs::create_dir_all(&cache_dir)?;
        // Construct the blocking client outside any active Tokio runtime to
        // avoid reqwest's blocking-runtime panic.
        let http_client = run_blocking_http(build_blocking_client)?;
        Ok(Self {
            base_url: base_url.trim_end_matches('/').to_string(),
            cache_dir,
            http_client,
        })
    }

    /// Return the base URL of the registry.
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// Fetch (or return cached) version information for a package.
    ///
    /// The index follows a sparse layout where package metadata lives at:
    /// `{base_url}/api/v1/index/{normalized_name}`
    ///
    /// HTTP ETag headers are used for cache revalidation.
    pub fn fetch_package_versions(&self, name: &str) -> Result<PackageIndexEntry> {
        let normalized = normalize_package_name(name);
        let cache_path = self.cache_dir.join(format!("{normalized}.json"));
        let meta_path = self.cache_dir.join(format!("{normalized}.meta.json"));

        // Read cached ETag if available.
        let cached_etag = fs::read_to_string(&meta_path)
            .ok()
            .and_then(|s| serde_json::from_str::<CacheMetadata>(&s).ok())
            .and_then(|m| m.etag);

        let url = format!(
            "{}/api/v1/index/{}",
            self.base_url,
            encode_url_path_segment(name)
        );
        let package_name = name.to_string();
        let client = self.http_client.clone();

        let (status, etag, body) = run_blocking_http(move || {
            let mut request = client.get(&url);
            if let Some(etag) = cached_etag.as_deref() {
                request = request.header("If-None-Match", etag);
            }

            let mut response = request.send().map_err(|e| {
                Error::PkgError(format!(
                    "failed to fetch index for '{}': {}",
                    package_name, e
                ))
            })?;

            let status = response.status();
            let etag = response
                .headers()
                .get("etag")
                .and_then(|v: &reqwest::header::HeaderValue| v.to_str().ok())
                .map(String::from);

            let body = if status == reqwest::StatusCode::NOT_MODIFIED {
                None
            } else {
                if response
                    .content_length()
                    .is_some_and(|len| len > MAX_INDEX_RESPONSE_BYTES)
                {
                    return Err(Error::PkgError(format!(
                        "index body for '{}' exceeds limit ({} bytes)",
                        package_name, MAX_INDEX_RESPONSE_BYTES
                    )));
                }
                Some(read_utf8_body_limited(
                    &mut response,
                    MAX_INDEX_RESPONSE_BYTES,
                    &package_name,
                )?)
            };

            Ok((status, etag, body))
        })?;

        // 304 Not Modified — use cached version.
        if status == reqwest::StatusCode::NOT_MODIFIED {
            let cached = fs::read_to_string(&cache_path).map_err(|e| {
                Error::PkgError(format!(
                    "cache hit (304) but failed to read cached index for '{}': {}",
                    name, e
                ))
            })?;
            let entry: PackageIndexEntry = serde_json::from_str(&cached)?;
            return Ok(entry);
        }

        if status == reqwest::StatusCode::NOT_FOUND {
            return Err(Error::PkgError(format!(
                "package '{}' not found in registry",
                name
            )));
        }

        if !status.is_success() {
            return Err(Error::PkgError(format!(
                "registry returned HTTP {} for package '{}'",
                status, name
            )));
        }

        let body = body.ok_or_else(|| {
            Error::PkgError(format!(
                "registry returned success without body for package '{}'",
                name
            ))
        })?;

        // Validate JSON before caching.
        let entry: PackageIndexEntry = serde_json::from_str(&body)
            .map_err(|e| Error::PkgError(format!("invalid index JSON for '{}': {}", name, e)))?;

        // Write cache.
        fs::write(&cache_path, &body)?;
        let meta = CacheMetadata { etag };
        let meta_json = serde_json::to_string(&meta)
            .map_err(|e| Error::PkgError(format!("failed to serialize cache metadata: {}", e)))?;
        fs::write(&meta_path, meta_json)?;

        Ok(entry)
    }

    /// Invalidate the cached index entry for a package.
    pub fn invalidate_cache(&self, name: &str) -> Result<()> {
        let normalized = normalize_package_name(name);
        let cache_path = self.cache_dir.join(format!("{normalized}.json"));
        let meta_path = self.cache_dir.join(format!("{normalized}.meta.json"));
        let _ = fs::remove_file(&cache_path);
        let _ = fs::remove_file(&meta_path);
        Ok(())
    }
}

/// Build a blocking HTTP client suitable for registry communication.
fn build_blocking_client() -> Result<reqwest::blocking::Client> {
    reqwest::blocking::Client::builder()
        .timeout(Duration::from_secs(30))
        .user_agent(format!("clawdstrike-pkg/{}", env!("CARGO_PKG_VERSION")))
        .build()
        .map_err(|e| Error::PkgError(format!("failed to build HTTP client: {}", e)))
}

fn read_utf8_body_limited<R: Read>(
    mut reader: R,
    max_bytes: u64,
    package_name: &str,
) -> Result<String> {
    let mut bytes = Vec::new();
    let mut limited = reader.by_ref().take(max_bytes + 1);
    limited.read_to_end(&mut bytes).map_err(|e| {
        Error::PkgError(format!(
            "failed to read index body for '{}': {}",
            package_name, e
        ))
    })?;

    if bytes.len() as u64 > max_bytes {
        return Err(Error::PkgError(format!(
            "index body for '{}' exceeds limit ({} bytes)",
            package_name, max_bytes
        )));
    }

    String::from_utf8(bytes).map_err(|e| {
        Error::PkgError(format!(
            "index body for '{}' is not valid UTF-8: {}",
            package_name, e
        ))
    })
}

fn run_blocking_http<T, F>(f: F) -> Result<T>
where
    T: Send + 'static,
    F: FnOnce() -> Result<T> + Send + 'static,
{
    if tokio::runtime::Handle::try_current().is_ok() {
        std::thread::spawn(f)
            .join()
            .map_err(|_| Error::PkgError("blocking HTTP worker panicked".to_string()))?
    } else {
        f()
    }
}

// ---------------------------------------------------------------------------
// In-memory mock for testing
// ---------------------------------------------------------------------------

/// An in-memory index for testing that doesn't require network access.
#[derive(Clone, Debug, Default)]
pub struct MockRegistryIndex {
    entries: BTreeMap<String, PackageIndexEntry>,
}

impl MockRegistryIndex {
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a package with its versions.
    pub fn add_entry(&mut self, entry: PackageIndexEntry) {
        self.entries.insert(entry.name.clone(), entry);
    }

    /// Add a single version to a package (creates the entry if needed).
    pub fn add_version(
        &mut self,
        name: &str,
        version: &str,
        checksum: &str,
        pkg_type: PkgType,
        dependencies: BTreeMap<String, String>,
        yanked: bool,
    ) {
        let entry = self
            .entries
            .entry(name.to_string())
            .or_insert_with(|| PackageIndexEntry {
                name: name.to_string(),
                versions: Vec::new(),
            });
        entry.versions.push(IndexVersion {
            version: version.to_string(),
            checksum: checksum.to_string(),
            dependencies,
            yanked,
            pkg_type,
        });
    }

    /// Look up a package's index entry.
    pub fn fetch_package_versions(&self, name: &str) -> Result<PackageIndexEntry> {
        self.entries
            .get(name)
            .cloned()
            .ok_or_else(|| Error::PkgError(format!("package '{}' not found in index", name)))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mock_index_add_and_fetch() {
        let mut index = MockRegistryIndex::new();
        index.add_version(
            "my-guard",
            "1.0.0",
            "sha256:aabb",
            PkgType::Guard,
            BTreeMap::new(),
            false,
        );
        index.add_version(
            "my-guard",
            "1.1.0",
            "sha256:ccdd",
            PkgType::Guard,
            BTreeMap::new(),
            false,
        );

        let entry = index.fetch_package_versions("my-guard").unwrap();
        assert_eq!(entry.name, "my-guard");
        assert_eq!(entry.versions.len(), 2);
        assert_eq!(entry.versions[0].version, "1.0.0");
        assert_eq!(entry.versions[1].version, "1.1.0");
    }

    #[test]
    fn mock_index_not_found() {
        let index = MockRegistryIndex::new();
        let err = index.fetch_package_versions("missing").unwrap_err();
        assert!(err.to_string().contains("not found"));
    }

    #[test]
    fn mock_index_with_dependencies() {
        let mut index = MockRegistryIndex::new();
        let mut deps = BTreeMap::new();
        deps.insert("base-guard".to_string(), "^1.0.0".to_string());

        index.add_version(
            "my-guard",
            "2.0.0",
            "sha256:eeff",
            PkgType::Guard,
            deps,
            false,
        );

        let entry = index.fetch_package_versions("my-guard").unwrap();
        let v = &entry.versions[0];
        assert_eq!(v.dependencies.len(), 1);
        assert_eq!(v.dependencies["base-guard"], "^1.0.0");
    }

    #[test]
    fn mock_index_yanked() {
        let mut index = MockRegistryIndex::new();
        index.add_version(
            "old-pkg",
            "1.0.0",
            "sha256:1111",
            PkgType::Guard,
            BTreeMap::new(),
            true,
        );

        let entry = index.fetch_package_versions("old-pkg").unwrap();
        assert!(entry.versions[0].yanked);
    }

    #[test]
    fn normalize_scoped_name() {
        assert_eq!(
            normalize_package_name("@acme/firewall"),
            "s--acme%2Ffirewall"
        );
        assert_eq!(normalize_package_name("simple-name"), "u--simple-name");
        assert_eq!(normalize_package_name("pkg%v1"), "u--pkg%25v1");
        assert_ne!(
            normalize_package_name("@acme/foo"),
            normalize_package_name("acme--foo")
        );
        assert_ne!(
            normalize_package_name("@a--b/c"),
            normalize_package_name("@a/b--c")
        );
    }

    #[test]
    fn index_version_serde_roundtrip() {
        let mut deps = BTreeMap::new();
        deps.insert("dep-a".to_string(), "^1.0".to_string());

        let iv = IndexVersion {
            version: "1.2.3".to_string(),
            checksum: "sha256:abcdef".to_string(),
            dependencies: deps,
            yanked: false,
            pkg_type: PkgType::Guard,
        };

        let json = serde_json::to_string(&iv).unwrap();
        let parsed: IndexVersion = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, iv);
    }

    #[test]
    fn package_index_entry_serde_roundtrip() {
        let entry = PackageIndexEntry {
            name: "test-pkg".to_string(),
            versions: vec![IndexVersion {
                version: "0.1.0".to_string(),
                checksum: "sha256:000".to_string(),
                dependencies: BTreeMap::new(),
                yanked: false,
                pkg_type: PkgType::PolicyPack,
            }],
        };

        let json = serde_json::to_string_pretty(&entry).unwrap();
        let parsed: PackageIndexEntry = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, entry);
    }

    #[test]
    fn registry_index_construction_is_runtime_safe() {
        let tmp = tempfile::tempdir().unwrap();
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();

        let result = runtime.block_on(async {
            RegistryIndex::with_cache_dir(DEFAULT_REGISTRY_URL, tmp.path().to_path_buf())
        });

        assert!(result.is_ok());
    }

    #[test]
    fn read_utf8_body_limited_rejects_oversized_payload() {
        let bytes = vec![b'a'; 11];
        let err = read_utf8_body_limited(bytes.as_slice(), 10, "demo").unwrap_err();
        assert!(err.to_string().contains("exceeds limit"));
    }

    #[test]
    fn read_utf8_body_limited_accepts_valid_utf8() {
        let body = read_utf8_body_limited(br#"{"name":"demo"}"#.as_slice(), 64, "demo").unwrap();
        assert_eq!(body, r#"{"name":"demo"}"#);
    }
}