Skip to content

Commit

Permalink
fix path resolution in EmpireConfig (BC-SECURITY#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinnybod authored Dec 22, 2023
1 parent 98fe698 commit d22dc51
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Fixed `Path` variables in EmpireConfig not properly expanding `~` (@Vinnybod)

## [5.8.3] - 2023-12-15

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion empire/server/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


class EmpireBaseModel(BaseModel):
@classmethod
@field_validator("*")
@classmethod
def set_path(cls, v):
if isinstance(v, Path):
return v.expanduser().resolve()
Expand Down
7 changes: 7 additions & 0 deletions empire/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path

import pytest
import yaml
from starlette.testclient import TestClient

from empire.client.src.utils.data_util import get_random_string
Expand Down Expand Up @@ -442,3 +443,9 @@ def client_config_dict():
config_dict = yaml.safe_load(f)

yield config_dict


def load_test_config():
with open(SERVER_CONFIG_LOC) as f:
loaded = yaml.safe_load(f)
return loaded
12 changes: 12 additions & 0 deletions empire/test/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pathlib import Path

from empire.server.core.config import EmpireConfig
from empire.test.conftest import load_test_config


def test_config_resolves_path():
server_config_dict = load_test_config()
server_config_dict["directories"]["downloads"] = "~/.empire/server/downloads"
empire_config = EmpireConfig(server_config_dict)
assert isinstance(empire_config.directories.downloads, Path)
assert not str(empire_config.directories.downloads).startswith("~")
18 changes: 5 additions & 13 deletions empire/test/test_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from pathlib import Path
from unittest.mock import MagicMock

import yaml

from empire.test.conftest import SERVER_CONFIG_LOC
from empire.test.conftest import SERVER_CONFIG_LOC, load_test_config


def test_simple_log_format(monkeypatch):
Expand Down Expand Up @@ -43,7 +41,7 @@ def test_extended_log_format(monkeypatch):
from empire.server.server import setup_logging
from empire.server.utils.log_util import LOG_FORMAT, ColorFormatter

test_config = _load_test_config()
test_config = load_test_config()
test_config["logging"]["simple_console"] = False
modified_config = EmpireConfig(test_config)
monkeypatch.setattr("empire.server.server.empire_config", modified_config)
Expand All @@ -70,7 +68,7 @@ def test_log_level_by_config(monkeypatch):
from empire.server.core.config import EmpireConfig
from empire.server.server import setup_logging

test_config = _load_test_config()
test_config = load_test_config()
test_config["logging"]["level"] = "WaRNiNG" # case insensitive
modified_config = EmpireConfig(test_config)
monkeypatch.setattr("empire.server.server.empire_config", modified_config)
Expand Down Expand Up @@ -101,7 +99,7 @@ def test_log_level_by_arg():
from empire.server.server import setup_logging

config_mock = MagicMock()
test_config = _load_test_config()
test_config = load_test_config()
test_config["logging"]["level"] = "WaRNiNG" # Should be overwritten by arg
config_mock.yaml = test_config

Expand All @@ -124,17 +122,11 @@ def test_log_level_by_debug_arg():
from empire.server.server import setup_logging

config_mock = MagicMock()
test_config = _load_test_config()
test_config = load_test_config()
test_config["logging"]["level"] = "WaRNiNG" # Should be overwritten by arg
config_mock.yaml = test_config

args = arguments.parent_parser.parse_args() # Force reparse of args between runs
setup_logging(args)

assert logging.getLogger().level == logging.DEBUG


def _load_test_config():
with open(SERVER_CONFIG_LOC) as f:
loaded = yaml.safe_load(f)
return loaded

0 comments on commit d22dc51

Please sign in to comment.