perf(media): OSS 资源 - 通过签名地址直连下载并禁止浏览器代理媒体
This commit is contained in:
1 parent
8436904316
commit
7737047a8d
9 files changed
+213
-19
No files matched your search
@@ -31,6 +31,7 @@ const (
|
||||
type AccessOptions struct {
|
||||
Purpose AccessPurpose `json:"purpose"`
|
||||
Variant ResourceVariant `json:"variant"`
|
||||
DownloadName string `json:"downloadName,omitempty"`
|
||||
ExpiresAt time.Time `json:"-"`
|
||||
DescribeOnly bool `json:"-"`
|
||||
}
|
||||
@@ -81,6 +82,9 @@ func NormalizeAccessOptions(options AccessOptions, allowProvider bool) (AccessOp
|
||||
if options.Purpose == PurposeCopy || options.Purpose == PurposeDownload || options.Purpose == PurposeProvider {
|
||||
options.Variant = VariantOriginal
|
||||
}
|
||||
if options.Purpose != PurposeDownload {
|
||||
options.DownloadName = ""
|
||||
}
|
||||
return options, nil
|
||||
}
|
||||
|
||||
@@ -120,6 +124,16 @@ func ResolveAccess(resource *model.Resource, setting storage.Settings, options A
|
||||
}
|
||||
if resource.Provider == "local" {
|
||||
access.Delivery = DeliveryLocal
|
||||
} else if options.Purpose == PurposeDownload {
|
||||
// Cross-origin `a[download]` is only advisory. A real browser download must
|
||||
// therefore be enforced by the object store response itself. Downloads use
|
||||
// a signed origin URL carrying Content-Disposition=attachment; media bytes
|
||||
// never pass through the application server or a Blob relay.
|
||||
if !storage.PublicOrigin(setting) {
|
||||
return nil, AccessError(503, "resource_origin_private", "对象存储源站不可由浏览器直连,无法在不占用服务器带宽的前提下下载")
|
||||
}
|
||||
access.URL, err = storage.SignedOriginObjectDownloadURL(setting, resource.ObjectKey, expires, options.DownloadName)
|
||||
access.Delivery = DeliveryOrigin
|
||||
} else if storage.CDNEnabled(setting) {
|
||||
access.URL, err = storage.SignCDNURL(setting, resource.ObjectKey, expires)
|
||||
access.Delivery = DeliveryCDN
|
||||
@@ -134,11 +148,14 @@ func ResolveAccess(resource *model.Resource, setting storage.Settings, options A
|
||||
if setting.CDNBaseURL != "" {
|
||||
access.FallbackReason = "cdn_auth_unconfigured"
|
||||
}
|
||||
} else if setting.Delivery.AllowPrivateProxy {
|
||||
} else if options.Purpose == PurposeProvider && setting.Delivery.AllowPrivateProxy {
|
||||
// Only the server-side provider-input path may opt into a private-origin
|
||||
// proxy. Browser display/copy/process/download traffic for OSS resources
|
||||
// must never relay media bytes through the application server.
|
||||
access.Delivery = DeliveryProxy
|
||||
access.FallbackReason = "private_origin"
|
||||
} else {
|
||||
return nil, AccessError(503, "resource_origin_private", "资源源站不可公网访问,请配置 CDN 或明确启用私网源站代理")
|
||||
return nil, AccessError(503, "resource_origin_private", "对象存储源站不可由浏览器直连,请配置公网 OSS 源站或 CDN")
|
||||
}
|
||||
if err != nil {
|
||||
failure := AccessError(503, "resource_signing_failed", "资源访问地址签发失败,请检查存储分发设置")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package assets
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -66,10 +67,17 @@ func TestResolveAccessPolicyMatrix(t *testing.T) {
|
||||
wantErr: "resource_cdn_unconfigured",
|
||||
},
|
||||
{
|
||||
name: "private origin requires explicit proxy",
|
||||
name: "browser access never proxies a private OSS origin",
|
||||
resource: testReadyResource("aliyun"),
|
||||
setting: storage.Settings{Provider: "aliyun", Endpoint: "http://storage.internal", Delivery: storage.DeliverySettings{AllowPrivateProxy: true}},
|
||||
options: AccessOptions{Purpose: PurposeDisplay},
|
||||
wantErr: "resource_origin_private",
|
||||
},
|
||||
{
|
||||
name: "provider input may use an explicitly enabled private proxy",
|
||||
resource: testReadyResource("aliyun"),
|
||||
setting: storage.Settings{Provider: "aliyun", Endpoint: "http://storage.internal", Delivery: storage.DeliverySettings{AllowPrivateProxy: true}},
|
||||
options: AccessOptions{Purpose: PurposeProvider},
|
||||
wantMode: DeliveryProxy, wantReason: "private_origin",
|
||||
},
|
||||
}
|
||||
@@ -124,6 +132,42 @@ func TestResolveAccessVariantAndExpiryContract(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDownloadUsesObjectOriginAttachmentInsteadOfCDNOrProxy(t *testing.T) {
|
||||
now := time.Date(2026, time.January, 2, 3, 4, 5, 0, time.UTC)
|
||||
resource := testReadyResource("aliyun")
|
||||
setting := storage.Settings{
|
||||
// A public IP keeps the test deterministic: PublicOrigin intentionally
|
||||
// resolves hostnames to reject private DNS answers, so a made-up OSS host
|
||||
// would depend on the machine's DNS configuration.
|
||||
Provider: "aliyun", Endpoint: "https://1.1.1.1", Bucket: "private-bucket",
|
||||
AccessKeyID: "access-id", AccessKeySecret: "secret-value", CDNBaseURL: "https://media.example.com",
|
||||
Delivery: storage.DeliverySettings{CDNAuthMode: "public"},
|
||||
}
|
||||
access, err := ResolveAccess(resource, setting, AccessOptions{Purpose: PurposeDownload, DownloadName: "画布_镜头01.mp4"}, now, testPlatformURL(&[]ResourceVariant{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
parsed, err := url.Parse(access.URL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
disposition := parsed.Query().Get("response-content-disposition")
|
||||
if access.Delivery != DeliveryOrigin || parsed.Host != "1.1.1.1" || !strings.HasPrefix(disposition, "attachment") || !strings.Contains(strings.ToLower(disposition), "utf-8''") {
|
||||
t.Fatalf("download access = %#v, disposition=%q", access, disposition)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDownloadRejectsPrivateOriginInsteadOfProxyingMediaBytes(t *testing.T) {
|
||||
setting := storage.Settings{
|
||||
Provider: "aliyun", Endpoint: "http://storage.internal", Bucket: "private-bucket",
|
||||
Delivery: storage.DeliverySettings{AllowPrivateProxy: true},
|
||||
}
|
||||
_, err := ResolveAccess(testReadyResource("aliyun"), setting, AccessOptions{Purpose: PurposeDownload}, time.Now(), testPlatformURL(&[]ResourceVariant{}))
|
||||
if err == nil || errorReason(err) != "resource_origin_private" {
|
||||
t.Fatalf("ResolveAccess() error = %v, want resource_origin_private", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveAccessRejectsPendingResource(t *testing.T) {
|
||||
resource := testReadyResource("local")
|
||||
resource.Status = model.ResourceStatusPending
|
||||
|
||||
Reference in new issue
Block a user