Back to skill

Security audit

Karpathy Compile

Security checks for vulnerabilities and agentic risk

Overview

The skill has a coherent wiki-to-knowledge workflow, but parts of its code allow overly broad local file access, risky test-time code execution, and persistent LLM-generated output without strong safeguards.

Review this skill before installing or running it in a workspace with sensitive files. Use it only with trusted wiki inputs, avoid exposing its filename parameters to untrusted callers, and do not run the bundled cross-project tests unless the sibling skill directories are trusted. The issues look like security hardening gaps rather than deliberate malware.

Vulnerability Patterns
  • Insecure Skill Coding PracticesFinds exploitable flaws such as hardcoded secrets or command injection
  • Skill Instruction HijackingAlters the agent's session goals or safety constraints when the skill loads
  • Agent Memory PoisoningWrites attacker-controlled rules into memory that affect later sessions
  • Remote Payload Retrieval and ExecutionFetches external code whose behavior can change after review
  • Embedded Malicious CodeShips malicious scripts inside the skill and executes them locally
Findings (3)

T09 · Insecure Skill Coding Practices

Error
Location
scripts/__init__.py:72
Finding

Path Traversal Enables Arbitrary Local File Read and Write

Content
View full analysis

Vulnerability Details

File Location: scripts/__init__.py:72 and scripts/__init__.py:299-317
Vulnerability Type: Unvalidated path traversal
Risk Level: High

Vulnerable Code

python
# WikiParser.parse_wiki_file()
filepath = self.wiki_path / filename
if not filepath.exists():
    return []

entries = []
lines = filepath.read_text(encoding="utf-8").split("\n")
python
# CompilePipeline.save_knowledge_points()
if filename is None:
    date_str = datetime.now().strftime("%Y-%m-%d")
    filename = f"knowledge-points-{date_str}.md"

filepath = self.output_path / filename

lines = [
    f"# Knowledge Points - {datetime.now().strftime('%Y-%m-%d')}",
    "",
    f"Generated {len(knowledge_points)} knowledge points",
    "",
    "---",
    ""
]

for kp in knowledge_points:
    lines.append(kp.to_markdown())
    lines.append("")
    lines.append("---")
    lines.append("")

with open(filepath, "w", encoding="utf-8") as f:
    f.write("\n".join(lines))

Technical Analysis

Both parse_wiki_file() and save_knowledge_points() accept a filename and append it to a configured base directory without validating the resulting path. Python's pathlib permits .. components to escape the base directory. If the supplied path is absolute, the absolute path supersedes the base directory entirely.

Consequently, a caller capable of controlling filename can cause:

  • parse_wiki_file() to read an arbitrary accessible UTF-8 file.
  • save_knowledge_points() to create or overwrite an arbitrary writable file.

No canonicalization, containment check, filename allowlist, extension restriction, or safe file-creation policy is applied.

Attack Path

  1. An attacker obtains access to an integration, API, or calling component that passes a filename to either affected method.
  2. For arbitrary reads, the attacker supplies a traversal value such as ../../sensitive-file.
  3. self.wiki_path / filename resolves outside the intended Wiki d ...[truncated 876 chars]
Remediation
View remediation

Remediation Suggestions

  • Reject absolute filenames and any filename containing traversal components.
  • Resolve both the base directory and candidate path before use, then enforce containment:
python
base = self.wiki_path.resolve()
candidate = (base / filename).resolve()

try:
    candidate.relative_to(base)
except ValueError:
    raise ValueError("Filename escapes the permitted directory")
  • Apply the same validation independently to the output directory.
  • Restrict filenames to the expected naming convention and .md extension.
  • For outputs, consider exclusive creation or atomic replacement through a temporary file inside the validated output directory.
  • Avoid exposing raw filename parameters to untrusted callers; accept a generated identifier where possible.
  • Add tests covering ../, nested traversal, absolute paths, symbolic links, and valid in-directory files.

T09 · Insecure Skill Coding Practices

Warning
Location
scripts/__init__.py:131
Finding

Untrusted Wiki Content Permits Indirect LLM Prompt Injection and Knowledge-Base Poisoning

Content
View full analysis

Vulnerability Details

File Location: scripts/__init__.py:131-188
Vulnerability Type: Indirect prompt injection with insufficient output validation
Risk Level: Medium

