127 lines
3.9 KiB
Go
127 lines
3.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
fileguard "sea-vhost/internal/files"
|
|
"sea-vhost/internal/nodefiles"
|
|
"sea-vhost/internal/service"
|
|
)
|
|
|
|
func TestBufferedFileCommitConflictReturns409WithoutOverwriting(t *testing.T) {
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "existing.txt")
|
|
if err := os.WriteFile(path, []byte("original-content"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
root := nodefiles.New(context.Background(), "sv0123456789", func(_ context.Context, request nodefiles.Request) (nodefiles.Response, error) {
|
|
return nodefiles.Execute(dir, request), nil
|
|
})
|
|
file, err := root.OpenFile("existing.txt", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0600)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer file.Abort()
|
|
if _, err = file.Write([]byte("unconfirmed-replacement")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = file.Close()
|
|
if !errors.Is(err, os.ErrExist) {
|
|
t.Fatalf("expected a commit-time conflict, got %v", err)
|
|
}
|
|
response := httptest.NewRecorder()
|
|
writeFileCommitError(response, err)
|
|
if response.Code != http.StatusConflict {
|
|
t.Fatalf("commit conflict must be HTTP 409, got %d", response.Code)
|
|
}
|
|
data, err := os.ReadFile(path)
|
|
if err != nil || string(data) != "original-content" {
|
|
t.Fatalf("unconfirmed upload changed existing content: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestFileCommitFailureDoesNotExposeInternalDetails(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
writeFileCommitError(response, errors.New("private-path-and-diagnostic"))
|
|
if response.Code != http.StatusUnprocessableEntity || strings.Contains(response.Body.String(), "private-path-and-diagnostic") {
|
|
t.Fatalf("unsafe commit failure: %d %s", response.Code, response.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestFileQuotaFailureRemainsClosedAndDoesNotExposeInternalError(t *testing.T) {
|
|
for _, test := range []struct {
|
|
err error
|
|
want int
|
|
}{
|
|
{service.ErrQuotaSuspended, http.StatusLocked},
|
|
{service.ErrStorageQuotaExceeded, http.StatusInsufficientStorage},
|
|
{errors.New("internal database diagnostic"), http.StatusServiceUnavailable},
|
|
} {
|
|
response := httptest.NewRecorder()
|
|
writeFileQuotaError(response, test.err)
|
|
if response.Code != test.want || strings.Contains(response.Body.String(), "internal database diagnostic") {
|
|
t.Fatalf("unsafe quota response: %d %s", response.Code, response.Body.String())
|
|
}
|
|
if test.want == http.StatusServiceUnavailable && response.Header().Get("Retry-After") != "3" {
|
|
t.Fatal("temporarily unavailable verification must indicate when to retry")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSafeProjectNameRejectsAmbiguousAndInternalPaths(t *testing.T) {
|
|
root := t.TempDir()
|
|
for _, requested := range []string{
|
|
"../outside",
|
|
"public/../outside",
|
|
"public//index.html",
|
|
`.sea-trash`,
|
|
`.sea-trash/deleted.txt`,
|
|
} {
|
|
if _, err := safeProjectName(root, requested); !errors.Is(err, fileguard.ErrOutsideRoot) {
|
|
t.Errorf("safeProjectName(%q) error = %v, want ErrOutsideRoot", requested, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestProjectRootBlocksSymlinkSwapOutsideTenant(t *testing.T) {
|
|
base := t.TempDir()
|
|
root := filepath.Join(base, "project")
|
|
outside := filepath.Join(base, "outside.txt")
|
|
if err := os.Mkdir(root, 0750); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(outside, []byte("root-equivalent-secret"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
victim := filepath.Join(root, "config.txt")
|
|
if err := os.WriteFile(victim, []byte("tenant-data"), 0640); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
name, err := safeProjectName(root, "config.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = os.Remove(victim); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = os.Symlink(outside, victim); err != nil {
|
|
t.Skipf("symlink creation is unavailable: %v", err)
|
|
}
|
|
|
|
projectRoot, err := os.OpenRoot(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer projectRoot.Close()
|
|
if _, err = projectRoot.ReadFile(name); err == nil {
|
|
t.Fatal("directory-scoped file operation followed a swapped symlink outside the tenant root")
|
|
}
|
|
}
|