| 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.closed', |
| 12 | 'issue.reopened', |
| 13 | 'issue.assigned', |
| 14 | 'issue.commented', |
| 15 | 'pull_request.merged', |
| 16 | 'pull_request.reviewed', |
| 17 | 'pull_request.review_commented', |
| 18 | ] as const satisfies |
| 19 | |
| 20 | export type WebhookEvent |
| 21 | |
| 22 | /** A new signing secret, shown to the user once. */ |
| 23 | export function newWebhookSecret() |
| 24 | return `whsec_${randomBytes |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Encrypts a signing secret for `webhook_endpoint.secret`. apps/delivery decrypts it to sign each request, so it is encrypted, not hashed. |
| 29 | * Format: `v1:` + base64(12-byte nonce || AES-256-GCM ciphertext || 16-byte tag), which is what Go's `gcm.Open` reads. |
| 30 | */ |
| 31 | export function sealWebhookSecret(key |
| 32 | const nonce = |
| 33 | const cipher = |
| 34 | const sealed = |
| 35 | nonce, |
| 36 | cipher.update(secret |
| 37 | cipher.final(), |
| 38 | cipher.getAuthTag(), |
| 39 | ]); |
| 40 | return `v1:${sealed |
| 41 | } |
| 42 | |
| 43 | /** The key from `WEBHOOK_SECRET_KEY`: 32 bytes, base64-encoded. */ |
| 44 | export function webhookSecretKey(value |
| 45 | const key = |
| 46 | return key?.length |
| 47 | } |
| 48 | |
| 49 | // 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. |
| 50 | const alwaysBlocked = new |
| 51 | alwaysBlocked.addSubnet('0.0.0.0' |
| 52 | alwaysBlocked.addSubnet('169.254.0.0' |
| 53 | alwaysBlocked.addSubnet('224.0.0.0' |
| 54 | alwaysBlocked.addSubnet('fe80::' |
| 55 | alwaysBlocked.addSubnet('ff00::' |
| 56 | alwaysBlocked.addAddress('::' |
| 57 | |
| 58 | const privateNetworks = new |
| 59 | privateNetworks.addSubnet('10.0.0.0' |
| 60 | privateNetworks.addSubnet('172.16.0.0' |
| 61 | privateNetworks.addSubnet('192.168.0.0' |
| 62 | privateNetworks.addSubnet('127.0.0.0' |
| 63 | privateNetworks.addSubnet('100.64.0.0' |
| 64 | privateNetworks.addSubnet('fc00::' |
| 65 | privateNetworks.addAddress('::1' |
| 66 | |
| 67 | export function blockedAddress(address |
| 68 | // ::ffff:10.0.0.1 is 10.0.0.1 |
| 69 | const mapped = |
| 70 | const ip = |
| 71 | const family = |
| 72 | if (alwaysBlocked.check |
| 73 | return !allowPrivate && |
| 74 | } |
| 75 | |
| 76 | /** Why `url` cannot be a webhook endpoint, or null when it can. */ |
| 77 | export async function webhookUrlProblem |
| 78 | url: string, |
| 79 | allowPrivate: boolean, |
| 80 | ): Promise<string |
| 81 | let parsed: |
| 82 | try { |
| 83 | parsed = new |
| 84 | } catch { |
| 85 | return 'Enter a full URL, such as https://example.com/webhook.'; |
| 86 | } |
| 87 | if (parsed.protocol |
| 88 | return 'The URL must start with https:// or http://.'; |
| 89 | } |
| 90 | if (parsed.username |
| 91 | return 'The URL cannot contain a username or password.'; |
| 92 | } |
| 93 | const host = |
| 94 | let addresses: |
| 95 | try { |
| 96 | addresses = isIP( |
| 97 | ? [host] |
| 98 | : (await lookup |
| 99 | } catch { |
| 100 | return `${host} |
| 101 | } |
| 102 | if (addresses.some |
| 103 | return `${host} |
| 104 | } |
| 105 | return null; |
| 106 | } |