Repository navigation
Conversation
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>
Contributor
Author
|
None of the four failed jobs failed a test. Each reached its 60-minute limit inside nimble and was cancelled:
Without |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs nim-lang/RFCs#578
Compiling a Nim project runs its compile-time code. Macros and
staticblocks can callstaticExec, read any file the compiler can read, write files and change the environment, and the project'snim.cfgand 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,gorgeExand NimScript'sexecrun only the command lines given with--staticExecWhitelist:"<command line>"(can be repeated). Any other command is a compile-time error that names it.gitwould also allowgit -c alias.x='!sh -c ...' x, andsh -cspeaks for itself.staticRead/slurp,readFile,readLines,fileExists,dirExistsand the procs ofstd/staticosread only below the project directory, the--pathand nimble package directories and Nim'slib, after symbolic links are resolved. Other paths are compile-time errors.writeFile,createDir,putEnvanddelEnvat compile time, and the NimScript procs that change files or the environment, are errors.nim.cfg,config.nims,<project>.nim.cfgand<project>.nimsare skipped. Anim.cfgcan 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:vmopsDangercannot be combined with it, as itsexecCmdExbypasses the whitelist.nim checkand nimsuggest keep skippingstaticExecand the procs that write files, with or without the switch.Change
compiler/options.nim:compileTimeEffectsanswers run / skip / restrict. The conditiondefined(nimsuggest) or cmd == cmdCheckwas repeated invm.nim,vmops.nimandscriptconfig.nim; every former site now calls this one function.compiler/gorgeimpl.nim:checkCommandAllowed, used byopGorgeand NimScript'sexec.compiler/vmdeps.nim:checkCompileTimeRead, used byopSlurpand the read callbacks invmops.nim.compiler/nimconf.nim:loadConfigsskips 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/:staticExec(a longer command line that starts with a whitelisted one), a whitelisted one that runs,gorgeExwith an empty whitelist,writeFile,createDir,putEnv,delEnv, andvmopsDangerwith the switch;trestricted.nimruns the compiler on generated projects:nim.cfgcannot switch the mode off or widen the whitelist; the user configuration can set both;exectakes the whitelist;staticExecresult does not bypass the whitelist;--pathandlibwork; reads outside, also through symbolic links, are refused;nim checkstill skipsstaticExecandwriteFile, with or without the switch.On devel 14991af (2.4.0),
testament pcat compiletime_restrictedandtestament cat vm:define:musl, as inci_alpine.yml): 9/9, 146/146.cat vm145/146. One timing test hit its time limit, a different one in each of two runs (t20746, thentslow_tables); both pass natively.The patched compiler builds itself.
Not covered
{.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.includeandimportof files outside the directories above.expandFilenamedoes not resolve them).