Skip to content

Commit c16366d

Browse files
committed
fix(exporter): manejar strings YAML y nombres largos de forma segura en Python 3.11+
1 parent 35f8767 commit c16366d

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

‎packages/prometheus-metrics-exporter/src/exporter/alert_evaluator.py‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,20 @@ def load_config(self, config_input: Union[str, Path, Dict[str, Any]]) -> None:
253253
if isinstance(config_input, dict):
254254
raw_dict = config_input
255255
elif isinstance(config_input, (str, Path)):
256-
path = Path(config_input)
257-
if path.exists() and path.is_file():
256+
input_str = str(config_input)
257+
if len(input_str) > MAX_CONFIG_FILE_SIZE_BYTES:
258+
raise ValueError("Alert configuration string exceeds 1 MB limit.")
259+
260+
is_file = False
261+
if "\n" not in input_str:
262+
try:
263+
path = Path(config_input)
264+
if path.exists() and path.is_file():
265+
is_file = True
266+
except (OSError, ValueError):
267+
is_file = False
268+
269+
if is_file:
258270
# Guardrail: Check file size to avoid DoS (CWE-400)
259271
file_size = path.stat().st_size
260272
if file_size > MAX_CONFIG_FILE_SIZE_BYTES:
@@ -267,9 +279,7 @@ def load_config(self, config_input: Union[str, Path, Dict[str, Any]]) -> None:
267279
raw_dict = loaded if isinstance(loaded, dict) else {}
268280
else:
269281
# Treat as raw YAML string
270-
if len(str(config_input)) > MAX_CONFIG_FILE_SIZE_BYTES:
271-
raise ValueError("Alert configuration string exceeds 1 MB limit.")
272-
loaded = yaml.safe_load(str(config_input))
282+
loaded = yaml.safe_load(input_str)
273283
raw_dict = loaded if isinstance(loaded, dict) else {}
274284
else:
275285
raise TypeError(f"Unsupported config type: {type(config_input)}")

0 commit comments

Comments
 (0)