| 1 | import { createCipheriv, randomBytes } from 'node:crypto'; |
| 2 | import { lookup } |
| 3 | import { BlockList, isIP |
| 4 | |
| 5 | import type { schema |
| 6 | |
| 7 | import type { EventType |
| 8 | import type { Commit |
| 9 | |
| 10 | /** Events an endpoint can subscribe to. `ping` is sent on create and on demand, whatever the endpoint chose. */ |
| 11 | export const webhookEvents = |
| 12 | 'push', |
| 13 | 'issue.opened', |
| 14 | 'issue.edited', |
| 15 | 'issue.closed', |
| 16 | 'issue.reopened', |
| 17 | 'issue.assigned', |
| 18 | 'issue.unassigned', |
| 19 | 'issue.labeled', |
| 20 | 'issue.unlabeled', |
| 21 | 'issue.commented', |
| 22 | 'issue.comment_edited', |
| 23 | 'issue.comment_deleted', |
| 24 | 'pull_request.ready_for_review', |
| 25 | 'pull_request.converted_to_draft', |
| 26 | 'pull_request.merged', |
| 27 | 'pull_request.reviewed', |
| 28 | 'pull_request.review_dismissed', |
| 29 | 'pull_request.review_commented', |
| 30 | 'label.created', |
| 31 | 'label.edited', |
| 32 | 'label.deleted', |
| 33 | 'release.created', |
| 34 | 'release.published', |
| 35 | 'release.edited', |
| 36 | 'release.deleted', |
| 37 | 'star.created', |
| 38 | 'star.deleted', |
| 39 | 'watch.started', |
| 40 | 'fork.created', |
| 41 | 'repository.edited', |
| 42 | 'repository.transferred', |
| 43 | 'member.added', |
| 44 | 'member.removed', |
| 45 | ] as const satisfies |
| 46 | |
| 47 | export type WebhookEvent |
| 48 | |
| 49 | /** A new signing secret, shown to the user once. */ |
| 50 | export function newWebhookSecret() |
| 51 | return `whsec_${randomBytes |
| 52 | } |
| 53 | |
| 54 | /** Encrypted, not hashed: apps/delivery needs the secret to sign. The format, `v1:` + base64(nonce || ciphertext || tag), is what Go's `gcm.Open` reads. */ |
| 55 | export function sealWebhookSecret(key |
| 56 | const nonce = |
| 57 | const cipher = |
| 58 | const sealed = |
| 59 | nonce, |
| 60 | cipher.update(secret |
| 61 | cipher.final(), |
| 62 | cipher.getAuthTag(), |
| 63 | ]); |
| 64 | return `v1:${sealed |
| 65 | } |
| 66 | |
| 67 | /** The key from `WEBHOOK_SECRET_KEY`: 32 bytes, base64-encoded. */ |
| 68 | export function webhookSecretKey(value |
| 69 | const key = |
| 70 | return key?.length |
| 71 | } |
| 72 | |
| 73 | // Mirrors apps/delivery/ssrf.go. Delivery checks the address it actually dials on every send; this check only gives a clear error when the endpoint is saved. |
| 74 | const alwaysBlocked = new |
| 75 | alwaysBlocked.addSubnet('0.0.0.0' |
| 76 | alwaysBlocked.addSubnet('169.254.0.0' |
| 77 | alwaysBlocked.addSubnet('224.0.0.0' |
| 78 | alwaysBlocked.addSubnet('fe80::' |
| 79 | alwaysBlocked.addSubnet('ff00::' |
| 80 | alwaysBlocked.addAddress('::' |
| 81 | |
| 82 | const privateNetworks = new |
| 83 | privateNetworks.addSubnet('10.0.0.0' |
| 84 | privateNetworks.addSubnet('172.16.0.0' |
| 85 | privateNetworks.addSubnet('192.168.0.0' |
| 86 | privateNetworks.addSubnet('127.0.0.0' |
| 87 | privateNetworks.addSubnet('100.64.0.0' |
| 88 | privateNetworks.addSubnet('fc00::' |
| 89 | privateNetworks.addAddress('::1' |
| 90 | |
| 91 | export function blockedAddress(address |
| 92 | // ::ffff:10.0.0.1 is 10.0.0.1 |
| 93 | const mapped = |
| 94 | const ip = |
| 95 | const family = |
| 96 | if (alwaysBlocked.check |
| 97 | return !allowPrivate && |
| 98 | } |
| 99 | |
| 100 | /** Why `url` cannot be a webhook endpoint, or null when it can. */ |
| 101 | export async function webhookUrlProblem |
| 102 | url: string, |
| 103 | allowPrivate: boolean, |
| 104 | ): Promise<string |
| 105 | let parsed: |
| 106 | try { |
| 107 | parsed = new |
| 108 | } catch { |
| 109 | return 'Enter a full URL, such as https://example.com/webhook.'; |
| 110 | } |
| 111 | if (parsed.protocol |
| 112 | return 'The URL must start with https:// or http://.'; |
| 113 | } |
| 114 | if (parsed.username |
| 115 | return 'The URL cannot contain a username or password.'; |
| 116 | } |
| 117 | const host = |
| 118 | let addresses: |
| 119 | try { |
| 120 | addresses = isIP( |
| 121 | ? [host] |
| 122 | : (await lookup |
| 123 | } catch { |
| 124 | return `${host} |
| 125 | } |
| 126 | if (addresses.some |
| 127 | return `${host} |
| 128 | } |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | /** Whose webhooks these are, once the requester was checked as its admin. `name` is how URLs spell it: `owner/repo` or the organization's slug. */ |
| 133 | export type WebhookOwner |
| 134 | | { repositoryId |
| 135 | | { organizationId |
| 136 | |
| 137 | type UserRef |
| 138 | |
| 139 | export type WebhookRepository |
| 140 | id: string |
| 141 | fullName: string |
| 142 | description: string |
| 143 | visibility: ( |
| 144 | defaultBranch: string |
| 145 | htmlUrl: string |
| 146 | }; |
| 147 | |
| 148 | type WebhookThread |
| 149 | id: string |
| 150 | number: number |
| 151 | title: string |
| 152 | body: string |
| 153 | state: ( |
| 154 | author: UserRef |
| 155 | htmlUrl: string |
| 156 | }; |
| 157 | |
| 158 | /** The JSON a receiver gets for an event. Which optional fields are set depends on the event. */ |
| 159 | export type WebhookBody |
| 160 | event: EventType |
| 161 | repository: WebhookRepository |
| 162 | sender: UserRef |
| 163 | createdAt: string |
| 164 | issue?: WebhookThread |
| 165 | pullRequest?: WebhookThread |
| 166 | comment?: { |
| 167 | review?: { |
| 168 | id: string |
| 169 | state: ( |
| 170 | body: string |
| 171 | dismissalMessage: string |
| 172 | }; |
| 173 | assignee?: UserRef |
| 174 | member?: UserRef |
| 175 | label?: { |
| 176 | id: string |
| 177 | name: string |
| 178 | description: string |
| 179 | color: string |
| 180 | }; |
| 181 | /** A deleted release carries only its id, tag and name. */ |
| 182 | release?: { |
| 183 | id: string |
| 184 | tagName: string |
| 185 | name: string |
| 186 | body?: string |
| 187 | isDraft?: boolean |
| 188 | isPrerelease?: boolean |
| 189 | publishedAt?: Date |
| 190 | htmlUrl?: string |
| 191 | }; |
| 192 | fork?: WebhookRepository |
| 193 | /** The previous owner of a transferred repository. */ |
| 194 | from?: string |
| 195 | ref?: string |
| 196 | before?: string |
| 197 | after?: string |
| 198 | created?: boolean |
| 199 | deleted?: boolean |
| 200 | commits?: Commit |
| 201 | }; |
| 202 | |
| 203 | /** Sent on create and from the "Send test" button. */ |
| 204 | export type WebhookPing |
| 205 | event: 'ping |
| 206 | webhook: { |
| 207 | repository?: { |
| 208 | organization?: { |
| 209 | createdAt: string |
| 210 | }; |