Repository navigation
Expand file tree
/
Copy pathpolicy.rs
More file actions
291 lines (240 loc) · 8.52 KB
/
Copy pathpolicy.rs
File metadata and controls
291 lines (240 loc) · 8.52 KB
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
//! Policy engine for network access control.
use super::{
NetworkProxyError, Result,
config::{NetworkMode, NetworkProxyConfig},
host::Host,
ip_validation::{host_resolves_to_non_public, is_loopback_host},
pattern::{CompiledPatterns, compile_patterns},
};
/// Decision from host blocking check.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HostBlockDecision {
/// Host is allowed.
Allowed,
/// Host is blocked.
Blocked(HostBlockReason),
}
impl HostBlockDecision {
/// Check if the decision allows access.
pub fn is_allowed(&self) -> bool {
matches!(self, HostBlockDecision::Allowed)
}
/// Check if the decision blocks access.
pub fn is_blocked(&self) -> bool {
matches!(self, HostBlockDecision::Blocked(_))
}
}
/// Reason for blocking a host.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HostBlockReason {
/// Host is explicitly denied.
Denied,
/// Host is not in the allowlist.
NotAllowed,
/// Host resolves to a local/private IP.
NotAllowedLocal,
/// Network mode is disabled.
NetworkDisabled,
}
impl std::fmt::Display for HostBlockReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HostBlockReason::Denied => write!(f, "explicitly denied"),
HostBlockReason::NotAllowed => write!(f, "not in allowlist"),
HostBlockReason::NotAllowedLocal => write!(f, "resolves to private/local IP"),
HostBlockReason::NetworkDisabled => write!(f, "network access disabled"),
}
}
}
/// Policy engine for network access control.
pub struct PolicyEngine {
/// Network mode.
mode: NetworkMode,
/// Compiled allow patterns.
allow_patterns: CompiledPatterns,
/// Compiled deny patterns.
deny_patterns: CompiledPatterns,
/// Whether to allow local/private IP binding.
allow_local_binding: bool,
/// Whether the proxy is enabled.
enabled: bool,
}
impl PolicyEngine {
/// Create a new policy engine from config.
pub fn new(config: NetworkProxyConfig) -> Result<Self> {
let allow_patterns = compile_patterns(&config.allowed_domains)?;
let deny_patterns = compile_patterns(&config.denied_domains)?;
Ok(Self {
mode: config.mode,
allow_patterns,
deny_patterns,
allow_local_binding: config.allow_local_binding,
enabled: config.enabled,
})
}
/// Create a permissive policy (allows everything).
pub fn permissive() -> Self {
Self {
mode: NetworkMode::Full,
allow_patterns: CompiledPatterns::new(),
deny_patterns: CompiledPatterns::new(),
allow_local_binding: true,
enabled: true,
}
}
/// Check if the policy is enabled.
pub fn is_enabled(&self) -> bool {
self.enabled
}
/// Get the network mode.
pub fn mode(&self) -> NetworkMode {
self.mode
}
/// Check if a method is allowed.
pub fn check_method(&self, method: &str) -> bool {
self.mode.allows_method(method)
}
/// Check if a host is blocked (synchronous check, no DNS resolution).
pub fn check_host_sync(&self, host: &str) -> HostBlockDecision {
// If disabled, block everything
if self.mode == NetworkMode::Disabled {
return HostBlockDecision::Blocked(HostBlockReason::NetworkDisabled);
}
let host_lower = host.to_lowercase();
// Order matters for security:
// 1) Explicit deny always wins
if self.deny_patterns.matches(&host_lower) {
return HostBlockDecision::Blocked(HostBlockReason::Denied);
}
// 2) Check loopback unless explicitly allowed
if let Ok(parsed_host) = Host::parse(&host_lower)
&& !self.allow_local_binding
&& is_loopback_host(&parsed_host)
{
return HostBlockDecision::Blocked(HostBlockReason::NotAllowedLocal);
}
// 3) If allowlist is configured, check it
if !self.allow_patterns.is_empty() && !self.allow_patterns.matches(&host_lower) {
return HostBlockDecision::Blocked(HostBlockReason::NotAllowed);
}
HostBlockDecision::Allowed
}
/// Check if a host is blocked (async, includes DNS resolution for SSRF protection).
pub async fn check_host(&self, host: &str, port: u16) -> HostBlockDecision {
// First do synchronous checks
let sync_result = self.check_host_sync(host);
if sync_result.is_blocked() {
return sync_result;
}
// 2) DNS rebinding protection - check if host resolves to non-public IP
if !self.allow_local_binding {
// Only resolve if it looks like a hostname (not an IP)
if host.parse::<std::net::IpAddr>().is_err()
&& host_resolves_to_non_public(host, port).await
{
return HostBlockDecision::Blocked(HostBlockReason::NotAllowedLocal);
}
}
HostBlockDecision::Allowed
}
/// Validate a full request (method + host).
pub async fn validate_request(&self, method: &str, host: &str, port: u16) -> Result<()> {
// Check method
if !self.check_method(method) {
return Err(NetworkProxyError::MethodNotAllowed(method.to_string()));
}
// Check host
let decision = self.check_host(host, port).await;
if let HostBlockDecision::Blocked(reason) = decision {
return Err(NetworkProxyError::HostBlocked(host.to_string(), reason));
}
Ok(())
}
}
impl Default for PolicyEngine {
fn default() -> Self {
Self::permissive()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::NetworkProxyConfigBuilder;
#[test]
fn test_policy_deny_wins() {
let config = NetworkProxyConfigBuilder::new()
.allow_domain("**.example.com")
.deny_domain("evil.example.com")
.build();
let policy = PolicyEngine::new(config).unwrap();
// Allow pattern matches but deny wins
assert!(matches!(
policy.check_host_sync("evil.example.com"),
HostBlockDecision::Blocked(HostBlockReason::Denied)
));
// Other subdomains still allowed
assert!(matches!(
policy.check_host_sync("good.example.com"),
HostBlockDecision::Allowed
));
}
#[test]
fn test_policy_allowlist() {
let config = NetworkProxyConfigBuilder::new()
.allow_domain("**.github.com")
.allow_domain("api.openai.com")
.build();
let policy = PolicyEngine::new(config).unwrap();
assert!(matches!(
policy.check_host_sync("api.github.com"),
HostBlockDecision::Allowed
));
assert!(matches!(
policy.check_host_sync("api.openai.com"),
HostBlockDecision::Allowed
));
assert!(matches!(
policy.check_host_sync("evil.example"),
HostBlockDecision::Blocked(HostBlockReason::NotAllowed)
));
}
#[test]
fn test_policy_network_disabled() {
let config = NetworkProxyConfigBuilder::new()
.mode(NetworkMode::Disabled)
.build();
let policy = PolicyEngine::new(config).unwrap();
assert!(matches!(
policy.check_host_sync("example.com"),
HostBlockDecision::Blocked(HostBlockReason::NetworkDisabled)
));
}
#[test]
fn test_policy_limited_mode() {
let config = NetworkProxyConfigBuilder::new()
.mode(NetworkMode::Limited)
.build();
let policy = PolicyEngine::new(config).unwrap();
assert!(policy.check_method("GET"));
assert!(policy.check_method("HEAD"));
assert!(policy.check_method("OPTIONS"));
assert!(!policy.check_method("POST"));
assert!(!policy.check_method("PUT"));
assert!(!policy.check_method("DELETE"));
}
#[test]
fn test_policy_loopback_blocking() {
let config = NetworkProxyConfigBuilder::new()
.allow_local_binding(false)
.build();
let policy = PolicyEngine::new(config).unwrap();
assert!(matches!(
policy.check_host_sync("localhost"),
HostBlockDecision::Blocked(HostBlockReason::NotAllowedLocal)
));
assert!(matches!(
policy.check_host_sync("127.0.0.1"),
HostBlockDecision::Blocked(HostBlockReason::NotAllowedLocal)
));
}
}