Skip to main content
JSSG gates network requests and process spawning behind explicit capabilities. Its default curated fs module reads within the target directory; writing through fs needs the fs capability. The filesystem rules below describe that module, not a guarantee of complete isolation for arbitrary untrusted code. Review codemods before running them. In a 2.0 workflow, callbacks and JSSG transforms use codemod/files instead of fs. The fs behavior and capability flags below apply to standalone JSSG commands.

Why defense in depth?

Codemods run arbitrary code transformations on your codebase. Capabilities help you review and limit access:
  • Explicit grants: Network requests, process spawning, and unrestricted fs require approval.
  • Audit trail: The capabilities field in codemod.yaml provides a clear record of what extra permissions a codemod requires.
  • Principle of least privilege: The default surface is the minimum a typical transformation needs — just the files under your target directory.

File system — sandboxed by default

Unlike fetch and child_process, the fs module is always available but is constrained to the workflow’s target directory (the folder you’re running the codemod against), and it only reads. Reads inside that directory succeed transparently; paths that normalize outside it are rejected with a Node-style EACCES error. Writes (writeFileSync, mkdirSync, unlinkSync and their promise forms) fail with EROFS and a message naming --allow-fs, as child_process needs --allow-child-process. A transform changes its own file by returning the new text, and other files with jssgTransform.

Inside target_dir

Reads allowed by defaultreadFileSync, readdirSync (including { withFileTypes: true } Dirents), statSync, existsSync, accessSync, and their fs/promises equivalents work. writeFileSync, mkdirSync and unlinkSync need --allow-fs.

Outside target_dir

Rejected with EACCESAbsolute paths outside the target or .. traversals that escape it throw a Node-style error with err.code === "EACCES".
scripts/codemod.ts
Path comparisons first normalize ./.. segments and collapse redundant slashes before the prefix check. When the curated fs is backed by real disk (the CLI), the resolver additionally walks each path component with symlink_metadata and rejects any path that traverses a symlink — a pre-existing target_dir/link-to-elsewhere/secret won’t slip past the guard even though its lexical form stays under target_dir.

Error codes

The curated fs surfaces standard Node err.code strings so existing catch logic keeps working:

Want unrestricted disk access?

If a codemod legitimately needs to write files, or to read outside the target directory (caches, home-directory config, scratch paths in /tmp), opt in explicitly — see Unsafe capabilities below. That swaps the curated fs for the unrestricted LLRT fs module and the path guard is no longer applied.

Unsafe capabilities require explicit permission

These still require opt-in via capabilities:

fs (unrestricted)

Full real-disk accessUpgrades from the sandboxed, read-only curated fs to LLRT’s real-disk fs module. No target_dir prefix check. Use only when the codemod genuinely needs to write files with fs, or paths outside the target. In a dry run, its writes go to memory.

fetch

Network requestsMake HTTP/HTTPS requests. Fully deny-by-default.

child_process

Process spawningExecute external commands. Fully deny-by-default.

Default (safe) modules

These modules are always available and require no special permissions:
  • assert - Assertion testing
  • buffer - Binary data manipulation
  • console - Logging and debugging
  • crypto - Cryptographic operations
  • events - Event emitter patterns
  • fs (read-only, sandboxed to target_dir)
  • os - Operating system information (read-only)
  • path - File path utilities
  • perf_hooks - Performance measurement
  • process - Process information (read-only, no spawning)
  • string_decoder - String encoding/decoding
  • timers - Timer functions
  • tty - TTY operations
  • util - Utility functions
  • stream_web - Web Streams API
  • url - URL parsing and manipulation
  • zlib - Compression/decompression
Default modules don’t perform network requests or spawn processes. fs is in this list because its default form only reads, within target_dir; adding fs to capabilities upgrades it to the unrestricted variant that can read and write anywhere on disk.

Enabling capabilities

To enable unsafe capabilities (or to upgrade fs from sandboxed to unrestricted), add a capabilities field to your codemod.yaml file:
codemod.yaml
Only request capabilities your codemod actually needs. Adding fs here removes the target_dir guard — the codemod can now read and write anywhere on disk. If you only need files in the repo, don’t list it.

Capability names

Use these exact strings in the capabilities array:

Usage examples

Default (sandboxed) file system

No capabilities entry needed — this just works:
scripts/codemod.ts

Unrestricted file system

1

Enable the fs capability (real-disk)

codemod.yaml
2

Use fs anywhere on disk

scripts/codemod.ts
Listing fs gives the codemod read/write access to every path the host user has permission for. Review untrusted codemods carefully before enabling it.

Network requests

1

Enable fetch capability

codemod.yaml
2

Use fetch in your transform

scripts/codemod.ts
Note the async transform function when using fetch or other asynchronous operations.

Running external commands

1

Enable child_process capability

codemod.yaml
2

Spawn processes in your transform

scripts/codemod.ts
Use an argument array without a shell, validate input, and reject values that could be interpreted as command options. LLRT provides only part of Node’s API; the public JSSG types expose spawn, not execSync. This example runs once per processed file; put repo-wide commands in a workflow shell step instead.

CLI override flags

You can also enable capabilities via CLI flags when running a codemod:
CLI flags take precedence over codemod.yaml. This is useful for testing or one-off runs where you trust the codemod source. --allow-fs swaps the read-only sandboxed fs for the unrestricted variant — if your codemod only reads files inside the target directory, you don’t need to pass it.
In a workflow (codemod workflow run, codemod <package>), transforms and forEachFile callbacks cannot make network requests, start processes or write through fs. For workflow runs, the --allow-* flags are deprecated and ignored. Use a shell, http or agent step instead; see Upgrade from 1.x.

Best practices

If your codemod only reads files inside the repo, don’t add fs to capabilities. The default sandboxed fs already works and keeps the scope of your codemod tight for reviewers. Change files by returning the new text, or with jssgTransform, rather than with fs writes.
Only request capabilities your codemod truly needs. For example, if you only read config from the repo, you don’t need any capabilities at all.
In your README, explain why each capability is required and what operations use it.
README.md
Pass validated arguments without enabling a shell. This illustrative helper accepts a branch supplied by your workflow and waits for Git to exit:
If possible, provide a version of your codemod that works without unsafe capabilities for maximum trust.

Security considerations

Untrusted codemods: Review the code before running it, especially before granting fs (unrestricted), fetch, or child_process. Target-scoped fs is not a substitute for isolating untrusted execution from sensitive files and credentials.

Next steps

JSSG

Build your first JSSG codemod

Advanced Topics

Transform APIs, workflow context, multi-file work, and performance