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