nono-cli 0.61.1

CLI for nono capability-based sandbox
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
//! CLI-specific query extensions for the sandbox
//!
//! This module provides query functions and output formatting for the
//! `nono why` command.

use crate::config;
use colored::Colorize;
use nono::{AccessMode, CapabilitySet, Result, try_canonicalize};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

/// Structured description of the capability that matched or nearly matched
/// a query.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CapabilityMatch {
    /// Granted path for the capability.
    pub path: String,
    /// Granted access mode.
    pub access: String,
    /// Capability source such as user, profile, group:<name>, or system.
    pub source: String,
}

/// Scope type for Landlock scope policy queries.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScopeQuery {
    /// `LANDLOCK_SCOPE_SIGNAL`.
    Signal,
    /// `LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET`.
    AbstractUnixSocket,
}

impl ScopeQuery {
    fn as_str(self) -> &'static str {
        match self {
            ScopeQuery::Signal => "signal",
            ScopeQuery::AbstractUnixSocket => "abstract-unix-socket",
        }
    }
}

/// Result of querying whether an operation is permitted
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "status")]
pub enum QueryResult {
    /// The operation is allowed
    #[serde(rename = "allowed")]
    Allowed {
        reason: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        granted_path: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        access: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        source: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        endpoint_rules: Option<Vec<crate::sandbox_state::EndpointRuleState>>,
    },
    /// The operation is denied
    #[serde(rename = "denied")]
    Denied {
        reason: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        details: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        policy_source: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        matching_capability: Option<CapabilityMatch>,
        #[serde(skip_serializing_if = "Option::is_none")]
        suggested_flag: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        endpoint_rules: Option<Vec<crate::sandbox_state::EndpointRuleState>>,
    },
    /// Not running inside a sandbox
    #[serde(rename = "not_sandboxed")]
    NotSandboxed { message: String },
    /// Landlock scope policy status.
    #[serde(rename = "scope")]
    Scope {
        scope: String,
        state: String,
        requested: bool,
        enforced: bool,
        supported: bool,
        #[serde(skip_serializing_if = "Option::is_none")]
        kernel_abi: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        details: Option<String>,
    },
}

