-
Notifications
You must be signed in to change notification settings - Fork 112
feat: Webhooks for Real-Time Click Events #697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { useState } from "react"; | ||
| import toast from "react-hot-toast"; | ||
|
|
||
| interface WebhookSectionProps { | ||
| workspaceId: string; | ||
| initialWebhookUrl?: string | null; | ||
| initialWebhookSecret?: string | null; | ||
| onUpdate?: (url: string | null, secret: string | null) => void; | ||
| } | ||
|
|
||
| export function WebhookSection({ workspaceId, initialWebhookUrl, initialWebhookSecret, onUpdate }: WebhookSectionProps) { | ||
| const [webhookUrl, setWebhookUrl] = useState(initialWebhookUrl || ""); | ||
| const [secret, setSecret] = useState(initialWebhookSecret || ""); | ||
| const [saving, setSaving] = useState(false); | ||
|
|
||
| const handleSave = async () => { | ||
| setSaving(true); | ||
| try { | ||
| const res = await fetch("/api/settings", { | ||
| method: "PUT", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "x-workspace-id": workspaceId | ||
| }, | ||
| body: JSON.stringify({ webhookUrl: webhookUrl.trim() === "" ? "" : webhookUrl }), | ||
| }); | ||
| const data = await res.json(); | ||
| if (data.success) { | ||
| toast.success("Webhook settings saved!"); | ||
| setSecret(data.webhookSecret || ""); | ||
| if (onUpdate) onUpdate(data.webhookUrl, data.webhookSecret); | ||
| } else { | ||
| toast.error(data.error || "Failed to save webhook settings"); | ||
| } | ||
| } catch (error) { | ||
| toast.error("Failed to save webhook settings"); | ||
| } | ||
| setSaving(false); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="bg-card text-card-foreground p-6 rounded-lg border border-border shadow-sm"> | ||
| <h2 className="text-xl font-semibold mb-4">Developer Webhooks</h2> | ||
| <p className="text-sm text-muted-foreground mb-6"> | ||
| Receive real-time HTTP POST requests whenever a visitor clicks a link on your profile. | ||
| </p> | ||
| <div className="space-y-4"> | ||
| <div> | ||
| <label className="block text-sm font-medium mb-1">Payload URL</label> | ||
| <input | ||
| type="url" | ||
| value={webhookUrl} | ||
| onChange={(e) => setWebhookUrl(e.target.value)} | ||
| placeholder="https://example.com/webhook" | ||
| className="w-full px-3 py-2 border rounded-md bg-background focus:outline-none focus:ring-2 focus:ring-primary" | ||
| /> | ||
| </div> | ||
| {secret && ( | ||
| <div> | ||
| <label className="block text-sm font-medium mb-1">Signing Secret</label> | ||
| <input | ||
| type="text" | ||
| readOnly | ||
| value={secret} | ||
| className="w-full px-3 py-2 border rounded-md bg-muted text-muted-foreground font-mono text-sm" | ||
| /> | ||
| <p className="text-xs text-muted-foreground mt-1">Use this secret to verify the HMAC SHA256 signature in the x-linkid-signature header.</p> | ||
| </div> | ||
| )} | ||
| <button | ||
| onClick={handleSave} | ||
| disabled={saving} | ||
| className="px-4 py-2 bg-primary text-primary-foreground rounded-md text-sm font-medium hover:bg-primary/90 disabled:opacity-50" | ||
| > | ||
| {saving ? "Saving..." : "Save Webhook"} | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import dns from 'dns/promises'; | ||
| import ipaddr from 'ipaddr.js'; | ||
|
|
||
| export class WebhookValidationError extends Error { | ||
| constructor(message: string) { | ||
| super(message); | ||
| this.name = 'WebhookValidationError'; | ||
| } | ||
| } | ||
|
|
||
| export async function validateWebhookUrl(urlString: string): Promise<void> { | ||
| let url: URL; | ||
| try { | ||
| url = new URL(urlString); | ||
| } catch { | ||
| throw new WebhookValidationError('Invalid URL format'); | ||
| } | ||
|
|
||
| if (url.protocol !== 'https:') { | ||
| throw new WebhookValidationError('Webhook URLs must use HTTPS'); | ||
| } | ||
|
|
||
| // Resolve the hostname | ||
| let addresses: { address: string; family: number }[] = []; | ||
| try { | ||
| addresses = await dns.lookup(url.hostname, { all: true }); | ||
|
Comment on lines
+23
to
+26
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift Bind webhook delivery to a validated address. This lookup result is checked and then discarded. Return validated addresses from this function and make the delivery client connect only to one of them. Preserve the original hostname for TLS and SNI. An egress proxy that enforces this policy is also valid. 🤖 Prompt for AI Agents |
||
| } catch (error) { | ||
| throw new WebhookValidationError(`Could not resolve hostname: ${url.hostname}`); | ||
| } | ||
|
|
||
| if (addresses.length === 0) { | ||
| throw new WebhookValidationError(`Could not resolve hostname: ${url.hostname}`); | ||
| } | ||
|
|
||
| for (const { address } of addresses) { | ||
| if (!ipaddr.isValid(address)) { | ||
| throw new WebhookValidationError(`Invalid IP address resolved: ${address}`); | ||
| } | ||
|
|
||
| const ip = ipaddr.parse(address); | ||
| const range = ip.range(); | ||
|
|
||
| // Block all non-unicast or private ranges | ||
| const blockedRanges = [ | ||
| 'unspecified', | ||
| 'broadcast', | ||
| 'multicast', | ||
| 'linkLocal', | ||
| 'loopback', | ||
| 'private', | ||
| 'carrierGradeNat', | ||
| 'uniqueLocal', | ||
| 'ipv4Mapped', | ||
| 'rfc6145', | ||
| 'rfc6052', | ||
| '6to4', | ||
| 'teredo' | ||
| ]; | ||
|
|
||
| if (blockedRanges.includes(range)) { | ||
| throw new WebhookValidationError(`Resolved IP address (${address}) is in a blocked range (${range})`); | ||
| } | ||
|
|
||
| // Specifically block AWS IMDS IPv4 (169.254.169.254) which might be covered by linkLocal | ||
| if (address === '169.254.169.254') { | ||
| throw new WebhookValidationError('Metadata service IP addresses are not allowed'); | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.