| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { createDatabase, type |
| 6 | import { ConfigService } |
| 7 | import { Test, type |
| 8 | import { eq } |
| 9 | import { migrate } |
| 10 | import * as openpgp |
| 11 | import { afterAll, beforeAll |
| 12 | |
| 13 | import { DATABASE } |
| 14 | import { insertSignature } |
| 15 | import { GITHUB_MERGE_COMMIT_OBJECT } |
| 16 | import { signPayload } |
| 17 | import { UsersService } |
| 18 | import { CommitSigningService } |
| 19 | import { CommitVerificationService } |
| 20 | |
| 21 | const CONNECTION = process |
| 22 | const MIGRATIONS = path |
| 23 | import.meta.dirname, |
| 24 | '../../../../../packages/db/drizzle', |
| 25 | ); |
| 26 | |
| 27 | const USER_ID = ' |
| 28 | const SIGNER_EMAIL = ' |
| 29 | |
| 30 | /** A commit object signed the way git signs one, authored by `authorEmail`. */ |
| 31 | async function signCommit( |
| 32 | message: string, |
| 33 | signingKey: openpgp |
| 34 | authorEmail = SIGNER_EMAIL, |
| 35 | ): Promise<string |
| 36 | const payload = |
| 37 | 'tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904', |
| 38 | `author Spec <${authorEmail}> 1700000000 +0000` |
| 39 | `committer Spec <${authorEmail}> 1700000000 +0000` |
| 40 | '', |
| 41 | `${message}\n`, |
| 42 | ].join('\n' |
| 43 | |
| 44 | return insertSignature( |
| 45 | payload, |
| 46 | await signPayload({ |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 51 | describe.skipIf(!CONNECTION |
| 52 | let db: |
| 53 | let pool: |
| 54 | let service: |
| 55 | let root: |
| 56 | let gitDir: |
| 57 | let sha: |
| 58 | let keyId: |
| 59 | let publicKey: |
| 60 | let platformKey: |
| 61 | |
| 62 | beforeAll(async () |
| 63 | ({ db, pool |
| 64 | await migrate(db |
| 65 | |
| 66 | const platform = |
| 67 | userIDs: [{ name: |
| 68 | format: 'object', |
| 69 | }); |
| 70 | platformKey = platform. |
| 71 | |
| 72 | const module: |
| 73 | providers: [ |
| 74 | CommitVerificationService, |
| 75 | CommitSigningService, |
| 76 | UsersService, |
| 77 | { provide: DATABASE, |
| 78 | { |
| 79 | provide: ConfigService, |
| 80 | useValue: new ConfigService |
| 81 | COMMIT_SIGNING_KEY: Buffer.from |
| 82 | 'base64', |
| 83 | ), |
| 84 | }), |
| 85 | }, |
| 86 | ], |
| 87 | }).compile(); |
| 88 | await module.init |
| 89 | service = module. |
| 90 | |
| 91 | // Signed here so the suite needs no gpg on the machine; git's own header layout is covered by the fixture in commit-signature.spec.ts. |
| 92 | const key = |
| 93 | userIDs: [{ name: |
| 94 | format: 'object', |
| 95 | }); |
| 96 | keyId = key. |
| 97 | publicKey = key. |
| 98 | |
| 99 | root = mkdtempSync( |
| 100 | gitDir = path. |
| 101 | execFileSync('git', |
| 102 | sha = execFileSync( |
| 103 | 'git', |
| 104 | ['hash-object', '-t' |
| 105 | { |
| 106 | cwd: root, |
| 107 | input: await signCommit |
| 108 | encoding: 'utf8', |
| 109 | }, |
| 110 | ).trim(); |
| 111 | }); |
| 112 | |
| 113 | afterAll(async () |
| 114 | await db.delete |
| 115 | await pool.end |
| 116 | rmSync(root, { |
| 117 | }); |
| 118 | |
| 119 | beforeEach(async () |
| 120 | await db.delete |
| 121 | await db.insert |
| 122 | id: USER_ID, |
| 123 | name: 'Spec', |
| 124 | email: SIGNER_EMAIL, |
| 125 | emailVerified: true, |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | async function addKey |
| 130 | await db.insert |
| 131 | id: 'gpg_spec', |
| 132 | userId: USER_ID, |
| 133 | keyId, |
| 134 | fingerprint: keyId.repeat |
| 135 | publicKey, |
| 136 | }); |
| 137 | } |
| 138 | |
| 139 | function verify(authorEmail |
| 140 | return service.verifyCommits |
| 141 | gitDir, |
| 142 | commits: [{ sha |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | it('verifies a commit signed by a key its author uploaded', |
| 147 | await addKey(); |
| 148 | |
| 149 | const verdict = |
| 150 | |
| 151 | expect(verdict?.verified |
| 152 | expect(verdict?.keyId |
| 153 | }); |
| 154 | |
| 155 | it('refuses a signature whose key is not linked to the author address', |
| 156 | await addKey(); |
| 157 | |
| 158 | // The signature is good; the address it is claimed for is someone else's. |
| 159 | const verdict = |
| 160 | |
| 161 | expect(verdict?.verified |
| 162 | expect(verdict?.reason |
| 163 | }); |
| 164 | |
| 165 | it('refuses a key no account has uploaded', |
| 166 | const verdict = |
| 167 | |
| 168 | expect(verdict?.verified |
| 169 | expect(verdict?.reason |
| 170 | }); |
| 171 | |
| 172 | it('leaves unsigned commits out of the result', |
| 173 | await addKey(); |
| 174 | const unsigned = |
| 175 | 'git', |
| 176 | [ |
| 177 | 'commit-tree', |
| 178 | '4b825dc642cb6eb9a060e54bf8d69288fbee4904', |
| 179 | '-m', |
| 180 | 'plain', |
| 181 | ], |
| 182 | { |
| 183 | cwd: root, |
| 184 | encoding: 'utf8', |
| 185 | env: { |
| 186 | ...process.env, |
| 187 | GIT_DIR: gitDir, |
| 188 | GIT_AUTHOR_NAME: 'Spec', |
| 189 | GIT_AUTHOR_EMAIL: SIGNER_EMAIL, |
| 190 | GIT_COMMITTER_NAME: 'Spec', |
| 191 | GIT_COMMITTER_EMAIL: SIGNER_EMAIL, |
| 192 | }, |
| 193 | }, |
| 194 | ).trim(); |
| 195 | |
| 196 | const verdicts = |
| 197 | gitDir, |
| 198 | commits: [ |
| 199 | { sha, authorEmail: |
| 200 | { sha: unsigned, |
| 201 | ], |
| 202 | }); |
| 203 | |
| 204 | expect([...verdicts. |
| 205 | }); |
| 206 | |
| 207 | function writeCommit(object |
| 208 | return execFileSync( |
| 209 | 'git', |
| 210 | ['hash-object', '-t' |
| 211 | { |
| 212 | cwd: root, |
| 213 | input: object, |
| 214 | encoding: 'utf8', |
| 215 | }, |
| 216 | ).trim(); |
| 217 | } |
| 218 | |
| 219 | it("verifies GitHub's own merge commits whoever authored them", |
| 220 | const merge = |
| 221 | |
| 222 | const verdict = |
| 223 | await service.verifyCommits |
| 224 | gitDir, |
| 225 | commits: [{ sha: |
| 226 | }) |
| 227 | ).get(merge) |
| 228 | |
| 229 | expect(verdict).toEqual |
| 230 | verified: true, |
| 231 | reason: 'Signed by GitHub', |
| 232 | keyId: 'b5690eeebb952194', |
| 233 | }); |
| 234 | }); |
| 235 | |
| 236 | it('verifies commits Ghost signed with its own key', |
| 237 | const merge = |
| 238 | await signCommit('merged by ghost |
| 239 | ); |
| 240 | |
| 241 | const verdict = |
| 242 | await service.verifyCommits |
| 243 | gitDir, |
| 244 | commits: [{ sha: |
| 245 | }) |
| 246 | ).get(merge) |
| 247 | |
| 248 | expect(verdict?.verified |
| 249 | expect(verdict?.reason |
| 250 | }); |
| 251 | |
| 252 | it("refuses a commit that names GitHub's key but was not signed by it", |
| 253 | const forged = |
| 254 | 'Verify a push before the log commits it', |
| 255 | 'Something GitHub never signed', |
| 256 | ); |
| 257 | const sha = |
| 258 | |
| 259 | const verdict = |
| 260 | await service.verifyCommits |
| 261 | gitDir, |
| 262 | commits: [{ sha |
| 263 | }) |
| 264 | ).get(sha) |
| 265 | |
| 266 | expect(verdict?.verified |
| 267 | expect(verdict?.reason |
| 268 | }); |
| 269 | }); |