/// Query whether a path operation is permitted
///
/// `overridden_paths` contains canonicalized paths that have been exempted from
/// deny groups via `bypass_protection`. The sensitive-path check is skipped for any
/// query path that is equal to or a child of an overridden path.
pub fn query_path(
    path: &Path,
    requested: AccessMode,
    caps: &CapabilitySet,
    overridden_paths: &[std::path::PathBuf],
) -> Result<QueryResult> {
    // Canonicalize the path for proper comparison using ancestor-walk fallback
    // so that macOS symlinks (/tmp → /private/tmp) are resolved correctly even
    // when the leaf path doesn't exist yet.
    let canonical = try_canonicalize(path);

    // Check if this path is covered by a bypass_protection exemption
    let is_overridden = overridden_paths
        .iter()
        .any(|op| canonical == *op || canonical.starts_with(op));

    // Check if this is a sensitive path (CLI security policy), but skip
    // the check for paths that have been explicitly overridden.
    if !is_overridden
        && let Some(matched) = config::check_sensitive_path(&canonical.to_string_lossy())?
    {
        return Ok(QueryResult::Denied {
            reason: "sensitive_path".to_string(),
            details: Some(format!(
                "Blocked by policy group '{}': {} Use filesystem.bypass_protection to exempt specific paths when appropriate.",
                matched.group_name, matched.description
            )),
            policy_source: Some(format!("group:{}", matched.group_name)),
            matching_capability: None,
            suggested_flag: None,
            endpoint_rules: None,
        });
    }

    // Check capabilities. Prefer the most specific matching grant so broad system
    // reads (e.g. /private on macOS) do not shadow explicit user grants.
    let mut best_covering: Option<&nono::FsCapability> = None;
    let mut best_sufficient: Option<&nono::FsCapability> = None;
    let mut best_covering_score = 0usize;
    let mut best_sufficient_score = 0usize;

    for cap in caps.fs_capabilities() {
        let covers = if cap.is_file {
            cap.resolved == canonical
        } else {
            canonical.starts_with(&cap.resolved)
        };

        if !covers {
            continue;
        }

        let score = cap.resolved.as_os_str().len();
        if score >= best_covering_score {
            best_covering = Some(cap);
            best_covering_score = score;
        }

        let sufficient = matches!(
            (cap.access, requested),
            (AccessMode::ReadWrite, _)
                | (AccessMode::Read, AccessMode::Read)
                | (AccessMode::Write, AccessMode::Write)
        );

        if sufficient && score >= best_sufficient_score {
            best_sufficient = Some(cap);
            best_sufficient_score = score;
        }
    }

    if let Some(cap) = best_sufficient {
        return Ok(QueryResult::Allowed {
            reason: "granted_path".to_string(),
            granted_path: Some(cap.resolved.display().to_string()),
            access: Some(cap.access.to_string()),
            source: Some(cap.source.to_string()),
            endpoint_rules: None,
        });
    }

    if let Some(cap) = best_covering {
        return Ok(QueryResult::Denied {
            reason: "insufficient_access".to_string(),
            details: Some(format!(
                "Path is covered by '{}', which grants {} access from {} but {} was requested",
                cap.resolved.display(),
                cap.access,
                cap.source,
                requested
            )),
            policy_source: None,
            matching_capability: Some(CapabilityMatch {
                path: cap.resolved.display().to_string(),
                access: cap.access.to_string(),
                source: cap.source.to_string(),
            }),
            suggested_flag: Some(suggested_flag_for_path(&canonical, requested)),
            endpoint_rules: None,
        });
    }

    Ok(QueryResult::Denied {
        reason: "path_not_granted".to_string(),
        details: Some(format!(
            "Path is not covered by any capability: {}",
            canonical.display()
        )),
        policy_source: None,
        matching_capability: None,
        suggested_flag: Some(suggested_flag_for_path(&canonical, requested)),
        endpoint_rules: None,
    })
}

