mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-20 17:09:54 +01:00
* chore(lint): ban context.Background in tests, flag unused nolints Add a forbidigo rule that rejects context.Background and context.TODO in _test.go files. The lint message points to the right fix: the spec's ctx from GinkgoT().Context(), or t.Context() and b.Context() in plain tests and benchmarks. AGENTS.md already asks for this, but nothing enforced it, so new tests kept adding Background calls. All 465 existing calls are replaced, with one ctx per top-level Describe set at the top of its BeforeEach. The migration forbidigo rules keep their old scope through an exclusion matched on their message text. Also enable nolintlint with require-specific and allow-unused: false. It found 41 //nolint directives that suppressed nothing, or named no linter, or had a leading space that made them inert. They are removed or fixed. The sweep also fixed a middleware test that wrote a user with a nil ctx before ctx was set. * test: keep cleanup ROLLBACKs and the scanner singleton off the spec ctx The ROLLBACK cleanups in the library, plugin and initial setup tests must run even if the spec context is already cancelled, so they use context.WithoutCancel. The same goes for the context handed to the process-wide scanner singleton in the GetInstance test. Also document that the forbidigo exclusions are scoped by message text. * test: pass the caller's ctx to tests.ClearDB ClearDB was the last context.Background call in shared test code. It now takes a ctx, and every caller passes its spec ctx. The forbidigo rule for tests also covers the tests/ helper package now, so new helpers there cannot add Background calls either.
183 lines
6 KiB
Go
183 lines
6 KiB
Go
package plugins
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/navidrome/navidrome/conf"
|
|
"github.com/navidrome/navidrome/model"
|
|
"github.com/navidrome/navidrome/tests"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
type sandboxInput struct {
|
|
Operation string `json:"operation"`
|
|
MountPoint string `json:"mount_point,omitempty"`
|
|
FilePath string `json:"file_path,omitempty"`
|
|
Content string `json:"content,omitempty"`
|
|
Target string `json:"target,omitempty"`
|
|
}
|
|
|
|
type sandboxOutput struct {
|
|
FileContent string `json:"file_content,omitempty"`
|
|
Error *string `json:"error,omitempty"`
|
|
}
|
|
|
|
// startSandboxManager loads test-library against libraryDir with the given grant.
|
|
func startSandboxManager(tmpDir, libraryDir string, grant func(*model.Plugin)) *Manager {
|
|
GinkgoHelper()
|
|
installed := installTestPlugins(tmpDir, "test-library"+PackageExtension)
|
|
grant(&installed[0])
|
|
|
|
DeferCleanup(conf.SnapshotConfig())
|
|
conf.Update(func(c *conf.Options) {
|
|
c.Plugins.Enabled = true
|
|
c.Plugins.Folder = conf.NewDir(tmpDir)
|
|
c.Plugins.AutoReload = false
|
|
})
|
|
|
|
mockPluginRepo := tests.CreateMockPluginRepo()
|
|
mockPluginRepo.Permitted = true
|
|
mockPluginRepo.SetData(installed)
|
|
|
|
mockLibraryRepo := &tests.MockLibraryRepo{}
|
|
mockLibraryRepo.SetData(model.Libraries{{ID: 1, Name: "Test Library", Path: libraryDir}})
|
|
|
|
manager := &Manager{
|
|
plugins: make(map[string]*plugin),
|
|
ds: &tests.MockDataStore{MockedPlugin: mockPluginRepo, MockedLibrary: mockLibraryRepo},
|
|
subsonicRouter: http.NotFoundHandler(),
|
|
}
|
|
Expect(manager.Start(GinkgoT().Context())).To(Succeed())
|
|
DeferCleanup(func() { _ = manager.Stop() })
|
|
return manager
|
|
}
|
|
|
|
// callSandbox runs one filesystem operation inside the plugin sandbox.
|
|
func callSandbox(manager *Manager, input sandboxInput) sandboxOutput {
|
|
GinkgoHelper()
|
|
manager.mu.RLock()
|
|
p := manager.plugins["test-library"]
|
|
manager.mu.RUnlock()
|
|
Expect(p).ToNot(BeNil())
|
|
|
|
ctx := GinkgoT().Context()
|
|
instance, err := p.instance(ctx)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
defer instance.Close(ctx)
|
|
|
|
inputBytes, err := json.Marshal(input)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
_, outputBytes, err := instance.Call("nd_test_library", inputBytes)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
var output sandboxOutput
|
|
Expect(json.Unmarshal(outputBytes, &output)).To(Succeed())
|
|
return output
|
|
}
|
|
|
|
var _ = Describe("Plugin filesystem sandbox", Ordered, ContinueOnFailure, func() {
|
|
var (
|
|
manager *Manager
|
|
libraryDir string
|
|
outsideDir string
|
|
secretFile string
|
|
mountPoint string
|
|
)
|
|
|
|
call := func(input sandboxInput) sandboxOutput {
|
|
GinkgoHelper()
|
|
input.MountPoint = mountPoint
|
|
return callSandbox(manager, input)
|
|
}
|
|
|
|
BeforeAll(func() {
|
|
tmpDir := GinkgoT().TempDir()
|
|
libraryDir = filepath.Join(tmpDir, "music-library")
|
|
Expect(os.MkdirAll(libraryDir, 0755)).To(Succeed())
|
|
|
|
// Sibling of the mount root: anything reached here escaped the sandbox
|
|
outsideDir = filepath.Join(tmpDir, "outside")
|
|
Expect(os.MkdirAll(outsideDir, 0755)).To(Succeed())
|
|
secretFile = filepath.Join(outsideDir, "secret.txt")
|
|
Expect(os.WriteFile(secretFile, []byte("secret"), 0600)).To(Succeed())
|
|
|
|
mountPoint = toPluginMountPoint(1)
|
|
manager = startSandboxManager(tmpDir, libraryDir, func(p *model.Plugin) {
|
|
p.AllLibraries = true
|
|
p.AllowWriteAccess = true
|
|
})
|
|
})
|
|
|
|
It("allows writing inside the mount", func() {
|
|
out := call(sandboxInput{Operation: "write_file", FilePath: "inside.txt", Content: "hello"})
|
|
|
|
Expect(out.Error).To(BeNil())
|
|
Expect(os.ReadFile(filepath.Join(libraryDir, "inside.txt"))).To(BeEquivalentTo("hello"))
|
|
})
|
|
|
|
It("cannot escape the mount by creating a symlink", func() {
|
|
linked := call(sandboxInput{Operation: "symlink", FilePath: "escape", Target: "../outside"})
|
|
Expect(linked.Error).ToNot(BeNil())
|
|
_, err := os.Lstat(filepath.Join(libraryDir, "escape"))
|
|
Expect(err).To(MatchError(os.ErrNotExist), "the plugin created a symlink out of the mount")
|
|
|
|
wrote := call(sandboxInput{Operation: "write_file", FilePath: "escape/via-created-symlink.txt", Content: "escaped"})
|
|
|
|
Expect(wrote.Error).ToNot(BeNil())
|
|
Expect(filepath.Join(outsideDir, "via-created-symlink.txt")).ToNot(BeAnExistingFile())
|
|
})
|
|
|
|
// Accepted residual, pinned so a future tightening can't happen silently
|
|
It("still follows a symlink planted in the mount by something else", func() {
|
|
if err := os.Symlink(outsideDir, filepath.Join(libraryDir, "planted")); err != nil {
|
|
Skip("cannot create symlinks here: " + err.Error()) // Windows without privileges
|
|
}
|
|
|
|
out := call(sandboxInput{Operation: "write_file", FilePath: "planted/via-symlink.txt", Content: "escaped"})
|
|
|
|
Expect(out.Error).To(BeNil())
|
|
Expect(filepath.Join(outsideDir, "via-symlink.txt")).To(BeAnExistingFile())
|
|
})
|
|
|
|
It("cannot read outside the mount with ..", func() {
|
|
out := call(sandboxInput{Operation: "read_file", FilePath: "../outside/secret.txt"})
|
|
|
|
Expect(out.Error).ToNot(BeNil())
|
|
Expect(out.FileContent).ToNot(Equal("secret"))
|
|
})
|
|
|
|
It("cannot write outside the mount with ..", func() {
|
|
out := call(sandboxInput{Operation: "write_file", FilePath: "../outside/via-dotdot.txt", Content: "escaped"})
|
|
|
|
Expect(out.Error).ToNot(BeNil())
|
|
Expect(filepath.Join(outsideDir, "via-dotdot.txt")).ToNot(BeAnExistingFile())
|
|
})
|
|
})
|
|
|
|
var _ = Describe("Plugin filesystem sandbox without library access", Ordered, func() {
|
|
var manager *Manager
|
|
var libraryDir string
|
|
|
|
BeforeAll(func() {
|
|
tmpDir := GinkgoT().TempDir()
|
|
libraryDir = filepath.Join(tmpDir, "music-library")
|
|
Expect(os.MkdirAll(libraryDir, 0755)).To(Succeed())
|
|
Expect(os.WriteFile(filepath.Join(libraryDir, "track.txt"), []byte("audio"), 0600)).To(Succeed())
|
|
|
|
manager = startSandboxManager(tmpDir, libraryDir, func(*model.Plugin) {})
|
|
})
|
|
|
|
It("mounts nothing when no library is granted", func() {
|
|
manager.mu.RLock()
|
|
p := manager.plugins["test-library"]
|
|
manager.mu.RUnlock()
|
|
Expect(p.fsConfig).To(BeNil())
|
|
|
|
out := callSandbox(manager, sandboxInput{Operation: "read_file", MountPoint: toPluginMountPoint(1), FilePath: "track.txt"})
|
|
Expect(out.Error).ToNot(BeNil())
|
|
})
|
|
})
|