-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
zap.Open: Invalidate relative paths and paths with ".." segments #1397
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,9 @@ | |
import ( | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"path/filepath" | ||
"strings" | ||
|
||
"go.uber.org/zap/zapcore" | ||
|
||
|
@@ -48,6 +51,10 @@ | |
// os.Stdout and os.Stderr. When specified without a scheme, relative file | ||
// paths also work. | ||
func Open(paths ...string) (zapcore.WriteSyncer, func(), error) { | ||
if err := checkOpenPaths(paths); err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
writers, closeAll, err := open(paths) | ||
if err != nil { | ||
return nil, nil, err | ||
|
@@ -84,6 +91,23 @@ | |
return writers, closeAll, nil | ||
} | ||
|
||
func checkOpenPaths(paths []string) error { | ||
var openErr error | ||
for _, path := range paths { | ||
if !strings.HasPrefix(path, "file:") { | ||
continue | ||
} | ||
u, err := url.Parse(path) | ||
if err != nil { | ||
return err | ||
} | ||
if !(filepath.IsAbs(u.Path) && !strings.Contains(u.Path, "..")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if filepath.IsAbs returns true, we're good. It's okay if an absolute path has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @abhinav, I think that means we don't need this feature then?
Experimentation: Arbitrary prefix sequences of double dot segments like will be treated as if these sequences of double dot segments were not provided.
If that make sense I can close the issue after adding test to confirm this behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @r-hang can we confirm with a test that it's not possible to provide a relative path or one with '..' to that API? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #1398 as a successor since I'm not able to reopen this one. |
||
openErr = multierr.Append(openErr, fmt.Errorf(`file URI %q attempts a relative path or contains ".." dot segments`, path)) | ||
} | ||
} | ||
return openErr | ||
} | ||
|
||
// CombineWriteSyncers is a utility that combines multiple WriteSyncers into a | ||
// single, locked WriteSyncer. If no inputs are supplied, it returns a no-op | ||
// WriteSyncer. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just change the
file://
sink instead?This is how the sink is created based on scheme:
zap/sink.go
Lines 64 to 72 in 332853b
It registers the
file://
sink by default.So when a
file://
URL is given, that calls newFileSinkFromURL,which then calls newFileSinkFromPath:
zap/sink.go
Lines 150 to 159 in 332853b
That's the function that finally calls os.OpenFile (through a function reference).
That's where we should have the validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just experimented and yes this location allows for the reuse of a lot more code.