/// Query whether network access is permitted.
///
/// `allowed_domains` contains the resolved proxy allowlist (from profile
/// `allow_domain`, network profile hosts, and CLI `--allow-domain`).
/// When the network mode is `ProxyOnly`, delegates to `HostFilter` for
/// consistent matching with the proxy (including cloud metadata deny list).
pub fn query_network(
    host: &str,
    port: u16,
    caps: &CapabilitySet,
    allowed_domains: &[String],
    domain_endpoints: &[crate::sandbox_state::DomainEndpointState],
) -> QueryResult {
    let (domain, url_path) = parse_host_input(host);

    match caps.network_mode() {
        nono::NetworkMode::Blocked => QueryResult::Denied {
            reason: "network_blocked".to_string(),
            details: Some(format!(
                "Network access is fully blocked. Connection to {}:{} would be denied.",
                domain, port
            )),
            policy_source: None,
            matching_capability: None,
            suggested_flag: Some(format!("--allow-domain {}", domain)),
            endpoint_rules: None,
        },
        nono::NetworkMode::ProxyOnly { .. } => {
            let filter = if allowed_domains.is_empty() {
                nono::net_filter::HostFilter::allow_all()
            } else {
                nono::net_filter::HostFilter::new(allowed_domains)
            };
            // Pass empty IPs: DNS resolution happens at proxy time, not query time.
            match filter.check_host(&domain, &[]) {
                nono::net_filter::FilterResult::Allow => {
                    let matching_endpoints = domain_endpoints
                        .iter()
                        .find(|de| de.domain.eq_ignore_ascii_case(&domain));

                    match (matching_endpoints, &url_path) {
                        (Some(de), Some(path)) => {
                            if path_matches_endpoint_rules(path, &de.endpoints) {
                                QueryResult::Allowed {
                                    reason: "proxy_allowed".to_string(),
                                    granted_path: None,
                                    access: Some(format!(
                                        "Connection to {}:{} allowed via proxy \
                                         (path {} matches endpoint rules)",
                                        domain, port, path,
                                    )),
                                    source: Some("domain allowlist".to_string()),
                                    endpoint_rules: Some(de.endpoints.clone()),
                                }
                            } else {
                                QueryResult::Denied {
                                    reason: "endpoint_restricted".to_string(),
                                    details: Some(format!(
                                        "{} is allowed but {} does not match any endpoint rule",
                                        domain, path,
                                    )),
                                    policy_source: Some("endpoint rules".to_string()),
                                    matching_capability: None,
                                    suggested_flag: Some(format!(
                                        "--allow-domain https://{}{}",
                                        domain, path,
                                    )),
                                    endpoint_rules: Some(de.endpoints.clone()),
                                }
                            }
                        }
                        (Some(de), None) => QueryResult::Allowed {
                            reason: "proxy_allowed".to_string(),
                            granted_path: None,
                            access: Some(format!(
                                "Connection to {}:{} allowed via proxy \
                                 (restricted to {} endpoint rules)",
                                domain,
                                port,
                                de.endpoints.len(),
                            )),
                            source: Some("domain allowlist".to_string()),
                            endpoint_rules: Some(de.endpoints.clone()),
                        },
                        (None, _) => QueryResult::Allowed {
                            reason: "proxy_allowed".to_string(),
                            granted_path: None,
                            access: Some(format!(
                                "Connection to {}:{} would be allowed via proxy{}",
                                domain,
                                port,
                                if allowed_domains.is_empty() {
                                    " (no domain filter)"
                                } else {
                                    ""
                                }
                            )),
                            source: Some(if allowed_domains.is_empty() {
                                "proxy".to_string()
                            } else {
                                "domain allowlist".to_string()
                            }),
                            endpoint_rules: None,
                        },
                    }
                }
                deny => QueryResult::Denied {
                    reason: "proxy_filtered".to_string(),
                    details: Some(format!("Domain filtering is active. {}", deny.reason())),
                    policy_source: Some("proxy domain filter".to_string()),
                    matching_capability: None,
                    suggested_flag: Some(format!("--allow-domain {}", domain)),
                    endpoint_rules: None,
                },
            }
        }
        nono::NetworkMode::AllowAll => QueryResult::Allowed {
            reason: "network_allowed".to_string(),
            granted_path: None,
            access: Some(format!(
                "Connection to {}:{} would be allowed",
                domain, port
            )),
            source: None,
            endpoint_rules: None,
        },
    }
}

/// Parse a `--host` input that may be a bare hostname or a full URL.
/// Returns `(domain, optional_path)`.
fn parse_host_input(input: &str) -> (String, Option<String>) {
    if let Ok(parsed) = url::Url::parse(input) {
        let domain = parsed.host_str().unwrap_or(input).to_lowercase();
        let path = parsed.path();
        let url_path = if path.is_empty() || path == "/" {
            None
        } else {
            Some(path.to_string())
        };
        (domain, url_path)
    } else {
        (input.to_lowercase(), None)
    }
}

/// Normalize a URL path for endpoint rule matching (mirrors proxy behavior).
fn normalize_path(path: &str) -> String {
    let path = path.split('?').next().unwrap_or(path);
    let binary = urlencoding::decode_binary(path.as_bytes());
    let decoded = String::from_utf8_lossy(&binary);
    let segments: Vec<&str> = decoded.split('/').filter(|s| !s.is_empty()).collect();
    if segments.is_empty() {
        "/".to_string()
    } else {
        format!("/{}", segments.join("/"))
    }
}

/// Check if a path matches any endpoint rule using glob matching.
fn path_matches_endpoint_rules(
    path: &str,
    rules: &[crate::sandbox_state::EndpointRuleState],
) -> bool {
    if rules.is_empty() {
        return true;
    }
    let normalized = normalize_path(path);
    rules.iter().any(|r| {
        let Ok(glob) = globset::Glob::new(&r.path) else {
            return false;
        };
        let matcher = glob.compile_matcher();
        matcher.is_match(&normalized)
    })
}

