| 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 { Test, type |
| 7 | import { eq } |
| 8 | import { migrate } |
| 9 | import { afterAll, beforeAll |
| 10 | |
| 11 | import * as openpgp |
| 12 | |
| 13 | import { DATABASE } |
| 14 | import { UsersService } |
| 15 | import { CommitVerificationService } |
| 16 | |
| 17 | const CONNECTION = process |
| 18 | const MIGRATIONS = path |
| 19 | import.meta.dirname, |
| 20 | '../../../../../packages/db/drizzle', |
| 21 | ); |
| 22 | |
| 23 | const USER_ID = ' |
| 24 | const SIGNER_EMAIL = ' |
| 25 | |
| 26 | /** A commit object signed the way git signs one: the armored signature goes in a `gpgsig` header whose continuation lines each carry a leading space, and everything else is what was signed. */ |
| 27 | async function signCommit( |
| 28 | message: string, |
| 29 | signingKey: openpgp |
| 30 | ): Promise<string |
| 31 | const payload = |
| 32 | 'tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904', |
| 33 | `author Spec <${SIGNER_EMAIL}> 1700000000 +0000` |
| 34 | `committer Spec <${SIGNER_EMAIL}> 1700000000 +0000` |
| 35 | '', |
| 36 | `${message}\n`, |
| 37 | ].join('\n' |
| 38 | |
| 39 | const signature = |
| 40 | message: await openpgp |
| 41 | binary: new Uint8Array |
| 42 | }), |
| 43 | signingKeys: signingKey, |
| 44 | detached: true, |
| 45 | format: 'armored', |
| 46 | }); |
| 47 | |
| 48 | // `sign` types its result as a possible stream; armored output is a string. |
| 49 | const header = |
| 50 | .trimEnd() |
| 51 | .split('\n') |
| 52 | .map((line, |
| 53 | .join('\n') |
| 54 | |
| 55 | const [tree, |
| 56 | return [tree, |
| 57 | } |
| 58 | |
| 59 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 60 | describe.skipIf(!CONNECTION |
| 61 | let db: |
| 62 | let pool: |
| 63 | let service: |
| 64 | let root: |
| 65 | let gitDir: |
| 66 | let sha: |
| 67 | let keyId: |
| 68 | let publicKey: |
| 69 | |
| 70 | beforeAll(async () |
| 71 | ({ db, pool |
| 72 | await migrate(db |
| 73 | |
| 74 | const module: |
| 75 | providers: [ |
| 76 | CommitVerificationService, |
| 77 | UsersService, |
| 78 | { provide: DATABASE, |
| 79 | ], |
| 80 | }).compile(); |
| 81 | service = module. |
| 82 | |
| 83 | // 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. |
| 84 | const key = |
| 85 | userIDs: [{ name: |
| 86 | format: 'object', |
| 87 | }); |
| 88 | keyId = key. |
| 89 | publicKey = key. |
| 90 | |
| 91 | root = mkdtempSync( |
| 92 | gitDir = path. |
| 93 | execFileSync('git', |
| 94 | sha = execFileSync( |
| 95 | 'git', |
| 96 | ['hash-object', '-t' |
| 97 | { |
| 98 | cwd: root, |
| 99 | input: await signCommit |
| 100 | encoding: 'utf8', |
| 101 | }, |
| 102 | ).trim(); |
| 103 | }); |
| 104 | |
| 105 | afterAll(async () |
| 106 | await db.delete |
| 107 | await pool.end |
| 108 | rmSync(root, { |
| 109 | }); |
| 110 | |
| 111 | beforeEach(async () |
| 112 | await db.delete |
| 113 | await db.insert |
| 114 | id: USER_ID, |
| 115 | name: 'Spec', |
| 116 | email: SIGNER_EMAIL, |
| 117 | emailVerified: true, |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | async function addKey |
| 122 | await db.insert |
| 123 | id: 'gpg_spec', |
| 124 | userId: USER_ID, |
| 125 | keyId, |
| 126 | fingerprint: keyId.repeat |
| 127 | publicKey, |
| 128 | }); |
| 129 | } |
| 130 | |
| 131 | function verify(authorEmail |
| 132 | return service.verifyCommits |
| 133 | gitDir, |
| 134 | commits: [{ sha |
| 135 | }); |
| 136 | } |
| 137 | |
| 138 | it('verifies a commit signed by a key its author uploaded', |
| 139 | await addKey(); |
| 140 | |
| 141 | const verdict = |
| 142 | |
| 143 | expect(verdict?.verified |
| 144 | expect(verdict?.keyId |
| 145 | }); |
| 146 | |
| 147 | it('refuses a signature whose key is not linked to the author address', |
| 148 | await addKey(); |
| 149 | |
| 150 | // The signature is good; the address it is claimed for is someone else's. |
| 151 | const verdict = |
| 152 | |
| 153 | expect(verdict?.verified |
| 154 | expect(verdict?.reason |
| 155 | }); |
| 156 | |
| 157 | it('refuses a key no account has uploaded', |
| 158 | const verdict = |
| 159 | |
| 160 | expect(verdict?.verified |
| 161 | expect(verdict?.reason |
| 162 | }); |
| 163 | |
| 164 | it('leaves unsigned commits out of the result', |
| 165 | await addKey(); |
| 166 | const unsigned = |
| 167 | 'git', |
| 168 | [ |
| 169 | 'commit-tree', |
| 170 | '4b825dc642cb6eb9a060e54bf8d69288fbee4904', |
| 171 | '-m', |
| 172 | 'plain', |
| 173 | ], |
| 174 | { |
| 175 | cwd: root, |
| 176 | encoding: 'utf8', |
| 177 | env: { |
| 178 | ...process.env, |
| 179 | GIT_DIR: gitDir, |
| 180 | GIT_AUTHOR_NAME: 'Spec', |
| 181 | GIT_AUTHOR_EMAIL: SIGNER_EMAIL, |
| 182 | GIT_COMMITTER_NAME: 'Spec', |
| 183 | GIT_COMMITTER_EMAIL: SIGNER_EMAIL, |
| 184 | }, |
| 185 | }, |
| 186 | ).trim(); |
| 187 | |
| 188 | const verdicts = |
| 189 | gitDir, |
| 190 | commits: [ |
| 191 | { sha, authorEmail: |
| 192 | { sha: unsigned, |
| 193 | ], |
| 194 | }); |
| 195 | |
| 196 | expect([...verdicts. |
| 197 | }); |
| 198 | }); |