Skip to content

Commit eeba0e7

Browse files
authored
refactor(runtime): confine and reduce unsafe code (#4130)
* refactor(runtime): confine and reduce unsafe code Signed-off-by: Drew Newberry <[email protected]> * fix(ci): make signal probe portable and gate Linux initialization Signed-off-by: Drew Newberry <[email protected]> * fix(cli): handle platform-specific SSH cancellation lints Signed-off-by: Drew Newberry <[email protected]> --------- Signed-off-by: Drew Newberry <[email protected]>
1 parent 0ccc6b9 commit eeba0e7

79 files changed

Lines changed: 5465 additions & 5483 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Cargo.lock‎

Lines changed: 5 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/openshell-bootstrap/src/edge_token.rs‎

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,21 @@ pub fn remove_edge_token(gateway_name: &str) -> Result<()> {
8888
mod tests {
8989
use super::*;
9090

91-
/// Helper: hold the shared XDG test lock, set `XDG_CONFIG_HOME` to a
92-
/// tempdir, run `f`, then restore the original value.
93-
#[allow(unsafe_code)]
94-
fn with_tmp_xdg<F: FnOnce()>(tmp: &std::path::Path, f: F) {
95-
let _guard = crate::XDG_TEST_LOCK
96-
.lock()
97-
.unwrap_or_else(std::sync::PoisonError::into_inner);
98-
let orig = std::env::var("XDG_CONFIG_HOME").ok();
99-
unsafe {
100-
std::env::set_var("XDG_CONFIG_HOME", tmp);
101-
}
102-
f();
103-
unsafe {
104-
match orig {
105-
Some(v) => std::env::set_var("XDG_CONFIG_HOME", v),
106-
None => std::env::remove_var("XDG_CONFIG_HOME"),
107-
}
108-
}
91+
/// Run the assertions in a child with an isolated configuration root.
92+
fn with_tmp_xdg(tmp: &std::path::Path, f: impl FnOnce(&std::path::Path)) {
93+
crate::test_environment::Environment::new()
94+
.set("XDG_CONFIG_HOME", tmp)
95+
.remove(crate::paths::SYSTEM_GATEWAY_DIR_ENV)
96+
.run(|| {
97+
let root = PathBuf::from(std::env::var_os("XDG_CONFIG_HOME").unwrap());
98+
f(&root);
99+
});
109100
}
110101

111102
#[test]
112103
fn store_and_load_edge_token_roundtrip() {
113104
let tmp = tempfile::tempdir().unwrap();
114-
with_tmp_xdg(tmp.path(), || {
105+
with_tmp_xdg(tmp.path(), |_| {
115106
store_edge_token("test-gateway", "eyJhbGciOiJSUzI1NiJ9.test.sig").unwrap();
116107
assert_eq!(
117108
load_edge_token("test-gateway"),
@@ -123,15 +114,15 @@ mod tests {
123114
#[test]
124115
fn load_edge_token_returns_none_when_not_set() {
125116
let tmp = tempfile::tempdir().unwrap();
126-
with_tmp_xdg(tmp.path(), || {
117+
with_tmp_xdg(tmp.path(), |_| {
127118
assert_eq!(load_edge_token("no-such-gateway"), None);
128119
});
129120
}
130121

131122
#[test]
132123
fn store_edge_token_overwrites_previous() {
133124
let tmp = tempfile::tempdir().unwrap();
134-
with_tmp_xdg(tmp.path(), || {
125+
with_tmp_xdg(tmp.path(), |_| {
135126
store_edge_token("c1", "token-1").unwrap();
136127
store_edge_token("c1", "token-2").unwrap();
137128
assert_eq!(load_edge_token("c1"), Some("token-2".to_string()));
@@ -141,7 +132,7 @@ mod tests {
141132
#[test]
142133
fn remove_edge_token_deletes_file() {
143134
let tmp = tempfile::tempdir().unwrap();
144-
with_tmp_xdg(tmp.path(), || {
135+
with_tmp_xdg(tmp.path(), |_| {
145136
store_edge_token("c2", "token").unwrap();
146137
assert!(load_edge_token("c2").is_some());
147138
remove_edge_token("c2").unwrap();
@@ -152,7 +143,7 @@ mod tests {
152143
#[test]
153144
fn remove_edge_token_noop_when_missing() {
154145
let tmp = tempfile::tempdir().unwrap();
155-
with_tmp_xdg(tmp.path(), || {
146+
with_tmp_xdg(tmp.path(), |_| {
156147
// Should not error when file doesn't exist.
157148
remove_edge_token("nonexistent").unwrap();
158149
});
@@ -161,7 +152,7 @@ mod tests {
161152
#[test]
162153
fn edge_token_paths_reject_multi_component_gateway_names() {
163154
let tmp = tempfile::tempdir().unwrap();
164-
with_tmp_xdg(tmp.path(), || {
155+
with_tmp_xdg(tmp.path(), |_| {
165156
assert!(store_edge_token("../escape", "token").is_err());
166157
assert_eq!(load_edge_token("../escape"), None);
167158
assert!(remove_edge_token("../escape").is_err());
@@ -170,7 +161,7 @@ mod tests {
170161
#[test]
171162
fn load_edge_token_trims_whitespace() {
172163
let tmp = tempfile::tempdir().unwrap();
173-
with_tmp_xdg(tmp.path(), || {
164+
with_tmp_xdg(tmp.path(), |_| {
174165
// Write manually with whitespace.
175166
let path = edge_token_path("ws-gateway").unwrap();
176167
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
@@ -182,7 +173,7 @@ mod tests {
182173
#[test]
183174
fn load_edge_token_returns_none_for_empty_file() {
184175
let tmp = tempfile::tempdir().unwrap();
185-
with_tmp_xdg(tmp.path(), || {
176+
with_tmp_xdg(tmp.path(), |_| {
186177
let path = edge_token_path("empty-gateway").unwrap();
187178
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
188179
std::fs::write(&path, " \n").unwrap();
@@ -193,7 +184,7 @@ mod tests {
193184
#[test]
194185
fn load_edge_token_falls_back_to_legacy_cf_token() {
195186
let tmp = tempfile::tempdir().unwrap();
196-
with_tmp_xdg(tmp.path(), || {
187+
with_tmp_xdg(tmp.path(), |_| {
197188
// Write to the legacy cf_token path.
198189
let path = legacy_token_path("legacy-gateway").unwrap();
199190
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
@@ -210,7 +201,7 @@ mod tests {
210201
fn store_edge_token_sets_permissions() {
211202
use std::os::unix::fs::PermissionsExt;
212203
let tmp = tempfile::tempdir().unwrap();
213-
with_tmp_xdg(tmp.path(), || {
204+
with_tmp_xdg(tmp.path(), |_| {
214205
store_edge_token("perm-test", "secret").unwrap();
215206
let path = edge_token_path("perm-test").unwrap();
216207
let mode = std::fs::metadata(&path).unwrap().permissions().mode() & 0o777;

‎crates/openshell-bootstrap/src/lib.rs‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@ pub mod mtls;
1010
mod paths;
1111
pub mod pki;
1212

13-
#[cfg(test)]
14-
use std::sync::Mutex;
15-
16-
/// Shared lock for tests that mutate the process-global `XDG_CONFIG_HOME`
17-
/// env var. All such tests in any module must hold this lock to avoid
18-
/// concurrent clobbering.
19-
#[cfg(test)]
20-
pub(crate) static XDG_TEST_LOCK: Mutex<()> = Mutex::new(());
21-
2213
pub use crate::metadata::{
2314
GatewayMetadata, GatewayMetadataSource, ListedGateway, clear_active_gateway,
2415
clear_last_sandbox_if_matches, extract_host_from_ssh_destination, gateway_metadata_source,
2516
get_gateway_metadata, list_gateways, list_gateways_with_source, load_active_gateway,
2617
load_gateway_metadata, load_last_sandbox, load_user_active_gateway, remove_gateway_metadata,
2718
resolve_ssh_hostname, save_active_gateway, save_last_sandbox, store_gateway_metadata,
2819
};
20+
21+
#[cfg(test)]
22+
#[path = "../../../tests/support/environment.rs"]
23+
mod test_environment;

0 commit comments

Comments
 (0)