Skip to content

Commit

Permalink
chore: restrict auth_config file permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
salome-voltz committed Feb 5, 2025
1 parent ddd988f commit 7e20611
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ggshield/core/config/auth_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def load(cls) -> "AuthConfig":
def save(self) -> None:
config_path = get_auth_config_filepath()
data = prepare_auth_config_dict_for_save(self.to_dict())
save_yaml_dict(data, config_path)
save_yaml_dict(data, config_path, restricted=True)

def get_instance(self, instance_name: str) -> InstanceConfig:
for instance in self.instances:
Expand Down
9 changes: 8 additions & 1 deletion ggshield/core/config/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from pathlib import Path
from typing import Any, Dict, List, Literal, Optional, Set, Union, overload

Expand Down Expand Up @@ -59,13 +60,19 @@ def load_yaml_dict(path: Union[str, Path]) -> Optional[Dict[str, Any]]:
return data


def save_yaml_dict(data: Dict[str, Any], path: Union[str, Path]) -> None:
def save_yaml_dict(
data: Dict[str, Any], path: Union[str, Path], restricted: bool = False
) -> None:
p = Path(path)
p.parent.mkdir(parents=True, exist_ok=True)
with p.open("w") as f:
try:
stream = yaml.dump(data, indent=2, default_flow_style=False)
f.write(stream)

if restricted:
# Restrict file permissions: read and write for owner only (600)
os.chmod(p, 0o600)
except Exception as e:
raise UnexpectedError(f"Failed to save config to {path}:\n{str(e)}") from e

Expand Down

0 comments on commit 7e20611

Please sign in to comment.