Files
seaweedfs/weed/s3api/s3api_object_handlers_symlink_test.go
T
Yip LeoandClaude Opus 5.5 77a070fe0a 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>
2026-12-02 12:05:12 -08:00

95 lines
4.4 KiB
Go

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])
})
}
}