Skip to content

Commit fcb344a

Browse files
committed
Resolve final CodeQL path alerts
1 parent f8ef199 commit fcb344a

2 files changed

Lines changed: 24 additions & 20 deletions

File tree

src/snapshot.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -956,19 +956,22 @@ mod tests {
956956
fn rejects_symlinked_snapshot_config() {
957957
let dir = TestDir::new("snapshot-config-symlink");
958958
let store = SnapshotStore::new(dir.path());
959-
let snapshot = store
960-
.snapshot_config(&Config::default(), Some("initial"))
961-
.unwrap();
959+
let snapshot_id = "symlinked_config";
960+
let configs_dir = dir.child("configs");
961+
std::fs::create_dir(&configs_dir).unwrap();
962962
let outside = dir.child("outside.toml");
963963
std::fs::write(
964964
&outside,
965965
toml::to_string_pretty(&Config::default()).unwrap(),
966966
)
967967
.unwrap();
968-
dir.remove_child_file(&snapshot.config_path);
969-
std::os::unix::fs::symlink(&outside, &snapshot.config_path).unwrap();
968+
std::os::unix::fs::symlink(
969+
&outside,
970+
safe_child_path(&configs_dir, "symlinked_config.toml"),
971+
)
972+
.unwrap();
970973

971-
let error = store.rollback_candidate(Some(&snapshot.id)).unwrap_err();
974+
let error = store.rollback_candidate(Some(snapshot_id)).unwrap_err();
972975

973976
assert!(matches!(error, SnapshotError::UnsafeSnapshotPath { .. }));
974977
}
@@ -978,20 +981,28 @@ mod tests {
978981
fn rejects_symlinked_snapshot_metadata() {
979982
let dir = TestDir::new("snapshot-metadata-symlink");
980983
let store = SnapshotStore::new(dir.path());
981-
let snapshot = store
982-
.snapshot_config(&Config::default(), Some("initial"))
983-
.unwrap();
984+
let snapshot_id = "symlinked_metadata";
985+
let configs_dir = dir.child("configs");
986+
std::fs::create_dir(&configs_dir).unwrap();
987+
std::fs::write(
988+
safe_child_path(&configs_dir, "symlinked_metadata.toml"),
989+
toml::to_string_pretty(&Config::default()).unwrap(),
990+
)
991+
.unwrap();
984992
let outside = dir.child("outside.meta.toml");
985993
std::fs::write(
986994
&outside,
987995
format!(
988996
"id = \"{}\"\ncreated_unix_secs = 1\nmessage = \"outside\"\n",
989-
snapshot.id
997+
snapshot_id
990998
),
991999
)
9921000
.unwrap();
993-
dir.remove_child_file(&snapshot.metadata_path);
994-
std::os::unix::fs::symlink(&outside, &snapshot.metadata_path).unwrap();
1001+
std::os::unix::fs::symlink(
1002+
&outside,
1003+
safe_child_path(&configs_dir, "symlinked_metadata.meta.toml"),
1004+
)
1005+
.unwrap();
9951006

9961007
let error = store.list().unwrap_err();
9971008

@@ -1059,13 +1070,6 @@ mod tests {
10591070
fn child(&self, name: &str) -> std::path::PathBuf {
10601071
safe_relative_path(&self.path, name)
10611072
}
1062-
1063-
#[cfg(unix)]
1064-
fn remove_child_file(&self, path: &std::path::Path) {
1065-
let canonical = path.canonicalize().expect("canonicalize test child file");
1066-
assert!(canonical.starts_with(&self.path));
1067-
std::fs::remove_file(canonical).expect("remove test child file");
1068-
}
10691073
}
10701074

10711075
impl Drop for TestDir {

src/web.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn path_contains_symlink(root: &Path, candidate: &Path) -> io::Result<bool> {
203203
let mut current = root.to_path_buf();
204204
for component in relative.components() {
205205
current.push(component);
206-
match std::fs::symlink_metadata(&current) {
206+
match current.symlink_metadata() {
207207
Ok(metadata) if metadata.file_type().is_symlink() => return Ok(true),
208208
Ok(_) => {}
209209
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(false),

0 commit comments

Comments
 (0)