Skip to content

Commit

Permalink
sisyphus: check base names for filepath separators
Browse files Browse the repository at this point in the history
Fixes #2.
  • Loading branch information
kortschak committed Sep 18, 2016
1 parent a56857f commit 4d3dbd8
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
17 changes: 16 additions & 1 deletion dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package sisyphus

import (
"os"
"path/filepath"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -34,14 +36,27 @@ var (
)

// NewDir returns a new Dir with the given name and file mode.
func NewDir(name string, mode os.FileMode) *Dir {
func NewDir(name string, mode os.FileMode) (*Dir, error) {
if name != "/" && strings.Contains(name, string(filepath.Separator)) {
return nil, ErrBadName
}
return &Dir{
name: name,
attr: attr{
mode: os.ModeDir | mode&^(os.ModeSymlink|os.ModeNamedPipe|os.ModeSocket),
},
files: make(map[string]Node),
}, nil
}

// MustNewDir returns a new Dir with the given name and file mode. It
// will panic if name contains a filepath separator unless name is "/".
func MustNewDir(name string, mode os.FileMode) *Dir {
d, err := NewDir(name, mode)
if err != nil {
panic(err)
}
return d
}

// Own sets the uid and gid of the directory.
Expand Down
2 changes: 1 addition & 1 deletion fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var nofs *FileSystem
func NewFileSystem(mode os.FileMode, clock func() time.Time) *FileSystem {
var fs FileSystem
fs.now = clock
fs.root = NewDir("/", mode)
fs.root, _ = NewDir("/", mode)
fs.root.SetSys(&fs)
return &fs
}
Expand Down
17 changes: 16 additions & 1 deletion ro_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package sisyphus
import (
"io"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -45,14 +47,27 @@ var (
)

// NewRO returns a new RO file with the given name and file mode.
func NewRO(name string, mode os.FileMode, dev Reader) *RO {
func NewRO(name string, mode os.FileMode, dev Reader) (*RO, error) {
if strings.Contains(name, string(filepath.Separator)) {
return nil, ErrBadName
}
return &RO{
name: name,
attr: attr{
mode: mode &^ (os.ModeDir | 0222),
},
dev: dev,
}, nil
}

// MustNewRO returns a new RO with the given name and file mode. It
// will panic if name contains a filepath separator.
func MustNewRO(name string, mode os.FileMode, dev Reader) *RO {
ro, err := NewRO(name, mode, dev)
if err != nil {
panic(err)
}
return ro
}

// Own sets the uid and gid of the file.
Expand Down
17 changes: 16 additions & 1 deletion rw_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package sisyphus
import (
"io"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -50,14 +52,27 @@ var (
)

// NewRW returns a new RW file with the given name and file mode.
func NewRW(name string, mode os.FileMode, dev ReadWriter) *RW {
func NewRW(name string, mode os.FileMode, dev ReadWriter) (*RW, error) {
if strings.Contains(name, string(filepath.Separator)) {
return nil, ErrBadName
}
return &RW{
name: name,
attr: attr{
mode: mode &^ os.ModeDir,
},
dev: dev,
}, nil
}

// MustNewRW returns a new RW with the given name and file mode. It
// will panic if name contains a filepath separator.
func MustNewRW(name string, mode os.FileMode, dev ReadWriter) *RW {
rw, err := NewRW(name, mode, dev)
if err != nil {
panic(err)
}
return rw
}

// Own sets the uid and gid of the file.
Expand Down
5 changes: 5 additions & 0 deletions sisyphus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package sisyphus

import (
"errors"
"io"
"os"
"sync"
Expand All @@ -16,6 +17,10 @@ import (
"bazil.org/fuse/fs"
)

// ErrBadName is returned when a new Node is created with a base name
// that contains a filepath separator.
var ErrBadName = errors.New("sisyphus: base contains filepath separator")

// server is a FUSE server for a FileSystem.
type server struct {
mnt string
Expand Down
8 changes: 4 additions & 4 deletions sisyphus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func init() {
}

var (
d = NewDir
ro = NewRO
rw = NewRW
wo = NewWO
d = MustNewDir
ro = MustNewRO
rw = MustNewRW
wo = MustNewWO
)

func sysfs(t *testing.T, comm chan string) *FileSystem {
Expand Down
17 changes: 16 additions & 1 deletion wo_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package sisyphus
import (
"io"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -48,14 +50,27 @@ var (
)

// NewWO returns a new WO file with the given name and file mode.
func NewWO(name string, mode os.FileMode, dev Writer) *WO {
func NewWO(name string, mode os.FileMode, dev Writer) (*WO, error) {
if strings.Contains(name, string(filepath.Separator)) {
return nil, ErrBadName
}
return &WO{
name: name,
attr: attr{
mode: mode &^ (os.ModeDir | 0444),
},
dev: dev,
}, nil
}

// MustNewWO returns a new RO with the given name and file mode. It
// will panic if name contains a filepath separator.
func MustNewWO(name string, mode os.FileMode, dev Writer) *WO {
wo, err := NewWO(name, mode, dev)
if err != nil {
panic(err)
}
return wo
}

// Own sets the uid and gid of the file.
Expand Down

0 comments on commit 4d3dbd8

Please sign in to comment.