AI Coding Agent Security: Why Untrusted Project Files Can Override Prompt Restrictions

    AI coding agents process more than user prompts: project instructions, local files, scripts, and tool output can also influence their actions. This article explains how untrusted files can lead to persistent configuration changes, malicious dependency installation, and unauthorized network access. It outlines trust boundaries, five practical security controls, and an end-to-end testing approach to limit the impact of unsafe agent behavior.

    1. The Core Problem: Agents Do Not Follow User Prompts Alone

    Development teams often assume that explicit instructions such as “Do not execute unknown commands” or “Do not modify global configuration” will prevent an AI coding agent from taking dangerous actions. That assumption overlooks the agent’s other input sources.

    An agent capable of reading code, editing files, running commands, and installing dependencies typically processes several types of information:

    • Instructions entered by the user in a conversation;
    • Project documentation in the current directory;
    • Development guidelines stored in version control;
    • Build scripts and dependency manifests;
    • Command output;
    • Data returned by tools;
    • Text retrieved from browsers or external systems.

    These inputs do not have equal trustworthiness. Yet many agents treat instruction files in the working directory as project context without automatically checking who created them, whether they were reviewed, or whether they request actions beyond the project’s scope.

    The risk is therefore not necessarily that the model misunderstood the user’s prompt. It may reflect a deeper failure in trust-boundary design: the user expects only conversational instructions to govern the agent, while the agent also treats local files, scripts, and tool output as authoritative guidance for its work.

    2. A Typical Attack Chain: From File Download to Persistent Configuration Changes

    This type of attack does not necessarily require a sophisticated vulnerability. It can exploit routine developer habits and automated workflows.

    2.1 A Web Page Places a File in a User-Controlled Directory

    Modern operating systems generally prevent web pages from directly executing local programs. Browsers, however, commonly allow pages to initiate file downloads. The download directory becomes an entry point where files can arrive without necessarily triggering defenses aimed at executable malware.

    An attacker does not need to deliver an executable. A seemingly ordinary text file—such as project guidelines, development notes, or environment configuration documentation—may be enough. Its filename can suggest authority, while its contents impersonate team engineering conventions.

    “Not an executable” does not mean “no execution risk.” When downstream tools automatically read and follow text, that text can become part of the control plane.

    2.2 The User Starts an Agent in an Untrusted Directory

    Developers often inspect projects in download, temporary, or extracted-archive directories. If an attacker can influence the agent’s working directory, instruction files placed there may be read.

    The workflow can look entirely routine:

    1. The user downloads an archive or sample project.
    2. They open a terminal in the directory containing the files.
    3. They start an AI coding agent.
    4. The agent scans the directory and reads its documentation.
    5. The agent treats that content as project rules.

    To the user, this is a normal development workflow. From a security perspective, it connects an external input source to an automation system with substantial privileges.

    2.3 Forged Guidelines Encourage High-Risk Actions

    Malicious instructions may avoid obviously dangerous commands at first. Instead, they can frame risky actions as engineering recommendations:

    • Modify user-level configuration to standardize the environment;
    • Install a dependency to provide a missing tool capability;
    • Add a connection to an external service for debugging;
    • Disable confirmation steps to improve efficiency;
    • Send diagnostic data to a remote destination.

    Global configuration changes are particularly dangerous. A file in a project directory should affect only that project. If it persuades the agent to write user-level configuration, its influence can extend to subsequent sessions and unrelated projects.

    2.4 Dependency Installation and External Communication Extend the Attack

    If the agent also installs a malicious dependency, the risk grows. That dependency may execute code during installation or runtime, read environment variables, access local credentials, or send data to an external address.

    Three distinct effects matter here:

    • Configuration changes enable persistence.
    • Dependency installation enables execution and further spread.
    • External connections enable data exfiltration or remote control.

    Together, these actions can give an attacker continuing influence across sessions and projects.

    3. Why Prompt Restrictions Alone Are Not Enough

    Prompts impose behavioral constraints, but configuration files, tool responses, and local scripts may also enter the agent’s context. Without explicit instruction precedence and permission isolation, a conversational “Do not execute” instruction may not reliably outweigh a more specific project file that appears directly relevant to the task.

    A more reliable security model should distinguish at least four layers.

    Layer 1: Explicit User Instructions

    User instructions define the task, but they should not automatically grant every permission. Even a request to “deploy the project” should not implicitly authorize system configuration changes, secret access, or data transmission to arbitrary destinations.

    Layer 2: Reviewed Project Guidelines

    Only guidelines from trusted repositories, reviewed and tracked in version control, should be eligible to influence project behavior. Their location, provenance, and version-control status should be verifiable.

    Layer 3: External Content and Tool Output

    Web pages, downloaded files, logs, API responses, and dependency documentation should be treated as untrusted data. They may provide reference information, but they should not automatically become execution instructions.

    Layer 4: Policies for High-Risk Operations

    Command execution, dependency installation, permission changes, credential access, global configuration changes, and outbound network access should be governed by independent policies—not natural-language judgment alone.

    4. Five Practical Detection and Control Measures

    4.1 Check Entry Points: Restrict Startup in Untrusted Directories

    Organizations can classify working directories by trust level:

    • Download, temporary, and desktop directories: Default to read-only access or block agent startup.
    • Ordinary working directories: Allow analysis, but prohibit automatic execution.
    • Controlled code repositories: Permit limited operations after verifying provenance.
    • Dedicated build environments: Run agents in containers with temporary identities and least-privilege access.

    Startup checks should cover at least the following:

    • Whether the current directory is under a download or temporary path;
    • Whether instruction files exist outside version control;
    • Whether relevant files were recently downloaded by the current user;
    • Whether the project’s source has been reviewed;
    • Whether the directory contains suspicious scripts or unexpected dependency changes.

    4.2 Monitor Configuration Integrity: Detect Global Configuration Drift

    Global configuration should be managed against a known baseline. The following illustrative script uses common system commands to detect changes:

    1. #!/bin/sh
    2. set -eu
    3. CONFIG_FILE="${HOME}/.agent/config.toml"
    4. BASELINE_FILE="${HOME}/.agent/config.toml.sha256"
    5. if [ ! -f "$CONFIG_FILE" ]; then
    6. echo "Configuration file does not exist: $CONFIG_FILE"
    7. exit 0
    8. fi
    9. CURRENT_HASH="$(sha256sum "$CONFIG_FILE" | awk '{print $1}')"
    10. if [ ! -f "$BASELINE_FILE" ]; then
    11. echo "$CURRENT_HASH" > "$BASELINE_FILE"
    12. echo "Initial baseline created; verify that the current configuration is trusted"
    13. exit 0
    14. fi
    15. BASELINE_HASH="$(cat "$BASELINE_FILE")"
    16. if [ "$CURRENT_HASH" != "$BASELINE_HASH" ]; then
    17. echo "Warning: configuration drift detected"
    18. echo "Baseline: $BASELINE_HASH"
    19. echo "Current: $CURRENT_HASH"
    20. echo "Recommendation: pause the agent, preserve audit evidence, and rotate relevant credentials"
    21. exit 2
    22. fi
    23. echo "Configuration integrity check passed"

    Production checks should also inspect new service definitions, startup commands, environment variable references, script paths, and network addresses. A hash shows that a file changed; it does not explain whether the change was legitimate.

    4.3 Separate Instructions from Execution: Require Approval for High-Risk Actions

    Agent capabilities can be divided into three levels:

    • Observation: Read files, analyze code, and generate recommendations.
    • Modification: Edit project files without touching user-level configuration.
    • Execution: Run commands, install dependencies, access the network, or change system settings.

    Start in observation mode by default. Increase permissions only when the task is clear, the directory is trusted, and changes can be audited.

    The following actions should require explicit human approval:

    • Modifying user-level or system-level configuration;
    • Installing dependencies without pinned versions;
    • Executing downloaded scripts;
    • Reading keys, tokens, or credential files;
    • Sending network requests to unregistered destinations;
    • Disabling security checks or confirmation mechanisms.

    4.4 Apply an Egress Allowlist: Limit External Destinations

    Network egress controls are an important defense against data exfiltration. Agent environments should deny outbound access by default and permit only registered code repositories, dependency mirrors, internal services, and logging systems.

    An allowlist should consider more than domain names:

    • Ports;
    • Protocols;
    • Request methods;
    • Process identity;
    • The task being performed;
    • Time windows;
    • Data types.

    Logs should record the destination, initiating process, request time, and response size. Even if an agent is induced to perform a malicious action, a restricted network boundary can reduce its impact.

    4.5 Rotate Credentials on Detection: Treat Configuration Compromise as a Credential Incident

    If an agent is confirmed to have read untrusted instructions and modified global configuration, deleting the offending configuration entry is not enough.

    The response should include:

    1. Stop the affected agent processes immediately.
    2. Preserve configuration files, command history, and system logs.
    3. Compare the configuration before and after the change.
    4. Inspect newly added dependencies and startup scripts.
    5. Revoke or rotate potentially exposed tokens, keys, and sessions.
    6. Review outbound connections and unusual data access.
    7. Rebuild the configuration in a clean environment.
    8. Add indicators from the incident to future detection rules.

    The principle is straightforward: when credential access cannot be ruled out, treat the credentials as potentially exposed.

    5. Security Control Matrix

    Risk Stage Main Concern Recommended Control Verification Method
    File arrival External files enter user directories Isolate download directories and mark file provenance Review directory audit records
    Agent startup Instructions load from an untrusted directory Validate paths and provenance before startup Simulate startup in a download directory
    Instruction ingestion Text is treated as an operational command Treat untrusted content as data, not instructions Review execution approval logs
    Configuration changes Project files influence global behavior Make user-level configuration read-only or tightly control writes Compare baseline hashes and review diffs
    Dependency installation Malicious scripts execute during installation Pin versions, isolate builds, and require human approval Review dependency change records
    Network access Data reaches unknown destinations Deny egress by default and allowlist destinations Query network audit logs
    Incident response Potentially exposed credentials remain in use Immediately disable and rotate affected credentials Review credential lifecycle records

    6. How Security Teams Should Validate the Defenses

    Security testing should go beyond checking whether an agent refuses dangerous prompts. It should cover the full chain:

    • Place a forged instruction file in a download directory.
    • Check whether the agent reads it automatically.
    • Use harmless markers to determine whether it attempts to modify global configuration.
    • Simulate a new service configuration and observe whether approval is required.
    • Introduce a test dependency and verify whether its installation scripts execute automatically.
    • Use an isolated network environment to confirm that unauthorized outbound connections are blocked.
    • Delete and restore configuration files to verify that drift alerts work.
    • Simulate credential exposure and confirm that rotation can be completed within the required response window.

    The goal is not to prove that an agent will never make a mistake. It is to verify that permissions, network restrictions, and audit boundaries contain the impact even when the agent incorrectly follows untrusted content.

    Conclusion

    The security risks of AI coding agents extend beyond the accuracy of model responses. Agents connect natural language, local files, and automated execution. An ordinary text file placed where an agent expects trusted instructions can move from reference material to a source of behavioral control.

    Effective defenses must therefore extend beyond prompts. They should cover entry points, provenance, permissions, configuration integrity, network access, and incident response:

    • Do not start agents in untrusted directories.
    • Do not let project guidelines modify global state by default.
    • Do not allow external text to trigger high-risk actions directly.
    • Do not grant unrestricted network access.
    • When suspicious configuration drift occurs, isolate the environment and rotate potentially exposed credentials.

    When these controls become development gates and runtime policies, an agent that follows the wrong instructions is far less likely to turn that mistake into a broader system compromise.