Skip to content

Commit

Permalink
set secure cache permissions on unix systems
Browse files Browse the repository at this point in the history
  • Loading branch information
b1ek committed Dec 1, 2024
1 parent 968c9b6 commit eabfbf3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/compiler/cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, path::PathBuf};
use std::{fs::{self, Permissions}, os::unix::fs::PermissionsExt, path::PathBuf};

const GIT_HASH: &'static str = env!("GIT_HASH");

Expand All @@ -11,6 +11,9 @@ pub fn home_cache() -> Option<PathBuf> {
home.push("amber");
if ! home.is_dir() {
fs::create_dir_all(&home).expect("Couldn't create ~/.cache/amber");

#[cfg(unix)]
fs::set_permissions(&home, Permissions::from_mode(0o700)).expect("Couldn't set permissions to ~/.cache/amber")
}
Some(home)
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/compiler/cache/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{error::Error, fs::{self, File, Metadata}, path::PathBuf, time::SystemTime};
use std::{error::Error, fs::{self, File, Metadata, Permissions}, os::unix::fs::PermissionsExt, path::PathBuf, time::SystemTime};
use serde::{Serialize, Deserialize};

use crate::{modules::block::Block, utils::ParserMetadata};
Expand Down Expand Up @@ -69,7 +69,10 @@ impl PreparsedFile {
filename.set_extension(FILE_EXT);

let serialized: Vec<u8> = self.try_into()?;
fs::write(filename, serialized)?;
fs::write(&filename, serialized)?;

#[cfg(unix)]
fs::set_permissions(&filename, Permissions::from_mode(0o700)).map_err(|x| format!("Cannot set perms to {filename:?}: {x}"))?;

Ok(())
}
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/cache/tokenize.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs::File;
use std::fs::{File, Permissions};
use std::os::unix::fs::PermissionsExt;
use std::{fs, path::PathBuf};
use std::time::SystemTime;

Expand Down Expand Up @@ -109,7 +110,12 @@ impl PretokenizedFile {

if let Some(cache_file) = Self::get_path(&filename, file_meta) {
let serialized: Vec<u8> = self.try_into()?;

fs::write(&cache_file, serialized).map_err(|x| format!("Cannot write to {cache_file:?}: {x}"))?;

#[cfg(unix)]
fs::set_permissions(&filename, Permissions::from_mode(0o700)).map_err(|x| format!("Cannot set perms to {cache_file:?}: {x}"))?;

Ok(())
} else {
Err(String::from("Couldn't get path to saved directory").into())
Expand Down

0 comments on commit eabfbf3

Please sign in to comment.