Stop an AI Agent from Accessing Sensitive Files
In this article
AI agents are increasingly used to automate tasks, but they can accidentally read or exfiltrate sensitive files if not properly restricted. This guide shows you how to lock down file access for AI agents on Windows, macOS, and Linux, using built-in permissions, sandboxing, and monitoring. You'll learn the essential steps to protect your data without breaking the agent's core functionality.
What You'll Need
- Administrator or root access to the system where the AI agent runs
- Knowledge of the AI agent's installation directory and configuration file location
- A list of the sensitive files or folders you want to protect (e.g., /etc/passwd, C:\Users\Admin\Documents\Private)
- Basic familiarity with your operating system's command line (Terminal, PowerShell, or Command Prompt)
Step-by-Step
- Identify the AI agent's user account. Check if the agent runs under your user account or a dedicated service account. On Linux/macOS, run
ps aux | grep agentto see the username. On Windows, open Task Manager > Details and look at the User name column. - Create a dedicated low-privilege user for the agent (recommended). On Linux/macOS:
sudo useradd -r -m ai_agent. On Windows: Settings > Accounts > Family & other users > Add account > Add a user without a Microsoft account, then set a strong password and change account type to Standard. - Run the agent under that user. Update the service configuration or launch script to use the new user. For systemd services, add
User=ai_agentin the service file. For Docker, use--user $(id -u ai_agent). - Remove read permissions from sensitive files for the agent's user. On Linux/macOS, use
chmodorsetfacl. For example, to deny read to the agent user on a file:setfacl -m u:ai_agent:--- /path/to/sensitive/file. On Windows, right-click the file/folder > Properties > Security > Edit > Add the agent user > Deny Read & Execute. - Use file system access control lists (ACLs) for granular control. On Linux,
setfacl -m u:ai_agent:--- /sensitive/dirandsetfacl -R -m u:ai_agent:--- /sensitive/dirfor recursive. On macOS, usechmod -R -Nto remove existing ACLs first, then add deny rules withchmod +a. - Sandbox the agent with AppArmor (Linux) or macOS sandbox. Create an AppArmor profile that denies read access to specific paths. Example profile snippet:
/sensitive/** r,(remove the 'r' to deny read). For macOS, usesandbox-execwith a profile that denies file-read-data on sensitive paths. - Use a container or VM with read-only mounts. If the agent runs in Docker, mount sensitive directories as read-only or don't mount them at all. For example,
docker run -v /sensitive:/sensitive:romakes it read-only. For a VM, configure shared folders as read-only. - Set environment variables to restrict the agent's working directory. Some AI agents respect a
WORKDIRorHOMEenvironment variable. Set it to a non-sensitive directory like/tmp/ai_agentorC:\AI_Agent\Workspace. - Configure the agent's own settings to exclude paths. Many AI coding assistants (like GitHub Copilot, Cursor) have ignore files. Create a
.aiignoreor similar in the project root and add patterns like*.pem,.env,secrets/. - Monitor access attempts. Enable audit logging. On Linux, use
auditctl -w /sensitive -p r -k sensitive. On Windows, enable Object Access auditing via Group Policy. Check logs regularly to ensure the agent isn't trying to read protected files. - Test the restrictions. Try to access a sensitive file as the agent user. On Linux:
sudo -u ai_agent cat /sensitive/fileshould fail. On Windows, run a command as the agent user and attempt to open the file. If it opens, revisit the permissions.
Common Mistakes
- Only restricting the agent's GUI, not its backend. The agent may have a CLI or API that runs under a different user. Always check all entry points.
- Using chmod 777 on the agent's directory. This gives everyone full access, including the agent, to write and execute, which can lead to privilege escalation.
- Forgetting to apply restrictions recursively. A single file may be safe, but subdirectories might be accessible. Use recursive commands.
- Denying read but allowing execute. On some systems, execute permission can allow reading scripts or binaries. Deny both read and execute for sensitive files.
- Not testing after changes. A misconfigured permission might still allow access. Always test as the agent user.
Tips
- Use the principle of least privilege: give the agent only the permissions it absolutely needs.
- Encrypt sensitive files at rest (e.g., with GPG or BitLocker). Even if the agent reads them, the content is unusable without the key.
- Keep the agent's software updated to patch any vulnerabilities that could bypass file restrictions.
- Use a separate network namespace or firewall rules to prevent the agent from exfiltrating data if it does read something.
- Document all permission changes so you can audit them later.
Troubleshooting
- The agent still reads sensitive files. Check if the agent is running as root or administrator. If so, it bypasses normal permissions. Force it to run as a non-privileged user. Also check for hardlinks or symlinks that might point to sensitive files outside the restricted path.
- Permissions reset after reboot. On Linux, ACLs may be lost if the filesystem doesn't support them or if you didn't use
setfaclproperly. Usechmodfor basic permissions that persist. On Windows, ensure you apply permissions to the folder and not just the file. - Agent fails to start after restrictions. It may need read access to its own configuration files. Add explicit allow rules for the agent's directory before the deny rules. Check the agent's logs for permission errors.
- Docker container ignores read-only mount. Ensure you didn't specify a volume in the Dockerfile that overrides the mount. Use
--read-onlyflag for the entire container root filesystem, and then mount specific writable directories. - AppArmor profile not enforced. Make sure the profile is loaded and the agent is confined. Use
aa-statusto check. Also, the profile might need to include the agent's binary path.
Frequently Asked Questions
Can I just delete the sensitive files?
If you don't need them, yes. But if you need them for other purposes, you must restrict access instead. Deleting is the ultimate restriction but not always feasible.
Will these steps break the AI agent?
If done correctly, no. The agent should still work with the files it needs. Test thoroughly after applying restrictions. If something breaks, adjust the permissions to allow access to the agent's required files only.
What if the agent runs as root or administrator?
You must change that first. Running as a privileged user bypasses all file permissions. Create a dedicated user and run the agent under it.
Can I use a firewall to block the agent from sending data?
Yes, a firewall can block outbound connections, which is a good defense-in-depth measure. But it won't stop the agent from reading files locally. Combine both.
Do I need to do this on every machine the agent runs on?
Yes, each machine that runs the agent needs the same restrictions. If you use a central configuration management tool (like Ansible or Group Policy), you can automate this.
Is it enough to just set the agent's working directory to a safe folder?
No, that's only a soft restriction. The agent can still read files outside that directory if it has permissions. Always enforce file system permissions as well.