I’m not a fan of Deno’s security model (2024)
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
I’m not a fan of Deno’s security model (2024)
I’m not a fan of Deno’s security model (published on August 31, 2024) this has been bothering me for a while now, so I want to write down my thoughts so I can get it out of my head and move on. we’re gonna talk about how Deno approaches security, specifically with its permissions system, and why I think this approach is fundamentally insecure and misleading.
我不喜欢 Deno 的安全模型(发布于 2024 年 8 月 31 日)。这件事困扰我有一段时间了,所以我打算写下我的想法,好让自己释怀并继续前行。我们将讨论 Deno 如何处理安全性,特别是它的权限系统,以及为什么我认为这种方法从根本上是不安全且具有误导性的。
wait, what’s Deno? Deno is a language runtime for JavaScript. it consists of a standard library, a package manager, a TypeScript transpiler and a bunch of other stuff that’s not important for our story. it’s not a JavaScript implementation, so it does not interpret or compile JavaScript, it reuses V8 for that. one of the flagship features of Deno is its permission system (it’s under the “Secure by default” header on their landing page).
等等,什么是 Deno?Deno 是一个 JavaScript 语言运行时。它由标准库、包管理器、TypeScript 转译器以及其他一些对我们这个故事不重要的东西组成。它不是一个 JavaScript 实现,因此它不解释或编译 JavaScript,而是复用了 V8 来完成这些工作。Deno 的旗舰功能之一是它的权限系统(在他们落地页的“默认安全”标题下)。
the idea is simple: as JS code running in Deno needs the standard library to access the outside world, Deno can choose which accesses it allows or disallows, so when you run shelldeno run —allow-read=/tmp/dir —allow-run=uname code.ts the standard library will only allow code.ts to read /tmp/dir and run uname. additionally, a program can request permissions at runtime, so code like typescriptconst r = { name: “read”, path: “/tmp/dir” } as const; await Deno.permissions.request(r); will display a nice little prompt in the terminal: ┌ ⚠️ Deno requests read access to “/tmp/dir”. ├ Requested by Deno.permissions.request() API. ├ Run again with —allow-read to bypass this prompt. └ Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all read permissions) > and you can grant or refuse it.
这个想法很简单:由于在 Deno 中运行的 JS 代码需要通过标准库来访问外部世界,Deno 可以选择允许或禁止哪些访问。因此,当你运行 deno run --allow-read=/tmp/dir --allow-run=uname code.ts 时,标准库将只允许 code.ts 读取 /tmp/dir 并运行 uname。此外,程序可以在运行时请求权限,因此像 const r = { name: "read", path: "/tmp/dir" } as const; await Deno.permissions.request(r); 这样的代码会在终端显示一个漂亮的提示:┌ ⚠️ Deno 请求读取权限以访问 “/tmp/dir”。├ 由 Deno.permissions.request() API 请求。├ 再次运行并加上 —allow-read 以绕过此提示。└ 允许吗?[y/n/A] (y = 是,允许;n = 否,拒绝;A = 允许所有读取权限) > 你可以选择授予或拒绝它。
(aside: I really miss this feature in browser extensions. currently, if your extension needs potential access to any website, it needs to request actual access to all the websites, which is less than ideal) if you like puzzles, this is the point where you can stop and think about what could go wrong with this system.
(题外话:我真的很怀念浏览器扩展中的这个功能。目前,如果你的扩展程序需要潜在地访问任何网站,它就需要请求访问所有网站的实际权限,这并不理想。)如果你喜欢解谜,现在可以停下来思考一下这个系统可能会出什么问题。
the prompts problem of the two specific issues I want to point out this is the less severe one. we’ll discuss it first because this was the first thing I noticed and it kinda immediately irked me when I first read about this. (if you’re only interested in the more juicy part, you can skip to the next section). so, this nice little prompt is displayed in the terminal. it’s not, like, a PolKit GUI window or anything, it’s just text on stdout waiting for input from stdin. this is a wild thing to do.
提示问题是我想要指出的两个具体问题中较轻的一个。我们先讨论它,因为这是我注意到的第一件事,当我第一次读到它时,它立刻让我感到恼火。(如果你只对更劲爆的部分感兴趣,可以跳到下一节)。所以,这个漂亮的提示显示在终端中。它不像 PolKit GUI 窗口之类的东西,它只是 stdout 上的文本,等待来自 stdin 的输入。这样做非常疯狂。
you see, terminal has a somewhat weird API. most of the commands / requests application can send to the terminal are sent in-band, by printing them to stdout. terminal can then reply by sending data to stdin. as Deno allows programs to print arbitrary data to stdout and read from stdin without requiring any permissions or doing any filtering, malicious code can send arbitrary commands to the terminal. it’s usually okay, because terminals won’t, like, execute shell commands just because stdout told them to, you’re supposed to be able to freely cat files without risking arbitrary code execution.
你看,终端有一个相当奇怪的 API。应用程序可以发送给终端的大多数命令/请求都是带内发送的,即通过打印到 stdout。终端随后可以通过向 stdin 发送数据来回复。由于 Deno 允许程序向 stdout 打印任意数据并从 stdin 读取数据,而无需任何权限或过滤,恶意代码可以向终端发送任意命令。这通常没问题,因为终端不会仅仅因为 stdout 告诉它就执行 shell 命令,你应该能够自由地 cat 文件而不会冒任意代码执行的风险。
it’s markedly less okay if you allow malicious code to do whatever with the terminal and then print a prompt on the same exact terminal. you could, like, hide the prompt by messing with terminal colors. (this one got closed in 2026 because the permission prompt is “partially visible” (you can see an emoji) and “AI agents tend to” change terminal colors, so that’s a “valid use-case”) or trick the user into pre-feeding approval into stdin. or trick the terminal into feeding approval into stdin as a response to some request. (terminal won’t ever send Enter to stdin to avoid executing commands, but “press Enter to continue” is common enough to not be suspicious; the user never sees the actual prompt).
如果你允许恶意代码对终端做任何事情,然后在同一个终端上打印提示,那就非常不妥了。你可以通过搞乱终端颜色来隐藏提示。(这个问题在 2026 年被关闭了,因为权限提示是“部分可见的”(你可以看到一个表情符号),而且“AI 代理倾向于”改变终端颜色,所以这是一个“有效的用例”),或者诱骗用户预先向 stdin 输入批准,或者诱骗终端将批准作为对某些请求的响应发送到 stdin。(终端永远不会向 stdin 发送回车键以避免执行命令,但“按回车键继续”非常常见,不会引起怀疑;用户永远看不到实际的提示)。
here’s an asciicast of that, I think it looks quite cool. the specific issue on the screencast is long patched; the issue with terminal colors is not. that doesn’t really matter because the real issue is using an attacker-controlled channel for security-sensitive information. it’s like executing attacker-controlled JS code on a webpage and then asking for password on the same page. you can’t know that the page is in a safe state, so you can’t trust any input.
这里有一个 asciicast 演示,我觉得看起来很酷。屏幕录像中的具体问题早已修复;但终端颜色的问题还没有。这其实并不重要,因为真正的问题在于将攻击者控制的通道用于安全敏感信息。这就像在网页上执行攻击者控制的 JS 代码,然后在同一个页面上询问密码一样。你无法知道页面是否处于安全状态,因此你不能信任任何输入。
even if Deno manages to patch every single issue with the “standard” escape codes, you never know which non-standard codes even exist out there. many terminals have some, it’s impossible to predict how they will interact with your prompt. (aside: how come sudo can do it then? the difference is that sudo asks for the password first, before running any other code. to trick sudo that way you’d need to fuck up terminal state before you even type sudo, and do it quietly enough that you don’t notice it while you type it. if some terminal allows it, I’d consider it a bug in the terminal)
即使 Deno 设法修复了所有关于“标准”转义代码的问题,你也永远不知道外面还存在哪些非标准代码。许多终端都有一些,不可能预测它们将如何与你的提示交互。(题外话:那为什么 sudo 可以做到呢?区别在于 sudo 在运行任何其他代码之前先询问密码。要以这种方式欺骗 sudo,你需要在输入 sudo 之前就搞乱终端状态,并且要足够安静,以至于你在输入时不会注意到它。如果某个终端允许这样做,我会认为那是终端的一个 bug)。
the —allow-run problem so, there’s a weird thing with this command I wrote above (I’ll copy it here for reference): shelldeno run —allow-read=/tmp/dir —allow-run=uname code.ts what does —allow-run=uname even mean? it’s not like programs have canonical names, it’s not org.gnu.coreutils.uname, it’s not even /nix/store/8k1dmzr3gwzqdgw45nzj76902ca7g1is-coreutils-full-9.5/bin/uname. it’s just a shortcut for “look at all the directories in PATH one-by-one to see if you can find uname there”.
--allow-run 问题:我在上面写的这个命令有一个奇怪的地方(我把它复制在这里供参考):deno run --allow-read=/tmp/dir --allow-run=uname code.ts。--allow-run=uname 到底是什么意思?程序并没有规范的名称,它不是 org.gnu.coreutils.uname,甚至不是 /nix/store/8k1dmzr3gwzqdgw45nzj76902ca7g1is-coreutils-full-9.5/bin/uname。它只是“逐个查看 PATH 中的所有目录以查看是否能在那里找到 uname”的快捷方式。
you can set environment variables for the child process without needing any extra permissions, so what if we just $ cat t.ts const command = new Deno.Command(“uname”, { // my current working directory, can be anything env: { ‘PATH’: ‘/tmp/tmp.7ofmKaNW83’ }, }); const { code, stdout, stderr } = command.outputSync(); console.log(new TextDecoder().decode(stdout)); $ cat uname #!/bin/sh printf lol $ deno run —allow-run=uname t.ts lol yeah. once again, we have a pretty and convenient interface that doesn’t really make sense from the security standpoint. it would be nice if programs were uniquely identified by their names, but they’re not, and pretending they are leads to security holes. funnily enough, I didn’t even t
你可以在不需要任何额外权限的情况下为子进程设置环境变量,那么如果我们这样做呢:$ cat t.ts,const command = new Deno.Command("uname", { // 我的当前工作目录,可以是任何地方 env: { 'PATH': '/tmp/tmp.7ofmKaNW83' }, }); const { code, stdout, stderr } = command.outputSync(); console.log(new TextDecoder().decode(stdout));,$ cat uname,#!/bin/sh,printf lol,$ deno run --allow-run=uname t.ts,lol。是的,我们再次拥有了一个漂亮且方便的界面,但从安全角度来看,它并没有什么意义。如果程序能通过名称唯一标识就好了,但事实并非如此,假装它们可以标识会导致安全漏洞。有趣的是,我甚至没有 t