Преглед изворни кода

lib/fs: Improve root check (#6033)

The root check would allow things like c:\foobar\baz if the root was
c:\foo, because string wise that's a prefix. Now it doesn't.
Jakob Borg пре 6 година
родитељ
комит
52d80d8144
2 измењених фајлова са 16 додато и 2 уклоњено
  1. 4 0
      lib/fs/basicfs_unix.go
  2. 12 2
      lib/fs/basicfs_watch_test.go

+ 4 - 0
lib/fs/basicfs_unix.go

@@ -62,6 +62,10 @@ func (f *BasicFilesystem) Roots() ([]string, error) {
 // pathseparator.
 func (f *BasicFilesystem) unrootedChecked(absPath string, roots []string) (string, *ErrWatchEventOutsideRoot) {
 	for _, root := range roots {
+		// Make sure the root ends with precisely one path separator, to
+		// ease prefix comparisons.
+		root := strings.TrimRight(root, string(PathSeparator)) + string(PathSeparator)
+
 		if absPath+string(PathSeparator) == root {
 			return ".", nil
 		}

+ 12 - 2
lib/fs/basicfs_watch_test.go

@@ -201,23 +201,33 @@ func TestWatchWinRoot(t *testing.T) {
 
 // TestWatchOutside checks that no changes from outside the folder make it in
 func TestWatchOutside(t *testing.T) {
+	expectErrorForPath(t, filepath.Join(filepath.Dir(testDirAbs), "outside"))
+
+	rootWithoutSlash := strings.TrimRight(filepath.ToSlash(testDirAbs), "/")
+	expectErrorForPath(t, rootWithoutSlash+"outside")
+	expectErrorForPath(t, rootWithoutSlash+"outside/thing")
+}
+
+func expectErrorForPath(t *testing.T, path string) {
 	outChan := make(chan Event)
 	backendChan := make(chan notify.EventInfo, backendBuffer)
 	errChan := make(chan error)
 
 	ctx, cancel := context.WithCancel(context.Background())
+	defer cancel()
 
 	// testFs is Filesystem, but we need BasicFilesystem here
 	fs := newBasicFilesystem(testDirAbs)
 
 	go fs.watchLoop(".", []string{testDirAbs}, backendChan, outChan, errChan, fakeMatcher{}, ctx)
 
-	backendChan <- fakeEventInfo(filepath.Join(filepath.Dir(testDirAbs), "outside"))
+	backendChan <- fakeEventInfo(path)
 
 	select {
 	case <-time.After(10 * time.Second):
-		cancel()
 		t.Errorf("Timed out before receiving error")
+	case e := <-outChan:
+		t.Errorf("Unexpected passed through event %v", e)
 	case <-errChan:
 	case <-ctx.Done():
 	}