Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path/filepath: filepath.Clean() does not ensure newlines are stripped. #14316

Closed
tduzan-te opened this issue Feb 13, 2016 · 2 comments
Closed

Comments

@tduzan-te
Copy link

I have a Go program that's being used for some queue management functions for Postfix. I'm getting the queue_directory value from postconf and then passing it into filepath.Clean() to return a valid path. Unfortunately, when I stat this it fails because it ends in a newline character. My understanding is that since a newline character is not a valid path character it should be stripped via filepath.Clean().

getQueueDir()

func getQueueDir(config *Config) (string, error) {
    var (
        out      []byte
        err      error
        queuedir string
        here     bool
    )
    args := []string{"-h", "queue_directory"}
    if out, err = exec.Command("postconf", args...).Output(); err != nil {
        panic(err)
    }
    queuedir = filepath.Clean(string(out))
    if config.Verbose {
        fmt.Println("Queue dir retrieved from postconf.")
        fmt.Println(queuedir)
    }
    if here, err = util.Exists(queuedir); err != nil {
        return queuedir, err
    } else if here != true {
        return queuedir, ErrQueueDirectoryMissing
    }
    return queuedir, nil
}

util.Exists()

func Exists(dirorfile string) (bool, error) {
    if _, err := os.Stat(dirorfile); err != nil {
        if os.IsNotExist(err) {
            return false, nil
        }
        return true, err
    }
    return true, nil
}

strace shows the issue:

[pid 17402] stat("/var/spool/postfix\n", 0xc208060240) = -1 ENOENT (No such file or directory)
[pid 17402] write(1, "UNKNOWN: Process Terminated: The"..., 105UNKNOWN: Process Terminated: The queue directory configured for Postfix is either missing or unreadable.
) = 105

I can get around this in the interim by passing the string through strings.TrimSuffix()

@bradfitz
Copy link
Contributor

My understanding is that since a newline character is not a valid path character it should be stripped via filepath.Clean

That's the root of the problem. filepath.Clean does not do that, and is not documented to do that.

I think this is working as intended & documented, and https://golang.org/doc/go1compat would preclude us from changing this right now in any case.

@dominikh
Copy link
Member

On top of that, newlines as part of file names are valid on many file systems.

@golang golang locked and limited conversation to collaborators Feb 28, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants