I didn’t set out to add “security features” to a scripting language. I wanted to stop shooting myself in the foot. These are the three levers I actually use on my laptop:

  • Unshare — a quick shell over your script so it can’t see or phone the whole world
  • Landlock — a polite wall that says “only touch these paths, please”
  • Intents — a way to say “this is what I’m about to do” and then, in dry‑run, just rehearse it

This isn’t a manifesto. It’s a couple of small habits that saved me from dumb mistakes. Here’s exactly how they work in Rye, and a few little stories of when they helped.

Exact call forms (Rye‑itself is a value‑kind)

You don’t cc Rye‑itself. It’s a value‑kind exposed via the rye function that returns “Rye‑itself.” Call helpers like this:

rye .Is-unshare          ; boolean
rye .Unshare-config?     ; {active fs net pid uts}
rye .Require-unshared    ; exits(1) if not unshared (CI guard)

ll: rye .Landlock        ; returns a builder
ll | landlock//Limit-to-cwd
ll | landlock//Allow %/tmp/out
ll | landlock//Enforce

; Convenience for common case
rye .Landlock-to-cwd

Part 1 — Unshare (Linux namespaces; OS‑level isolation)

Case: Tuesday afternoon save

I pasted a clever snippet from a gist. It tried to “tidy” my repo and also ping an API. I ran it with unshare and watched the network part fail harmlessly, and the “tidy” part see only my project under /app. No damage, lesson learned.

  • Run it safely:
    rye --unshare myscript.rye
    # need network for a follow-up? opt in:
    rye --unshare --unshare-net=false myscript.rye
    
  • Inside the script, show posture:
    cfg: rye .Unshare-config?
    print cfg   ; {active fs net pid uts}
    if rye .Is-unshare-net { print "offline" } else { print "online" }
    

What it does (concretely):

  • Parent re‑execs Rye with clone flags (new user/mount/net/pid/uts as requested). Your UID/GID is mapped to root inside the user namespace so mounts work without real root.
  • Filesystem:
    • Makes mounts private
    • Creates tmpfs jail under /tmp/rye_jail_<pid>
    • Bind‑mounts your CWD to <jail>/app
    • Attempts to remount <jail>/app read‑only (best‑effort)
    • pivot_root into the jail and chdir to /app
  • PID:
    • If enabled, the child is PID 1 in its namespace
    • Procfs is mounted inside the jail at /proc for predictable tooling
  • Network:
    • If enabled, new net namespace → no outbound network
  • UTS:
    • Separate hostname (cosmetic isolation)

Flags and policy:

  • CLI: --unshare (master), --unshare-fs, --unshare-net, --unshare-pid, --unshare-uts (all default true)
  • .ryesec:
unshare:
  enabled: true
  fs: true
  net: true
  pid: true
  uts: true

Runtime detection:

rye .Is-unshare
rye .Is-unshare-fs
rye .Is-unshare-net
rye .Is-unshare-pid
rye .Is-unshare-uts
rye .Unshare-config?   ; {active fs net pid uts}
rye .Require-unshared  ; exit(1) if not sandboxed (great for CI)

Notes and caveats:

  • Linux only
  • Read‑only remount of /app is best‑effort: if RO fails, a warning is printed. Prefer writing to /tmp or an explicit artifacts dir.
  • Unshare happens at startup; you can’t toggle it mid‑run.

Part 2 — Landlock (Filesystem allowlist; Linux)

Case: Friday CI mystery

A flaky test sometimes wrote a temp file into the repo, sometimes into /tmp. I added a tiny Landlock allowlist: project tree + /tmp/ci-artifacts. The test either behaved or failed with a clear message. That’s all I wanted.

  • Apply after startup (and after unshare, if you use it):
    ll: rye .Landlock
    ll | landlock//Limit-to-cwd
    ll | landlock//Allow %/tmp/ci-artifacts
    ll | landlock//Enforce    ; one-shot lockdown
    
    ; writes that obey the policy
    write %/tmp/ci-artifacts/report.json (to-json data)
    

What it does (and doesn’t):

  • Landlock restricts filesystem access of the running process to a ruleset you choose.
  • It does NOT restrict network, processes, or syscalls. Filesystem only.
  • Unprivileged and composes with unshare.

How to use (builder flow):

ll: rye .Landlock              ; builder
ll | landlock//Limit-to-cwd    ; allow project tree
ll | landlock//Allow %/tmp/out ; allow a temp artifacts dir
ll | landlock//Enforce         ; one‑shot lockdown

