Skip to content

compiler: --compileTime:restricted with a staticExec whitelist (RFC #578) - #26570

Open
jasagiri wants to merge 5 commits into
nim-lang:develfrom
jasagiri:compile-time-restricted
Open

jasagiri wants to merge 5 commits into
nim-lang:develfrom
jasagiri:compile-time-restricted

Conversation

@jasagiri

Copy link
Copy Markdown
Contributor

Refs nim-lang/RFCs#578

Compiling a Nim project runs its compile-time code. Macros and static blocks can call staticExec, read any file the compiler can read, write files and change the environment, and the project's nim.cfg and NimScript files can run commands too. There is no way to compile code nobody has read without trusting it. Following the discussion in the RFC, this PR does not switch these features off but limits them under a new switch, --compileTime:restricted. The default, --compileTime:normal, is unchanged.

What the switch does

  • staticExec, gorge, gorgeEx and NimScript's exec run only the command lines given with --staticExecWhitelist:"<command line>" (can be repeated). Any other command is a compile-time error that names it.
    • The whitelist holds whole command lines, compared as strings, not program names. A program name is not enough: most programs have options that run other commands, e.g. whitelisting git would also allow git -c alias.x='!sh -c ...' x, and sh -c speaks for itself.
    • The check runs before the gorge cache is consulted, so a cached result does not bypass it.
  • staticRead/slurp, readFile, readLines, fileExists, dirExists and the procs of std/staticos read only below the project directory, the --path and nimble package directories and Nim's lib, after symbolic links are resolved. Other paths are compile-time errors.
  • writeFile, createDir, putEnv and delEnv at compile time, and the NimScript procs that change files or the environment, are errors.
  • No project configuration is read: the project's and its parent directories' nim.cfg, config.nims, <project>.nim.cfg and <project>.nims are skipped. A nim.cfg can run commands without NimScript (gcc.exe = ..., @putenv), so refusing dangerous keys one by one would leave a hole for every key missed. The system and user configuration and the command line still apply; since they are read before the project's, a project can neither switch the mode off nor widen the whitelist. A NimScript file named on the command line still runs.
  • --experimental:vmopsDanger cannot be combined with it, as its execCmdEx bypasses the whitelist.

nim check and nimsuggest keep skipping staticExec and the procs that write files, with or without the switch.

Change

  • compiler/options.nim: compileTimeEffects answers run / skip / restrict. The condition defined(nimsuggest) or cmd == cmdCheck was repeated in vm.nim, vmops.nim and scriptconfig.nim; every former site now calls this one function.
  • compiler/gorgeimpl.nim: checkCommandAllowed, used by opGorge and NimScript's exec.
  • compiler/vmdeps.nim: checkCompileTimeRead, used by opSlurp and the read callbacks in vmops.nim.
  • compiler/nimconf.nim: loadConfigs skips the project and parent configuration in restricted mode.
  • compiler/commands.nim: the two switches.
  • doc/nimc.md (new section "Compiling code you have not read"), doc/advopt.txt, changelog.md.

Tests

tests/compiletime_restricted/:

  • one file each for a refused staticExec (a longer command line that starts with a whitelisted one), a whitelisted one that runs, gorgeEx with an empty whitelist, writeFile, createDir, putEnv, delEnv, and vmopsDanger with the switch;
  • trestricted.nim runs the compiler on generated projects:
    • every project and parent configuration file takes effect without the switch and none does with it (each leaves a marker file or environment value);
    • a project nim.cfg cannot switch the mode off or widen the whitelist; the user configuration can set both;
    • NimScript cannot change files or the environment, and its exec takes the whitelist;
    • a cached staticExec result does not bypass the whitelist;
    • reads below the project, --path and lib work; reads outside, also through symbolic links, are refused;
    • nim check still skips staticExec and writeFile, with or without the switch.

On devel 14991af (2.4.0), testament pcat compiletime_restricted and testament cat vm:

  • macOS arm64: 9/9, 146/146.
  • Linux arm64 (Alpine 3.23 with define:musl, as in ci_alpine.yml): 9/9, 146/146.
  • Linux amd64 (Ubuntu 24.04 under emulation): 9/9; cat vm 145/146. One timing test hit its time limit, a different one in each of two runs (t20746, then tslow_tables); both pass natively.

The patched compiler builds itself.

Not covered

  • The {.passC.}, {.passL.}, {.compile.} and {.link.} pragmas, which hand flags and files to the C compiler and linker.
  • getEnv, which can embed environment variables in the program.
  • include and import of files outside the directories above.
  • On Windows, symbolic links in the paths read are not resolved (expandFilename does not resolve them).
  • Whatever a package manager does before it starts the compiler.
  • Not run on Windows.

jasagiri and others added 4 commits October 28, 2026 16:51
Compiling a project runs its compile-time code. RFC nim-lang#578 asks for a way to
compile code nobody has read without letting it run commands or change
the machine. Araq suggested taming staticExec with a whitelist instead
of switching it off.

The condition `defined(nimsuggest) or cmd == cmdCheck` was repeated in
vm.nim, vmops.nim and scriptconfig.nim. It becomes one function,
`compileTimeEffects`, which answers run / skip (nim check, nimsuggest;
unchanged) / restrict (the new mode), and every former site calls it.

Under --compileTime:restricted:
- staticExec, gorge and gorgeEx (all through opGorge) and NimScript's
  exec run only the command lines given with --staticExecWhitelist, compared
  as whole strings and checked before the gorge cache is consulted;
- compile-time writeFile, createDir, putEnv, delEnv and the NimScript
  procs that change files or the environment are errors;
- --experimental:vmopsDanger is refused, as its execCmdEx bypasses the
  whitelist.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No existing check answered whether compile-time code may read a path:
staticRead/slurp resolved any absolute path, and the VM's readFile,
readLines, fileExists, dirExists and the std/staticos procs read
anything the compiler process can. Araq suggested keeping staticRead to
the source directories.

checkCompileTimeRead (vmdeps.nim) is that one check, called by opSlurp
and by the vmops read callbacks. In restricted mode the path, with
symbolic links resolved, must lie below the project directory, a --path
or nimble package directory, or Nim's lib; anything else is an error that
names the path. On Windows expandFilename does not resolve links.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tricted

A project's nim.cfg can run commands as well as its NimScript files: it
chooses the C compiler executable (gcc.exe, clang.exe) and sets the
environment with @putenv. <project>.nims is not even covered by
--skipProjCfg. Refusing the dangerous keys one by one would leave a hole
for every key missed.

loadConfigs is the one place that reads configuration. In restricted
mode it reads only the system and user configuration; the parent
directories' and the project's nim.cfg, config.nims, <project>.nim.cfg
and <project>.nims are skipped. A NimScript file named on the command
line still runs. Because the user configuration is read before the
project's, a project can then neither switch the mode off nor widen
--staticExecWhitelist; no separate check of where a switch came from is
needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jasagiri

Copy link
Copy Markdown
Contributor Author

None of the four failed jobs failed a test. Each reached its 60-minute limit inside nimble and was cancelled:

  • Packages CI, ubuntu 2_3: nimble install --depsOnly of alea, the batch's first package.
  • Packages CI, macOS 0_3: nimble test of json_serialization. The same job hung at the same step on nimsuggest2: thin proxy that runs each query as a one-shot IC process (PoC) #26494 (run 40640414632).
  • Packages CI, macOS 2_3: nimble test of nimlsp.
  • Nim CI, OSX_arm64_cpp: nimble install opengl while fetching x11, before any test ran.

Without --compileTime:restricted, the changed calls behave as before. The read check returns at once, and the other paths reach the same cteRun/cteSkip answers as the old nimsuggest or cmdCheck condition. Could someone rerun the failed jobs?

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant