fix(im): prefer tenant ID from context, shorten presigned URL TTL

Addresses review feedback on the IM storage URL rewrite:

- localFileService.GetFileURL now reads tenant ID from request context
  first, falling back to ParseTenantIDFromStoragePath only when context
  is absent. Fixes ambiguity for cloud providers whose paths embed
  numeric bucket/region names before the tenant segment, which could
  mint presigned URLs bound to the wrong tenant ID.
- Shorten presigned URL default TTL from 24h to 2h. A leaked HMAC key
  authorizes cross-tenant file reads, so URLs should expire quickly;
  IM clients fetch referenced images within seconds anyway.
- Document ParseTenantIDFromStoragePath as a best-effort fallback.
- Add unit tests covering context-first, path-fallback, and the
  no-external-URL backward-compat path.
This commit is contained in:
wizardchen
2026-04-30 10:59:54 +08:00
committed by lyingbug
parent 7fd566bc15
commit f27a1e083c
3 changed files with 73 additions and 3 deletions
+7 -1
View File
@@ -11,6 +11,7 @@ import (
"time"
"github.com/Tencent/WeKnora/internal/logger"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
)
@@ -204,7 +205,12 @@ func (s *localFileService) GetFileURL(ctx context.Context, filePath string) (str
// If external URL is configured, generate a presigned HTTP URL.
if s.externalURL != "" {
tenantID := secutils.ParseTenantIDFromStoragePath(normalized)
// Prefer tenant ID from context (authoritative); fall back to parsing
// the storage path for callers that don't propagate tenant context.
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok || tenantID == 0 {
tenantID = secutils.ParseTenantIDFromStoragePath(normalized)
}
presignedURL, err := secutils.SignFileURL(s.externalURL, normalized, tenantID, 0)
if err != nil {
logger.Warnf(ctx, "Failed to generate presigned URL for %s: %v, returning local:// path", normalized, err)