2016-11-30 02:32:28 -07:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-11-30 02:32:28 -07:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-07-25 02:36:09 -07:00
|
|
|
"path/filepath"
|
2016-12-13 03:24:10 -07:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-11-30 02:32:28 -07:00
|
|
|
"testing"
|
2016-12-13 03:24:10 -07:00
|
|
|
"time"
|
2016-11-30 02:32:28 -07:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/config"
|
|
|
|
"github.com/syncthing/syncthing/lib/db"
|
2017-08-19 07:36:56 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2016-11-30 02:32:28 -07:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestSimple(t *testing.T) {
|
|
|
|
// Verify that the model performs a request and creates a file based on
|
|
|
|
// an incoming index update.
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
m, fc, tmpFolder := setupModelWithConnection()
|
2016-11-30 02:32:28 -07:00
|
|
|
defer m.Stop()
|
2017-11-11 12:18:17 -07:00
|
|
|
defer os.RemoveAll(tmpFolder)
|
2016-11-30 02:32:28 -07:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
|
|
|
done := make(chan struct{})
|
2016-11-30 05:11:06 -07:00
|
|
|
fc.mut.Lock()
|
2016-11-30 02:32:28 -07:00
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == "testfile" {
|
|
|
|
close(done)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 05:11:06 -07:00
|
|
|
fc.mut.Unlock()
|
2016-11-30 02:32:28 -07:00
|
|
|
|
|
|
|
// Send an update for the test file, wait for it to sync and be reported back.
|
|
|
|
contents := []byte("test file contents\n")
|
2016-12-13 03:24:10 -07:00
|
|
|
fc.addFile("testfile", 0644, protocol.FileInfoTypeFile, contents)
|
2016-11-30 02:32:28 -07:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
<-done
|
|
|
|
|
|
|
|
// Verify the contents
|
2017-11-11 12:18:17 -07:00
|
|
|
bs, err := ioutil.ReadFile(filepath.Join(tmpFolder, "testfile"))
|
2016-11-30 02:32:28 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error("File did not sync correctly:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bytes.Equal(bs, contents) {
|
|
|
|
t.Error("File did not sync correctly: incorrect data")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-13 03:24:10 -07:00
|
|
|
func TestSymlinkTraversalRead(t *testing.T) {
|
|
|
|
// Verify that a symlink can not be traversed for reading.
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no symlink support on CI")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
m, fc, tmpFolder := setupModelWithConnection()
|
2016-12-13 03:24:10 -07:00
|
|
|
defer m.Stop()
|
2017-11-11 12:18:17 -07:00
|
|
|
defer os.RemoveAll(tmpFolder)
|
2016-12-13 03:24:10 -07:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
|
|
|
done := make(chan struct{})
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == "symlink" {
|
|
|
|
close(done)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Send an update for the symlink, wait for it to sync and be reported back.
|
|
|
|
contents := []byte("..")
|
2017-02-07 01:34:24 -07:00
|
|
|
fc.addFile("symlink", 0644, protocol.FileInfoTypeSymlink, contents)
|
2016-12-13 03:24:10 -07:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
<-done
|
|
|
|
|
|
|
|
// Request a file by traversing the symlink
|
|
|
|
buf := make([]byte, 10)
|
|
|
|
err := m.Request(device1, "default", "symlink/requests_test.go", 0, nil, false, buf)
|
|
|
|
if err == nil || !bytes.Equal(buf, make([]byte, 10)) {
|
|
|
|
t.Error("Managed to traverse symlink")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSymlinkTraversalWrite(t *testing.T) {
|
|
|
|
// Verify that a symlink can not be traversed for writing.
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no symlink support on CI")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
m, fc, tmpFolder := setupModelWithConnection()
|
2016-12-13 03:24:10 -07:00
|
|
|
defer m.Stop()
|
2017-11-11 12:18:17 -07:00
|
|
|
defer os.RemoveAll(tmpFolder)
|
2016-12-13 03:24:10 -07:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected names.
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
badReq := make(chan string, 1)
|
|
|
|
badIdx := make(chan string, 1)
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
for _, f := range fs {
|
|
|
|
if f.Name == "symlink" {
|
|
|
|
done <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(f.Name, "symlink") {
|
|
|
|
badIdx <- f.Name
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.requestFn = func(folder, name string, offset int64, size int, hash []byte, fromTemporary bool) ([]byte, error) {
|
|
|
|
if name != "symlink" && strings.HasPrefix(name, "symlink") {
|
|
|
|
badReq <- name
|
|
|
|
}
|
|
|
|
return fc.fileData[name], nil
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Send an update for the symlink, wait for it to sync and be reported back.
|
|
|
|
contents := []byte("..")
|
2017-02-07 01:34:24 -07:00
|
|
|
fc.addFile("symlink", 0644, protocol.FileInfoTypeSymlink, contents)
|
2016-12-13 03:24:10 -07:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
<-done
|
|
|
|
|
|
|
|
// Send an update for things behind the symlink, wait for requests for
|
|
|
|
// blocks for any of them to come back, or index entries. Hopefully none
|
|
|
|
// of that should happen.
|
|
|
|
contents = []byte("testdata testdata\n")
|
|
|
|
fc.addFile("symlink/testfile", 0644, protocol.FileInfoTypeFile, contents)
|
|
|
|
fc.addFile("symlink/testdir", 0644, protocol.FileInfoTypeDirectory, contents)
|
2017-02-07 01:34:24 -07:00
|
|
|
fc.addFile("symlink/testsyml", 0644, protocol.FileInfoTypeSymlink, contents)
|
2016-12-13 03:24:10 -07:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case name := <-badReq:
|
|
|
|
t.Fatal("Should not have requested the data for", name)
|
|
|
|
case name := <-badIdx:
|
|
|
|
t.Fatal("Should not have sent the index entry for", name)
|
|
|
|
case <-time.After(3 * time.Second):
|
|
|
|
// Unfortunately not much else to trigger on here. The puller sleep
|
|
|
|
// interval is 1s so if we didn't get any requests within two
|
|
|
|
// iterations we should be fine.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestCreateTmpSymlink(t *testing.T) {
|
2017-11-11 12:18:17 -07:00
|
|
|
// Test that an update for a temporary file is invalidated
|
2016-12-13 03:24:10 -07:00
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
m, fc, tmpFolder := setupModelWithConnection()
|
2016-12-13 03:24:10 -07:00
|
|
|
defer m.Stop()
|
2017-11-11 12:18:17 -07:00
|
|
|
defer os.RemoveAll(tmpFolder)
|
2016-12-13 03:24:10 -07:00
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
2017-11-11 12:18:17 -07:00
|
|
|
goodIdx := make(chan struct{})
|
|
|
|
name := fs.TempName("testlink")
|
2016-12-13 03:24:10 -07:00
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
for _, f := range fs {
|
2017-11-11 12:18:17 -07:00
|
|
|
if f.Name == name {
|
|
|
|
if f.Invalid {
|
|
|
|
goodIdx <- struct{}{}
|
|
|
|
} else {
|
|
|
|
t.Fatal("Received index with non-invalid temporary file")
|
|
|
|
}
|
2016-12-13 03:24:10 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Send an update for the test file, wait for it to sync and be reported back.
|
2017-11-11 12:18:17 -07:00
|
|
|
fc.addFile(name, 0644, protocol.FileInfoTypeSymlink, []byte(".."))
|
2016-12-13 03:24:10 -07:00
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
select {
|
2017-11-11 12:18:17 -07:00
|
|
|
case <-goodIdx:
|
2016-12-13 03:24:10 -07:00
|
|
|
case <-time.After(3 * time.Second):
|
2017-11-11 12:18:17 -07:00
|
|
|
t.Fatal("Timed out without index entry being sent")
|
2016-12-13 03:24:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:36:09 -07:00
|
|
|
func TestRequestVersioningSymlinkAttack(t *testing.T) {
|
2017-08-07 23:05:24 -07:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("no symlink support on Windows")
|
|
|
|
}
|
|
|
|
|
2017-07-25 02:36:09 -07:00
|
|
|
// Sets up a folder with trashcan versioning and tries to use a
|
|
|
|
// deleted symlink to escape
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
tmpFolder, err := ioutil.TempDir(".", "_request-")
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to create temporary testing dir")
|
|
|
|
}
|
2017-07-25 02:36:09 -07:00
|
|
|
cfg := defaultConfig.RawCopy()
|
2017-12-07 00:08:24 -07:00
|
|
|
cfg.Folders[0] = config.NewFolderConfiguration(protocol.LocalDeviceID, "default", "default", fs.FilesystemTypeBasic, tmpFolder)
|
2017-07-25 02:36:09 -07:00
|
|
|
cfg.Folders[0].Devices = []config.FolderDeviceConfiguration{
|
|
|
|
{DeviceID: device1},
|
|
|
|
{DeviceID: device2},
|
|
|
|
}
|
|
|
|
cfg.Folders[0].Versioning = config.VersioningConfiguration{
|
|
|
|
Type: "trashcan",
|
|
|
|
}
|
|
|
|
w := config.Wrap("/tmp/cfg", cfg)
|
|
|
|
|
|
|
|
db := db.OpenMemory()
|
|
|
|
m := NewModel(w, device1, "syncthing", "dev", db, nil)
|
|
|
|
m.AddFolder(cfg.Folders[0])
|
|
|
|
m.ServeBackground()
|
|
|
|
m.StartFolder("default")
|
|
|
|
defer m.Stop()
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
defer os.RemoveAll(tmpFolder)
|
2017-07-25 02:36:09 -07:00
|
|
|
|
|
|
|
fc := addFakeConn(m, device2)
|
|
|
|
fc.folder = "default"
|
|
|
|
|
|
|
|
// Create a temporary directory that we will use as target to see if
|
|
|
|
// we can escape to it
|
|
|
|
tmpdir, err := ioutil.TempDir("", "syncthing-test")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We listen for incoming index updates and trigger when we see one for
|
|
|
|
// the expected test file.
|
|
|
|
idx := make(chan int)
|
|
|
|
fc.mut.Lock()
|
|
|
|
fc.indexFn = func(folder string, fs []protocol.FileInfo) {
|
|
|
|
idx <- len(fs)
|
|
|
|
}
|
|
|
|
fc.mut.Unlock()
|
|
|
|
|
|
|
|
// Send an update for the test file, wait for it to sync and be reported back.
|
|
|
|
fc.addFile("foo", 0644, protocol.FileInfoTypeSymlink, []byte(tmpdir))
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the symlink, hoping for it to get versioned
|
|
|
|
fc.deleteFile("foo")
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate foo and a file in it with some data
|
|
|
|
fc.addFile("foo", 0755, protocol.FileInfoTypeDirectory, nil)
|
|
|
|
fc.addFile("foo/test", 0644, protocol.FileInfoTypeFile, []byte("testtesttest"))
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the test file and see if it escaped
|
|
|
|
fc.deleteFile("foo/test")
|
|
|
|
fc.sendIndexUpdate()
|
|
|
|
for updates := 0; updates < 1; updates += <-idx {
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(tmpdir, "test")
|
|
|
|
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
|
|
|
t.Fatal("File escaped to", path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
func setupModelWithConnection() (*Model, *fakeConnection, string) {
|
|
|
|
tmpFolder, err := ioutil.TempDir(".", "_request-")
|
|
|
|
if err != nil {
|
|
|
|
panic("Failed to create temporary testing dir")
|
|
|
|
}
|
2016-11-30 02:32:28 -07:00
|
|
|
cfg := defaultConfig.RawCopy()
|
2017-12-07 00:08:24 -07:00
|
|
|
cfg.Folders[0] = config.NewFolderConfiguration(protocol.LocalDeviceID, "default", "default", fs.FilesystemTypeBasic, tmpFolder)
|
2016-11-30 02:32:28 -07:00
|
|
|
cfg.Folders[0].Devices = []config.FolderDeviceConfiguration{
|
|
|
|
{DeviceID: device1},
|
|
|
|
{DeviceID: device2},
|
|
|
|
}
|
|
|
|
w := config.Wrap("/tmp/cfg", cfg)
|
|
|
|
|
|
|
|
db := db.OpenMemory()
|
2017-05-22 12:58:33 -07:00
|
|
|
m := NewModel(w, device1, "syncthing", "dev", db, nil)
|
2016-11-30 02:32:28 -07:00
|
|
|
m.AddFolder(cfg.Folders[0])
|
|
|
|
m.ServeBackground()
|
|
|
|
m.StartFolder("default")
|
|
|
|
|
|
|
|
fc := addFakeConn(m, device2)
|
|
|
|
fc.folder = "default"
|
|
|
|
|
2017-11-11 12:18:17 -07:00
|
|
|
return m, fc, tmpFolder
|
2016-11-30 02:32:28 -07:00
|
|
|
}
|