Giter Site home page Giter Site logo

filepath-securejoin's Issues

Error not being raised when expected for unsafePath of `../upperfile.txt`

This might be more of a question than an issue, but I'll log it anyway and see what the expectation is.

In https://github.com/cyphar/filepath-securejoin/blob/master/join.go#L61, if the values passed in to SecureJoinVFS are:

(/root/testdir, ../upperfile.txt, nil) and on the machine the file /root/upperfile.txt exists, but the file /root/testdir/upperfile.txt does not, SecureJoinVFS() is not returning an error as I would expect.

Instead, it's cleaning up the .. and returning (/root/testdir/upperfile.txt, nil)

If by chance I had a /root/testdir/upperfile.txt I still would not expect to get a pointer to it instead of /root/upperfile.txt.

I think the correct behavior would be to return an error in this circumstance. Thoughts?

FWIW, this came up due to an investigation into a Podman issue logged here: containers/podman#5035

loop error message contains a non-existent path

The error message that is triggered by too many symlink dereferences contains a non-existent path instead of the path to the symlink that causes the loop.

The path is set here:

return "", &os.PathError{Op: "SecureJoin", Path: root + string(filepath.Separator) + unsafePath, Err: syscall.ELOOP}

Steps to reproduce the issue

On Fedora CoreOS perform the following steps

  1. mkdir ~/test
  2. cd ~/test
  3. create the file Dockerfile with this contents
    FROM docker.io/library/fedora:38
    RUN dnf install -y golang
    
  4. podman build -t go .
  5. create the file reproducer.go with this contents
    package main
    import (
        "os"
        "errors"
        "fmt"
        securejoin "github.com/cyphar/filepath-securejoin"
    )
    func main() {
      path := "/root/sub"
      os.MkdirAll(path, 0755)
      target := "symlink"
      symlink := "/root/sub/symlink"
      os.Symlink(target, symlink)
      _, err := securejoin.SecureJoin("/root","sub/symlink")
      if (err != nil) {
        var pErr *os.PathError
        if errors.As(err, &pErr) {
          fmt.Printf("Error = %v\n", pErr.Err)
          fmt.Printf("Path = %v\n", pErr.Path)
        }
      }
    }
  6. podman run --rm -v ./:/src:Z -w /src localhost/go go mod init reproducer
  7. podman run --rm -v ./:/src:Z -w /src localhost/go go mod tidy
  8. podman run --rm -v ./:/src:Z -w /src localhost/go go run .
    The command prints
    go: downloading github.com/cyphar/filepath-securejoin v0.2.4
    Error = too many levels of symbolic links
    Path = /root/symlink/
    

Describe the results you received

At step 8 I see

Path = /root/symlink/

Describe the results you expected

At step 8 I would have expected to see

Path = /root/sub/symlink

as /root/sub/symlink is the path to the symlink that points to itself. The path /root/symlink/ does not exist.

Comment

I'm not blocked by this issue in any way. Probably this issue is of low importance.

consider migrating to io/fs.StatFS for SecureJoinVFS

Now the Go has their own "generic" interfaces for filesystem roots, maybe it would make sense to migrate to using that rather than our own home-brew thing. Note that we want to be given the equivalent of fs.DirFS("/") not fs.DirFS(root) -- we are implementing our own resolution logic, and the unsafe path.Join logic in io/fs is not going to work.

cut a release

Can we have a tagged release? Mostly interested in having #7 included into a released version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.