|
| 1 | +/** |
| 2 | + * Default SSH host keys for common Git hosting providers |
| 3 | + * |
| 4 | + * These fingerprints are the SHA256 hashes of the ED25519 host keys. |
| 5 | + * They should be verified against official documentation periodically. |
| 6 | + * |
| 7 | + * Sources: |
| 8 | + * - GitHub: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/githubs-ssh-key-fingerprints |
| 9 | + * - GitLab: https://docs.gitlab.com/ee/user/gitlab_com/ |
| 10 | + */ |
| 11 | + |
| 12 | +export interface KnownHostsConfig { |
| 13 | + [hostname: string]: string; |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Default known host keys for GitHub and GitLab |
| 18 | + * Last updated: 2025-01-26 |
| 19 | + */ |
| 20 | +export const DEFAULT_KNOWN_HOSTS: KnownHostsConfig = { |
| 21 | + 'github.com': 'SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU', |
| 22 | + 'gitlab.com': 'SHA256:eUXGGm1YGsMAS7vkcx6JOJdOGHPem5gQp4taiCfCLB8', |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Get known hosts configuration with defaults merged |
| 27 | + */ |
| 28 | +export function getKnownHosts(customHosts?: KnownHostsConfig): KnownHostsConfig { |
| 29 | + return { |
| 30 | + ...DEFAULT_KNOWN_HOSTS, |
| 31 | + ...(customHosts || {}), |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Verify a host key fingerprint against known hosts |
| 37 | + * |
| 38 | + * @param hostname The hostname being connected to |
| 39 | + * @param keyHash The SSH key fingerprint (e.g., "SHA256:abc123...") |
| 40 | + * @param knownHosts Known hosts configuration |
| 41 | + * @returns true if the key matches, false otherwise |
| 42 | + */ |
| 43 | +export function verifyHostKey( |
| 44 | + hostname: string, |
| 45 | + keyHash: string, |
| 46 | + knownHosts: KnownHostsConfig, |
| 47 | +): boolean { |
| 48 | + const expectedKey = knownHosts[hostname]; |
| 49 | + |
| 50 | + if (!expectedKey) { |
| 51 | + console.error(`[SSH] Host key verification failed: Unknown host '${hostname}'`); |
| 52 | + console.error(` Add the host key to your configuration:`); |
| 53 | + console.error(` "ssh": { "knownHosts": { "${hostname}": "SHA256:..." } }`); |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + if (keyHash !== expectedKey) { |
| 58 | + console.error(`[SSH] Host key verification failed for '${hostname}'`); |
| 59 | + console.error(` Expected: ${expectedKey}`); |
| 60 | + console.error(` Received: ${keyHash}`); |
| 61 | + console.error(` `); |
| 62 | + console.error(` WARNING: This could indicate a man-in-the-middle attack!`); |
| 63 | + console.error(` If the host key has legitimately changed, update your configuration.`); |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + return true; |
| 68 | +} |
0 commit comments