s3api: tell a symlink apart from an empty object on GET and HEAD (#13299)

A symlink made through weed mount holds no data, so GET and HEAD
answered for it as for an empty file: Content-Length 0 and a type
guessed from its name. An S3 client could not tell it from an empty
file, nor see where it pointed.

For an entry with the symlink mode bit, send the target as user
metadata, x-amz-meta-seaweedfs-symlink-target, escaped like a key under
encoding-type=url, and use Content-Type inode/symlink (what file(1)
reports) when none is stored. User metadata is the one place AWS has for
an object's own fields, so SDKs and the AWS CLI show it as they are. The
link is not followed: the target is a path on the client that made it.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yip LeoandClaude Opus 5.5 authored and GitHub committed 2026-12-02 12:05:12 -08:00
1 parent acb7728915
commit 77a070fe0a
3 files changed
+116

No files matched your search

+5
View File
@@ -71,6 +71,11 @@ const (
AmzMpPartsCount = "x-amz-mp-parts-count"
AmzMultipartUploadStatus = "x-amz-multipart-upload-status"
// GET/HEAD of a symlink made through a mount: its target, URL-escaped,
// as lowercase user metadata, and a Content-Type of inode/symlink.
AmzMetaSymlinkTarget = "x-amz-meta-seaweedfs-symlink-target"
SymlinkContentType = "inode/symlink"
// S3 ACL headers
AmzCannedAcl = "X-Amz-Acl"
AmzAclFullControl = "X-Amz-Grant-Full-Control"
+17
View File
@@ -12,6 +12,7 @@ import (
"mime"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
@@ -2524,11 +2525,19 @@ func (s3a *S3ApiServer) setResponseHeaders(w http.ResponseWriter, r *http.Reques
w.Header().Set("Last-Modified", modTime.Format(http.TimeFormat))
}
// A symlink made through a mount holds no data, so it reads as an empty
// object; its type and target below tell it apart from an empty file.
isSymlink := os.FileMode(entry.GetAttributes().GetFileMode())&os.ModeSymlink != 0
// Set Content-Type
mimeType := ""
if entry.Attributes != nil && entry.Attributes.Mime != "" {
mimeType = entry.Attributes.Mime
}
if mimeType == "" && isSymlink {
// The name says nothing about an empty body; call it what file(1) does.
mimeType = s3_constants.SymlinkContentType
}
if mimeType == "" {
// Try to detect from entry name
if entry.Name != "" {
@@ -2567,6 +2576,14 @@ func (s3a *S3ApiServer) setResponseHeaders(w http.ResponseWriter, r *http.Reques
}
}
// The target goes out as user metadata, the one place AWS has for an
// object's own fields. It is not followed: it names a path on the client
// that made it. A target may hold any byte but NUL, so it is escaped like a
// key under encoding-type=url.
if isSymlink {
w.Header()[s3_constants.AmzMetaSymlinkTarget] = []string{urlPathEscape(entry.Attributes.GetSymlinkTarget())}
}
// Set tag count header (matches filer logic)
if entry.Extended != nil {
tagCount := 0
@@ -0,0 +1,94 @@
package s3api
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
"github.com/stretchr/testify/require"
)
// A symlink made through a mount holds no data. GET and HEAD must still tell
// it apart from an empty file: Content-Type inode/symlink unless one is
// stored, and the target, escaped, as x-amz-meta-seaweedfs-symlink-target.
func TestSetResponseHeadersDescribesSymlink(t *testing.T) {
symlink := uint32(os.ModeSymlink | 0777)
for _, tt := range []struct {
name, entryName, target, mime string
mode uint32
extended map[string][]byte
wantTarget, wantType string
}{
{name: "relative target", entryName: "python", mode: symlink, target: "python3.10",
wantTarget: "python3.10", wantType: "inode/symlink"},
{name: "absolute target", entryName: "data", mode: symlink, target: "/mnt/shared/data",
wantTarget: "/mnt/shared/data", wantType: "inode/symlink"},
{name: "target escaped like a url-encoded key", entryName: "latest", mode: symlink, target: "../数据/a b+c;d",
wantTarget: "../%E6%95%B0%E6%8D%AE/a%20b%2Bc%3Bd", wantType: "inode/symlink"},
{name: "type not guessed from the name", entryName: "config.json", mode: symlink, target: "config.v2.json",
wantTarget: "config.v2.json", wantType: "inode/symlink"},
{name: "stored content type kept", entryName: "python", mode: symlink, target: "python3.10", mime: "text/plain",
wantTarget: "python3.10", wantType: "text/plain"},
{name: "stored metadata of the same name does not replace the target", entryName: "python", mode: symlink, target: "python3.10",
extended: map[string][]byte{"X-Amz-Meta-Seaweedfs-Symlink-Target": []byte("elsewhere")},
wantTarget: "python3.10", wantType: "inode/symlink"},
{name: "regular file", entryName: "config.json", mode: 0644, wantType: "application/json"},
{name: "target without the symlink mode is a regular file", entryName: "config.json", mode: 0660, target: "config.v2.json",
wantType: "application/json"},
} {
t.Run(tt.name, func(t *testing.T) {
entry := &filer_pb.Entry{Name: tt.entryName, Extended: tt.extended,
Attributes: &filer_pb.FuseAttributes{Mtime: 100, FileMode: tt.mode, SymlinkTarget: tt.target, Mime: tt.mime}}
for _, method := range []string{http.MethodGet, http.MethodHead} {
w := httptest.NewRecorder()
(&S3ApiServer{}).setResponseHeaders(w, httptest.NewRequest(method, "/bucket/object", nil), entry, 0)
require.Equal(t, tt.wantType, w.Header().Get("Content-Type"), method)
got := w.Header()[s3_constants.AmzMetaSymlinkTarget] // raw: sent lowercase, as AWS sends user metadata
if tt.wantTarget == "" {
require.Empty(t, got, method)
continue
}
require.Equal(t, []string{tt.wantTarget}, got, method)
unescaped, err := url.PathUnescape(got[0])
require.NoError(t, err)
require.Equal(t, tt.target, unescaped, method)
}
})
}
}
// Through the handlers, a symlink answers 200 with an empty body and its
// type and target; having no children, it is not taken for an implicit
// directory, which HEAD answers 404.
func TestGetHeadSymlink(t *testing.T) {
const bucket, object = "b", "env/bin/python"
f := &ambiguousPutFiler{entries: map[string]*filer_pb.Entry{
"/buckets/" + bucket + "/" + object: {Name: "python", Attributes: &filer_pb.FuseAttributes{
FileMode: uint32(os.ModeSymlink | 0777), SymlinkTarget: "python3.10", Mtime: 784111777, Crtime: 784111777}},
}}
for _, method := range []string{http.MethodGet, http.MethodHead} {
t.Run(method, func(t *testing.T) {
s3a := newPutTestServer(t, startFakeFiler(t, f))
s3a.bucketConfigCache = NewBucketConfigCache(time.Minute)
s3a.bucketConfigCache.Set(bucket, &BucketConfig{Name: bucket})
r := mux.SetURLVars(httptest.NewRequest(method, "/"+bucket+"/"+object, nil), map[string]string{"bucket": bucket, "object": object})
w := httptest.NewRecorder()
handler := s3a.GetObjectHandler
if method == http.MethodHead {
handler = s3a.HeadObjectHandler
}
handler(w, r)
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
require.Equal(t, "0", w.Header().Get("Content-Length"))
require.Empty(t, w.Body.Bytes())
require.Equal(t, "inode/symlink", w.Header().Get("Content-Type"))
require.Equal(t, []string{"python3.10"}, w.Header()[s3_constants.AmzMetaSymlinkTarget])
})
}
}