Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/apiclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,15 @@ func (c *Client) EndSession(ctx context.Context, token string) error {
type ServerRef struct {
Namespace string `json:"namespace"`
Server string `json:"server"`
// Write is the API's write decision for this server. Nil (an API that
// does not send it) leaves the decision to the API at commit time.
Write *bool `json:"write,omitempty"`
}

// ReadOnly reports whether the API said the session may not change files on
// this server.
func (s ServerRef) ReadOnly() bool { return s.Write != nil && !*s.Write }

// Servers lists the servers the session may reach.
func (c *Client) Servers(ctx context.Context, sessionToken string) ([]ServerRef, error) {
if sessionToken == "" {
Expand Down
53 changes: 53 additions & 0 deletions internal/vfs/readonly_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vfs

import (
"errors"
"testing"

"github.com/GameplanePanel/gameplane/sftp/internal/apiclient"
"github.com/pkg/sftp"
)

// A server the API marks read-only refuses an upload when the file is opened,
// and refuses mkdir, before any change reaches the API. A writable server and
// one without a write decision still accept writes.
func TestReadOnlyServerRefusesWritesAtOpen(t *testing.T) {
no, yes := false, true
servers := []apiclient.ServerRef{
{Namespace: testNS, Server: "survival", Write: &no},
{Namespace: testNS, Server: "creative", Write: &yes},
{Namespace: otherNS, Server: "modded"},
}
fx := newFixture(t, nil, Options{}, servers)

if _, err := fx.fs.Filewrite(openReq("/survival/denied.txt", fxfWrite|fxfCreat|fxfTrunc)); !errors.Is(err, sftp.ErrSSHFxPermissionDenied) {
t.Fatalf("open for write on read-only server: err = %v, want permission denied", err)
}
if err := fx.fs.Filecmd(opReq("Mkdir", "/survival/denied")); !errors.Is(err, sftp.ErrSSHFxPermissionDenied) {
t.Fatalf("mkdir on read-only server: err = %v, want permission denied", err)
}
if fx.api.has(testNS, "survival", "/denied.txt") || fx.api.has(testNS, "survival", "/denied") {
t.Fatalf("read-only server changed")
}
if got := fx.api.snapshot().streamQueries; len(got) != 0 {
t.Fatalf("read-only open reached the stream route: %v", got)
}

for _, p := range []string{"/creative/ok.txt", "/_namespaces/" + otherNS + "/modded/ok.txt"} {
w, err := fx.fs.Filewrite(openReq(p, fxfWrite|fxfCreat|fxfTrunc))
if err != nil {
t.Fatalf("open %s: %v", p, err)
}
closeOrFail(t, w)
}
}

// A per-server login reads the same decision from the inventory.
func TestTargetLoginReadOnlyRefusesWritesAtOpen(t *testing.T) {
no := false
servers := []apiclient.ServerRef{{Namespace: testNS, Server: "survival", Write: &no}}
fx := newFixture(t, &apiclient.Target{Namespace: testNS, Server: "survival"}, Options{}, servers)
if _, err := fx.fs.Filewrite(openReq("/denied.txt", fxfWrite|fxfCreat|fxfTrunc)); !errors.Is(err, sftp.ErrSSHFxPermissionDenied) {
t.Fatalf("target login open for write: err = %v, want permission denied", err)
}
}
24 changes: 24 additions & 0 deletions internal/vfs/vfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,33 @@ func (f *FS) resolveWrite(ctx context.Context, p string) (location, error) {
if !loc.writable() {
return location{}, sftp.ErrSSHFxPermissionDenied
}
readOnly, err := f.serverReadOnly(ctx, loc.namespace, loc.server)
if err != nil {
return location{}, f.fail("servers", err)
}
if readOnly {
// Refuse now, so an upload fails when the file is opened rather than
// when it is closed. The API still checks every change itself.
return location{}, sftp.ErrSSHFxPermissionDenied
}
return loc, nil
}

// serverReadOnly reports whether the inventory marks the server read-only for
// this session. A server missing from the inventory is left to the API.
func (f *FS) serverReadOnly(ctx context.Context, ns, server string) (bool, error) {
list, err := f.inventory(ctx, false)
if err != nil {
return false, err
}
for _, s := range list {
if s.Namespace == ns && s.Server == server {
return s.ReadOnly(), nil
}
}
return false, nil
}

// inventory returns the servers the session may reach. It is cached for
// serverCacheTTL unless force is set.
func (f *FS) inventory(ctx context.Context, force bool) ([]apiclient.ServerRef, error) {
Expand Down
3 changes: 2 additions & 1 deletion specs.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Exit code 2 with a message on stderr for a usage or startup error.
### File tree

- Tree login: `/<server>/...` for servers in the default namespace; `/_namespaces/<ns>/<server>/...` for others. The root, `/_namespaces`, `/_namespaces/<ns>` and each server's top folder are read-only virtual directories (create, rename, remove and write are refused). Kubernetes names cannot start with `_`, so `_namespaces` never collides with a server.
- Target login: `/` is the server's root, read-only itself; the server list is never fetched.
- Target login: `/` is the server's root, read-only itself; the server list is fetched only to check write access before a change.
- Write access: `GET /sftp/v1/servers` marks each server with `write`, the API's file-access decision. On a server marked `write: false`, open-for-write, mkdir, rename and remove are refused with permission denied before any request reaches the file routes, so an upload fails when the file is opened. A server without the field is left to the API, which checks every change itself.
- Paths are cleaned before use, so `..` cannot leave a server and no path can reach another server. A rename between servers is refused.
- Dotfiles: any path component starting with `.` is not found for reads and stat, refused for writes, and filtered from listings.
- Reads stream with `Range` requests. Reads a little ahead of the stream skip forward and keep the skipped bytes, because `pkg/sftp` may deliver pipelined reads slightly out of order; a far seek reopens the download.
Expand Down