forge-policy 0.1.0

Workspace and database safety policy primitives for forge-engine
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
//! # forge-policy
//!
//! Filesystem, environment, and SQLite guardrails for internal forge runtimes.
//!
//! This Tier 3 crate centralizes low-level path safety, patch-cap checks,
//! allowlisting, and database identity validation. It is intentionally policy
//! infrastructure rather than an execution or storage layer.

use std::path::{Path, PathBuf};

#[derive(Debug, thiserror::Error)]
pub enum PolicyError {
    #[error("refuse to open database: {reason}")]
    RefuseToOpenDb { reason: String },

    #[error("invalid path: {0}")]
    InvalidPath(String),

    #[error("io error: {0}")]
    Io(#[from] std::io::Error),

    #[error("database error: {0}")]
    Database(#[from] rusqlite::Error),
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Violation {
    pub kind: ViolationKind,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ViolationKind {
    ForbiddenPath,
    CapExceeded,
    EmptyPatch,
    UselessEdit,
    DegenerateRange,
    InvalidOccurrence,
    DuplicatePath,
}

#[derive(Debug, Clone)]
pub struct DbIdentitySpec<'a> {
    pub min_user_version: u32,
    pub max_user_version: u32,
    pub required_tables: &'a [&'a str],
    pub schema_table: &'a str,
    pub schema_key: &'a str,
    pub expected_schema_hash: &'a str,
}

const ALLOWED_ENV_PREFIXES: &[&str] = &[
    "PATH",
    "HOME",
    "USER",
    "LOGNAME",
    "LANG",
    "LC_",
    "LANGUAGE",
    "TERM",
    "COLORTERM",
    "SHELL",
    "CARGO",
    "RUSTUP",
    "RUST",
    "XDG_",
    "TMPDIR",
    "TMP",
    "TEMP",
    "CC",
    "CXX",
    "LD_",
    "PKG_CONFIG",
    "SCCACHE",
    "CCACHE",
];

pub fn verify_sqlite_db_identity(
    path: &Path,
    spec: &DbIdentitySpec<'_>,
) -> Result<(), PolicyError> {
    if !path.exists() {
        return Ok(());
    }

    use std::io::Read;

    let mut file = std::fs::File::open(path).map_err(|error| PolicyError::RefuseToOpenDb {
        reason: format!("cannot read file: {error}"),
    })?;
    let mut magic = [0_u8; 16];
    let count = file
        .read(&mut magic)
        .map_err(|error| PolicyError::RefuseToOpenDb {
            reason: format!("cannot read magic bytes: {error}"),
        })?;
    if count < 16 || magic != *b"SQLite format 3\0" {
        return Err(PolicyError::RefuseToOpenDb {
            reason: "file is not a valid SQLite database".to_string(),
        });
    }

    let connection = rusqlite::Connection::open_with_flags(
        path,
        rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY | rusqlite::OpenFlags::SQLITE_OPEN_NO_MUTEX,
    )
    .map_err(|error| PolicyError::RefuseToOpenDb {
        reason: format!("cannot open database: {error}"),
    })?;

    let user_version: u32 = connection
        .pragma_query_value(None, "user_version", |row| row.get(0))
        .map_err(|error| PolicyError::RefuseToOpenDb {
            reason: format!("cannot read user_version: {error}"),
        })?;
    if !(spec.min_user_version..=spec.max_user_version).contains(&user_version) {
        return Err(PolicyError::RefuseToOpenDb {
            reason: format!(
                "user_version {user_version} is outside valid range [{}, {}]",
                spec.min_user_version, spec.max_user_version
            ),
        });
    }

    let has_schema_table: bool = connection
        .query_row(
            "SELECT COUNT(*) > 0 FROM sqlite_master WHERE type='table' AND name = ?1",
            [spec.schema_table],
            |row| row.get(0),
        )
        .map_err(|error| PolicyError::RefuseToOpenDb {
            reason: format!("cannot query sqlite_master: {error}"),
        })?;
    if !has_schema_table {
        return Err(PolicyError::RefuseToOpenDb {
            reason: format!("table '{}' does not exist", spec.schema_table),
        });
    }

    let query = format!("SELECT value FROM {} WHERE key = ?1", spec.schema_table);
    let stored_hash: String = connection
        .query_row(query.as_str(), [spec.schema_key], |row| row.get(0))
        .map_err(|_| PolicyError::RefuseToOpenDb {
            reason: format!("{} row '{}' not found", spec.schema_table, spec.schema_key),
        })?;
    if stored_hash != spec.expected_schema_hash {
        return Err(PolicyError::RefuseToOpenDb {
            reason: format!(
                "schema_hash mismatch: stored={stored_hash}, expected={}",
                spec.expected_schema_hash
            ),
        });
    }

    let existing_tables: Vec<String> = connection
        .prepare("SELECT name FROM sqlite_master WHERE type='table'")
        .map_err(|error| PolicyError::RefuseToOpenDb {
            reason: format!("cannot query sqlite_master for tables: {error}"),
        })?
        .query_map([], |row| row.get(0))
        .map_err(|error| PolicyError::RefuseToOpenDb {
            reason: format!("cannot iterate tables: {error}"),
        })?
        .filter_map(|row| row.ok())
        .collect();

    for table in spec.required_tables {
        if !existing_tables.iter().any(|existing| existing == table) {
            return Err(PolicyError::RefuseToOpenDb {
                reason: format!("required table '{}' missing from database", table),
            });
        }
    }

    Ok(())
}

pub fn ensure_relative_path(path: &Path) -> Result<(), PolicyError> {
    if path.is_absolute() {
        return Err(PolicyError::InvalidPath(format!(
            "path '{}' is absolute",
            path.display()
        )));
    }

    for component in path.components() {
        if component == std::path::Component::ParentDir {
            return Err(PolicyError::InvalidPath(format!(
                "path '{}' contains '..'",
                path.display()
            )));
        }
    }

    Ok(())
}

pub fn reject_symlinks(root: &Path, relative_path: &Path) -> Result<(), PolicyError> {
    let mut current = root.to_path_buf();
    for component in relative_path.components() {
        current.push(component);
        if current.exists() {
            let metadata = std::fs::symlink_metadata(&current)?;
            if metadata.file_type().is_symlink() {
                return Err(PolicyError::InvalidPath(format!(
                    "symlink detected at {}",
                    current.display()
                )));
            }
        }
    }
    Ok(())
}

pub fn resolve_workspace_path(root: &Path, relative_path: &Path) -> Result<PathBuf, PolicyError> {
    ensure_relative_path(relative_path)?;
    reject_symlinks(root, relative_path)?;

    let full_path = root.join(relative_path);
    if let Ok(root_canonical) = root.canonicalize() {
        if let Some(parent) = full_path.parent() {
            if parent.exists() {
                let parent_canonical = parent.canonicalize()?;
                if !parent_canonical.starts_with(&root_canonical) {
                    return Err(PolicyError::InvalidPath(format!(
                        "resolved path '{}' escapes workspace '{}'",
                        parent_canonical.display(),
                        root_canonical.display()
                    )));
                }
            }
        }
    }

    Ok(full_path)
}

pub fn validate_forbidden_paths(
    paths: &[String],
    forbidden_patterns: &[String],
    allow_test_modifications: bool,
) -> Vec<Violation> {
    let hardcoded_denies: &[&str] = &[
        "tests/**",
        "**/*_test.rs",
        "**/fixtures/**",
        "**/*.snap",
        "Cargo.lock",
    ];

    let mut violations = Vec::new();
    for path in paths {
        if !allow_test_modifications {
            for pattern in hardcoded_denies {
                if glob_matches(pattern, path) {
                    violations.push(Violation {
                        kind: ViolationKind::ForbiddenPath,
                        message: format!(
                            "path '{}' matches hardcoded forbidden pattern '{}'",
                            path, pattern
                        ),
                    });
                    break;
                }
            }
        }

        for pattern in forbidden_patterns {
            if !allow_test_modifications && hardcoded_denies.contains(&pattern.as_str()) {
                continue;
            }
            if glob_matches(pattern, path) {
                violations.push(Violation {
                    kind: ViolationKind::ForbiddenPath,
                    message: format!("path '{}' matches forbidden pattern '{}'", path, pattern),
                });
                break;
            }
        }
    }

    violations
}

pub fn validate_patch_caps(
    files_changed: usize,
    total_lines_changed: usize,
    per_file_lines: &[usize],
    max_files: usize,
    max_total_lines: usize,
    max_per_file: usize,
) -> Vec<Violation> {
    let mut violations = Vec::new();

    if files_changed > max_files {
        violations.push(Violation {
            kind: ViolationKind::CapExceeded,
            message: format!("files changed ({files_changed}) exceeds cap ({max_files})"),
        });
    }

    if total_lines_changed > max_total_lines {
        violations.push(Violation {
            kind: ViolationKind::CapExceeded,
            message: format!(
                "total lines changed ({total_lines_changed}) exceeds cap ({max_total_lines})"
            ),
        });
    }

    for (index, lines) in per_file_lines.iter().enumerate() {
        if *lines > max_per_file {
            violations.push(Violation {
                kind: ViolationKind::CapExceeded,
                message: format!(
                    "file {index}: lines changed ({lines}) exceeds per-file cap ({max_per_file})"
                ),
            });
        }
    }

    violations
}

pub fn is_env_allowed(key: &str) -> bool {
    ALLOWED_ENV_PREFIXES
        .iter()
        .any(|prefix| key.starts_with(prefix))
}

fn glob_matches(pattern: &str, path: &str) -> bool {
    match glob::Pattern::new(pattern) {
        Ok(glob) => glob.matches(path),
        Err(error) => {
            tracing::warn!("invalid glob pattern '{}': {}", pattern, error);
            false
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;
    use std::path::Path;

    #[test]
    fn path_guard_rejects_absolute_and_parent_components() {
        assert!(ensure_relative_path(Path::new("src/lib.rs")).is_ok());
        assert!(ensure_relative_path(Path::new("/tmp/nope")).is_err());
        assert!(ensure_relative_path(Path::new("../nope")).is_err());
    }

    #[test]
    fn path_guard_rejects_multiple_escape_shapes() {
        for candidate in [
            "/tmp/escape",
            "../escape",
            "nested/../../escape",
            "./../escape",
        ] {
            assert!(
                ensure_relative_path(Path::new(candidate)).is_err(),
                "expected '{candidate}' to be rejected"
            );
        }
    }

    #[cfg(unix)]
    #[test]
    fn reject_symlinks_blocks_existing_symlink_segments() {
        let root = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        std::os::unix::fs::symlink(outside.path(), root.path().join("linked")).unwrap();

        let error = reject_symlinks(root.path(), Path::new("linked/escape.txt")).unwrap_err();
        assert!(matches!(error, PolicyError::InvalidPath(_)));
        assert!(error.to_string().contains("symlink detected"));
    }

    #[cfg(unix)]
    #[test]
    fn resolve_workspace_path_rejects_symlinked_parent_escape() {
        let root = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        std::fs::create_dir_all(root.path().join("dir")).unwrap();
        std::os::unix::fs::symlink(outside.path(), root.path().join("dir/link")).unwrap();

        let error =
            resolve_workspace_path(root.path(), Path::new("dir/link/outside.txt")).unwrap_err();
        assert!(matches!(error, PolicyError::InvalidPath(_)));
    }

    #[test]
    fn env_allowlist_matches_expected_prefixes() {
        assert!(is_env_allowed("CARGO_HOME"));
        assert!(is_env_allowed("PATH"));
        assert!(!is_env_allowed("AWS_SECRET_ACCESS_KEY"));
    }

    proptest! {
        #[test]
        fn relative_path_guard_rejects_parent_escape_shapes(path_tail in "[A-Za-z0-9_./-]{1,24}") {
            for candidate in [
                format!("../{path_tail}"),
                format!("nested/../../{path_tail}"),
                format!("./../{path_tail}"),
            ] {
                prop_assert!(ensure_relative_path(Path::new(&candidate)).is_err());
            }
        }

        #[test]
        fn relative_path_guard_accepts_safe_relative_shapes(path_tail in "[A-Za-z0-9_./-]{1,24}") {
            prop_assume!(!path_tail.starts_with('/'));
            prop_assume!(!path_tail.contains(".."));
            let path = format!("safe/{path_tail}");
            prop_assert!(ensure_relative_path(Path::new(&path)).is_ok());
        }
    }
}