Vulnerable Code

python
# Build the prompt
contents = "\n".join([f"- {e['content']}" for e in wiki_entries])
prompt = f"""你是知识蒸馏专家。请将以下关于「{topic}」的信息蒸馏为一个知识精华。

原始信息:
{contents}

请生成一个知识精华,包含:
1. 核心概念(一句话概括)
2. 详细说明(2-3句深入解释)
3. 标签(最多3个)

以JSON格式返回:
{{
  "core_concept": "...",
  "detailed_explanation": "...",
  "tags": ["tag1", "tag2"],
  "confidence": "high/medium/low"
}}
"""

# Call the Ollama LLM
try:
    import openai
    client = openai.OpenAI(
        base_url=self.endpoint,
        api_key=self.api_key
    )

    response = client.chat.completions.create(
        model="qwen2.5:14b-instruct-q8_0",
        messages=[
            {"role": "system", "content": "你是一个知识蒸馏专家。"},
            {"role": "user", "content": prompt}
        ],
        temperature=0.3,
        max_tokens=500
    )

    result_text = response.choices[0].message.content

    # Parse JSON
    json_start = result_text.find("{")
    json_end = result_text.rfind("}") + 1
    if json_start >= 0 and json_end > json_start:
        result_json = json.loads(result_text[json_start:json_end])
    else:
        result_json = {
            "core_concept": wiki_entries[0]["content"][:50] if wiki_entries else topic,
            "detailed_explanation": result_text[:200],
            "tags": [topic],
            "confidence": "medium"
        }

    return KnowledgePoint(
        title=topic,
        core_concept=result_json.get("core_concept", ""),
        source=f"compiled from {len(wiki_entries)} wiki entries",
        detailed_explanation=result_json.get("detailed_explanation", ""),
        tags=result_json.get("tags", [topic]),
        created_at=datetime.now().strftime("%Y-%m-%d"),
        confidence=result_json.get("confidence", "medium")
    )

Technical Analysis

Wiki entry content is inser ...[truncated 2151 chars]

Remediation
View remediation

Remediation Suggestions

  • Explicitly mark Wiki records as untrusted reference data and instruct the model never to execute or follow instructions found inside them.
  • Place source records in a structured format with clear boundaries rather than interpolating them into prose.
  • Use provider-supported structured-output or JSON-schema enforcement when available.
  • Validate the parsed response before constructing a KnowledgePoint:
    • Require expected fields and exact types.
    • Restrict confidence to high, medium, or low.
    • Require tags to be a list of no more than three bounded strings.
    • Set maximum lengths for all generated text fields.
    • Reject unexpected properties if they are not needed.
  • Preserve source-level provenance and flag generated records for review where source content is untrusted.
  • Add prompt-injection test cases to ensure source-contained commands are ignored.
  • Consider deterministic extraction or non-LLM preprocessing for fields that do not require generative processing.

T09 · Insecure Skill Coding Practices

Warning
Location
scripts/test_compile.py:14
Finding

Test Collection Dynamically Executes Unverified Sibling Project Modules

Content
View full analysis

Vulnerability Details

File Location: scripts/test_compile.py:14-38 and scripts/test_e2e.py:15-26
Vulnerability Type: Unsafe dynamic module execution during test import
Risk Level: Medium

Vulnerable Code

From scripts/test_compile.py:

python
def load_module(name, path):
    spec = importlib.util.spec_from_file_location(name, path)
    module = importlib.util.module_from_spec(spec)
    sys.modules[name] = module
    spec.loader.exec_module(module)
    return module

# Load all skill __init__.py files
skills_dir = Path(__file__).parent.parent.parent

# Phase 1
p1_init = skills_dir / "karpathy-query-feedback" / "scripts" / "__init__.py"
p1 = load_module("karpathy_query_feedback", p1_init)

# Phase 2
p2_init = skills_dir / "karpathy-compile" / "scripts" / "__init__.py"
p2 = load_module("karpathy_compile", p2_init)

# Phase 3
p3_init = skills_dir / "karpathy-lint" / "scripts" / "__init__.py"
p3 = load_module("karpathy_lint", p3_init)

# Phase 4
p4_init = skills_dir / "dual-retrieval" / "scripts" / "__init__.py"
p4 = load_module("dual_retrieval", p4_init)

