Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/brahamanbtp_securamind.json
T

1 line
3.5 KiB
JSON

{"repo": "brahamanbtp/securamind", "files": {"agent/fixer.py": {"fa2c24fad623951da27a3a652ec01d7d0cd352fb": {"t": 1785674528.1884243, "n": 3167, "h": "9a52bba11dfc22ab", "c": "# agent/fixer.py — Modal 1.0.2 compatible with local test + remote fixing + dotenv\n\nimport os\nfrom dotenv import load_dotenv\n\n# ✅ Load environment variables from .env for local testing\nload_dotenv()\n\ntry:\n import modal\n\n # ✅ Define Modal App and image (correct API)\n app = modal.App(\"securamind-fixer\")\n image = modal.Image.debian_slim().pip_install(\"requests\")\n\n # ✅ Modal cloud function with secret for remote access\n @app.function(\n image=image,\n secrets=[modal.Secret.from_name(\"securamind-api-keys\")]\n )\n def fix_code_modal(code: str, issues: list) -> dict:\n import requests\n\n def build_prompt(code, issues):\n issue_summary = \"\\n\".join(\n f\"- {i['issue']} on line {i['line_number']}: {i['line']}\" for i in issues\n )\n return f\"\"\"\nYou are a secure code assistant.\n\nBelow is a potentially insecure code snippet. Analyze the code, explain the security issues listed, and then rewrite the code securely.\n\n### Insecure Code:\n{code}\n\n### Issues Detected:\n{issue_summary}\n\n### Your Tasks:\n1. Explain each issue in simple terms.\n2. Provide a secure and corrected version of the code.\nReturn only the explanation followed by the fixed code.\n\"\"\"\n\n prompt = build_prompt(code, issues)\n\n def call_mistral(prompt):\n try:\n api_key = os.environ[\"MISTRAL_API_KEY\"]\n print(\"🔑 API_KEY loaded: ✅ Yes\")\n except KeyError:\n print(\"🔑 API_KEY loaded: ❌ No (MISTRAL_API_KEY not found)\")\n return \"❌ API Key not loaded from Modal secret (missing MISTRAL_API_KEY)\"\n\n url = \"https://api.mistral.ai/v1/chat/completions\"\n headers = {\"Authorization\": f\"Bearer {api_key}\"}\n data = {\n \"model\": \"mistral-small\",\n \"messages\": [{\"role\": \"user\", \"content\": prompt}],\n \"temperature\": 0.4,\n \"max_tokens\": 800\n }\n\n response = requests.post(url, json=data, headers=headers)\n response.raise_for_status()\n return response.json()[\"choices\"][0][\"message\"][\"content\"]\n\n try:\n result = call_mistral(prompt)\n return {\"explanation_and_fix\": result}\n except Exception as e:\n return {\"error\": str(e)}\n\n IS_MODAL_FUNCTION = True\n\nexcept ImportError:\n # Fallback: Modal not available\n def fix_code_modal(code: str, issues: list) -> dict:\n return {\n \"error\": \"Modal is not available in this environment. Cloud fixing disabled.\"\n }\n\n IS_MODAL_FUNCTION = False\n\n# ✅ Local test entrypoint (used via: `modal run -m agent.fixer`)\nif IS_MODAL_FUNCTION:\n @app.local_entrypoint()\n def main():\n test_code = '''\nimport os\npassword = \"1234\"\nos.system(\"rm -rf /\")\n'''\n test_issues = [\n {\"line_number\": 2, \"line\": 'password = \"1234\"', \"issue\": \"Hardcoded password/API key\"},\n {\"line_number\": 3, \"line\": 'os.system(\"rm -rf /\")', \"issue\": \"Use of os.system()\"}\n ]\n result = fix_code_modal.remote(test_code, test_issues)\n print(\"🧪 Fix result:\\n\", result)\n"}}}}