Skip to content
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
27 changes: 27 additions & 0 deletions tests/unit/test_config_env_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Une variable ${VAR} absente se résout en chaîne vide avec un avertissement
correctement formaté (champs structurés, pas d'argument positionnel orphelin)."""

import logging

from xcore.configurations.helper import _resolve


def test_variable_absente_donne_chaine_vide_sans_erreur_de_logging(monkeypatch):
erreurs: list[BaseException] = []
monkeypatch.setattr(logging, "raiseExceptions", True)
monkeypatch.setattr(
logging.Handler,
"handleError",
lambda self, record: erreurs.append(record),
)
monkeypatch.delenv("XCORE_TEST_VAR_ABSENTE", raising=False)

out = _resolve({"k": "${XCORE_TEST_VAR_ABSENTE}", "l": ["a-${XCORE_TEST_VAR_ABSENTE}"]})

assert out == {"k": "", "l": ["a-"]}
assert erreurs == [], "le log d'avertissement ne doit pas lever d'erreur de formatage"


def test_variable_definie_est_substituee(monkeypatch):
monkeypatch.setenv("XCORE_TEST_VAR_OK", "valeur")
assert _resolve("pre-${XCORE_TEST_VAR_OK}-post") == "pre-valeur-post"
5 changes: 2 additions & 3 deletions xcore/configurations/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ def _sub(m: re.Match) -> str:
resolved = os.environ.get(var)
if resolved is None:
logger.warning(
"Environement Variable not defined; using empty string:",
var,
extra={"var": var},
"variable d'environnement non définie, chaîne vide utilisée",
var=var,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Config can forge text log lines

When an attacker-controlled configuration contains an unresolved ${...} placeholder whose contents include a newline, and the configuration is loaded while the unescaped text formatter is active, _resolve passes the captured variable name as var, and _TextFormatter.format inserts field values into text output without escaping; a newline in the name therefore forges additional log lines, misleading log consumers.

Escape or remove control characters from structured field values before rendering them in the text formatter.

Prompt for AI agents
In `xcore/configurations/helper.py` at line 25:

**Config can forge text log lines**

When an attacker-controlled configuration contains an unresolved `${...}` placeholder whose contents include a newline, and the configuration is loaded while the unescaped text formatter is active, `_resolve` passes the captured variable name as `var`, and `_TextFormatter.format` inserts field values into text output without escaping; a newline in the name therefore forges additional log lines, misleading log consumers.

Escape or remove control characters from structured field values before rendering them in the text formatter.

return ""
return resolved
Expand Down
Loading