/// Query whether a Landlock scope is requested and enforced.
#[cfg(target_os = "linux")]
pub fn query_scope(scope: ScopeQuery, caps: &CapabilitySet) -> QueryResult {
    match nono::landlock_scope_policy(caps) {
        Ok(policy) => {
            let (requested, enforced) = match scope {
                ScopeQuery::Signal => (policy.signal_requested, policy.signal_enforced),
                ScopeQuery::AbstractUnixSocket => (
                    policy.abstract_unix_socket_requested,
                    policy.abstract_unix_socket_enforced,
                ),
            };
            QueryResult::Scope {
                scope: scope.as_str().to_string(),
                state: scope_state(requested, enforced, policy.scoping_supported).to_string(),
                requested,
                enforced,
                supported: policy.scoping_supported,
                kernel_abi: Some(policy.abi_version.to_string()),
                details: Some(scope_details(
                    scope,
                    requested,
                    enforced,
                    policy.scoping_supported,
                )),
            }
        }
        Err(err) => QueryResult::Scope {
            scope: scope.as_str().to_string(),
            state: "unavailable".to_string(),
            requested: false,
            enforced: false,
            supported: false,
            kernel_abi: None,
            details: Some(format!(
                "Landlock scope policy could not be resolved: {err}"
            )),
        },
    }
}

/// Query whether a Landlock scope is requested and enforced.
#[cfg(not(target_os = "linux"))]
pub fn query_scope(scope: ScopeQuery, _caps: &CapabilitySet) -> QueryResult {
    QueryResult::Scope {
        scope: scope.as_str().to_string(),
        state: "not_applicable".to_string(),
        requested: false,
        enforced: false,
        supported: false,
        kernel_abi: None,
        details: Some("Landlock scope queries are only available on Linux.".to_string()),
    }
}

#[cfg(target_os = "linux")]
fn scope_state(requested: bool, enforced: bool, supported: bool) -> &'static str {
    match (requested, enforced, supported) {
        (true, true, _) => "enforced",
        (true, false, false) => "unsupported",
        (true, false, true) => "not_enforced",
        (false, _, _) => "not_requested",
    }
}

#[cfg(target_os = "linux")]
fn scope_details(scope: ScopeQuery, requested: bool, enforced: bool, supported: bool) -> String {
    let label = scope.as_str();
    match (requested, enforced, supported) {
        (true, true, _) => {
            format!("{label} scope is requested by the capability set and enforced.")
        }
        (true, false, false) => {
            format!("{label} scope is requested, but this Landlock ABI does not support scoping.")
        }
        (true, false, true) => {
            format!("{label} scope is requested, but it is not enforced.")
        }
        (false, _, true) => format!("{label} scope is not requested by the capability set."),
        (false, _, false) => {
            format!("{label} scope is not requested; this Landlock ABI has no scope support.")
        }
    }
}

