Skip to content

Commit da20063

Browse files
committed
make Root mandatory
1 parent 44c5fd6 commit da20063

6 files changed

Lines changed: 43 additions & 24 deletions

File tree

‎internal/deb/extract.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ func extractHardLinks(pkgReader io.ReadSeeker, opts *extractHardLinkOptions) err
350350
// Create the remaining hard links.
351351
for _, link := range links[1:] {
352352
createOptions := &fsutil.CreateOptions{
353-
Path: filepath.Join(opts.TargetDir, link.path),
353+
Root: opts.TargetDir,
354+
Path: link.path,
354355
Mode: tarHeader.FileInfo().Mode(),
355356
// Link to the first file extracted for the hard links.
356357
Link: absLink,

‎internal/fsutil/create.go‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
)
1414

1515
type CreateOptions struct {
16-
// Root defaults to "/" if empty.
1716
Root string
1817
// Path is relative to Root.
1918
Path string
@@ -49,10 +48,17 @@ func Create(options *CreateOptions) (*Entry, error) {
4948
optsCopy.Data = rp
5049
o := &optsCopy
5150

52-
err := setValidPath(o)
51+
if o.Root == "" {
52+
return nil, fmt.Errorf("root cannot be empty")
53+
}
54+
if o.Root != "/" {
55+
o.Root = filepath.Clean(o.Root) + "/"
56+
}
57+
path, err := absPath(o.Root, o.Path)
5358
if err != nil {
5459
return nil, err
5560
}
61+
o.Path = path
5662

5763
var hash string
5864
if o.MakeParents {
@@ -66,7 +72,7 @@ func Create(options *CreateOptions) (*Entry, error) {
6672
if o.Link != "" {
6773
o.Link = filepath.Clean(o.Link)
6874
if filepath.IsAbs(o.Link) {
69-
if !strings.HasPrefix(o.Link, o.Root) {
75+
if !strings.HasPrefix(filepath.Clean(o.Link), o.Root) {
7076
return nil, fmt.Errorf("invalid hardlink %s target: %s is outside of root %s", o.Path, o.Link, o.Root)
7177
}
7278
}
@@ -125,16 +131,23 @@ func CreateWriter(options *CreateOptions) (io.WriteCloser, *Entry, error) {
125131
optsCopy := *options
126132
o := &optsCopy
127133

128-
err := setValidPath(o)
134+
if o.Root == "" {
135+
return nil, nil, fmt.Errorf("root cannot be empty")
136+
}
137+
if o.Root != "/" {
138+
o.Root = filepath.Clean(o.Root) + "/"
139+
}
140+
path, err := absPath(o.Root, o.Path)
129141
if err != nil {
130142
return nil, nil, err
131143
}
144+
o.Path = path
132145

133146
if !o.Mode.IsRegular() {
134147
return nil, nil, fmt.Errorf("unsupported file type: %s", o.Path)
135148
}
136149
if o.MakeParents {
137-
if err := os.MkdirAll(filepath.Dir(o.Path), 0755); err != nil {
150+
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
138151
return nil, nil, err
139152
}
140153
}
@@ -221,18 +234,13 @@ func createHardLink(o *CreateOptions) error {
221234
return err
222235
}
223236

224-
func setValidPath(o *CreateOptions) error {
225-
if o.Root == "" {
226-
o.Root = "/"
227-
}
228-
if o.Root != "/" {
229-
o.Root = filepath.Clean(o.Root) + "/"
230-
}
231-
o.Path = filepath.Clean(filepath.Join(o.Root, o.Path))
232-
if !strings.HasPrefix(o.Path, o.Root) {
233-
return fmt.Errorf("cannot create path %s outside of root %s", o.Path, o.Root)
237+
// absPath requires root to be a clean path that ends in "/".
238+
func absPath(root, relPath string) (string, error) {
239+
path := filepath.Clean(filepath.Join(root, relPath))
240+
if !strings.HasPrefix(path, root) {
241+
return "", fmt.Errorf("cannot create path %s outside of root %s", path, root)
234242
}
235-
return nil
243+
return path, nil
236244
}
237245

238246
// readerProxy implements the io.Reader interface proxying the calls to its

‎internal/fsutil/create_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,11 @@ func (s *S) TestCreateWriter(c *C) {
462462
c.Assert(testutil.TreeDumpEntry(entry), DeepEquals, test.result[slashPath])
463463
}
464464
}
465+
466+
func (s *S) TestCreateEmptyRoot(c *C) {
467+
options := &fsutil.CreateOptions{Root: ""}
468+
_, err := fsutil.Create(options)
469+
c.Assert(err, ErrorMatches, "root cannot be empty")
470+
_, _, err = fsutil.CreateWriter(options)
471+
c.Assert(err, ErrorMatches, "root cannot be empty")
472+
}

‎internal/scripts/scripts.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ func (c *ContentValue) Write(thread *starlark.Thread, fn *starlark.Builtin, args
178178
// No mode parameter for now as slices are supposed to list files
179179
// explicitly instead.
180180
entry, err := fsutil.Create(&fsutil.CreateOptions{
181+
Root: "/",
181182
Path: fpath,
182183
Data: bytes.NewReader(fdata),
183184
Mode: 0644,

‎internal/setup/fetch.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ func extractTar(dataReader io.Reader, targetDir string) error {
138138
//debugf("Extracting header: %#v", tarHeader)
139139

140140
_, err = fsutil.Create(&fsutil.CreateOptions{
141-
Path: filepath.Join(targetDir, sourcePath),
141+
Root: targetDir,
142+
Path: sourcePath,
142143
Mode: tarHeader.FileInfo().Mode(),
143144
Data: tarReader,
144145
Link: tarHeader.Linkname,

‎internal/slicer/slicer.go‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ func Run(options *RunOptions) error {
276276
mutable: pathInfo.Mutable,
277277
}
278278
addKnownPath(knownPaths, relPath, data)
279-
targetPath := filepath.Join(targetDir, relPath)
280-
entry, err := createFile(targetPath, pathInfo)
279+
entry, err := createFile(targetDir, relPath, pathInfo)
281280
if err != nil {
282281
return err
283282
}
@@ -334,9 +333,9 @@ func generateManifests(targetDir string, selection *setup.Selection,
334333
var writers []io.Writer
335334
for relPath, slices := range manifestSlices {
336335
logf("Generating manifest at %s...", relPath)
337-
absPath := filepath.Join(targetDir, relPath)
338336
createOptions := &fsutil.CreateOptions{
339-
Path: absPath,
337+
Root: targetDir,
338+
Path: relPath,
340339
Mode: manifestMode,
341340
MakeParents: true,
342341
}
@@ -426,7 +425,7 @@ func addKnownPath(knownPaths map[string]pathData, path string, data pathData) {
426425
}
427426
}
428427

429-
func createFile(targetPath string, pathInfo setup.PathInfo) (*fsutil.Entry, error) {
428+
func createFile(targetDir, relPath string, pathInfo setup.PathInfo) (*fsutil.Entry, error) {
430429
targetMode := pathInfo.Mode
431430
if targetMode == 0 {
432431
if pathInfo.Kind == setup.DirPath {
@@ -454,7 +453,8 @@ func createFile(targetPath string, pathInfo setup.PathInfo) (*fsutil.Entry, erro
454453
}
455454

456455
return fsutil.Create(&fsutil.CreateOptions{
457-
Path: targetPath,
456+
Root: targetDir,
457+
Path: relPath,
458458
Mode: tarHeader.FileInfo().Mode(),
459459
Data: fileContent,
460460
Link: linkTarget,

0 commit comments

Comments
 (0)