Skip to content

Commit 1d37a4c

Browse files
committed
fix(sandbox): trust exact declared private endpoints
1 parent cd70249 commit 1d37a4c

9 files changed

Lines changed: 631 additions & 16 deletions

File tree

‎crates/openshell-policy/src/merge.rs‎

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,12 @@ fn expand_access_preset(protocol: &str, access: &str) -> Option<Vec<L7Rule>> {
723723
fn append_unique_binaries(existing: &mut Vec<NetworkBinary>, incoming: &[NetworkBinary]) {
724724
let mut seen: HashSet<String> = existing.iter().map(|binary| binary.path.clone()).collect();
725725
for binary in incoming {
726+
if let Some(existing_binary) = existing.iter_mut().find(|item| item.path == binary.path) {
727+
if !is_advisor_proposed_binary(binary) {
728+
mark_user_declared_binary(existing_binary);
729+
}
730+
continue;
731+
}
726732
if seen.insert(binary.path.clone()) {
727733
existing.push(binary.clone());
728734
}
@@ -778,8 +784,30 @@ fn dedup_strings(values: &mut Vec<String>) {
778784
}
779785

780786
fn dedup_binaries(values: &mut Vec<NetworkBinary>) {
781-
let mut seen = HashSet::new();
782-
values.retain(|binary| seen.insert(binary.path.clone()));
787+
let mut deduped: Vec<NetworkBinary> = Vec::with_capacity(values.len());
788+
for binary in std::mem::take(values) {
789+
if let Some(existing) = deduped.iter_mut().find(|item| item.path == binary.path) {
790+
if !is_advisor_proposed_binary(&binary) {
791+
mark_user_declared_binary(existing);
792+
}
793+
} else {
794+
deduped.push(binary);
795+
}
796+
}
797+
*values = deduped;
798+
}
799+
800+
fn is_advisor_proposed_binary(binary: &NetworkBinary) -> bool {
801+
#[allow(deprecated)]
802+
let advisor_proposed = binary.harness;
803+
advisor_proposed
804+
}
805+
806+
fn mark_user_declared_binary(binary: &mut NetworkBinary) {
807+
#[allow(deprecated)]
808+
{
809+
binary.harness = false;
810+
}
783811
}
784812

785813
fn dedup_l7_rules(values: &mut Vec<L7Rule>) {
@@ -878,6 +906,18 @@ mod tests {
878906
}
879907
}
880908

909+
fn advisor_binary(path: &str) -> NetworkBinary {
910+
let mut binary = NetworkBinary {
911+
path: path.to_string(),
912+
..Default::default()
913+
};
914+
#[allow(deprecated)]
915+
{
916+
binary.harness = true;
917+
}
918+
binary
919+
}
920+
881921
fn rest_rule(method: &str, path: &str) -> L7Rule {
882922
L7Rule {
883923
allow: Some(L7Allow {
@@ -949,6 +989,75 @@ mod tests {
949989
assert_eq!(rule.binaries.len(), 2);
950990
}
951991

992+
#[test]
993+
fn add_rule_user_binary_clears_advisor_marker_for_same_path() {
994+
let mut policy = restrictive_default_policy();
995+
policy.network_policies.insert(
996+
"existing".to_string(),
997+
NetworkPolicyRule {
998+
name: "existing".to_string(),
999+
endpoints: vec![endpoint("api.github.com", 443)],
1000+
binaries: vec![advisor_binary("/usr/bin/curl")],
1001+
},
1002+
);
1003+
1004+
let incoming = NetworkPolicyRule {
1005+
name: "incoming".to_string(),
1006+
endpoints: vec![endpoint("api.github.com", 443)],
1007+
binaries: vec![NetworkBinary {
1008+
path: "/usr/bin/curl".to_string(),
1009+
..Default::default()
1010+
}],
1011+
};
1012+
1013+
let result = merge_policy(
1014+
policy,
1015+
&[PolicyMergeOp::AddRule {
1016+
rule_name: "existing".to_string(),
1017+
rule: incoming,
1018+
}],
1019+
)
1020+
.expect("merge should succeed");
1021+
1022+
let rule = &result.policy.network_policies["existing"];
1023+
assert_eq!(rule.binaries.len(), 1);
1024+
#[allow(deprecated)]
1025+
{
1026+
assert!(!rule.binaries[0].harness);
1027+
}
1028+
}
1029+
1030+
#[test]
1031+
fn add_rule_duplicate_binaries_prefer_user_declared_marker() {
1032+
let incoming = NetworkPolicyRule {
1033+
name: "incoming".to_string(),
1034+
endpoints: vec![endpoint("api.github.com", 443)],
1035+
binaries: vec![
1036+
advisor_binary("/usr/bin/curl"),
1037+
NetworkBinary {
1038+
path: "/usr/bin/curl".to_string(),
1039+
..Default::default()
1040+
},
1041+
],
1042+
};
1043+
1044+
let result = merge_policy(
1045+
restrictive_default_policy(),
1046+
&[PolicyMergeOp::AddRule {
1047+
rule_name: "github".to_string(),
1048+
rule: incoming,
1049+
}],
1050+
)
1051+
.expect("merge should succeed");
1052+
1053+
let rule = &result.policy.network_policies["github"];
1054+
assert_eq!(rule.binaries.len(), 1);
1055+
#[allow(deprecated)]
1056+
{
1057+
assert!(!rule.binaries[0].harness);
1058+
}
1059+
}
1060+
9521061
#[test]
9531062
fn add_rule_merges_websocket_credential_rewrite_flag() {
9541063
let mut policy = restrictive_default_policy();

‎crates/openshell-sandbox/data/sandbox-policy.rego‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,32 @@ binary_allowed(policy, exec) if {
152152
glob.match(b.path, ["/"], p)
153153
}
154154

155+
user_declared_binary_allowed(policy, exec) if {
156+
some b
157+
b := policy.binaries[_]
158+
not object.get(b, "advisor_proposed", false)
159+
not contains(b.path, "*")
160+
b.path == exec.path
161+
}
162+
163+
user_declared_binary_allowed(policy, exec) if {
164+
some b
165+
b := policy.binaries[_]
166+
not object.get(b, "advisor_proposed", false)
167+
not contains(b.path, "*")
168+
ancestor := exec.ancestors[_]
169+
b.path == ancestor
170+
}
171+
172+
user_declared_binary_allowed(policy, exec) if {
173+
some b in policy.binaries
174+
not object.get(b, "advisor_proposed", false)
175+
contains(b.path, "*")
176+
all_paths := array.concat([exec.path], exec.ancestors)
177+
some p in all_paths
178+
glob.match(b.path, ["/"], p)
179+
}
180+
155181
# --- Network action (allow / deny) ---
156182
#
157183
# These rules are mutually exclusive by construction:
@@ -638,6 +664,21 @@ matched_endpoint_config := _matching_endpoint_configs[0] if {
638664
count(_matching_endpoint_configs) > 0
639665
}
640666

667+
_policy_has_exact_declared_endpoint(policy) if {
668+
some ep
669+
ep := policy.endpoints[_]
670+
not contains(ep.host, "*")
671+
lower(ep.host) == lower(input.network.host)
672+
ep.ports[_] == input.network.port
673+
}
674+
675+
exact_declared_endpoint_host if {
676+
some pname
677+
policy := data.network_policies[pname]
678+
user_declared_binary_allowed(policy, input.exec)
679+
_policy_has_exact_declared_endpoint(policy)
680+
}
681+
641682
# Hosted endpoint: exact host match + port in ports list.
642683
endpoint_matches_request(ep, network) if {
643684
not contains(ep.host, "*")

‎crates/openshell-sandbox/src/mechanistic_mapper.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,17 @@ pub fn generate_proposals(summaries: &[DenialSummary]) -> Vec<PolicyChunk> {
143143
let binaries: Vec<NetworkBinary> = if binary.is_empty() {
144144
vec![]
145145
} else {
146-
vec![NetworkBinary {
146+
let mut proposal_binary = NetworkBinary {
147147
path: binary.clone(),
148148
..Default::default()
149-
}]
149+
};
150+
// The deprecated harness bit is ignored by policy YAML, but OPA
151+
// maps it to advisor_proposed to preserve the SSRF two-step flow.
152+
#[allow(deprecated)]
153+
{
154+
proposal_binary.harness = true;
155+
}
156+
vec![proposal_binary]
150157
};
151158

152159
let proposed_rule = NetworkPolicyRule {
@@ -500,6 +507,10 @@ mod tests {
500507
assert_eq!(rule.endpoints[0].port, 443);
501508
assert_eq!(rule.binaries.len(), 1);
502509
assert_eq!(rule.binaries[0].path, "/usr/bin/curl");
510+
#[allow(deprecated)]
511+
{
512+
assert!(rule.binaries[0].harness);
513+
}
503514

504515
// No L7 fields when no samples provided.
505516
assert!(rule.endpoints[0].protocol.is_empty());

0 commit comments

Comments
 (0)