mediafetch

package
v1.30.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package mediafetch downloads scraped image assets with SSRF and size guards, and detects signed URLs whose expiry has already passed.

Index

Constants

View Source
const MaxBytes = 10 * 1024 * 1024

MaxBytes caps how much of a response is read, so a hostile or misconfigured host cannot exhaust memory.

Variables

This section is empty.

Functions

func DataURI added in v1.30.0

func DataURI(ctx context.Context, client *http.Client, rawURL string, allowPrivate bool) (string, error)

DataURI fetches an image and encodes it as a data URI, the form Stash's cover_image field takes.

A URL carrying an already-passed signed expiry is rejected without a request: scraped CDN thumbnails are often dead within hours of the scrape.

func Expired

func Expired(rawURL string, now time.Time) bool

Expired reports whether rawURL carries a signed expiry that has already passed at time now.

Many CDNs hand out short-lived signed URLs — `?expires=…&token=…` — so a stored thumbnail is often dead within hours. Detecting that offline lets a caller skip a request it knows will 403, and lets the NFO writer omit a `<thumb>` rather than bake in a broken link.

It is deliberately conservative: an unrecognised or unparseable expiry returns false. A false negative costs one failed request; a false positive would discard a working URL.

func ValidateURL

func ValidateURL(rawURL string, allowPrivate bool) error

ValidateURL enforces the SSRF defense: http(s) only, and unless allowPrivate is set, no host that resolves to a private or loopback address.

This resolves DNS once, before the request, so DNS rebinding is not mitigated. For the threat model here — consuming someone else's scraped JSON — the dump author would also need to control DNS for a domain the importer resolves.

Types

type Asset

type Asset struct {
	Data        []byte
	ContentType string
}

Asset is a downloaded image.

func Fetch

func Fetch(ctx context.Context, client *http.Client, rawURL string, allowPrivate bool) (Asset, error)

Fetch downloads rawURL after validating it. allowPrivate permits hosts that resolve to private or loopback addresses, for local media servers.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL