This commit is contained in:
greeneclipse committed 2026-10-15 21:22:43 +02:00
1 parent 04794aea54
commit cfd31f398c
61 files changed
+2062 -254

No files matched your search

+50 -2
View File
@@ -172,6 +172,13 @@ func ValidateFeedURL(ctx context.Context, rawURL string) error {
// downloadFeed streams an http(s) feed to a temp file with SSRF controls and a hard size cap.
// Callers must Close the returned blob to remove the temp file.
func downloadFeed(ctx context.Context, rawURL string) (*feedBlob, error) {
return downloadFeedWithHeaders(ctx, rawURL, nil)
}
func downloadFeedWithHeaders(ctx context.Context, rawURL string, extra map[string]string) (*feedBlob, error) {
extra, err := validatedFeedHeaders(extra)
if err != nil {
return nil, err
}
ensureAllowlist()
rawURL = strings.TrimSpace(rawURL)
if rawURL == "" {
@@ -203,7 +210,15 @@ func downloadFeed(ctx context.Context, rawURL string) (*feedBlob, error) {
if req.URL.Scheme != "http" && req.URL.Scheme != "https" {
return errURLScheme
}
return assertPublicHost(req.Context(), req.URL.Hostname())
if err := assertPublicHost(req.Context(), req.URL.Hostname()); err != nil {
return err
}
if len(via) > 0 && !strings.EqualFold(req.URL.Hostname(), via[0].URL.Hostname()) {
for name := range extra {
req.Header.Del(name)
}
}
return nil
},
}
@@ -212,7 +227,10 @@ func downloadFeed(ctx context.Context, rawURL string) (*feedBlob, error) {
return nil, err
}
req.Header.Set("User-Agent", defaultUserAgent)
req.Header.Set("Accept", "text/csv,application/csv,application/xml,text/xml,application/atom+xml,*/*")
req.Header.Set("Accept", "application/json,text/csv,application/csv,application/xml,text/xml,application/atom+xml,*/*")
for name, value := range extra {
req.Header.Set(name, value)
}
res, err := client.Do(req)
if err != nil {
@@ -402,3 +420,33 @@ func isBlockedIP(ip net.IP) bool {
}
return false
}
// DownloadImportSource safely fetches a one-time catalog import URL with optional headers.
// The caller must close the returned reader, which removes the temporary file.
func DownloadImportSource(ctx context.Context, rawURL string, headers map[string]string, maxSize int64) (io.ReadCloser, error) {
b, err := downloadFeedWithHeaders(ctx, rawURL, headers)
if err != nil {
return nil, err
}
if b.size > maxSize {
b.Close()
return nil, ClientMsg("catalog import exceeds size limit")
}
f, err := b.Open()
if err != nil {
b.Close()
return nil, err
}
return &cleanupImportReader{ReadCloser: f, blob: b}, nil
}
type cleanupImportReader struct {
io.ReadCloser
blob *feedBlob
}
func (r *cleanupImportReader) Close() error {
err := r.ReadCloser.Close()
_ = r.blob.Close()
return err
}