How I hand two secrets between servers without a page, a URL or a clipboard

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Connecting my issue tracker to my agent runner took seven steps and two secrets copied between browser tabs: make an agent member in one app, copy its API key into the other, make a webhook there, copy its signing secret back. Every copy is a place for a secret to end up in a clipboard manager, a screenshot or a chat message. I replaced it with one button. Neither secret is ever in a page, an address or a clipboard. This is how the handoff works, and the two things that went wrong the first time I connected the halves.

将我的问题追踪器连接到代理运行器需要七个步骤,并在浏览器标签页之间复制两个密钥:在一个应用程序中创建一个代理成员,将其 API 密钥复制到另一个应用程序中,在那里创建一个 Webhook,然后将签名密钥复制回来。每一次复制都可能导致密钥最终出现在剪贴板管理器、截图或聊天记录中。我用一个按钮取代了这一过程。这两个密钥都不会出现在页面、地址栏或剪贴板中。这就是移交的工作原理,以及我第一次连接这两部分时出错的两个地方。

The shape: the PKCE idea, outside OAuth I wrote both apps, so I could design both ends. The flow borrows from OAuth’s PKCE, but there is no OAuth server here: The asking app (the agent runner) makes a random verifier and keeps it on its server. It sends the admin’s browser to the tracker’s approval page with a challenge: the SHA-256 of the verifier. The verifier never goes to the browser. The admin signs in to the tracker, sees the asking origin in large type and what Allow will create, and presses Allow. The tracker makes the agent member and the webhook, stores the secrets, and sends the browser back with a one-time code in the address.

架构:PKCE 理念,脱离 OAuth。我编写了这两个应用程序,因此我可以设计两端。该流程借鉴了 OAuth 的 PKCE,但这里没有 OAuth 服务器:请求应用程序(代理运行器)生成一个随机验证器并将其保存在自己的服务器上。它将管理员的浏览器发送到追踪器的批准页面,并附带一个挑战:验证器的 SHA-256 哈希值。验证器永远不会发送到浏览器。管理员登录追踪器,看到大字显示的请求来源以及“允许”将创建的内容,然后点击“允许”。追踪器创建代理成员和 Webhook,存储密钥,并将浏览器重定向回带有一次性代码的地址。

The asking app’s server posts { code, verifier } to the tracker, server to server. The tracker hashes the verifier, compares it with the stored challenge, and answers with both secrets. The browser only ever carries a hash and a code. A code read from a log, a Referer header or the address bar is useless without the verifier. The challenge is what binds the code to the server that started the flow.

请求应用程序的服务器通过服务器对服务器的方式将 { code, verifier } 发送给追踪器。追踪器对验证器进行哈希处理,将其与存储的挑战进行比较,并返回两个密钥。浏览器始终只携带哈希值和代码。从日志、Referer 标头或地址栏中读取的代码,如果没有验证器,将毫无用处。挑战是将代码绑定到启动该流程的服务器的关键。

Secrets at rest wait under a key made from the code. For 60 seconds the tracker keeps a row holding the challenge in the clear and everything else sealed with AES-GCM: both secrets, the ids, an undo token. The row’s identifier is track-connect: plus the SHA-256 of the code, never the code. The key is HKDF of the server’s secret, salted with the code. So a copy of the table opens nothing, because it has neither the code nor the server secret, and the code alone opens nothing either.

静态密钥在由代码生成的密钥保护下等待。追踪器会保留一行数据 60 秒,其中挑战以明文形式存储,而其他所有内容(两个密钥、ID、撤销令牌)均使用 AES-GCM 加密。该行的标识符是“track-connect:”加上代码的 SHA-256 哈希值,绝非代码本身。密钥是服务器密钥通过代码加盐后的 HKDF 派生值。因此,即使复制了数据库表也无法解密,因为它既没有代码也没有服务器密钥,而仅有代码也无法解密。

The delete decides. The part I like most is the exchange’s first move. It doesn’t read the row and then delete it. It deletes by identifier and takes the row from the RETURNING clause. Whoever’s statement removes the row is the one that goes on. Of two exchanges at once, exactly one gets a row. And because the delete comes before the expiry check, the challenge check and the decryption, a code that fails any of them is spent anyway: a wrong guess at the verifier burns the code.

删除决定一切。我最喜欢的部分是交换的第一步。它不是先读取行再删除,而是通过标识符进行删除,并从 RETURNING 子句中获取该行。谁的语句删除了该行,谁就能继续执行。如果同时发生两次交换,只有一个能获取到行数据。由于删除操作先于过期检查、挑战检查和解密,任何失败的代码都会被消耗掉:对验证器的错误猜测会直接销毁该代码。

Both or neither, and a signed way back. On Allow, the tracker makes the agent member, then the webhook for it. If the webhook fails, or the code can’t be stored, it deletes the agent again, and if that fails too, the message names the member to remove by hand. The exchange also returns an undo token: workspace, agent and webhook ids plus an expiry ten minutes out, signed with HMAC-SHA-256 under a key derived with its own purpose string.

要么全部成功,要么全部失败,并提供签名回退路径。点击“允许”后,追踪器会创建代理成员,然后创建对应的 Webhook。如果 Webhook 创建失败或代码无法存储,它会再次删除代理;如果删除也失败,消息会指明需要手动移除的成员。交换过程还会返回一个撤销令牌:包含工作区、代理和 Webhook 的 ID 以及十分钟后的过期时间,并使用通过特定用途字符串派生的密钥进行 HMAC-SHA-256 签名。

What went wrong when the halves met. Each half had passing tests. The first real connection still failed in two ways. 1. A dot in the state. The asking app needed to know which workspace a returning browser belonged to, so it put the workspace id in the state: .. The tracker’s approval page accepts only letters, digits, _ and - there, so it refused every request. A test double that is more lenient than the thing it stands for is a bug with good coverage numbers.

两部分对接时出了什么问题?每一部分都有通过的测试,但第一次实际连接时仍然出现了两个问题。1. 状态中的点号。请求应用程序需要知道返回的浏览器属于哪个工作区,因此它将工作区 ID 放入了状态中:.。追踪器的批准页面只接受字母、数字、下划线和连字符,因此拒绝了所有请求。一个比实际对象更宽松的测试替身,就是一种拥有良好覆盖率数据的 Bug。

  1. A notice that was false. The tracker creates the member and the webhook on Allow, before the exchange. If the exchange then fails (refused, slow, unreachable, unreadable), the asking app has no undo token, because the token arrives in the exchange’s answer. My error notice still said “Nothing was made.” It now says what may still be in the tracker and where to remove it. I had written a sentence that was true only on the paths I’d thought about.

  2. 错误的通知。追踪器在“允许”时(即交换之前)创建成员和 Webhook。如果随后的交换失败(被拒绝、缓慢、无法访问、不可读),请求应用程序将没有撤销令牌,因为令牌是在交换的响应中到达的。我的错误通知当时写着“未创建任何内容”。现在它会说明追踪器中可能残留的内容以及在哪里移除它们。我之前写下的句子,只在我想到的路径上才是正确的。