/// Print a query result in human-readable format
pub fn print_result(result: &QueryResult) {
    match result {
        QueryResult::Allowed {
            reason,
            granted_path,
            access,
            source,
            endpoint_rules,
        } => {
            println!("{}", "ALLOWED".green().bold());
            println!("  Reason: {}", reason);
            if let Some(path) = granted_path {
                println!("  Granted by: {}", path);
            }
            if let Some(acc) = access {
                println!("  Access: {}", acc);
            }
            if let Some(src) = source {
                println!("  Source: {}", src);
            }
            if let Some(rules) = endpoint_rules {
                println!("  Endpoint rules ({} total):", rules.len());
                for rule in rules {
                    println!("    {} {}", rule.method, rule.path);
                }
            }
        }
        QueryResult::Denied {
            reason,
            details,
            policy_source,
            matching_capability,
            suggested_flag,
            endpoint_rules,
        } => {
            println!("{}", "DENIED".red().bold());
            println!("  Reason: {}", reason);
            if let Some(d) = details {
                println!("  Details: {}", d);
            }
            if let Some(policy) = policy_source {
                println!("  Policy: {}", policy);
            }
            if let Some(cap) = matching_capability {
                println!(
                    "  Closest match: {} ({}, {})",
                    cap.path, cap.access, cap.source
                );
            }
            if let Some(flag) = suggested_flag {
                println!("  Suggested fix: {}", flag);
            }
            if let Some(rules) = endpoint_rules {
                println!("  Permitted endpoints ({} total):", rules.len());
                for rule in rules {
                    println!("    {} {}", rule.method, rule.path);
                }
            }
        }
        QueryResult::NotSandboxed { message } => {
            println!("{}", "NOT SANDBOXED".yellow().bold());
            println!("  {}", message);
        }
        QueryResult::Scope {
            scope,
            state,
            requested,
            enforced,
            supported,
            kernel_abi,
            details,
        } => {
            println!("{}", "SCOPE".blue().bold());
            println!("  Scope: {}", scope);
            println!("  State: {}", state);
            println!("  Requested: {}", requested);
            println!("  Enforced: {}", enforced);
            println!("  Supported: {}", supported);
            if let Some(abi) = kernel_abi {
                println!("  Kernel ABI: {}", abi);
            }
            if let Some(detail) = details {
                println!("  Details: {}", detail);
            }
        }
    }
}

fn suggested_flag_for_path(path: &Path, requested: AccessMode) -> String {
    let (flag, target) = suggested_flag_parts(path, requested);
    format!("{flag} {}", target.display())
}

pub(crate) fn suggested_flag_parts(path: &Path, requested: AccessMode) -> (&'static str, PathBuf) {
    let flag = if path.is_file() {
        match requested {
            AccessMode::Read => "--read-file",
            AccessMode::Write => "--write-file",
            AccessMode::ReadWrite => "--allow-file",
        }
    } else {
        match requested {
            AccessMode::Read => "--read",
            AccessMode::Write => "--write",
            AccessMode::ReadWrite => "--allow",
        }
    };

    let target = if path.exists() || path.is_dir() || path.parent().is_none() {
        path.to_path_buf()
    } else if let Some(parent) = path.parent() {
        // Never suggest granting access to the root filesystem
        if parent == Path::new("/") {
            path.to_path_buf()
        } else {
            parent.to_path_buf()
        }
    } else {
        path.to_path_buf()
    };

    (flag, target)
}

#[cfg(test)]
mod tests {
    use super::*;
    use nono::{CapabilitySource, FsCapability};
    use std::path::PathBuf;
    use tempfile::tempdir;

    #[test]
    fn test_query_path_granted() {
        let dir = tempdir().expect("Failed to create temp dir");
        let mut caps = CapabilitySet::new();
        caps.add_fs(FsCapability {
            original: dir.path().to_path_buf(),
            resolved: dir.path().canonicalize().expect("Failed to canonicalize"),
            access: AccessMode::ReadWrite,
            is_file: false,
            source: CapabilitySource::User,
        });

        let test_file = dir.path().join("test.txt");
        std::fs::write(&test_file, "test").expect("Failed to write test file");
        let expected_grant = dir
            .path()
            .canonicalize()
            .expect("Failed to canonicalize dir");

        let result = query_path(&test_file, AccessMode::Read, &caps, &[]).expect("Query failed");
        match result {
            QueryResult::Allowed {
                source,
                granted_path,
                access,
                ..
            } => {
                assert_eq!(source.as_deref(), Some("user"));
                assert_eq!(
                    granted_path.as_deref(),
                    Some(expected_grant.to_string_lossy().as_ref())
                );
                assert_eq!(access.as_deref(), Some("read+write"));
            }
            _ => panic!("expected allowed result"),
        }
    }

