-
Notifications
You must be signed in to change notification settings - Fork 7
fix(sandbox): verify a sandbox's identity before sending a token #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e6dbb5
fix(sandbox): verify a sandbox's identity before sending a token
jedisct1 95bc7d4
Add tests for ssh host key pinning
jedisct1 1c1a1ab
Add changeset for ssh host key pinning
jedisct1 ae32702
Treat an unpinned key type for a known host like a changed key
jedisct1 a48a1f7
If sandbox_known_hosts already exists but is unreadable, bail out
jedisct1 c2cf848
Match how OpenSSH parses known_hosts
jedisct1 4d69f6a
Cleanup th known hosts file after a sandbox is deleted
jedisct1 1fdc94e
Quote UserKnownHostsFile path
jedisct1 0e68c18
Show a proper error when host key verification fails
jedisct1 9651b16
Merge branch 'main' into sshhostkey
jedisct1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { appendFileSync, mkdirSync, readFileSync } from "node:fs"; | ||
| import { homedir } from "node:os"; | ||
| import { dirname, join } from "node:path"; | ||
|
|
||
| /** | ||
| * The known-hosts file shared by the CLI and libssh2. | ||
| */ | ||
| export function sandboxKnownHostsPath(): string { | ||
| const base = process.env.XDG_CONFIG_HOME ?? join(homedir(), ".config"); | ||
| return join(base, "bunny", "sandbox_known_hosts"); | ||
| } | ||
|
|
||
| /** OpenSSH known_hosts label; non-default ports are bracketed. */ | ||
| function hostLabel(host: string, port: number): string { | ||
| return port === 22 ? host : `[${host}]:${port}`; | ||
| } | ||
|
|
||
| /** Algorithm name from the front of an SSH public-key blob, or null if malformed. */ | ||
| function keyType(key: Buffer): string | null { | ||
| if (key.length < 4) return null; | ||
| const len = key.readUInt32BE(0); | ||
| if (4 + len > key.length) return null; | ||
| return key.toString("ascii", 4, 4 + len); | ||
| } | ||
|
|
||
| /** | ||
| * Trust-on-first-use check for a server host key (`key` is the raw public-key | ||
| * blob). | ||
| * The first key seen for a host and type is recorded and trusted; a later | ||
| * connection presenting a different key for that host is rejected, catching an | ||
| * impostor before the token is sent as the password. | ||
| */ | ||
| export function verifyKnownHost( | ||
| host: string, | ||
| port: number, | ||
| key: Buffer, | ||
| path: string = sandboxKnownHostsPath(), | ||
| ): boolean { | ||
| const type = keyType(key); | ||
| if (!type) return false; | ||
| const label = hostLabel(host, port); | ||
| const encoded = key.toString("base64"); | ||
|
|
||
| let text = ""; | ||
| try { | ||
| text = readFileSync(path, "utf8"); | ||
| } catch { | ||
| // No file yet — first contact. | ||
|
jedisct1 marked this conversation as resolved.
Outdated
|
||
| } | ||
| for (const line of text.split("\n")) { | ||
| const trimmed = line.trim(); | ||
| if (!trimmed || trimmed.startsWith("#")) continue; | ||
| const [hosts, lineType, lineKey] = trimmed.split(/\s+/); | ||
| if (!hosts || !lineType || !lineKey) continue; | ||
| // known_hosts allows several comma-separated hosts per line. | ||
| if (lineType !== type || !hosts.split(",").includes(label)) continue; | ||
|
jedisct1 marked this conversation as resolved.
Outdated
|
||
| return lineKey === encoded; | ||
|
jedisct1 marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| try { | ||
| mkdirSync(dirname(path), { recursive: true }); | ||
| // Append so concurrent connects don't clobber each other. | ||
| appendFileSync(path, `${label} ${type} ${encoded}\n`, { mode: 0o600 }); | ||
| } catch { | ||
| // Can't persist (e.g. read-only home): trust it anyway, like OpenSSH accept-new — no cross-run pin. | ||
| } | ||
| return true; | ||
| } | ||
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.