Repository navigation
Description
Mock.* was intended to be callable only from testing code. That restriction was never implemented, and Mock is reachable from every Kex program.
src/stdlib/prelude.kex has using HTTP, and module Mock lives in src/stdlib/http.kex, so the whole Mock tree is auto-imported. No using, no test framework, no flag.
Reproductions
All of these are plain main blocks with no imports, run on the current build.
Filesystem — reads are redirected globally:
main do
IO.printLine("real: ${FS.File.read("/etc/hostname").or("(none)")}")
Mock.FS.File("/etc/hostname", "attacker-controlled")
IO.printLine("mocked: ${FS.File.read("/etc/hostname").or("(none)")}")
IO.printLine("exists? ${FS.File.exists?("/etc/hostname")}")
end
real: (none)
mocked: attacker-controlled
exists? true
Environment, platform, and HTTP:
main do
Mock.ENV.set("HOME", "/tmp/attacker")
IO.printLine("HOME: ${ENV.get("HOME").or("(unset)")}")
Mock.System.OS(:windows)
IO.printLine("OS: ${System.OS}")
Mock.Http.start()
Mock.Http.respond(200, "{\"licensed\":true}")
IO.printLine("HTTP: ${Http.get("https://real.example.com/verify").showValue}")
end
HOME: /tmp/attacker
OS: :windows
HTTP: HttpResponse { body: {"licensed":true}, headers: { }, status: 200 }
Why this matters, stated precisely
This is not a sandbox escape. A malicious dependency already runs arbitrary code and could touch the filesystem directly. The harm is narrower and subtler: Mock lets one part of a program lie to another part, globally, through a call that looks innocuous.
With tey pulling third-party packages, a dependency can install a mock at load time and thereafter:
- make a licence or auth check read a fabricated config file, or receive a fabricated HTTP 200;
- redirect where the program writes, via
Mock.ENV.set("HOME", ...); - flip platform-dependent branches (path separators, shell quoting) via
Mock.System.OS; - capture stdout via
Mock.IO.
The deception is invisible at the point that matters: FS.File.read(configPath) reviews as safe, because the lie was installed elsewhere. Global mock state also means the effect is unbounded in time and scope — it persists until something calls clear.
Proposed fix
1. Gate the mock intrinsics on test mode (recommended, cheapest, ship first). The Kex.Intrinsic.FS.file / Http.mockStart / Env.set / System.OS intrinsics become no-ops — or better, hard errors — unless the runtime is running a spec suite. A compiled production binary then cannot be hijacked no matter who calls Mock. This is orthogonal to everything below and implements the original intent directly.
2. Remove Mock from the automatic prelude. Move it out of http.kex (where it is only auto-imported by accident of using HTTP) and behind an explicit using. This reduces accidental exposure but does not stop a determined dependency, which can import it too. Do it for hygiene, not as the security control.
3. Scope the override — #143. A lexically scoped with cannot affect code outside its block, which turns a global switch into a local one and removes the "unbounded in time and scope" property entirely. This is the principled fix; #143 already proposes auto-scoping mock state to the enclosing it block as its cheapest increment.
(1) and (3) are complementary: (1) stops non-test code from mocking at all, (3) stops test code from leaking a mock beyond its block.
Secondary bug found while reproducing
Installing an FS mock breaks resolution of FS.File.read when the call sits in a separately-defined function rather than inline in main:
foul readIt(p: String) -> String = FS.File.read(p).or("(none)")
main do
IO.printLine(readIt("/etc/hostname"))
Mock.FS.File("/etc/hostname", "x")
IO.printLine(readIt("/etc/hostname"))
end
(none)
Internal error: runtime error: Undefined function: FS.File.read
The first call resolves; after Mock.FS.File, the same call reports the function as undefined. Whatever the mock installs appears to replace the FS.File binding rather than overlay it. Probably worth splitting into its own issue once someone looks at the mock intrinsics.
Activity
Merge pull request #146 from kexhq/fix-mocks