Safeguard
Developer Tools•

Are Swift macros sandboxed? What a macro can and cannot touch

Swift macros are separate programs that rewrite syntax. The design proposal says they run sandboxed, with no file or network access. Here is what that means.

Safeguard Security Team
3 min read

Per the Swift expression macros proposal, macro implementations are intended to run in a sandbox, like SwiftPM plugins, which prevents file system and network access. A macro should depend on the syntax it is given and nothing else. That reduces risk but does not remove the need to review macro packages you add.

What a macro is

A macro is a separate program that works on Swift syntax trees through the swift-syntax package. The externalMacro built in invokes that program and hands it the source code of the macro expansion. The result is more Swift code, inserted at the use site and compiled with your app.

What the proposal says about security

PointDetail
SandboxImplementations are executed in a sandbox like SwiftPM plugins
BlockedFile system and network access
InputThe expansion node, its child nodes, and what the expansion context provides
RecursionMacro expansion cannot be recursive

The stated reasons are security and encouraging macros to depend on their input alone. The proposal leaves the details of how implementation modules are built and provided to the compiler to a separate proposal, and it does not specify how the sandbox is enforced on each platform.

What a sandbox does not cover

The sandbox restricts what the macro can do while it runs. It does not constrain what code the macro writes. A macro could expand into code that reads files or calls a server when your app runs. The sandbox protects the build machine, not your users.

A review routine for macro packages

  1. Treat a macro package like any other dependency. Add it to your inventory with a version.
  2. Expand the macros in a scratch project and read the generated code.
  3. Check what the generated code does at runtime: network calls, storage, reflection.
  4. Pin the version, and review again on updates.
  5. Prefer macros whose generated code is small and readable.
  6. Check the maintainer and the repository URL before adding.

See also how to list every dependency in an iOS app for keeping that inventory.

Try it yourself

Click through it at your own pace, or open it full screen. Notice how every package is listed with its version, which is where a macro package belongs.

Safeguard builds an SBOM and scans your dependencies for known vulnerabilities, and you can run a free scan at gold.safeguard.sh/scan to see what your own project pulls in.

Quick check

Can a macro read my files during compilation?

The proposal says the sandbox prevents file system and network access for macro implementations.

Can macro generated code still be harmful?

Yes. Generated code is compiled into your app and runs with your app's permissions, so read it.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.