Documentation
¶
Overview ¶
Package linkpreview is chalkd's server-side page fetcher for sender-built link previews (phase 57, docs/phases/PHASE-57-LINKPREVIEW.md).
Privacy model: the SENDER's client asks chalkd to fetch a page they are about to link, chalkd returns OpenGraph metadata (and, separately, the thumbnail bytes), and the client embeds the preview inside the normal E2E-encrypted body. Recipients render from ciphertext and never fetch anything. chalkd learns the URL and the requesting user -- the same trust carve-out as Giphy search -- and never the channel or recipients.
Security model: users can whitelist arbitrary domains client-side, so this fetcher must survive arbitrary URLs. The SSRF boundary is IP vetting at DIAL time (net.Dialer.Control): the vetted address is the exact one being connected to, which covers DNS rebinding, and every redirect hop re-dials through the same guard. Plus: https only, bounded redirects, bounded response sizes, content-type enforcement, no cookies, a generic UA.
YouTube oEmbed fast-path. YouTube's bot mitigation intermittently serves the fetcher an og-less shell page (empty " - YouTube" title, geo-localized generic description), which the <title>/meta-description fallback turned into a wrong-but-plausible card. The public oEmbed endpoint returns the real title, channel, and thumbnail as ~1 KB of JSON and 404s on unknown ids, so video URLs skip HTML fetching entirely.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBadURL = errors.New("linkpreview: invalid url")
ErrBadURL marks a request the client formed wrong (not https, no host, userinfo present, unparseable). Handlers map it to 400; everything else from Fetch/FetchImage is an upstream problem and maps to 502.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client fetches pages and thumbnails with the guards above. Construct with New.
func (*Client) Fetch ¶
Fetch retrieves rawURL and extracts its OpenGraph metadata. YouTube video URLs bypass the HTML path entirely and go through oEmbed (see oembed.go).
func (*Client) FetchImage ¶
FetchImage retrieves a thumbnail (typically the og:image from Fetch). Returns the raw bytes and their content type; the caller re-serves them to the sender's client, which encrypts them into an attachment.
type Option ¶
type Option func(*Client)
Option customizes a Client.
func WithAllowPrivateAddrs ¶
func WithAllowPrivateAddrs() Option
WithAllowPrivateAddrs disables the dial-time address guard. TESTS ONLY -- httptest servers listen on loopback, which production must reject.
func WithHTTPClient ¶
WithHTTPClient replaces the guarded HTTP client wholesale (tests). The dial guard, redirect policy, and timeout all live in the default client, so production callers must never use this.
func WithIPv4Only ¶ added in v0.12.2
func WithIPv4Only() Option
WithIPv4Only makes the default HTTP client dial with network "tcp4". The client then tries no IPv6 address. WithHTTPClient replaces the client, and then this option has no effect.
type Preview ¶
type Preview struct {
URL string `json:"url"` // final URL after redirects
Title string `json:"title"`
Description string `json:"description"`
SiteName string `json:"site_name"`
ImageURL string `json:"image_url"`
}
Preview is the metadata handed back to the sender's client. All fields may be empty (the composer then offers nothing). ImageURL is absolute https or empty; the client fetches it through FetchImage's endpoint, encrypts the bytes, and ships them as a normal attachment.