    #[test]
    fn test_query_path_denied() {
        let caps = CapabilitySet::new();
        let path = PathBuf::from("/some/random/path");

        let result = query_path(&path, AccessMode::Read, &caps, &[]).expect("Query failed");
        match result {
            QueryResult::Denied {
                reason,
                suggested_flag,
                matching_capability,
                ..
            } => {
                assert_eq!(reason, "path_not_granted");
                assert_eq!(suggested_flag.as_deref(), Some("--read /some/random"));
                assert!(matching_capability.is_none());
            }
            _ => panic!("expected denied result"),
        }
    }

    #[test]
    fn test_query_path_prefers_more_specific_sufficient_capability() {
        let dir = tempdir().expect("Failed to create temp dir");
        let dir_canon = dir.path().canonicalize().expect("Failed to canonicalize");

        let mut caps = CapabilitySet::new();
        let parent = dir_canon
            .parent()
            .expect("tempdir has parent")
            .to_path_buf();

        // Broad read-only capability.
        caps.add_fs(FsCapability {
            original: parent.clone(),
            resolved: parent,
            access: AccessMode::Read,
            is_file: false,
            source: CapabilitySource::System,
        });

        // More specific read-write user capability.
        caps.add_fs(FsCapability {
            original: dir_canon.clone(),
            resolved: dir_canon.clone(),
            access: AccessMode::ReadWrite,
            is_file: false,
            source: CapabilitySource::User,
        });

        let test_file = dir_canon.join("test.txt");
        std::fs::write(&test_file, "test").expect("Failed to write test file");

        let result = query_path(&test_file, AccessMode::Write, &caps, &[]).expect("Query failed");
        assert!(matches!(result, QueryResult::Allowed { .. }));
    }

    #[test]
    fn test_query_path_reports_near_miss_with_source_and_fix() {
        let dir = tempdir().expect("Failed to create temp dir");
        let dir_canon = dir.path().canonicalize().expect("Failed to canonicalize");
        let test_file = dir.path().join("test.txt");
        std::fs::write(&test_file, "test").expect("Failed to write test file");
        let test_file_canon = test_file
            .canonicalize()
            .expect("Failed to canonicalize file");

        let mut caps = CapabilitySet::new();
        caps.add_fs(FsCapability {
            original: dir_canon.clone(),
            resolved: dir_canon,
            access: AccessMode::Read,
            is_file: false,
            source: CapabilitySource::Group("dev".to_string()),
        });

        let result = query_path(&test_file, AccessMode::Write, &caps, &[]).expect("Query failed");
        match result {
            QueryResult::Denied {
                reason,
                matching_capability,
                suggested_flag,
                details,
                ..
            } => {
                let expected_flag = format!("--write-file {}", test_file_canon.display());
                assert_eq!(reason, "insufficient_access");
                assert_eq!(suggested_flag.as_deref(), Some(expected_flag.as_str()));
                let capability = matching_capability.expect("expected matching capability");
                assert_eq!(capability.access, "read");
                assert_eq!(capability.source, "group:dev");
                assert!(
                    details.as_deref().is_some_and(
                        |d| d.contains("group:dev") && d.contains("write was requested")
                    )
                );
            }
            _ => panic!("expected denied result"),
        }
    }

    #[test]
    fn test_query_path_sensitive_policy_includes_policy_source() {
        let _lock = match crate::test_env::ENV_LOCK.lock() {
            Ok(lock) => lock,
            Err(poisoned) => poisoned.into_inner(),
        };
        let ssh_path = PathBuf::from(format!(
            "{}/.ssh",
            crate::config::validated_home().expect("HOME should be valid in test")
        ));
        let caps = CapabilitySet::new();

        let result = query_path(&ssh_path, AccessMode::Read, &caps, &[]).expect("Query failed");
        match result {
            QueryResult::Denied {
                reason,
                policy_source,
                suggested_flag,
                details,
                ..
            } => {
                assert_eq!(reason, "sensitive_path");
                assert!(
                    policy_source
                        .as_deref()
                        .is_some_and(|policy| policy.starts_with("group:"))
                );
                assert!(
                    details
                        .as_deref()
                        .is_some_and(|detail| detail.contains("filesystem.bypass_protection"))
                );
                assert!(suggested_flag.is_none());
            }
            _ => panic!("expected denied result"),
        }
    }

