Home | History | Annotate | Download | only in fileapi
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/file_util.h"
      9 #include "base/files/scoped_temp_dir.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/message_loop/message_loop.h"
     12 #include "base/message_loop/message_loop_proxy.h"
     13 #include "content/public/test/test_file_system_options.h"
     14 #include "testing/gtest/include/gtest/gtest.h"
     15 #include "url/gurl.h"
     16 #include "webkit/browser/fileapi/file_system_url.h"
     17 
     18 using fileapi::FileSystemURL;
     19 
     20 namespace content {
     21 
     22 namespace {
     23 
     24 FileSystemURL CreateFileSystemURL(const char* path) {
     25   const GURL kOrigin("http://foo/");
     26   return fileapi::FileSystemURL::CreateForTest(
     27       kOrigin, fileapi::kFileSystemTypeTemporary,
     28       base::FilePath::FromUTF8Unsafe(path));
     29 }
     30 
     31 }  // namespace
     32 
     33 class SandboxFileSystemBackendDelegateTest : public testing::Test {
     34  protected:
     35   virtual void SetUp() {
     36     ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
     37     delegate_.reset(new fileapi::SandboxFileSystemBackendDelegate(
     38         NULL /* quota_manager_proxy */,
     39         base::MessageLoopProxy::current().get(),
     40         data_dir_.path(),
     41         NULL /* special_storage_policy */,
     42         CreateAllowFileAccessOptions()));
     43   }
     44 
     45   bool IsAccessValid(const FileSystemURL& url) const {
     46     return delegate_->IsAccessValid(url);
     47   }
     48 
     49   base::ScopedTempDir data_dir_;
     50   base::MessageLoop message_loop_;
     51   scoped_ptr<fileapi::SandboxFileSystemBackendDelegate> delegate_;
     52 };
     53 
     54 TEST_F(SandboxFileSystemBackendDelegateTest, IsAccessValid) {
     55   // Normal case.
     56   EXPECT_TRUE(IsAccessValid(CreateFileSystemURL("a")));
     57 
     58   // Access to a path with parent references ('..') should be disallowed.
     59   EXPECT_FALSE(IsAccessValid(CreateFileSystemURL("a/../b")));
     60 
     61   // Access from non-allowed scheme should be disallowed.
     62   EXPECT_FALSE(IsAccessValid(
     63       FileSystemURL::CreateForTest(
     64           GURL("unknown://bar"), fileapi::kFileSystemTypeTemporary,
     65           base::FilePath::FromUTF8Unsafe("foo"))));
     66 
     67   // Access with restricted name should be disallowed.
     68   EXPECT_FALSE(IsAccessValid(CreateFileSystemURL(".")));
     69   EXPECT_FALSE(IsAccessValid(CreateFileSystemURL("..")));
     70 
     71   // This is also disallowed due to Windows XP parent path handling.
     72   EXPECT_FALSE(IsAccessValid(CreateFileSystemURL("...")));
     73 
     74   // These are identified as unsafe cases due to weird path handling
     75   // on Windows.
     76   EXPECT_FALSE(IsAccessValid(CreateFileSystemURL(" ..")));
     77   EXPECT_FALSE(IsAccessValid(CreateFileSystemURL(".. ")));
     78 
     79   // Similar but safe cases.
     80   EXPECT_TRUE(IsAccessValid(CreateFileSystemURL(" .")));
     81   EXPECT_TRUE(IsAccessValid(CreateFileSystemURL(". ")));
     82   EXPECT_TRUE(IsAccessValid(CreateFileSystemURL("b.")));
     83   EXPECT_TRUE(IsAccessValid(CreateFileSystemURL(".b")));
     84 
     85   // A path that looks like a drive letter.
     86   EXPECT_TRUE(IsAccessValid(CreateFileSystemURL("c:")));
     87 }
     88 
     89 }  // namespace content
     90