From scripts/test_e2e.py:

python
def load_module(name, path):
    spec = importlib.util.spec_from_file_location(name, path)
    module = importlib.util.module_from_spec(spec)
    sys.modules[name] = module
    spec.loader.exec_module(module)
    return module

# Load all modules
p1 = load_module("karpathy_query_feedback", skills_dir / "karpathy-query-feedback" / "scripts" / "__init__.py")
p2 = load_module("karpathy_compile", skills_dir / "karpathy-compile" / "scripts" / "__init__.py")
p3 = load_module("karpathy_lint", skills_dir / "karpathy-lint" / "scripts" / "__init__.py")
p4 = load_module("dual_retrieval", skills_dir / "dual-retrieval" / "scripts" / "__init__.py")

Technical Analysis

Both test files dynamically load Python files from neighboring project directories using exec_module(). These calls occur at module scope rather than inside an explicitly in ...[truncated 1579 chars]

Remediation
View remediation

Remediation Suggestions

  • Do not execute sibling source files at test-module import time.
  • Move integration-only module loading into explicit test functions or fixtures so ordinary test discovery has no cross-project execution side effects.
  • Prefer normal, pinned package dependencies and standard imports over loading arbitrary source paths.
  • Resolve every module path and verify that it belongs to an explicitly trusted root before loading it.
  • Run cross-project integration tests only through an opt-in marker or dedicated test command.
  • Execute integration tests in an isolated environment with minimal filesystem, network, and credential access.
  • Where sibling source loading is unavoidable, verify its integrity against a trusted manifest or immutable build artifact before execution.
Vulnerability Patterns
  • Prompt InjectionInstruction Override, Hidden Instructions, Exfiltration Commands
  • Data ExfiltrationExternal Transmission, Env Variable Harvesting, File System Enumeration
  • MCP Tool PoisoningHidden Instructions, Unicode Deception, Parameter Description Injection
  • Privilege EscalationExcessive Permissions, Sudo/Root Execution, Credential Access
  • Supply ChainUnpinned Dependencies, External Script Fetching, Obfuscated Code
Findings (5)

Natural-Language Policy Violations

Medium
Category
Not specified by scanner
Confidence
84% confidence
Finding

The natural-language instructions and headings are presented in Chinese throughout the skill description, with no indication that users may choose another language or that the locale restriction is required for a region-specific purpose. Under the language/locale policy, forcing a specific language without opt-in is a policy concern.

Content

No source excerpt is available for this finding.

Ssd 1

Medium
Category
Not specified by scanner
Confidence
97% confidence
Finding

Untrusted wiki content is concatenated directly into the LLM prompt without isolation or defensive instruction design. An attacker who can influence wiki entries could embed prompt-injection text that causes the model to ignore the intended distillation task, produce manipulated output, or emit unsafe/irrelevant content into generated knowledge points; the risk is elevated because this code treats model output as trusted structured data for persistence.

Content

No source excerpt is available for this finding.

Natural-Language Policy Violations

Medium
Category
Not specified by scanner
Confidence
94% confidence
Finding

The comment and reassignment of sys.stdout explicitly enforce UTF-8 output behavior for all runs of the script. This is a language/locale-related constraint applied unconditionally, and the file does not offer user opt-in or explain a region-specific requirement that would justify the override.

Content

No source excerpt is available for this finding.

Missing User Warnings

Low
Category
Not specified by scanner
Confidence
93% confidence
Finding

The skill explicitly describes reading wiki files and saving compiled output to the local knowledge-points/ directory and M-Flow, but it does not warn the user about these read/write and persistence side effects. This can cause users to unintentionally expose sensitive wiki content or create stored artifacts in external/local systems without informed consent, especially if the wiki contains proprietary or personal data.

Content

No source excerpt is available for this finding.

Intent-Code Divergence

Low
Category
Not specified by scanner
Confidence
87% confidence
Finding

The docstring at L324 describes main() as a '测试 Compile Pipeline' function, suggesting a simple test harness. In practice, when no wiki entries exist, it creates directories and writes a wiki markdown file to the real knowledge/wiki path, which is a persistent side effect beyond a typical no-side-effect test label.

Content

No source excerpt is available for this finding.