| 1 | import { createCipheriv, randomBytes } from 'node:crypto'; |
| 2 | import { lookup } |
| 3 | import { BlockList, isIP |
| 4 | |
| 5 | import type { EventType |
| 6 | |
| 7 | /** Events an endpoint can subscribe to. `ping` is sent on create and on demand, whatever the endpoint chose. */ |
| 8 | export const webhookEvents = |
| 9 | 'push', |
| 10 | 'issue.opened', |
| 11 | 'issue.edited', |
| 12 | 'issue.closed', |
| 13 | 'issue.reopened', |
| 14 | 'issue.assigned', |
| 15 | 'issue.unassigned', |
| 16 | 'issue.labeled', |
| 17 | 'issue.unlabeled', |
| 18 | 'issue.commented', |
| 19 | 'issue.comment_edited', |
| 20 | 'issue.comment_deleted', |
| 21 | 'pull_request.ready_for_review', |
| 22 | 'pull_request.converted_to_draft', |
| 23 | 'pull_request.merged', |
| 24 | 'pull_request.reviewed', |
| 25 | 'pull_request.review_dismissed', |
| 26 | 'pull_request.review_commented', |
| 27 | 'label.created', |
| 28 | 'label.edited', |
| 29 | 'label.deleted', |
| 30 | 'release.created', |
| 31 | 'release.published', |
| 32 | 'release.edited', |
| 33 | 'release.deleted', |
| 34 | 'star.created', |
| 35 | 'star.deleted', |
| 36 | 'watch.started', |
| 37 | 'fork.created', |
| 38 | 'repository.edited', |
| 39 | 'repository.transferred', |
| 40 | 'member.added', |
| 41 | 'member.removed', |
| 42 | ] as const satisfies |
| 43 | |
| 44 | export type WebhookEvent |
| 45 | |
| 46 | /** A new signing secret, shown to the user once. */ |
| 47 | export function newWebhookSecret() |
| 48 | return `whsec_${randomBytes |
| 49 | } |
| 50 | |
| 51 | /** Encrypted, not hashed: apps/delivery needs the secret to sign. The format, `v1:` + base64(nonce || ciphertext || tag), is what Go's `gcm.Open` reads. */ |
| 52 | export function sealWebhookSecret(key |
| 53 | const nonce = |
| 54 | const cipher = |
| 55 | const sealed = |
| 56 | nonce, |
| 57 | cipher.update(secret |
| 58 | cipher.final(), |
| 59 | cipher.getAuthTag(), |
| 60 | ]); |
| 61 | return `v1:${sealed |
| 62 | } |
| 63 | |
| 64 | /** The key from `WEBHOOK_SECRET_KEY`: 32 bytes, base64-encoded. */ |
| 65 | export function webhookSecretKey(value |
| 66 | const key = |
| 67 | return key?.length |
| 68 | } |
| 69 | |
| 70 | // 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. |
| 71 | const alwaysBlocked = new |
| 72 | alwaysBlocked.addSubnet('0.0.0.0' |
| 73 | alwaysBlocked.addSubnet('169.254.0.0' |
| 74 | alwaysBlocked.addSubnet('224.0.0.0' |
| 75 | alwaysBlocked.addSubnet('fe80::' |
| 76 | alwaysBlocked.addSubnet('ff00::' |
| 77 | alwaysBlocked.addAddress('::' |
| 78 | |
| 79 | const privateNetworks = new |
| 80 | privateNetworks.addSubnet('10.0.0.0' |
| 81 | privateNetworks.addSubnet('172.16.0.0' |
| 82 | privateNetworks.addSubnet('192.168.0.0' |
| 83 | privateNetworks.addSubnet('127.0.0.0' |
| 84 | privateNetworks.addSubnet('100.64.0.0' |
| 85 | privateNetworks.addSubnet('fc00::' |
| 86 | privateNetworks.addAddress('::1' |
| 87 | |
| 88 | export function blockedAddress(address |
| 89 | // ::ffff:10.0.0.1 is 10.0.0.1 |
| 90 | const mapped = |
| 91 | const ip = |
| 92 | const family = |
| 93 | if (alwaysBlocked.check |
| 94 | return !allowPrivate && |
| 95 | } |
| 96 | |
| 97 | /** Why `url` cannot be a webhook endpoint, or null when it can. */ |
| 98 | export async function webhookUrlProblem |
| 99 | url: string, |
| 100 | allowPrivate: boolean, |
| 101 | ): Promise<string |
| 102 | let parsed: |
| 103 | try { |
| 104 | parsed = new |
| 105 | } catch { |
| 106 | return 'Enter a full URL, such as https://example.com/webhook.'; |
| 107 | } |
| 108 | if (parsed.protocol |
| 109 | return 'The URL must start with https:// or http://.'; |
| 110 | } |
| 111 | if (parsed.username |
| 112 | return 'The URL cannot contain a username or password.'; |
| 113 | } |
| 114 | const host = |
| 115 | let addresses: |
| 116 | try { |
| 117 | addresses = isIP( |
| 118 | ? [host] |
| 119 | : (await lookup |
| 120 | } catch { |
| 121 | return `${host} |
| 122 | } |
| 123 | if (addresses.some |
| 124 | return `${host} |
| 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | /** 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. */ |
| 130 | export type WebhookOwner |
| 131 | | { repositoryId |
| 132 | | { organizationId |