fix: force file urls to https

This commit is contained in:
richjohnrambo
2026-09-16 09:52:55 +08:00
parent 9626e8ee7c
commit 1de69fcc5b
4 changed files with 90 additions and 2 deletions
+24 -1
View File
@@ -17,6 +17,7 @@ import (
"io"
"math/rand"
"mime"
"net"
"net/http"
"net/url"
"path/filepath"
@@ -369,6 +370,7 @@ func largeFileReceiveHTTP(wrt http.ResponseWriter, req *http.Request) {
logs.Warn.Printf("media upload: failed to enqueue file lifecycle event file=%s: %v", fdef.Id, err)
}
url = normalizeUploadURL(url)
params := map[string]any{"url": url, "file_id": fdef.Uid().String()}
if globals.mediaGcPeriod > 0 {
// How long this file is guaranteed to exist without being attached to a message or a topic.
@@ -700,7 +702,7 @@ func (*grpcNodeServer) LargeFileReceive(stream pbx.Node_LargeFileReceiveServer)
Code: http.StatusOK,
Text: http.StatusText(http.StatusOK),
Meta: &pbx.FileMeta{
Name: url,
Name: normalizeUploadURL(url),
MimeType: mimeType,
Etag: fdef.ETag,
Size: size,
@@ -719,6 +721,27 @@ func (*grpcNodeServer) LargeFileReceive(stream pbx.Node_LargeFileReceiveServer)
return err
}
func normalizeUploadURL(rawURL string) string {
parsed, err := url.Parse(strings.TrimSpace(rawURL))
if err != nil || parsed.Scheme != "http" || parsed.Host == "" || isLocalUploadHost(parsed.Hostname()) {
return rawURL
}
parsed.Scheme = "https"
return parsed.String()
}
func isLocalUploadHost(host string) bool {
host = strings.ToLower(strings.TrimSpace(host))
if host == "" || host == "localhost" {
return true
}
ip := net.ParseIP(host)
if ip == nil {
return false
}
return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast()
}
// largeFileRunGarbageCollection runs every 'period' and deletes up to 'blockSize' unused files.
// Returns channel which can be used to stop the process.
func largeFileRunGarbageCollection(period time.Duration, blockSize int) chan<- bool {