keepcalculator

06 Numbers and Powers

Run AI-Generated Code Safely in VS Code

Card No. 06·25578 · Updated · Standard formula, shown below

Running AI-generated code in Visual Studio Code can help you test a script or project, but code may contain mistakes, destructive commands, or unexpected network activity. Use an isolated workspace, inspect the files and commands first, and start with the least access the code needs.

What You’ll Need

  • Visual Studio Code, installed from its official download source.
  • The code files and any setup instructions. Treat instructions from a chat, repository, or generated README as untrusted until checked.
  • A language runtime or compiler only if the code needs one, such as Python or Node.js. Install these from their official sources.
  • A way to run the project with limited access, such as a disposable virtual machine or container, if the code is unfamiliar or could affect important data.

Step-by-Step

  1. Choose a safe place for the project. Create a new, empty folder for the code. Do not put it in your home folder, a synced folder, a system directory, or a folder containing personal or work files. If you are testing code you do not trust, use a disposable virtual machine or container. A separate folder by itself is not a security boundary: programs you run normally inherit your account’s permissions.
  2. Open only that folder in VS Code. Choose File > Open Folder and select the new project folder. Check the Explorer pane to see what is there. Watch for scripts, hidden files, unfamiliar extensions, and unexpected folders such as startup scripts or dependency hooks. Do not open a random project in a window where you have other sensitive work open.
  3. Review the code before running it. Read all files that could execute, not just the main file. Search for commands that delete or overwrite files, access environment variables, read credentials, launch subprocesses, download or upload data, or change system settings. In VS Code, use Search to look across the folder. Be cautious with commands such as rm, del, sudo, shell execution functions, and code that makes network requests. These patterns are not automatically malicious, but you should understand why they are present.
  4. Inspect setup instructions and dependencies. Check files such as package.json, requirements.txt, lockfiles, shell scripts, and README instructions. A package install can run code, so do not blindly copy commands or install packages just because the project requests them. Confirm that a dependency is needed and that its name is spelled correctly; lookalike package names can be a warning sign. Avoid installing globally when a project-local environment will work.
  5. Review VS Code workspace trust. If VS Code asks whether you trust the authors of files in the folder, choose Restricted Mode or do not trust the folder until you have reviewed it. Trusting a workspace can enable project tasks, debugging configurations, and extensions to run code. Do not approve a prompt simply to make it disappear. You can manage this through the workspace trust prompt or the Workspaces: Manage Workspace Trust command in the Command Palette.
  6. Check extensions and automatic actions. Use only extensions you need, and install them from the VS Code Marketplace after checking the publisher and purpose. Extensions can access workspace files and may run code. Do not install an extension recommended by an unfamiliar project without verifying it. Be wary of prompts to enable automatic tasks, configure a debugger, or run a post-install script.
  7. Set up an isolated environment. For Python, a virtual environment keeps project packages separate from other Python projects, but it does not isolate the program from your files or account. For stronger separation, use a disposable VM or container with no personal folders mounted and no unnecessary network access. Do not put passwords, API keys, cloud credentials, or private SSH keys in the project or its environment. Containers are useful but are not a perfect security boundary; keep them updated and avoid privileged settings or broad host-folder mounts.
  8. Run a small, understandable test. Start with a non-sensitive sample input and a command you have inspected. In the integrated terminal, verify the current folder before running anything, and avoid commands that require administrator privileges. Do not pipe downloaded content directly into a shell. Watch the terminal output and any file changes. Stop the process if it behaves unexpectedly; use the terminal’s stop control or interrupt it with the appropriate keyboard shortcut for your system.
  9. Check the results and clean up. Confirm that the output matches what the code should do and that it has not created, changed, or sent unexpected data. Review files changed in the Explorer or Source Control view. Once finished, delete the disposable environment or project folder if you no longer need it. If you entered a secret into code that may have exposed it, revoke or rotate that credential using the relevant service.

Common Mistakes

  • Assuming generated code is safe because it looks plausible. Code can be syntactically correct and still delete data, leak information, or install unwanted software.
  • Confusing a Python virtual environment with a sandbox. It separates packages, not permissions. Code can generally still access files and network resources available to your account.
  • Trusting the workspace to enable a feature. Workspace trust affects whether certain project features can run; it does not certify that the code is safe.
  • Running setup commands without reading them. A short install command can execute scripts or fetch additional code. Understand each command before approving it.
  • Testing with real data or credentials. Use synthetic sample data and keep secrets out of source files, terminal commands, and logs.

Tips

  • Ask for a plain-language explanation of each file and command, then verify the explanation against the actual code. An explanation is not a safety guarantee.
  • Use version control or a disposable copy so you can identify and undo unexpected file changes.
  • Prefer a test environment with minimal permissions, no sensitive files, and network access disabled unless the task requires it.
  • Keep VS Code, runtimes, extensions, and your operating system updated through their normal update channels.
  • For code that handles sensitive data or performs consequential actions, ask a qualified developer or security professional to review it before use.

Troubleshooting

VS Code says the folder is untrusted: Keep it in Restricted Mode while reviewing its contents. Trust it only if you understand the project and accept the consequences of enabling workspace features. If uncertain, inspect a copy in a disposable environment.

The program asks for administrator access: Stop and find out why it needs elevated permissions. Most simple tests should not require them. Do not approve the request merely because the instructions say to; seek a safer method or review.

It tries to access a file or the network: Stop the process and inspect the relevant code and dependencies. If the access is unnecessary, remove or block it before retrying. If it is required, test with non-sensitive data in an isolated environment and allow only what is needed.

A command is not recognized or a package is missing: Confirm the language runtime is installed and that the terminal is in the project folder. Read the project setup instructions, verify each dependency, and install it in a project-local environment where possible. Avoid downloading executables from unknown sites.

The run produces unexpected files or errors: Stop it, inspect terminal output and changed files, and return to a clean copy rather than rerunning blindly. Check that the test input and runtime match the project’s requirements. Do not use real data until the cause is understood.

Frequently Asked Questions

Is AI-generated code safe to run in VS Code?
Not automatically. Review the code, dependencies, and commands, and run unfamiliar projects with limited permissions.

Does VS Code run code just by opening a folder?
Opening a folder does not ordinarily run its main program, but workspace features, tasks, extensions, or prompts can enable code execution. Review trust and run prompts carefully.

Is a virtual environment enough to protect my files?
No. It isolates language packages, not the program’s access to your files, account, or network. Use a VM or appropriately restricted container for stronger isolation.

Should I trust the workspace?
Only after reviewing the project and deciding that its tasks, debugging setup, and other workspace features are acceptable. You can remain in Restricted Mode while inspecting it.

What should I do if I already ran suspicious code?
Stop it, disconnect network access if data transfer may be occurring, and avoid entering more secrets. Review changed files and seek help from your IT or security team if the device or data is important. Rotate any credentials the code may have accessed.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →