From 2585f2702054c04b5c5abb5d4c083024181ae9b2 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 18:04:14 +0000 Subject: [PATCH] fix: refuse writes on read-only servers when the file is opened GET /sftp/v1/servers now marks each server with the API's write decision. On a server marked write: false, open-for-write, mkdir, rename and remove fail with permission denied before reaching the API, so a read-only user's upload fails at open instead of at close (Gameplane spec 040, US2 scenario 3). A missing field leaves the decision to the API. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- internal/apiclient/client.go | 7 +++++ internal/vfs/readonly_test.go | 53 +++++++++++++++++++++++++++++++++++ internal/vfs/vfs.go | 24 ++++++++++++++++ specs.md | 3 +- 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 internal/vfs/readonly_test.go diff --git a/internal/apiclient/client.go b/internal/apiclient/client.go index 38b26ef..7902d30 100644 --- a/internal/apiclient/client.go +++ b/internal/apiclient/client.go @@ -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 == "" { diff --git a/internal/vfs/readonly_test.go b/internal/vfs/readonly_test.go new file mode 100644 index 0000000..735ced4 --- /dev/null +++ b/internal/vfs/readonly_test.go @@ -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) + } +} diff --git a/internal/vfs/vfs.go b/internal/vfs/vfs.go index 6726d2e..3f898de 100644 --- a/internal/vfs/vfs.go +++ b/internal/vfs/vfs.go @@ -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) { diff --git a/specs.md b/specs.md index 85527fd..06f3557 100644 --- a/specs.md +++ b/specs.md @@ -59,7 +59,8 @@ Exit code 2 with a message on stderr for a usage or startup error. ### File tree - Tree login: `//...` for servers in the default namespace; `/_namespaces///...` for others. The root, `/_namespaces`, `/_namespaces/` 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.