|
| 1 | +package registry |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/junixlabs/devbox/internal/template" |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +// DefaultRegistryURL is the base URL for the community template registry. |
| 15 | +const DefaultRegistryURL = "https://raw.githubusercontent.com/junixlabs/devbox-templates/main" |
| 16 | + |
| 17 | +// maxResponseBytes limits HTTP response body size to prevent memory exhaustion. |
| 18 | +const maxResponseBytes = 1 << 20 // 1 MB |
| 19 | + |
| 20 | +// IndexEntry describes a single template in the remote registry index. |
| 21 | +type IndexEntry struct { |
| 22 | + Name string `yaml:"name"` |
| 23 | + Version string `yaml:"version"` |
| 24 | + Description string `yaml:"description"` |
| 25 | + URL string `yaml:"url"` |
| 26 | + UpdatedAt time.Time `yaml:"updated_at"` |
| 27 | +} |
| 28 | + |
| 29 | +// Index is the top-level structure of the registry index file. |
| 30 | +type Index struct { |
| 31 | + Templates []IndexEntry `yaml:"templates"` |
| 32 | +} |
| 33 | + |
| 34 | +// RemoteRegistry fetches templates from an HTTP-based registry. |
| 35 | +type RemoteRegistry struct { |
| 36 | + baseURL string |
| 37 | + client *http.Client |
| 38 | +} |
| 39 | + |
| 40 | +// NewRemoteRegistry creates a new remote registry client. |
| 41 | +// If baseURL is empty, DefaultRegistryURL is used. |
| 42 | +func NewRemoteRegistry(baseURL string) *RemoteRegistry { |
| 43 | + if baseURL == "" { |
| 44 | + baseURL = DefaultRegistryURL |
| 45 | + } |
| 46 | + return &RemoteRegistry{ |
| 47 | + baseURL: strings.TrimRight(baseURL, "/"), |
| 48 | + client: &http.Client{Timeout: 30 * time.Second}, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// FetchIndex downloads and parses the registry index. |
| 53 | +func (r *RemoteRegistry) FetchIndex() ([]IndexEntry, error) { |
| 54 | + url := r.baseURL + "/index.yaml" |
| 55 | + resp, err := r.client.Get(url) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("failed to fetch registry index: %w", err) |
| 58 | + } |
| 59 | + defer resp.Body.Close() |
| 60 | + |
| 61 | + if resp.StatusCode != http.StatusOK { |
| 62 | + return nil, fmt.Errorf("registry returned HTTP %d", resp.StatusCode) |
| 63 | + } |
| 64 | + |
| 65 | + data, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes)) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("failed to read registry index: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + var idx Index |
| 71 | + if err := yaml.Unmarshal(data, &idx); err != nil { |
| 72 | + return nil, fmt.Errorf("failed to parse registry index: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + return idx.Templates, nil |
| 76 | +} |
| 77 | + |
| 78 | +// Search returns index entries matching the query (case-insensitive substring |
| 79 | +// match on name or description). |
| 80 | +func (r *RemoteRegistry) Search(query string) ([]IndexEntry, error) { |
| 81 | + entries, err := r.FetchIndex() |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + if query == "" { |
| 87 | + return nil, nil |
| 88 | + } |
| 89 | + |
| 90 | + q := strings.ToLower(query) |
| 91 | + var matches []IndexEntry |
| 92 | + for _, e := range entries { |
| 93 | + if strings.Contains(strings.ToLower(e.Name), q) || |
| 94 | + strings.Contains(strings.ToLower(e.Description), q) { |
| 95 | + matches = append(matches, e) |
| 96 | + } |
| 97 | + } |
| 98 | + return matches, nil |
| 99 | +} |
| 100 | + |
| 101 | +// Pull downloads a template by name from the registry and saves it to the |
| 102 | +// local registry. |
| 103 | +func (r *RemoteRegistry) Pull(name string, localReg *template.LocalRegistry) (*template.Template, error) { |
| 104 | + entries, err := r.FetchIndex() |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + var entry *IndexEntry |
| 110 | + for i := range entries { |
| 111 | + if entries[i].Name == name { |
| 112 | + entry = &entries[i] |
| 113 | + break |
| 114 | + } |
| 115 | + } |
| 116 | + if entry == nil { |
| 117 | + return nil, fmt.Errorf("template %q not found in registry", name) |
| 118 | + } |
| 119 | + |
| 120 | + // Only allow safe relative URLs to prevent SSRF via malicious index entries. |
| 121 | + templateURL := entry.URL |
| 122 | + if strings.HasPrefix(templateURL, "http://") || strings.HasPrefix(templateURL, "https://") || |
| 123 | + strings.HasPrefix(templateURL, "//") || strings.Contains(templateURL, "..") || |
| 124 | + strings.Contains(templateURL, ":") { |
| 125 | + return nil, fmt.Errorf("template %q has unsafe URL %q", name, templateURL) |
| 126 | + } |
| 127 | + templateURL = r.baseURL + "/" + strings.TrimLeft(templateURL, "/") |
| 128 | + |
| 129 | + resp, err := r.client.Get(templateURL) |
| 130 | + if err != nil { |
| 131 | + return nil, fmt.Errorf("failed to download template %q: %w", name, err) |
| 132 | + } |
| 133 | + defer resp.Body.Close() |
| 134 | + |
| 135 | + if resp.StatusCode != http.StatusOK { |
| 136 | + return nil, fmt.Errorf("failed to download template %q: HTTP %d", name, resp.StatusCode) |
| 137 | + } |
| 138 | + |
| 139 | + data, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes)) |
| 140 | + if err != nil { |
| 141 | + return nil, fmt.Errorf("failed to read template %q: %w", name, err) |
| 142 | + } |
| 143 | + |
| 144 | + var t template.Template |
| 145 | + if err := yaml.Unmarshal(data, &t); err != nil { |
| 146 | + return nil, fmt.Errorf("failed to parse template %q: %w", name, err) |
| 147 | + } |
| 148 | + |
| 149 | + if t.Name == "" { |
| 150 | + t.Name = name |
| 151 | + } |
| 152 | + |
| 153 | + if err := localReg.Save(&t); err != nil { |
| 154 | + return nil, fmt.Errorf("failed to save template %q: %w", name, err) |
| 155 | + } |
| 156 | + |
| 157 | + return &t, nil |
| 158 | +} |
| 159 | + |
| 160 | +// Push reads a template from the local registry and outputs its YAML content |
| 161 | +// for manual submission to the community registry. |
| 162 | +func (r *RemoteRegistry) Push(name string, localReg *template.LocalRegistry) (string, error) { |
| 163 | + t, err := localReg.Get(name) |
| 164 | + if err != nil { |
| 165 | + return "", fmt.Errorf("failed to read local template %q: %w", name, err) |
| 166 | + } |
| 167 | + |
| 168 | + data, err := yaml.Marshal(t) |
| 169 | + if err != nil { |
| 170 | + return "", fmt.Errorf("failed to marshal template %q: %w", name, err) |
| 171 | + } |
| 172 | + |
| 173 | + return string(data), nil |
| 174 | +} |
0 commit comments