    #[test]
    fn test_query_network_allowed() {
        let caps = CapabilitySet::new();
        let result = query_network("example.com", 443, &caps, &[], &[]);
        assert!(matches!(result, QueryResult::Allowed { .. }));
    }

    #[test]
    fn test_query_network_blocked() {
        let caps = CapabilitySet::new().block_network();
        let result = query_network("example.com", 443, &caps, &[], &[]);
        assert!(matches!(result, QueryResult::Denied { .. }));
    }

    #[test]
    fn test_query_scope_returns_structured_result() {
        let caps = CapabilitySet::new();
        let result = query_scope(ScopeQuery::AbstractUnixSocket, &caps);
        match result {
            QueryResult::Scope {
                scope,
                state,
                requested,
                enforced,
                ..
            } => {
                assert_eq!(scope, "abstract-unix-socket");
                assert!(!state.is_empty());
                assert!(!enforced || requested);
            }
            _ => panic!("expected scope result"),
        }
    }

    #[test]
    fn test_query_network_proxy_domain_filtering() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        let allowed = vec!["api.example.com".to_string()];

        let result = query_network("api.example.com", 443, &caps, &allowed, &[]);
        assert!(matches!(result, QueryResult::Allowed { .. }));

        match query_network("evil.com", 443, &caps, &allowed, &[]) {
            QueryResult::Denied {
                reason,
                suggested_flag,
                ..
            } => {
                assert_eq!(reason, "proxy_filtered");
                assert_eq!(suggested_flag.as_deref(), Some("--allow-domain evil.com"));
            }
            _ => panic!("expected denied result"),
        }
    }

    #[test]
    fn test_query_network_proxy_wildcard_and_bare_domain() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        let allowed = vec!["*.example.com".to_string()];

        assert!(matches!(
            query_network("sub.example.com", 443, &caps, &allowed, &[]),
            QueryResult::Allowed { .. }
        ));
        // *.example.com must NOT match bare example.com (mirrors HostFilter)
        assert!(matches!(
            query_network("example.com", 443, &caps, &allowed, &[]),
            QueryResult::Denied { .. }
        ));
    }

    #[test]
    fn test_query_network_proxy_no_domain_filter() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        assert!(matches!(
            query_network("anything.com", 443, &caps, &[], &[]),
            QueryResult::Allowed { .. }
        ));
    }

    #[test]
    fn test_query_network_proxy_denies_cloud_metadata() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        // Cloud metadata endpoints are denied even with an empty allowlist
        assert!(matches!(
            query_network("169.254.169.254", 80, &caps, &[], &[]),
            QueryResult::Denied { .. }
        ));
        // Also denied even if explicitly in the allowlist
        let allowed = vec!["169.254.169.254".to_string()];
        assert!(matches!(
            query_network("169.254.169.254", 80, &caps, &allowed, &[]),
            QueryResult::Denied { .. }
        ));
    }

    #[test]
    fn test_query_network_url_extracts_domain() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        let allowed = vec!["github.com".to_string()];
        // Full URL should extract domain and match
        assert!(matches!(
            query_network("https://github.com/some/repo", 443, &caps, &allowed, &[]),
            QueryResult::Allowed { .. }
        ));
    }

    #[test]
    fn test_query_network_url_with_endpoint_rules_path_matches() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        let allowed = vec!["github.com".to_string()];
        let endpoints = vec![crate::sandbox_state::DomainEndpointState {
            domain: "github.com".to_string(),
            endpoints: vec![
                crate::sandbox_state::EndpointRuleState {
                    method: "*".to_string(),
                    path: "/atko-cic/**".to_string(),
                },
                crate::sandbox_state::EndpointRuleState {
                    method: "*".to_string(),
                    path: "/always-further/**".to_string(),
                },
            ],
        }];
        // Path that matches a rule
        let result = query_network(
            "https://github.com/atko-cic/repo",
            443,
            &caps,
            &allowed,
            &endpoints,
        );
        assert!(matches!(result, QueryResult::Allowed { .. }));
    }

    #[test]
    fn test_query_network_url_with_endpoint_rules_path_denied() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        let allowed = vec!["github.com".to_string()];
        let endpoints = vec![crate::sandbox_state::DomainEndpointState {
            domain: "github.com".to_string(),
            endpoints: vec![crate::sandbox_state::EndpointRuleState {
                method: "*".to_string(),
                path: "/atko-cic/**".to_string(),
            }],
        }];
        // Path that does NOT match any rule
        let result = query_network(
            "https://github.com/openai/codex",
            443,
            &caps,
            &allowed,
            &endpoints,
        );
        match result {
            QueryResult::Denied {
                reason,
                details,
                endpoint_rules,
                ..
            } => {
                assert_eq!(reason, "endpoint_restricted");
                assert!(
                    details
                        .as_deref()
                        .is_some_and(|d| d.contains("github.com") && d.contains("/openai/codex"))
                );
                let rules = endpoint_rules.expect("expected endpoint rules");
                assert_eq!(rules.len(), 1);
                assert_eq!(rules[0].path, "/atko-cic/**");
            }
            _ => panic!("expected denied result, got: {:?}", result),
        }
    }

    #[test]
    fn test_query_network_bare_domain_with_endpoint_rules_shows_allowed() {
        let caps = CapabilitySet::new().set_network_mode(nono::NetworkMode::ProxyOnly {
            port: 0,
            bind_ports: vec![],
        });
        let allowed = vec!["github.com".to_string()];
        let endpoints = vec![crate::sandbox_state::DomainEndpointState {
            domain: "github.com".to_string(),
            endpoints: vec![crate::sandbox_state::EndpointRuleState {
                method: "*".to_string(),
                path: "/atko-cic/**".to_string(),
            }],
        }];
        // Bare domain (no path) shows allowed with endpoint rules
        let result = query_network("github.com", 443, &caps, &allowed, &endpoints);
        match result {
            QueryResult::Allowed {
                endpoint_rules,
                access,
                ..
            } => {
                let rules = endpoint_rules.expect("expected endpoint rules");
                assert_eq!(rules.len(), 1);
                assert!(
                    access
                        .expect("expected access message")
                        .contains("restricted to 1 endpoint rules")
                );
            }
            _ => panic!("expected allowed result, got: {:?}", result),
        }
    }

    #[test]
    fn test_parse_host_input_url() {
        let (domain, path) = parse_host_input("https://github.com/openai/codex");
        assert_eq!(domain, "github.com");
        assert_eq!(path.as_deref(), Some("/openai/codex"));
    }

    #[test]
    fn test_parse_host_input_bare_hostname() {
        let (domain, path) = parse_host_input("github.com");
        assert_eq!(domain, "github.com");
        assert_eq!(path, None);
    }

    #[test]
    fn test_parse_host_input_url_root_path() {
        let (domain, path) = parse_host_input("https://api.example.com/");
        assert_eq!(domain, "api.example.com");
        assert_eq!(path, None);
    }

    #[test]
    fn test_path_matches_endpoint_rules_glob() {
        let rules = vec![
            crate::sandbox_state::EndpointRuleState {
                method: "*".to_string(),
                path: "/atko-cic/**".to_string(),
            },
            crate::sandbox_state::EndpointRuleState {
                method: "GET".to_string(),
                path: "/public/*".to_string(),
            },
        ];
        assert!(path_matches_endpoint_rules(
            "/atko-cic/repo/tree/main",
            &rules
        ));
        assert!(path_matches_endpoint_rules("/public/file.txt", &rules));
        assert!(!path_matches_endpoint_rules("/openai/codex", &rules));
        assert!(!path_matches_endpoint_rules("/other/path", &rules));
    }

    #[test]
    fn test_path_matches_empty_rules_allows_all() {
        assert!(path_matches_endpoint_rules("/any/path", &[]));
    }
}