Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix: exclude dotenv inline comments from credential values (#257)
Rudy Mizrahi Celeklib94343329cef
diff --git a/ifixai/cli/init.py b/ifixai/cli/init.pyindex d5006a6..64d66f0 100644--- a/ifixai/cli/init.py+++ b/ifixai/cli/init.py@@ -1,6 +1,7 @@import os+import refrom pathlib import Pathimport click@@ -52,6 +53,22 @@ def detect_available_providers() -> list[tuple[str, str]]:return available+def _parse_dotenv_value(raw: str) -> str:+ value = raw.strip()+ if value.startswith(("\"", "'")):+ quote = value[0]+ escaped = False+ for index, character in enumerate(value[1:], start=1):+ if character == quote and not escaped:+ suffix = value[index + 1:].strip()+ if not suffix or suffix.startswith("#"):+ return value[1:index]+ return value+ escaped = character == "\\" and not escaped+ return value+ return re.sub(r"\s+#.*$", "", raw).strip()++def load_dotenv_file(path: "Path | None" = None) -> list[str]:"""Load ``KEY=VALUE`` pairs from a ``.env`` file (the cwd's by default) into``os.environ`` without overriding variables already set, returning the names of@@ -78,9 +95,7 @@ def load_dotenv_file(path: "Path | None" = None) -> list[str]:if not sep:continuekey = key.strip()- value = value.strip()- if len(value) >= 2 and value[0] == value[-1] and value[0] in ('"', "'"):- value = value[1:-1]+ value = _parse_dotenv_value(value)if key and key not in os.environ:os.environ[key] = valueloaded.append(key)diff --git a/ifixai/tests/test_dotenv_inline_comments.py b/ifixai/tests/test_dotenv_inline_comments.pynew file mode 100644index 0000000..3dbabc1--- /dev/null+++ b/ifixai/tests/test_dotenv_inline_comments.py@@ -0,0 +1,17 @@+from pathlib import Path++import pytest++from ifixai.cli.init import load_dotenv_file++[email protected]('assignment,expected', [('owned-value # explanatory comment', 'owned-value'), ('"owned-value" # explanatory comment', 'owned-value'), ("'owned-value' # explanatory comment", 'owned-value'), ('owned#value', 'owned#value'), ('"owned # value"', 'owned # value')])+def test_dotenv_comments_do_not_become_part_of_credential(tmp_path: Path, monkeypatch, assignment, expected):+ key = 'IFIXAI_OWNED_TEST_CREDENTIAL'+ monkeypatch.delenv(key, raising=False)+ path = tmp_path / '.env'+ path.write_text(key + '=' + assignment + '\n', encoding='utf-8')+ assert load_dotenv_file(path) == [key]+ import os+ assert os.environ[key] == expected+ monkeypatch.delenv(key)