Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: restrict auth_config file permissions #1054

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand Up @@ -59,13 +59,20 @@ 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:
if restricted:
# Restrict file permissions: read and write for owner only (600)
p.chmod(0o600)

stream = yaml.dump(data, indent=2, default_flow_style=False)
f.write(stream)

except Exception as e:
raise UnexpectedError(f"Failed to save config to {path}:\n{str(e)}") from e

Expand Down