; Or the convenience helper
rye .Landlock-to-cwd

Advice:

  • If you also use unshare, apply unshare first (CLI), then Landlock in code so the rules match the jail view.
  • Linux‑only; branch gracefully elsewhere.

Part 3 — Intents and dry‑run (designing side effects)

Case: Midnight deploy rehearsal

I wanted to see exactly what a report job would write without touching disk. I wrapped the writes in an output-intent block, flipped --dry-run, and got a clean list of “would write …” lines. Then I turned dry‑run off and let it do the real thing.

  • Tiny helper and usage:
    output-intent: fn { meta blk } {
      if rye .Is-dry-run {
        print "DRY-RUN: " + (meta |to-json)
      } else {
        do blk
      }
    }
    
    html: "<h1>Report</h1>"
    output-intent { file: "/tmp/report.html", note: "final report" } {
      write %/tmp/report.html html
    }
    

Why intents matter:

  • Intents make side effects explicit and replaceable.
    • Output‑intent: declare what you’ll write and where; during dry‑run, log instead of writing.
    • Input‑intent: declare how values are obtained; a Scenario can provide fixtures.

Dry‑run (runtime flag):

  • --dry-run sets a flag so rye .Is-dry-run returns true.

Output‑intent pattern:

output-intent: fn { meta blk } {
  if rye .Is-dry-run {
    print "DRY-RUN: " + (meta |to-json)
  } else {
    do blk
  }
}

; Usage: logs ‘x’ and ‘y’ when dry‑run, otherwise writes the file
x: "report head"
y: "report tail"
output-intent { file: "/tmp/report.txt", note: "write report" } {
  write %/tmp/report.txt (+ x y)
}

Input‑intent + Scenario (offline fixtures):

; Pseudo‑shape: scenario registers a provider for a key
; Your code checks unshare and picks the intent when offline

fetch-homepage: fn { } {
  if rye .Is-unshare-net {
    ; use scenario‑provided fixture
    (intent.input.http "GET https://example.com")  ; align with your scenario API
  } else {
    cc batteries
    http-get "https://example.com"
  }
}

Recipes

  1. CI strict mode (no net, controlled writes)
  • .ryesec enables unshare
  • Prelude:
rye .Require-unshared
if rye .Is-dry-run { print "dry-run: outputs logged; no writes" }
  • Landlock and outputs:
ll: rye .Landlock
ll | landlock//Limit-to-cwd
ll | landlock//Allow %/tmp/ci-artifacts
ll | landlock//Enforce
  • Wrap writes in output-intent; Scenario provides inputs for network.
  1. Local dev, safe by default
  • rye --unshare .
  • Write artifacts to /tmp (or a configured dir)
  • Offline branches when rye .Is-unshare-net
  • Use dry‑run to rehearse outputs without touching disk
  1. Workshops/training
  • .ryesec with unshare enabled
  • Scripts branch on rye .Is-unshare-* and show posture explicitly
  • No network by default; output-intent logs or writes to /tmp

Caveats (what to expect)

  • Linux‑only for unshare and Landlock; degrade gracefully on other OSes
  • Read‑only /app remount is best‑effort; prefer explicit out dirs or /tmp
  • Landlock cannot block network — use unshare‑net (and/or intents) for that
  • Enforce Landlock only once; it’s process‑lifetime

Minimal exact snippets

Check sandbox posture:

cfg: rye .Unshare-config?
print cfg           ; {active: true, fs: true, net: true, pid: true, uts: true}

Require it (CI):

rye .Require-unshared

Offline‑friendly fetch:

res: if rye .Is-unshare-net {
  (intent.input.http "GET https://example.com")
} else {
  cc batteries
  http-get "https://example.com"
}

Write with output‑intent:

output-intent { file: "/tmp/out.txt", why: "report" } {
  write %/tmp/out.txt "hello"
}

Lock FS to project and artifacts:

ll: rye .Landlock
ll | landlock//Limit-to-cwd
ll | landlock//Allow %/tmp/artifacts
ll | landlock//Enforce

Wrap‑up

  • Unshare reduces ambient hazards at the kernel level (namespaces + a small jail)
  • Landlock narrows which files/dirs the process can touch (allowlist)
  • Intents make side effects explicit; output‑intent + dry‑run lets you rehearse; input‑intent + Scenario lets you stay offline and deterministic
  • The exact call style is rye .Word — Rye‑itself is a value‑kind, not a cc context

Combine them to make Rye scripts safer by default without container machinery.