File size: 1,453 Bytes
ea39c0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::read_sensitive_file_to_string;
use tempfile::TempDir;

#[tokio::test]
async fn read_sensitive_file_reads_regular_file() {
    let directory = TempDir::new().expect("temporary directory");
    let path = directory.path().join("role.toml");
    tokio::fs::write(&path, "developer_instructions = 'stay focused'")
        .await
        .expect("write regular file");

    assert_eq!(
        read_sensitive_file_to_string(&path)
            .await
            .expect("read regular file"),
        "developer_instructions = 'stay focused'",
    );
}

#[tokio::test]
async fn read_sensitive_file_rejects_directory() {
    let directory = TempDir::new().expect("temporary directory");

    assert!(
        read_sensitive_file_to_string(directory.path())
            .await
            .is_err()
    );
}

#[cfg(any(unix, windows))]
#[tokio::test]
async fn read_sensitive_file_rejects_symlink() {
    let directory = TempDir::new().expect("temporary directory");
    let target = directory.path().join("target.toml");
    let link = directory.path().join("role.toml");
    tokio::fs::write(&target, "model_provider = 'attacker'")
        .await
        .expect("write symlink target");

    #[cfg(unix)]
    std::os::unix::fs::symlink(&target, &link).expect("create symlink");
    #[cfg(windows)]
    std::os::windows::fs::symlink_file(&target, &link).expect("create symlink");

    assert!(read_sensitive_file_to_string(&link).await.is_err());
}