Skip to main content

laravel_rust_debugbar/
config.rs

1use laravel::config::Section;
2
3pub const ALWAYS_MASKED: [&str; 6] = [
4    "authorization",
5    "proxy-authorization",
6    "cookie",
7    "set-cookie",
8    "x-xsrf-token",
9    "x-csrf-token",
10];
11
12#[derive(Debug, Clone, Section)]
13pub struct DebugbarConfig {
14    #[env("DEBUGBAR_ENABLED")]
15    pub enabled: Option<bool>,
16    #[env("DEBUGBAR_HISTORY", default = 50)]
17    pub history: usize,
18    #[env("DEBUGBAR_HISTORY_HOURS", default = 24)]
19    pub history_hours: u64,
20    #[env("DEBUGBAR_SLOW_QUERY_MS", default = 100)]
21    pub slow_query_ms: u64,
22    #[env("DEBUGBAR_EDITOR", default = "vscode")]
23    pub editor: String,
24    #[env("DEBUGBAR_COLLECT_HEADERS", default = true)]
25    pub headers: bool,
26    #[env("DEBUGBAR_COLLECT_INERTIA_PROPS", default = true)]
27    pub inertia_props: bool,
28    #[env("DEBUGBAR_BACKTRACE", default = false)]
29    pub backtrace: bool,
30    #[env("DEBUGBAR_EXPLAIN", default = true)]
31    pub explain: bool,
32    #[env("DEBUGBAR_MASKED", separator = ",")]
33    pub masked: Vec<String>,
34    #[env("DEBUGBAR_STORAGE", default = "file")]
35    pub storage: String,
36    #[env("DEBUGBAR_REDIS_CONNECTION", default = "default")]
37    pub redis_connection: String,
38    #[env("DEBUGBAR_STORAGE_PATH")]
39    pub storage_path: Option<String>,
40    #[env("DEBUGBAR_OPEN_STORAGE")]
41    pub open_storage: Option<bool>,
42    #[env("DEBUGBAR_FORCE_ALLOW_ENABLE", default = false)]
43    pub force_allow_enable: bool,
44    #[env("DEBUGBAR_INJECT", default = true)]
45    pub inject: bool,
46    #[env("DEBUGBAR_ROUTE_PREFIX", default = "_debugbar")]
47    pub route_prefix: String,
48    #[env("DEBUGBAR_ROUTE_DOMAIN")]
49    pub route_domain: Option<String>,
50    #[env("DEBUGBAR_EXCEPT", separator = ",")]
51    pub except: Vec<String>,
52    #[env("DEBUGBAR_REMOTE_SITES_PATH")]
53    pub remote_sites_path: Option<String>,
54    #[env("DEBUGBAR_LOCAL_SITES_PATH")]
55    pub local_sites_path: Option<String>,
56    #[env("DEBUGBAR_SERVER_TIMING", default = false)]
57    pub server_timing: bool,
58    #[env("DEBUGBAR_CLOCKWORK", default = false)]
59    pub clockwork: bool,
60    #[env("DEBUGBAR_DISABLED_COLLECTORS", separator = ",")]
61    pub disabled_collectors: Vec<String>,
62    #[env("DEBUGBAR_COLLECT_SESSION", default = false)]
63    pub session: bool,
64    #[env("DEBUGBAR_COLLECT_USER", default = false)]
65    pub user: bool,
66    #[env("DEBUGBAR_COLLECT_BODY", default = true)]
67    pub body: bool,
68    #[env("DEBUGBAR_COLLECT_CONFIG", default = false)]
69    pub config: bool,
70    #[env("DEBUGBAR_LOG_TAIL", default = false)]
71    pub log_tail: bool,
72    #[env("DEBUGBAR_LOG_TAIL_LINES", default = 124)]
73    pub log_tail_lines: usize,
74    #[env("DEBUGBAR_BACKTRACE_FRAMES", default = 5)]
75    pub backtrace_frames: usize,
76    #[env("DEBUGBAR_BACKTRACE_EXCLUDE", separator = ",")]
77    pub backtrace_exclude: Vec<String>,
78    #[env("DEBUGBAR_QUERY_SOFT_LIMIT", default = 100)]
79    pub query_soft_limit: usize,
80    #[env("DEBUGBAR_QUERY_HARD_LIMIT", default = 500)]
81    pub query_hard_limit: usize,
82    #[env("DEBUGBAR_ONLY_SLOW_QUERIES", default = false)]
83    pub only_slow_queries: bool,
84    #[env("DEBUGBAR_QUERY_EXCLUDE_PATHS", separator = ",")]
85    pub query_exclude_paths: Vec<String>,
86    #[env("DEBUGBAR_TIMELINE_QUERIES", default = false)]
87    pub timeline_queries: bool,
88    #[env("DEBUGBAR_SHOW_QUERY_RESULT", default = false)]
89    pub show_query_result: bool,
90    #[env("DEBUGBAR_VIEW_EXCLUDE", separator = ",")]
91    pub view_exclude: Vec<String>,
92}
93
94impl DebugbarConfig {
95    pub fn collects(&self, collector: &str) -> bool {
96        !self
97            .disabled_collectors
98            .iter()
99            .any(|disabled| disabled.trim().eq_ignore_ascii_case(collector))
100    }
101
102    pub fn prefix(&self) -> String {
103        format!("/{}", self.route_prefix.trim_matches('/'))
104    }
105
106    pub fn editor_root(&self, root: &str) -> String {
107        match (&self.remote_sites_path, &self.local_sites_path) {
108            (Some(remote), Some(local)) if root.starts_with(remote.as_str()) => {
109                format!("{local}{}", &root[remote.len()..])
110            }
111            _ => root.to_owned(),
112        }
113    }
114
115    pub fn allows_history_from(&self, ip: std::net::IpAddr) -> bool {
116        self.open_storage.unwrap_or_else(|| is_local(ip))
117    }
118
119    pub fn allows_record_from(&self, ip: std::net::IpAddr) -> bool {
120        self.open_storage == Some(true) || is_local(ip)
121    }
122
123    pub fn is_masked(&self, name: &str) -> bool {
124        ALWAYS_MASKED.iter().any(|masked| masked.eq_ignore_ascii_case(name))
125            || self
126                .masked
127                .iter()
128                .any(|masked| masked.trim().eq_ignore_ascii_case(name))
129    }
130}
131
132fn is_local(ip: std::net::IpAddr) -> bool {
133    match ip {
134        std::net::IpAddr::V4(ip) => ip.is_loopback() || ip.is_private() || ip.is_link_local(),
135        std::net::IpAddr::V6(ip) => {
136            ip.is_loopback() || ip.is_unique_local() || ip.to_ipv4_mapped().is_some_and(|ip| is_local(ip.into()))
137        }
138    }
139}
140
141#[cfg(test)]
142mod tests {
143    use super::is_local;
144
145    #[test]
146    fn loopback_and_private_addresses_are_local() {
147        for ip in [
148            "127.0.0.1",
149            "10.1.2.3",
150            "192.168.1.9",
151            "172.16.0.1",
152            "::1",
153            "fd00::1",
154            "::ffff:192.168.1.1",
155        ] {
156            assert!(is_local(ip.parse().unwrap()), "{ip}");
157        }
158        for ip in ["8.8.8.8", "2001:4860:4860::8888"] {
159            assert!(!is_local(ip.parse().unwrap()), "{ip}");
160        }
161    }
162}