Support unbounded host filesystem access
This commit is contained in:
@@ -190,6 +190,10 @@ native Go context syntax. Runtime wiring can remain incomplete until later stage
|
||||
|
||||
## Stage 3 — Add unbounded host filesystem access
|
||||
|
||||
Completed: the existing access owner dispatches unbounded host operations to the
|
||||
OS, preserves namespace tokens and bounded/native access, and validates actual
|
||||
anchor/base directories. The focused access, directory-read and cleanup test passes.
|
||||
|
||||
In `filesystem_access.go` and the access-opening path in
|
||||
`filesystem_context.go`, add unbounded host operation dispatch to the existing
|
||||
access object. Keep absolute host access names and namespace tokens; bounded
|
||||
|
||||
@@ -30,8 +30,8 @@ type resourceIdentity struct {
|
||||
occurrence *namespaceToken
|
||||
}
|
||||
|
||||
// filesystemAccess is a load-scoped view of either a contained host root or a
|
||||
// borrowed native filesystem. Names are canonical host paths or fs.FS paths.
|
||||
// filesystemAccess is a load-scoped view of bounded or unbounded host access,
|
||||
// or a borrowed native filesystem. Names are canonical host paths or fs.FS paths.
|
||||
type filesystemAccess struct {
|
||||
rootPath string
|
||||
root *os.Root
|
||||
@@ -41,6 +41,9 @@ type filesystemAccess struct {
|
||||
}
|
||||
|
||||
func openHostAccess(rootPath string) (*filesystemAccess, error) {
|
||||
if rootPath == "" {
|
||||
return &filesystemAccess{token: &namespaceToken{}}, nil
|
||||
}
|
||||
root, err := os.OpenRoot(rootPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -83,7 +86,7 @@ func (a *filesystemAccess) dir(name string) string {
|
||||
|
||||
func (a *filesystemAccess) contains(name string) bool {
|
||||
if a.native == nil {
|
||||
return containedPath(a.rootPath, filepath.Clean(name))
|
||||
return filepath.IsAbs(name) && (a.rootPath == "" || containedPath(a.rootPath, filepath.Clean(name)))
|
||||
}
|
||||
if !fs.ValidPath(name) || !fs.ValidPath(a.rootPath) {
|
||||
return false
|
||||
@@ -112,6 +115,12 @@ func (a *filesystemAccess) relative(name string) (string, error) {
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
if a.rootPath == "" {
|
||||
if !filepath.IsAbs(name) {
|
||||
return "", fs.ErrInvalid
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
relative, err := filepath.Rel(a.rootPath, name)
|
||||
if err != nil || filepath.IsAbs(relative) || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
return "", fs.ErrInvalid
|
||||
@@ -127,6 +136,9 @@ func (a *filesystemAccess) stat(name string) (fs.FileInfo, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.root == nil {
|
||||
return os.Stat(relative)
|
||||
}
|
||||
return a.root.Stat(relative)
|
||||
}
|
||||
|
||||
@@ -138,6 +150,9 @@ func (a *filesystemAccess) open(name string) (fs.File, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if a.root == nil {
|
||||
return os.Open(relative)
|
||||
}
|
||||
return a.root.Open(relative)
|
||||
}
|
||||
|
||||
|
||||
78
internal/engine/filesystem_access_test.go
Normal file
78
internal/engine/filesystem_access_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHostAccessModes(t *testing.T) {
|
||||
t.Parallel()
|
||||
anchor := canonicalTestPath(t, t.TempDir())
|
||||
external := canonicalTestPath(t, t.TempDir())
|
||||
file := writeImportFile(t, external, "fragment.yml", "value: external\n")
|
||||
token := &namespaceToken{}
|
||||
for _, bounded := range []bool{false, true} {
|
||||
name := "unbounded"
|
||||
boundary := ""
|
||||
if bounded {
|
||||
name = "bounded"
|
||||
boundary = anchor
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
access, err := openResolvedFilesystemAccess(resolvedFilesystemContext{rootPath: anchor, basePath: anchor, importRoot: boundary, hostToken: token})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer access.close()
|
||||
if access.token != token {
|
||||
t.Fatal("host namespace was replaced")
|
||||
}
|
||||
if access.contains(file) == bounded {
|
||||
t.Fatalf("contains external = %v", access.contains(file))
|
||||
}
|
||||
info, err := access.stat(file)
|
||||
if bounded {
|
||||
if err == nil {
|
||||
t.Fatal("bounded stat escaped root")
|
||||
}
|
||||
if _, err := access.open(file); err == nil {
|
||||
t.Fatal("bounded open escaped root")
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil || !info.Mode().IsRegular() {
|
||||
t.Fatalf("external stat = %v, %v", info, err)
|
||||
}
|
||||
opened, err := access.open(file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := io.ReadAll(opened)
|
||||
if err != nil || string(data) != "value: external\n" {
|
||||
t.Fatalf("read = %q, %v", data, err)
|
||||
}
|
||||
if err := opened.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
count := 0
|
||||
if err := access.readDirectory(external, func(fs.DirEntry) bool { count++; return true }); err != nil || count != 1 {
|
||||
t.Fatalf("directory count=%d, error=%v", count, err)
|
||||
}
|
||||
if err := access.readDirectory(filepath.Join(external, "missing"), func(fs.DirEntry) bool { return true }); !missingDirectoryOpen(err) {
|
||||
t.Fatalf("missing directory = %v", err)
|
||||
}
|
||||
if _, err := access.open("relative.yml"); !errors.Is(err, fs.ErrInvalid) {
|
||||
t.Fatalf("relative host open = %v", err)
|
||||
}
|
||||
if access.root != nil {
|
||||
t.Fatal("unbounded access retained root handle")
|
||||
}
|
||||
if err := access.close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -213,7 +213,7 @@ func openResolvedFilesystemAccess(resolved resolvedFilesystemContext) (*filesyst
|
||||
access.label = resolved.native.label
|
||||
return access, nil
|
||||
}
|
||||
access, err := openHostAccess(resolved.rootPath)
|
||||
access, err := openHostAccess(resolved.importRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ func acquireImportedWithAccess(request importAcquisitionRequest, policy *acquisi
|
||||
_ = closeAccess()
|
||||
return nil, sourceInfos(main.source), []Diagnostic{importDiagnostic(CodeImportOutsideRoot, "selected source and import base must stay inside the configuration root", nil)}
|
||||
}
|
||||
for _, directory := range []string{access.rootPath, basePath} {
|
||||
for _, directory := range []string{resolved.rootPath, basePath} {
|
||||
info, err := access.stat(directory)
|
||||
if err != nil || !info.IsDir() {
|
||||
_ = closeAccess()
|
||||
|
||||
Reference in New Issue
Block a user