fix: keep upload urls unchanged

This commit is contained in:
richjohnrambo
2026-09-16 09:56:15 +08:00
parent 1de69fcc5b
commit 68b196215f
2 changed files with 1 additions and 69 deletions
+1 -24
View File
@@ -17,7 +17,6 @@ import (
"io"
"math/rand"
"mime"
"net"
"net/http"
"net/url"
"path/filepath"
@@ -370,7 +369,6 @@ 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.
@@ -702,7 +700,7 @@ func (*grpcNodeServer) LargeFileReceive(stream pbx.Node_LargeFileReceiveServer)
Code: http.StatusOK,
Text: http.StatusText(http.StatusOK),
Meta: &pbx.FileMeta{
Name: normalizeUploadURL(url),
Name: url,
MimeType: mimeType,
Etag: fdef.ETag,
Size: size,
@@ -721,27 +719,6 @@ 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 {
-45
View File
@@ -74,51 +74,6 @@ func TestAuthFileRequestFromSessionRequiresTenant(t *testing.T) {
}
}
func TestNormalizeUploadURL(t *testing.T) {
for _, tc := range []struct {
name string
in string
want string
}{
{
name: "public http becomes https",
in: "http://api.118im.com/v0/file/s/abc.jpg",
want: "https://api.118im.com/v0/file/s/abc.jpg",
},
{
name: "query preserved",
in: "http://cdn.example.com/a/b.png?x=1",
want: "https://cdn.example.com/a/b.png?x=1",
},
{
name: "relative unchanged",
in: "/v0/file/s/abc.jpg",
want: "/v0/file/s/abc.jpg",
},
{
name: "https unchanged",
in: "https://api.118im.com/v0/file/s/abc.jpg",
want: "https://api.118im.com/v0/file/s/abc.jpg",
},
{
name: "localhost unchanged",
in: "http://localhost:6060/v0/file/s/abc.jpg",
want: "http://localhost:6060/v0/file/s/abc.jpg",
},
{
name: "private ip unchanged",
in: "http://10.42.0.1/v0/file/s/abc.jpg",
want: "http://10.42.0.1/v0/file/s/abc.jpg",
},
} {
t.Run(tc.name, func(t *testing.T) {
if got := normalizeUploadURL(tc.in); got != tc.want {
t.Fatalf("normalizeUploadURL(%q)=%q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestAuthFileRequestFromTokenReturnsPrincipal(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()