|
| 1 | +// Package source resolves image sources: file paths, data URIs, and remote URLs. |
| 2 | +package source |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// Kind tags how the source should be loaded. |
| 15 | +type Kind int |
| 16 | + |
| 17 | +const ( |
| 18 | + KindFile Kind = iota |
| 19 | + KindBytes |
| 20 | +) |
| 21 | + |
| 22 | +// Resolved is the resolver result. Exactly one of Path or Bytes is set. |
| 23 | +type Resolved struct { |
| 24 | + Kind Kind |
| 25 | + Path string // when Kind == KindFile |
| 26 | + Bytes []byte // when Kind == KindBytes |
| 27 | +} |
| 28 | + |
| 29 | +// MaxRemoteBytes caps remote/data URI payloads to prevent OOM on hostile servers. |
| 30 | +const MaxRemoteBytes = 64 * 1024 * 1024 |
| 31 | + |
| 32 | +// Resolve inspects src and returns either a file path or pre-loaded bytes. |
| 33 | +// HTTP(S) URLs and data: URIs are fetched/decoded here; everything else is |
| 34 | +// treated as a file path. |
| 35 | +func Resolve(ctx context.Context, src string) (*Resolved, error) { |
| 36 | + if strings.HasPrefix(src, "data:") { |
| 37 | + b, err := decodeDataURI(src) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + return &Resolved{Kind: KindBytes, Bytes: b}, nil |
| 42 | + } |
| 43 | + |
| 44 | + if u, err := url.Parse(src); err == nil && (u.Scheme == "http" || u.Scheme == "https") { |
| 45 | + b, err := fetch(ctx, src) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + return &Resolved{Kind: KindBytes, Bytes: b}, nil |
| 50 | + } |
| 51 | + |
| 52 | + return &Resolved{Kind: KindFile, Path: src}, nil |
| 53 | +} |
| 54 | + |
| 55 | +// decodeDataURI parses RFC 2397 data: URIs. Only base64-encoded payloads are |
| 56 | +// supported (the common form for images); plain percent-encoded payloads are |
| 57 | +// rejected — callers wanting that should pre-decode. |
| 58 | +func decodeDataURI(s string) ([]byte, error) { |
| 59 | + rest := strings.TrimPrefix(s, "data:") |
| 60 | + comma := strings.IndexByte(rest, ',') |
| 61 | + if comma < 0 { |
| 62 | + return nil, fmt.Errorf("data URI: missing comma") |
| 63 | + } |
| 64 | + meta, payload := rest[:comma], rest[comma+1:] |
| 65 | + if !strings.Contains(meta, "base64") { |
| 66 | + return nil, fmt.Errorf("data URI: only base64 payloads supported") |
| 67 | + } |
| 68 | + b, err := base64.StdEncoding.DecodeString(payload) |
| 69 | + if err != nil { |
| 70 | + return nil, fmt.Errorf("data URI: base64 decode: %w", err) |
| 71 | + } |
| 72 | + if len(b) > MaxRemoteBytes { |
| 73 | + return nil, fmt.Errorf("data URI: payload exceeds %d bytes", MaxRemoteBytes) |
| 74 | + } |
| 75 | + return b, nil |
| 76 | +} |
| 77 | + |
| 78 | +func fetch(ctx context.Context, rawURL string) ([]byte, error) { |
| 79 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("remote: build request: %w", err) |
| 82 | + } |
| 83 | + resp, err := http.DefaultClient.Do(req) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("remote: fetch: %w", err) |
| 86 | + } |
| 87 | + defer func() { _ = resp.Body.Close() }() |
| 88 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 89 | + return nil, fmt.Errorf("remote: HTTP %s", resp.Status) |
| 90 | + } |
| 91 | + b, err := io.ReadAll(io.LimitReader(resp.Body, MaxRemoteBytes+1)) |
| 92 | + if err != nil { |
| 93 | + return nil, fmt.Errorf("remote: read body: %w", err) |
| 94 | + } |
| 95 | + if len(b) > MaxRemoteBytes { |
| 96 | + return nil, fmt.Errorf("remote: response exceeds %d bytes", MaxRemoteBytes) |
| 97 | + } |
| 98 | + return b, nil |
| 99 | +} |
0 commit comments